aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/shared/card-generator.test.ts
blob: 8c78ca5fc19d45e47ae2cd66ff7d731a070318fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { describe, expect, it } from "vitest";
import { generateCardsForNote, renderCardTemplate } from "./card-generator";

describe("renderCardTemplate", () => {
	it("substitutes a single placeholder", () => {
		const fields = new Map([["Front", "Hello"]]);
		expect(renderCardTemplate("{{Front}}", fields)).toBe("Hello");
	});

	it("substitutes multiple placeholders", () => {
		const fields = new Map([
			["Front", "Q"],
			["Back", "A"],
		]);
		expect(renderCardTemplate("{{Front}} -> {{Back}}", fields)).toBe("Q -> A");
	});

	it("substitutes the same placeholder repeated in a template", () => {
		const fields = new Map([["Word", "x"]]);
		expect(renderCardTemplate("{{Word}}{{Word}}", fields)).toBe("xx");
	});

	it("leaves unknown placeholders unchanged", () => {
		const fields = new Map([["Front", "Q"]]);
		expect(renderCardTemplate("{{Front}} {{Missing}}", fields)).toBe(
			"Q {{Missing}}",
		);
	});

	it("replaces placeholders with empty strings when value is empty", () => {
		const fields = new Map([["Front", ""]]);
		expect(renderCardTemplate("[{{Front}}]", fields)).toBe("[]");
	});
});

const noteTypeBase = {
	frontTemplate: "{{Front}}",
	backTemplate: "{{Back}}",
	isReversible: false,
};

const fieldTypes = [
	{ id: "ft-front", name: "Front" },
	{ id: "ft-back", name: "Back" },
];

const fieldValues = [
	{ noteFieldTypeId: "ft-front", value: "Question" },
	{ noteFieldTypeId: "ft-back", value: "Answer" },
];

describe("generateCardsForNote", () => {
	it("returns one card when isReversible is false", () => {
		const cards = generateCardsForNote({
			noteType: noteTypeBase,
			fieldTypes,
			fieldValues,
		});

		expect(cards).toHaveLength(1);
		expect(cards[0]).toMatchObject({
			isReversed: false,
			front: "Question",
			back: "Answer",
		});
	});

	it("returns two cards when isReversible is true and swaps templates", () => {
		const cards = generateCardsForNote({
			noteType: { ...noteTypeBase, isReversible: true },
			fieldTypes,
			fieldValues,
		});

		expect(cards).toHaveLength(2);
		expect(cards[0]).toMatchObject({
			isReversed: false,
			front: "Question",
			back: "Answer",
		});
		expect(cards[1]).toMatchObject({
			isReversed: true,
			front: "Answer",
			back: "Question",
		});
	});

	it("initialises FSRS state to a fresh new-card payload", () => {
		const now = new Date("2026-05-02T10:00:00Z");
		const [card] = generateCardsForNote({
			noteType: noteTypeBase,
			fieldTypes,
			fieldValues,
			now,
		});

		expect(card).toMatchObject({
			state: 0,
			stability: 0,
			difficulty: 0,
			elapsedDays: 0,
			scheduledDays: 0,
			reps: 0,
			lapses: 0,
		});
		expect(card?.due.getTime()).toBe(now.getTime());
	});

	it("treats missing field values as empty strings", () => {
		const [card] = generateCardsForNote({
			noteType: noteTypeBase,
			fieldTypes,
			fieldValues: [{ noteFieldTypeId: "ft-front", value: "Q only" }],
		});

		expect(card?.front).toBe("Q only");
		expect(card?.back).toBe("{{Back}}");
	});

	it("ignores field values with unknown field type ids", () => {
		const [card] = generateCardsForNote({
			noteType: noteTypeBase,
			fieldTypes,
			fieldValues: [
				...fieldValues,
				{ noteFieldTypeId: "ft-unknown", value: "ghost" },
			],
		});

		expect(card?.front).toBe("Question");
		expect(card?.back).toBe("Answer");
	});

	it("returns independent due Date instances per card", () => {
		const now = new Date("2026-05-02T10:00:00Z");
		const cards = generateCardsForNote({
			noteType: { ...noteTypeBase, isReversible: true },
			fieldTypes,
			fieldValues,
			now,
		});

		expect(cards[0]?.due).not.toBe(cards[1]?.due);
		expect(cards[0]?.due).not.toBe(now);
	});
});