Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/LIBRARY_IMPORT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions server/__tests__/library-scanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
44 changes: 37 additions & 7 deletions server/library-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,13 @@
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);
Expand Down Expand Up @@ -386,7 +387,7 @@
}
} 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);
Expand All @@ -395,9 +396,33 @@
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<string, unknown> } {
const { year } = parseFolderHints(folderName);
const details: Record<string, unknown> = {
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 ----------

/**
Expand Down Expand Up @@ -795,7 +820,7 @@
}

/** Classify and (auto-)resolve a single scan candidate, updating `progress` in place. */
async function processCandidate(

Check failure on line 823 in server/library-scanner.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 17 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=Doezer_Questarr&issues=AaHaO_9gGiVmDmX-93Hn&open=AaHaO_9gGiVmDmX-93Hn&pullRequest=1340
rootFolder: RootFolderRow,
cand: FolderCandidate,
userId: string,
Expand Down Expand Up @@ -835,17 +860,22 @@
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;
}
Expand Down
Loading