fix: address follow-up CodeRabbit review

This commit is contained in:
2026-03-31 23:08:18 -07:00
parent 6e3f072fdc
commit 6aadde4577
3 changed files with 35 additions and 2 deletions

View File

@@ -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 });

View File

@@ -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);
});

View File

@@ -122,6 +122,10 @@ function findExactDuplicateNoteIds(
deps: DuplicateDetectionDeps,
maxMatches?: number,
): Promise<number[]> {
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) {