Files
poli0981andClaude Opus 5.5 38fdb6bd32 fix(import): a helper a store names is never the game, and an NW.js runtime outranks its launcher (beta.19 PR-1) (#292)
The owner's A Doll's Locket - Remake (itch 5085165, RPG Maker MZ) was imported as
Dolls Locket\notification_helper.exe. The pick was butler's verdict, taken as is:
butler stores a filtered verdict - the shallowest candidates, minus those whose
manifest asks for elevation - and does not rank what is left, so with the 213 MB
DollsLocket.exe launcher asking for administrator and the NW.js runtime a level
deeper in css\, the helper was the whole verdict (read from the owner's butler.db,
read-only).

- ItchLibrarySource skips a verdict candidate whose whole name is a runtime helper
  (ExecutableLocator.RuntimeHelperNames) or that sits in a Unity plugin folder; with
  nothing left the locator guesses and the review marks the row guessed. Whole
  names, not the locator's fragments: "node" is inside Nodebuster.
- ExecutableLocator ranks an executable beside nw.dll + package.json as an NW.js
  runtime (owner decision D56): below the Unity and Unreal layouts, above a name
  match. On the owner's disk it now picks css\Game.exe for Doll's Locket and
  swiftshader\Game.exe for HELLO, HELLO WORLD! (what D28 chose by hand); every other
  install it was run over picks what it picked before. notification_helper, nwjc
  and chromedriver join the helper fragments.
- LibraryImporter matches a store title to an entry by platform + store id as well
  as by path, so a corrected guess does not offer the same game a second time; the
  existing entry is re-pointed with Change executable... (nothing rewrites it).

HANDOFF owner-only item 14 (beta.18 on real hardware) is struck: passed 2026-10-08.

* fix(import): a store title is its entry by store id also when the entry's executable is inside the title's folder (beta.19 PR-1)

Found in a self-review after the stack's gate: the platform an import writes
is badged "detected", and the detection sweep refreshes it with what its own
rules find - an itch build that ships steam_api64.dll reads as Steam. Matching
the store id under the same platform only would then offer the same itch
title again beside its entry. The id now matches under the same platform, or
with the entry's executable inside the title's install folder; the same number
under another platform elsewhere is another store's numbering and stays a new
candidate (LibraryImporterTests.TheSameStoreIdInsideTheTitlesFolderIsTheSameTitleWhateverThePlatformReadsAs).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 15:10:47 +07:00

220 lines
13 KiB
C#

// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 poli0981 - additional terms under GPLv3 section 7: see NOTICE
using FluentAssertions;
using FrameLedger.Application.Import;
using FrameLedger.Application.Persistence;
using FrameLedger.Application.Tests.Recording;
using FrameLedger.Application.Watch;
using FrameLedger.Domain.Consent;
namespace FrameLedger.Application.Tests.Import;
/// <summary>
/// FR-1.2 (P4 PR-4): discovery joins every store's titles to the ledger — the store's executable where it names one,
/// the locator's guess where it does not, "already in the library" where the row exists, no executable where none
/// is found — and the import adds exactly the ticked rows, hooking off, with the store's facts applied. A store that
/// throws costs its own titles, never the others'.
/// </summary>
public sealed class LibraryImporterTests
{
private static CancellationToken Ct => TestContext.Current.CancellationToken;
private sealed class ScriptedStore(string platform, params StoreGame[] games) : IStoreLibrarySource
{
public bool Throws { get; set; }
public string Platform => platform;
public ValueTask<IReadOnlyList<StoreGame>> ListAsync(CancellationToken ct = default) =>
Throws ? throw new IOException("launcher files unreadable") : ValueTask.FromResult<IReadOnlyList<StoreGame>>(games);
}
private sealed class FakeIdentity : IExecutableIdentitySource
{
public HashSet<string> Existing { get; } = new(StringComparer.OrdinalIgnoreCase);
public ExecutableFingerprint? Read(string normalisedExePath) =>
Existing.Contains(normalisedExePath) ? new ExecutableFingerprint { ExePath = normalisedExePath, SizeBytes = 7, MtimeUnixMs = 7 } : null;
public string Normalise(string exePath) => exePath.ToUpperInvariant();
}
private sealed class FakeLocator : IExecutableLocator
{
public Dictionary<string, string?> Guesses { get; } = new(StringComparer.OrdinalIgnoreCase);
public string? PickPrimary(string installDirectory) => Guesses.GetValueOrDefault(installDirectory);
}
/// <summary>
/// A game whose entry was made while its drive had another letter (2026-09-23): the entry's file is gone and this is the
/// same path on the new letter. Importing it again made the second, side-by-side entry the owner's library showed for
/// every game on the re-lettered drive; the review says where it is, and the import adds nothing — the Agent's
/// relocator moves the entry to the file within one sweep.
/// </summary>
[Fact]
public async Task AGameInTheLibraryUnderAnotherDriveLetterIsNotImportedAgain()
{
var games = new FakeGameRepository();
var identity = new FakeIdentity();
List<string> log = [];
const string stale = @"H:\STEAM\STEAMAPPS\COMMON\TITLE\TITLE.EXE";
await games.EnsureAsync(new ExecutableFingerprint { ExePath = stale, SizeBytes = 7, MtimeUnixMs = 7 }, "Title", Ct);
identity.Existing.Add(@"D:\STEAM\STEAMAPPS\COMMON\TITLE\TITLE.EXE");
var steam = new ScriptedStore("steam", new StoreGame("steam", "1", "Title", @"D:\Steam\steamapps\common\Title", @"D:\Steam\steamapps\common\Title\Title.exe", null));
var importer = new LibraryImporter([steam], games, identity, new FakeLocator(), log.Add);
ImportCandidate found = (await importer.DiscoverAsync(Ct)).Should().ContainSingle().Subject;
found.MovedFrom.Should().Be(stale);
found.AlreadyInLibrary.Should().BeFalse("not at this path");
found.CanImport.Should().BeFalse("the entry follows the file; a second entry is the bug");
ImportReport report = await importer.ImportAsync([found], Ct);
report.Added.Should().Be(0);
report.Skipped.Should().Be(1);
games.Rows.Should().ContainSingle().Which.Key.Should().Be(stale, "nothing was added beside it");
log.Should().Contain(static l => l.Contains("drive whose letter changed", StringComparison.Ordinal));
// Both files there: two copies, not a move — the second is a game of its own.
identity.Existing.Add(stale);
(await importer.DiscoverAsync(Ct)).Single().MovedFrom.Should().BeNull();
}
[Fact]
public async Task DiscoveryJoinsTheStoresToTheLedgerAndImportAddsOnlyWhatWasTicked()
{
var games = new FakeGameRepository();
var identity = new FakeIdentity();
var locator = new FakeLocator();
List<string> log = [];
var steam = new ScriptedStore("steam",
new StoreGame("steam", "1091500", "Cyberpunk 2077", @"D:\Steam\steamapps\common\Cyberpunk 2077", null, "15877371"),
new StoreGame("steam", "2358720", "Wukong", @"D:\Steam\steamapps\common\BlackMythWukong", null, null));
var gog = new ScriptedStore("gog", new StoreGame("gog", "1207658924", "The Witcher 3", @"D:\GOG\W3", @"D:\GOG\W3\bin\witcher3.exe", "4.04"));
locator.Guesses[@"D:\Steam\steamapps\common\Cyberpunk 2077"] = @"D:\Steam\steamapps\common\Cyberpunk 2077\bin\x64\Cyberpunk2077.exe";
identity.Existing.Add(@"D:\STEAM\STEAMAPPS\COMMON\CYBERPUNK 2077\BIN\X64\CYBERPUNK2077.EXE");
identity.Existing.Add(@"D:\GOG\W3\BIN\WITCHER3.EXE");
// The Witcher is already in the library, added by hand.
await games.EnsureAsync(new ExecutableFingerprint { ExePath = @"D:\GOG\W3\BIN\WITCHER3.EXE", SizeBytes = 1, MtimeUnixMs = 1 }, "witcher3", Ct);
var importer = new LibraryImporter([steam, gog], games, identity, locator, log.Add);
IReadOnlyList<ImportCandidate> found = await importer.DiscoverAsync(Ct);
found.Should().HaveCount(3);
ImportCandidate cp = found.Single(static c => string.Equals(c.Game.StoreId, "1091500", StringComparison.Ordinal));
cp.ExePath.Should().Be(@"D:\STEAM\STEAMAPPS\COMMON\CYBERPUNK 2077\BIN\X64\CYBERPUNK2077.EXE", "the locator's guess, normalised");
cp.ExecutableGuessed.Should().BeTrue();
cp.CanImport.Should().BeTrue();
ImportCandidate wukong = found.Single(static c => string.Equals(c.Game.StoreId, "2358720", StringComparison.Ordinal));
wukong.ExePath.Should().BeNull("nothing found under its install directory");
wukong.CanImport.Should().BeFalse();
ImportCandidate w3 = found.Single(static c => string.Equals(c.Game.StoreId, "1207658924", StringComparison.Ordinal));
w3.ExecutableGuessed.Should().BeFalse("GOG named it");
w3.AlreadyInLibrary.Should().BeTrue();
w3.CanImport.Should().BeFalse();
ImportReport report = await importer.ImportAsync([cp, w3], Ct);
report.Added.Should().Be(1);
report.Skipped.Should().Be(1, "the Witcher was already there — left as it was, its store facts applied");
GameRow added = games.Rows[cp.ExePath!];
added.Name.Should().Be("Cyberpunk 2077", "the store's name, not the exe's");
added.HookEnabled.Should().BeFalse("import never enables hooking — FR-1.2");
added.Platform.Should().Be("steam");
added.StoreId.Should().Be("1091500");
added.GameVersion.Should().Be("15877371");
games.Stores.Should().HaveCount(2);
games.Rows.Should().HaveCount(2);
log.Should().Contain(static l => l.Contains("added Cyberpunk 2077", StringComparison.Ordinal) && l.Contains("hooking off", StringComparison.Ordinal));
}
/// <summary>
/// beta.19: the owner's library holds A Doll's Locket - Remake at the helper an earlier import took from butler's
/// verdict. This build guesses the NW.js runtime instead — and without matching the store's own id it would offer the
/// same game a second time beside the entry the owner already has (and re-points with Change executable…).
/// </summary>
[Fact]
public async Task AStoreTitleAlreadyInTheLibraryAtAnotherExecutableIsNotOfferedAgain()
{
var games = new FakeGameRepository();
var identity = new FakeIdentity();
var locator = new FakeLocator();
List<string> log = [];
const string install = @"D:\another\it\a-dolls-locket-remake";
const string helper = @"D:\ANOTHER\IT\A-DOLLS-LOCKET-REMAKE\DOLLS LOCKET\NOTIFICATION_HELPER.EXE";
GameRow old = await games.EnsureAsync(new ExecutableFingerprint { ExePath = helper, SizeBytes = 7, MtimeUnixMs = 7 }, "A Doll's Locket - Remake", Ct);
await games.ApplyStoreMetadataAsync(old.Id, new StoreMetadata { Platform = "itch", StoreId = "5085165" }, Ct);
identity.Existing.Add(helper);
identity.Existing.Add(@"D:\ANOTHER\IT\A-DOLLS-LOCKET-REMAKE\DOLLS LOCKET\CSS\GAME.EXE");
locator.Guesses[install] = install + @"\Dolls Locket\css\Game.exe";
var itch = new ScriptedStore("itch",
new StoreGame("itch", "5085165", "A Doll's Locket - Remake", install, null, null),
new StoreGame("itch", "1659887", "Plethora of Bullets 2", @"D:\another\it\plethora-of-bullets-2", @"D:\another\it\plethora-of-bullets-2\POB2.exe", null));
identity.Existing.Add(@"D:\ANOTHER\IT\PLETHORA-OF-BULLETS-2\POB2.EXE");
var importer = new LibraryImporter([itch], games, identity, locator, log.Add);
IReadOnlyList<ImportCandidate> found = await importer.DiscoverAsync(Ct);
ImportCandidate dolls = found.Single(static c => string.Equals(c.Game.StoreId, "5085165", StringComparison.Ordinal));
dolls.AlreadyInLibrary.Should().BeTrue("the same itch title is an entry already, at the executable an earlier import chose");
dolls.CanImport.Should().BeFalse();
found.Single(static c => string.Equals(c.Game.StoreId, "1659887", StringComparison.Ordinal)).CanImport.Should().BeTrue("another title of the same store is not matched");
log.Should().Contain(l => l.Contains("already in the library as " + helper, StringComparison.Ordinal));
(await importer.ImportAsync([.. found.Where(static c => c.CanImport)], Ct)).Added.Should().Be(1);
games.Rows.Should().HaveCount(2, "Plethora was added and A Doll's Locket stayed one entry");
}
/// <summary>
/// The entry's platform is the detection sweep's to refresh — an itch build that ships Steam's API reads as Steam — so the
/// same store id with the entry's executable inside the title's install folder is the same title too; outside it, an id
/// under another platform is another store's numbering, not this game.
/// </summary>
[Fact]
public async Task TheSameStoreIdInsideTheTitlesFolderIsTheSameTitleWhateverThePlatformReadsAs()
{
var games = new FakeGameRepository();
var identity = new FakeIdentity();
var locator = new FakeLocator();
const string install = @"D:\another\it\some-game";
const string old = @"D:\ANOTHER\IT\SOME-GAME\NOTIFICATION_HELPER.EXE";
GameRow entry = await games.EnsureAsync(new ExecutableFingerprint { ExePath = old, SizeBytes = 7, MtimeUnixMs = 7 }, "Some Game", Ct);
await games.ApplyStoreMetadataAsync(entry.Id, new StoreMetadata { Platform = "steam", StoreId = "42" }, Ct);
identity.Existing.Add(@"D:\ANOTHER\IT\SOME-GAME\GAME.EXE");
locator.Guesses[install] = install + @"\Game.exe";
identity.Existing.Add(@"D:\ANOTHER\IT\OTHER\OTHER.EXE");
locator.Guesses[@"D:\another\it\other"] = @"D:\another\it\other\Other.exe";
var itch = new ScriptedStore("itch",
new StoreGame("itch", "42", "Some Game", install, null, null),
new StoreGame("itch", "42", "Another store's 42", @"D:\another\it\other", null, null));
IReadOnlyList<ImportCandidate> found = await new LibraryImporter([itch], games, identity, locator, static _ => { }).DiscoverAsync(Ct);
found.Single(static c => string.Equals(c.Game.Name, "Some Game", StringComparison.Ordinal)).AlreadyInLibrary.Should().BeTrue("its entry's executable is inside the title's folder");
found.Single(static c => string.Equals(c.Game.Name, "Another store's 42", StringComparison.Ordinal)).CanImport.Should().BeTrue("the same number under another platform, elsewhere, is not this title");
}
[Fact]
public async Task AStoreThatThrowsCostsItsOwnTitlesAndTheSameExecutableUnderTwoStoresIsOneRow()
{
var games = new FakeGameRepository();
var identity = new FakeIdentity();
var locator = new FakeLocator();
List<string> log = [];
var epic = new ScriptedStore("epic", new StoreGame("epic", "e1", "Shared", @"D:\G\Shared", @"D:\G\Shared\game.exe", null)) { Throws = true };
var gog = new ScriptedStore("gog", new StoreGame("gog", "g1", "Shared", @"D:\G\Shared", @"D:\G\Shared\game.exe", "1.0"));
var itch = new ScriptedStore("itch", new StoreGame("itch", "i1", "Shared again", @"D:\G\Shared", @"D:\G\Shared\GAME.EXE", null));
IReadOnlyList<ImportCandidate> found = await new LibraryImporter([epic, gog, itch], games, identity, locator, log.Add).DiscoverAsync(Ct);
found.Should().ContainSingle().Which.Game.Platform.Should().Be("gog", "epic threw and itch named the same executable, so the first store to name it keeps it");
log.Should().Contain(static l => l.StartsWith("import: epic library unreadable", StringComparison.Ordinal));
ImportReport report = await new LibraryImporter([gog], games, identity, locator, log.Add).ImportAsync(found, Ct);
report.Added.Should().Be(0);
report.Skipped.Should().Be(1, "the executable is not on disk, so nothing can be fingerprinted or added");
games.Rows.Should().BeEmpty();
}
}