diff --git a/docs/LIBRARY_IMPORT.md b/docs/LIBRARY_IMPORT.md index 0460c09d3..1b0952f29 100644 --- a/docs/LIBRARY_IMPORT.md +++ b/docs/LIBRARY_IMPORT.md @@ -196,6 +196,7 @@ configuration does not delete the directory itself. - The scan asks IGDB for ten candidates per folder, since a short name such as "Portal" is crowded with sequels and mods. **Needs Review** lists up to ten, likeliest first, each with its cover and release year. +- A folder that stays in Needs Review is explained in the server log (search `Library scan: folder left in Needs Review`): the query sent to IGDB, how many candidates came back, and the best one with its score against the 0.85 threshold. - Under **Needs Review**, choose a suggested IGDB game or enter a different name in **Not listed? Search IGDB by name**. Search alone does not import the game; clicking a candidate confirms it and adds it to your library. diff --git a/server/__tests__/library-scanner.test.ts b/server/__tests__/library-scanner.test.ts index 1f420e282..366c217dc 100644 --- a/server/__tests__/library-scanner.test.ts +++ b/server/__tests__/library-scanner.test.ts @@ -318,6 +318,17 @@ describe("scanRootFolderById full scan", () => { expect(unmatched[0].folderName).toBe("Mystery Game.iso"); expect(unmatched[0].candidates).toEqual([]); + // The reason is logged so a missed match can be diagnosed from the scan log. + const { igdbLogger } = await import("../logger.js"); + expect(igdbLogger.info).toHaveBeenCalledWith( + expect.objectContaining({ + folderName: "Mystery Game.iso", + candidates: 0, + threshold: expect.any(Number), + }), + expect.stringContaining("IGDB returned no candidates") + ); + // The IGDB-less ignored folder was never queried. expect(igdbClient.searchGames).not.toHaveBeenCalledWith("IgnoredOnly", 10); diff --git a/server/library-scanner.ts b/server/library-scanner.ts index dfdd5abb1..e1fa87757 100644 --- a/server/library-scanner.ts +++ b/server/library-scanner.ts @@ -349,12 +349,13 @@ async function bestIgdbMatch( candidates: IGDBGame[]; cleaned: string; score: number; + searchFailed: boolean; }> { const cleaned = cleanReleaseName(folderName) || folderName; // An id the person wrote in the folder name is an exact match. const byId = await matchByIdTag(folderName); - if (byId) return { best: byId, candidates: [byId], cleaned, score: 1 }; + if (byId) return { best: byId, candidates: [byId], cleaned, score: 1, searchFailed: false }; const { year } = parseFolderHints(folderName); const rank = (list: IGDBGame[]) => rankByTitle(list, cleaned, year); @@ -386,7 +387,7 @@ async function bestIgdbMatch( } } catch (err) { igdbLogger.warn({ err, query: cleaned }, "IGDB search failed during library scan"); - return { best: null, candidates: [], cleaned, score: 0 }; + return { best: null, candidates: [], cleaned, score: 0, searchFailed: true }; } // Best first, so the review list shows the likeliest games at the top. const ranked = rank(candidates); @@ -395,9 +396,33 @@ async function bestIgdbMatch( candidates: ranked.map((r) => r.game), cleaned, score: ranked[0]?.score ?? 0, + searchFailed: false, }; } +/** + * Why a folder was not auto-matched, for the scan log: the cleaned query IGDB saw, the number + * of candidates, and the best one with its score against the threshold. + */ +function describeMatchFailure( + folderName: string, + match: { best: IGDBGame | null; candidates: IGDBGame[]; cleaned: string; score: number } +): { reason: string; details: Record } { + const { year } = parseFolderHints(folderName); + const details: Record = { + folderName, + query: match.cleaned, + folderYear: year ?? null, + candidates: match.candidates.length, + threshold: AUTO_MATCH_THRESHOLD, + }; + if (!match.best) return { reason: "IGDB returned no candidates", details }; + details.bestName = match.best.name; + details.bestYear = releaseYearOf(match.best); + details.bestScore = Number(match.score.toFixed(2)); + return { reason: "best candidate scored below the auto-match threshold", details }; +} + // ---------- Ingestion ---------- /** @@ -835,17 +860,22 @@ async function processCandidate( return; } - const { - best, - candidates: igdbCandidates, - score, - } = await bestIgdbMatch(cand.folderName, switchInfo ? SWITCH_IGDB_PLATFORM_ID : undefined); + const match = await bestIgdbMatch( + cand.folderName, + switchInfo ? SWITCH_IGDB_PLATFORM_ID : undefined + ); + const { best, candidates: igdbCandidates, score } = match; if (best && score >= AUTO_MATCH_THRESHOLD) { const gameId = await recordMatchedCandidate(cand, best, userId, files, progress); registerSwitchFamilies(files, gameId, switchFamilies); progress.matched += 1; } else { + // The search error itself is already logged by bestIgdbMatch. + if (!match.searchFailed) { + const { reason, details } = describeMatchFailure(cand.folderName, match); + igdbLogger.info(details, `Library scan: folder left in Needs Review: ${reason}`); + } recordUnmatchedCandidate(rootFolder, cand, igdbCandidates); progress.unmatched += 1; }