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
42 changes: 42 additions & 0 deletions server/__tests__/library-scanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,48 @@ describe("matchUnmatchedFolder with release-style folder names", () => {
expect(result.gameId).toBe("game-777");
});

it("ranks name-search results by title score, then by the year typed", async () => {
const igdbClient = await scanReleaseFolder("rf-release-rank");
const at = (year: number) => Date.UTC(year, 5, 1) / 1000;
vi.mocked(igdbClient.searchGames).mockResolvedValue([
{ id: 1, name: "Portal 2", first_release_date: at(2011) },
{ id: 2, name: "Portal: Prelude", first_release_date: at(2008) },
{ id: 3, name: "Portal", first_release_date: at(2007) },
{ id: 4, name: "Portal", first_release_date: at(2022) },
] as never);

const plain = await searchUnmatchedFolder("rf-release-rank", folderName, "Portal");
expect(plain.map((c) => c.igdbId)).toEqual([3, 4, 1, 2]);

// A year in the typed name breaks the tie between two games with the same title.
const withYear = await searchUnmatchedFolder("rf-release-rank", folderName, "Portal (2022)");
expect(withYear.map((c) => c.igdbId)).toEqual([4, 3, 1, 2]);
});

it("scores a hyphenated typed title whole, not as a release group suffix", async () => {
const igdbClient = await scanReleaseFolder("rf-release-hyphen");
vi.mocked(igdbClient.searchGames).mockResolvedValue([
{ id: 1, name: "X" },
{ id: 2, name: "X-Men" },
] as never);

const found = await searchUnmatchedFolder("rf-release-hyphen", folderName, "X-Men (2000)");

expect(found.map((c) => c.igdbId)).toEqual([2, 1]);
});

it("keeps a typed standalone year as part of the title", async () => {
const igdbClient = await scanReleaseFolder("rf-release-f1");
vi.mocked(igdbClient.searchGames).mockResolvedValue([
{ id: 1, name: "F1" },
{ id: 2, name: "F1 2020" },
] as never);

const found = await searchUnmatchedFolder("rf-release-f1", folderName, "F1 2020");

expect(found.map((c) => c.igdbId)).toEqual([2, 1]);
});

it("still accepts a candidate offered before a later name search", async () => {
// Another tab still shows the scan's list after this one searched a name.
const igdbClient = await scanReleaseFolder("rf-release-two-tabs");
Expand Down
52 changes: 40 additions & 12 deletions server/library-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,40 @@ async function matchByIdTag(folderName: string): Promise<IGDBGame | null> {
}
}

/**
* A typed search is a title, not a release name: only id tags and a bracketed year go, so a
* trailing "-Men" or "-Life" is not mistaken for a release group.
*/
function typedTitle(query: string): string {
const stripped = query
.replace(/[{[(]\s*(?:igdb|steam)[-:= ]?\d{1,10}\s*[}\])]/gi, " ")
.replace(/[([]\s*(?:19|20)\d{2}\s*[)\]]/g, " ")
.replace(/\s+/g, " ")
.trim();
return stripped || query;
}

/**
* Orders IGDB results by how well their title matches `title`, best first. The year only breaks
* ties between equally good titles: it never changes a score, so it cannot lift a poor title over
* the auto-match threshold or hide a better one. Order: best title score, then a matching year,
* then IGDB's own order.
*/
function rankByTitle(
list: IGDBGame[],
title: string,
year?: number
): Array<{ game: IGDBGame; score: number; yearMatches: boolean; order: number }> {
return list
.map((game, order) => ({
game,
score: scoreMatch(title, game.name),
yearMatches: year !== undefined && releaseYearOf(game) === year,
order,
}))
.sort((a, b) => b.score - a.score || +b.yearMatches - +a.yearMatches || a.order - b.order);
}

async function bestIgdbMatch(
folderName: string,
platformId?: number
Expand All @@ -323,17 +357,7 @@ async function bestIgdbMatch(
if (byId) return { best: byId, candidates: [byId], cleaned, score: 1 };

const { year } = parseFolderHints(folderName);
const rank = (list: IGDBGame[]) =>
list
.map((game, order) => {
const base = scoreMatch(cleaned, game.name);
const yearMatches = year !== undefined && releaseYearOf(game) === year;
return { game, score: base, yearMatches, order };
})
// The year only breaks ties between equally good titles: it never changes a score, so
// it cannot lift a poor title over the auto-match threshold or hide a better one.
// Order: best title score, then a matching year, then IGDB's own order.
.sort((a, b) => b.score - a.score || +b.yearMatches - +a.yearMatches || a.order - b.order);
const rank = (list: IGDBGame[]) => rankByTitle(list, cleaned, year);
const pick = (list: IGDBGame[]) => {
const ranked = rank(list);
return {
Expand Down Expand Up @@ -526,7 +550,11 @@ export async function searchUnmatchedFolder(
): Promise<UnmatchedEntry["candidates"]> {
const entry = getUnmatchedEntry(rootFolderId, folderName);
if (!entry) throw new Error("No matching unmatched entry for this root folder");
const results = await igdbClient.searchGames(query, SCAN_CANDIDATE_LIMIT);
const found = await igdbClient.searchGames(query, SCAN_CANDIDATE_LIMIT);
// Best match first, like the scan's own candidates; a "(2018)" in the typed name breaks ties.
const results = rankByTitle(found, typedTitle(query), parseFolderHints(query).year).map(
(r) => r.game
);
// A rescan may have rebuilt the review list while IGDB answered; candidates
// stored on the old entry would never be offered for matching.
if (getUnmatchedEntry(rootFolderId, folderName) !== entry) {
Expand Down
Loading