mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-05-25 12:55:18 -07:00
fix: normalize anki deck sample size
This commit is contained in:
@@ -124,6 +124,36 @@ test('AnkiConnectClient derives field names from sampled notes in a deck', async
|
||||
});
|
||||
});
|
||||
|
||||
test('AnkiConnectClient treats negative deck note sample sizes as empty samples', async () => {
|
||||
const client = new AnkiConnectClient('http://127.0.0.1:8765') as unknown as {
|
||||
client: { post: (url: string, body: { action: string; params: unknown }) => Promise<unknown> };
|
||||
};
|
||||
const calls: Array<{ action: string; params: unknown }> = [];
|
||||
client.client = {
|
||||
post: async (_url, body) => {
|
||||
calls.push({ action: body.action, params: body.params });
|
||||
if (body.action === 'findNotes') {
|
||||
return { data: { result: [3, 1, 2], error: null } };
|
||||
}
|
||||
if (body.action === 'notesInfo') {
|
||||
return {
|
||||
data: {
|
||||
result: [{ fields: { Sentence: { value: 'x' } } }],
|
||||
error: null,
|
||||
},
|
||||
};
|
||||
}
|
||||
return { data: { result: [], error: null } };
|
||||
},
|
||||
};
|
||||
|
||||
assert.deepEqual(await (client as unknown as AnkiConnectClient).fieldNamesForDeck('Mining', -1), []);
|
||||
assert.deepEqual(
|
||||
calls.map((call) => call.action),
|
||||
['findNotes'],
|
||||
);
|
||||
});
|
||||
|
||||
test('AnkiConnectClient derives model names from sampled notes in a deck', async () => {
|
||||
const client = new AnkiConnectClient('http://127.0.0.1:8765') as unknown as {
|
||||
client: { post: (url: string, body: { action: string; params: unknown }) => Promise<unknown> };
|
||||
|
||||
+7
-1
@@ -187,7 +187,13 @@ export class AnkiConnectClient {
|
||||
return [];
|
||||
}
|
||||
|
||||
return this.notesInfo(noteIds.slice(0, sampleSize));
|
||||
const finiteSampleSize = Number.isFinite(sampleSize) ? sampleSize : 0;
|
||||
const normalizedSampleSize = Math.min(noteIds.length, Math.max(0, Math.floor(finiteSampleSize)));
|
||||
if (normalizedSampleSize === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return this.notesInfo(noteIds.slice(0, normalizedSampleSize));
|
||||
}
|
||||
|
||||
async fieldNamesForDeck(deckName: string, sampleSize = 100): Promise<string[]> {
|
||||
|
||||
Reference in New Issue
Block a user