From 6aadde4577b438a9cc85be1f7326eef91db3e81e Mon Sep 17 00:00:00 2001 From: sudacode Date: Tue, 31 Mar 2026 23:08:18 -0700 Subject: [PATCH] fix: address follow-up CodeRabbit review --- src/anki-integration/card-creation.test.ts | 2 +- src/anki-integration/duplicate.test.ts | 31 +++++++++++++++++++++- src/anki-integration/duplicate.ts | 4 +++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/anki-integration/card-creation.test.ts b/src/anki-integration/card-creation.test.ts index 0ca979c7..022a3775 100644 --- a/src/anki-integration/card-creation.test.ts +++ b/src/anki-integration/card-creation.test.ts @@ -474,7 +474,7 @@ test('CardCreationService tracks pre-add duplicate note ids for kiku sentence ca trackLastAddedNoteId: () => undefined, findDuplicateNoteIds: async (expression) => { duplicateLookupExpressions.push(expression); - return [18, 7, 30]; + return [18, 7, 30, 7]; }, trackLastAddedDuplicateNoteIds: (noteId, duplicateNoteIds) => { trackedDuplicates.push({ noteId, duplicateNoteIds }); diff --git a/src/anki-integration/duplicate.test.ts b/src/anki-integration/duplicate.test.ts index 1c8ab593..19d58646 100644 --- a/src/anki-integration/duplicate.test.ts +++ b/src/anki-integration/duplicate.test.ts @@ -1,6 +1,6 @@ import test from 'node:test'; import assert from 'node:assert/strict'; -import { findDuplicateNote, type NoteInfo } from './duplicate'; +import { findDuplicateNote, findDuplicateNoteIds, type NoteInfo } from './duplicate'; function createFieldResolver(noteInfo: NoteInfo, preferredName: string): string | null { const names = Object.keys(noteInfo.fields); @@ -297,3 +297,32 @@ test('findDuplicateNote stops after the first exact-match chunk', async () => { assert.equal(duplicateId, 200); assert.equal(notesInfoCalls, 1); }); + +test('findDuplicateNoteIds returns no matches when maxMatches is zero', async () => { + const currentNote: NoteInfo = { + noteId: 100, + fields: { + Expression: { value: '貴様' }, + }, + }; + + let notesInfoCalls = 0; + const duplicateIds = await findDuplicateNoteIds('貴様', 100, currentNote, { + findNotes: async () => [200], + notesInfo: async (noteIds) => { + notesInfoCalls += 1; + return noteIds.map((noteId) => ({ + noteId, + fields: { + Expression: { value: '貴様' }, + }, + })); + }, + getDeck: () => 'Japanese::Mining', + resolveFieldName: (noteInfo, preferredName) => createFieldResolver(noteInfo, preferredName), + logWarn: () => {}, + }, 0); + + assert.deepEqual(duplicateIds, []); + assert.equal(notesInfoCalls, 0); +}); diff --git a/src/anki-integration/duplicate.ts b/src/anki-integration/duplicate.ts index 882e500e..d3cc70d4 100644 --- a/src/anki-integration/duplicate.ts +++ b/src/anki-integration/duplicate.ts @@ -122,6 +122,10 @@ function findExactDuplicateNoteIds( deps: DuplicateDetectionDeps, maxMatches?: number, ): Promise { + if (maxMatches !== undefined && maxMatches <= 0) { + return Promise.resolve([]); + } + const candidates = Array.from(candidateNoteIds).filter((id) => id !== excludeNoteId); deps.logDebug?.(`[duplicate] candidateIds=${candidates.length} exclude=${excludeNoteId}`); if (candidates.length === 0) {