import { describe, expect, it } from "vitest";
import {
fetchCardsFromNotes,
formatCardList,
mapNoteToAnkiCard,
type RawAnkiNote,
} from "../ankiUtils.js";
describe("mapNoteToAnkiCard", () => {
it("maps basic notes to card format", () => {
const card = mapNoteToAnkiCard({
noteId: 42,
modelName: "Basic",
fields: {
Front: { value: "Question" },
Back: { value: "Answer" },
},
tags: ["tag1", "tag2"],
});
expect(card).toEqual({
noteId: 42,
fields: {
Front: { value: "Question" },
Back: { value: "Answer" },
},
tags: ["tag1", "tag2"],
});
});
it("maps cloze notes and falls back when Back Extra is missing", () => {
const card = mapNoteToAnkiCard({
noteId: 7,
modelName: "Cloze",
fields: {
Text: { value: "{{c1::Paris}} is in France" },
},
tags: [],
});
expect(card).toEqual({
noteId: 7,
fields: {
Front: { value: "{{c1::Paris}} is in France" },
Back: { value: "[Cloze deletion]" },
},
tags: [],
});
});
it("uses placeholders for unknown note types", () => {
const card = mapNoteToAnkiCard({
noteId: 99,
modelName: "Custom",
fields: {},
tags: [],
});
expect(card.fields.Front.value).toBe("[Unknown note type]");
expect(card.fields.Back.value).toBe("[Unknown note type]");
});
});
describe("fetchCardsFromNotes", () => {
it("fetches notes in chunks and maps them", async () => {
const noteIds = [1, 2, 3, 4];
const calls: number[][] = [];
const noteFactory = (id: number): RawAnkiNote => ({
noteId: id,
modelName: "Basic",
fields: {
Front: { value: `Front ${id}` },
Back: { value: `Back ${id}` },
},
tags: ["sample"],
});
const cards = await fetchCardsFromNotes(
noteIds,
async (chunk) => {
calls.push([...chunk]);
return chunk.map((id) => noteFactory(id));
},
2
);
expect(calls).toEqual([[1, 2], [3, 4]]);
expect(cards.map((card) => card.noteId)).toEqual(noteIds);
expect(cards[0].fields.Front.value).toBe("Front 1");
});
});
describe("formatCardList", () => {
it("prints a human-readable summary for cards", () => {
const output = formatCardList([
{
noteId: 10,
fields: {
Front: { value: "F1" },
Back: { value: "B1" },
},
tags: [],
},
{
noteId: 11,
fields: {
Front: { value: "F2" },
Back: { value: "B2" },
},
tags: ["t1", "t2"],
},
]);
expect(output).toContain("Note ID: 10\nFront: F1\nBack: B1\nTags: \n---");
expect(output).toContain(
"Note ID: 11\nFront: F2\nBack: B2\nTags: t1, t2\n---"
);
});
});