mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-10-25 23:35:21 -07:00
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:
1 file changed
+23
-18
+23
-18
@@ -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,27 +64,32 @@ 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: [],
|
fields: Object.fromEntries(
|
||||||
fields: Object.fromEntries(
|
MODEL_FIELDS.map((name, order) => [
|
||||||
MODEL_FIELDS.map((name, order) => [
|
name,
|
||||||
name,
|
{ value: String(fields[name] ?? ''), order },
|
||||||
{ value: String(fields[name] ?? ''), order },
|
]),
|
||||||
]),
|
),
|
||||||
),
|
};
|
||||||
},
|
|
||||||
];
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
case 'findNotes':
|
case 'findNotes':
|
||||||
|
|||||||
Reference in new issue
Block a user