From d9b78a9fa440d84c6cd0c1f2a6ebb43df895ccdf Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 2 May 2026 11:24:16 +0900 Subject: refactor(notes): extract card generation into shared module Pull pure card-generation logic (template rendering and the initial FSRS state for new cards) out of the server note repository into src/shared/card-generator.ts so both server and client can call it. The server's note.create / createMany now delegate the rendering work to the shared function and only insert the resulting payload, preserving the existing behavior. Groundwork for offline-first note CRUD on the client. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/shared/card-generator.test.ts | 146 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 src/shared/card-generator.test.ts (limited to 'src/shared/card-generator.test.ts') diff --git a/src/shared/card-generator.test.ts b/src/shared/card-generator.test.ts new file mode 100644 index 0000000..8c78ca5 --- /dev/null +++ b/src/shared/card-generator.test.ts @@ -0,0 +1,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); + }); +}); -- cgit v1.3.1