test(e2e): mirror AnkiConnect notesInfo and updateNoteFields in fake

notesInfo now returns one entry per requested id ({} for unknown notes),
and updateNoteFields writes through so later reads see current fields.
This commit is contained in:
sudacode committed 2026-10-25 21:53:52 -07:00
1 parent 4d27e068c6
commit 8a2da51e41
1 file changed
+14 -9
+14 -9
View File
@@ -38,8 +38,8 @@ function isRecord(value: unknown): value is Record<string, unknown> {
} }
// A recording AnkiConnect stand-in: one deck and one Lapis-shaped note type. // A recording AnkiConnect stand-in: one deck and one Lapis-shaped note type.
// Added notes are echoed back by `notesInfo` so the app can resolve their fields; // Added notes (with later `updateNoteFields` writes) are echoed back by `notesInfo`
// scenarios still assert on `requests`, not on stored notes. // so the app can resolve their fields; scenarios still assert on `requests`.
export async function startFakeAnki(): Promise<FakeAnki> { export async function startFakeAnki(): Promise<FakeAnki> {
const requests: AnkiRequest[] = []; const requests: AnkiRequest[] = [];
const addedNotes = new Map<number, Record<string, unknown>>(); const addedNotes = new Map<number, Record<string, unknown>>();
@@ -64,16 +64,22 @@ export async function startFakeAnki(): Promise<FakeAnki> {
case 'addNote': { case 'addNote': {
const noteId = nextNoteId++; const noteId = nextNoteId++;
const note = isRecord(params.note) ? params.note : {}; const note = isRecord(params.note) ? params.note : {};
addedNotes.set(noteId, isRecord(note.fields) ? note.fields : {}); addedNotes.set(noteId, isRecord(note.fields) ? { ...note.fields } : {});
return noteId; return noteId;
} }
case 'updateNoteFields': {
const note = isRecord(params.note) ? params.note : {};
const fields = typeof note.id === 'number' ? addedNotes.get(note.id) : undefined;
if (fields && isRecord(note.fields)) Object.assign(fields, note.fields);
return null;
}
case 'notesInfo': { case 'notesInfo': {
// Like AnkiConnect: one entry per requested id, `{}` for unknown notes.
const ids = Array.isArray(params.notes) ? params.notes : []; const ids = Array.isArray(params.notes) ? params.notes : [];
return ids.flatMap((id) => { return ids.map((id) => {
const fields = typeof id === 'number' ? addedNotes.get(id) : undefined; const fields = typeof id === 'number' ? addedNotes.get(id) : undefined;
if (!fields) return []; if (!fields) return {};
return [ return {
{
noteId: id, noteId: id,
modelName: FAKE_ANKI_MODEL, modelName: FAKE_ANKI_MODEL,
tags: [], tags: [],
@@ -83,8 +89,7 @@ export async function startFakeAnki(): Promise<FakeAnki> {
{ value: String(fields[name] ?? ''), order }, { value: String(fields[name] ?? ''), order },
]), ]),
), ),
}, };
];
}); });
} }
case 'findNotes': case 'findNotes':