// 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.Infrastructure.Art;
using FrameLedger.Infrastructure.Import;
namespace FrameLedger.Infrastructure.Tests.Import;
///
/// beta.33 (owner decisions D78/D79, spike-notes §25): the Xbox app's games, read from a games folder laid out as the
/// owner's are — <Game>\Content\ with MicrosoftGame.config and appxmanifest.xml in the shapes Valor
/// Mortis and FINAL FANTASY ship — and Windows' package answers stubbed, so the source's own decisions are what is tested.
///
public sealed class XboxLibrarySourceTests : IDisposable
{
private const string _valorConfig = """
76300F35
9P861V1542BL
""";
private const string _valorManifest = """
Valor Mortis
""";
private readonly string _root = Path.Combine(Path.GetTempPath(), "fl-xbox-" + Guid.NewGuid().ToString("N"));
public XboxLibrarySourceTests() => Directory.CreateDirectory(_root);
public void Dispose() => Directory.Delete(_root, recursive: true);
private static CancellationToken Ct => TestContext.Current.CancellationToken;
private string Game(string folder, string config, string manifest, string configName = GdkPackageFiles.ConfigFileName)
{
string content = Path.Combine(_root, folder, XboxLibrarySource.ContentFolder);
Directory.CreateDirectory(content);
File.WriteAllText(Path.Combine(content, configName), config);
File.WriteAllText(Path.Combine(content, GdkPackageFiles.ManifestFileName), manifest);
return content;
}
[Fact]
public async Task AnInstalledGdkPackageIsListedWithItsFamilyNameItsAumidAndItsContentFolder()
{
string content = Game("Valor Mortis", _valorConfig, _valorManifest);
var packages = new Packages("LyricalGamesLLC.ValorMortis_72fag5ekhk96y");
IReadOnlyList games = await new XboxLibrarySource([_root], packages).ListAsync(Ct);
games.Should().ContainSingle();
StoreGame game = games[0];
game.Platform.Should().Be("xbox");
game.StoreId.Should().Be("LyricalGamesLLC.ValorMortis_72fag5ekhk96y", "the family name is stable across updates; the WindowsApps folder's name is not");
game.Name.Should().Be("Valor Mortis");
game.InstallDirectory.Should().Be(content, "the spelling the game's own process reports (§25), never the WindowsApps junction");
game.ExePath.Should().BeNull("the package launches the stub; the locator picks the shipping executable that presents");
game.Version.Should().Be("1.3.5124.0");
game.LaunchKey.Should().Be("LyricalGamesLLC.ValorMortis_72fag5ekhk96y!AppValorMortisShipping", "the AUMID Windows' Start menu lists, which Play activates");
packages.Asked.Should().Equal(("LyricalGamesLLC.ValorMortis", "CN=C32D4B9A-1AEC-416A-8124-789B35AD6753"));
}
[Fact]
public async Task APackageWindowsDoesNotHaveInstalledForThisUserIsNotOffered()
{
Game("Left Behind", _valorConfig, _valorManifest);
(await new XboxLibrarySource([_root], new Packages()).ListAsync(Ct)).Should().BeEmpty(
"a folder the Xbox app left behind, or one installed for another user, is not a game this user can start");
}
[Fact]
public async Task FoldersThatAreNotPackagesCostOnlyThemselves()
{
Game("Valor Mortis", _valorConfig, _valorManifest);
Directory.CreateDirectory(Path.Combine(_root, "GameSave")); // the Xbox app's own folder, no Content
Directory.CreateDirectory(Path.Combine(_root, "Half", "Content"));
await File.WriteAllTextAsync(Path.Combine(_root, "Half", "Content", GdkPackageFiles.ConfigFileName), _valorConfig, Ct); // no manifest
Game("Broken", "", _valorManifest);
IReadOnlyList games = await new XboxLibrarySource([_root, Path.Combine(_root, "no-such-folder")],
new Packages("LyricalGamesLLC.ValorMortis_72fag5ekhk96y")).ListAsync(Ct);
games.Select(static g => g.Name).Should().Equal("Valor Mortis");
}
[Fact]
public async Task AResourceNameFallsBackToTheManifestsThenToTheFolders()
{
string config = _valorConfig.Replace("DefaultDisplayName=\"Valor Mortis\"", "DefaultDisplayName=\"ms-resource:AppDisplayName\"", StringComparison.Ordinal);
Game("Valor Mortis", config, _valorManifest);
Game("FINAL FANTASY", config.Replace("LyricalGamesLLC.ValorMortis", "39EA002F.FF1DF", StringComparison.Ordinal),
_valorManifest.Replace("Valor Mortis", "ms-resource:AppDisplayName", StringComparison.Ordinal),
configName: "MicrosoftGame.Config");
IReadOnlyList games = await new XboxLibrarySource([_root], new Packages("LyricalGamesLLC.ValorMortis_72fag5ekhk96y", "39EA002F.FF1DF_n746a19ndrrjg")).ListAsync(Ct);
games.Select(static g => g.Name).Should().BeEquivalentTo(["Valor Mortis", "FINAL FANTASY"],
"an ms-resource reference is not a name: the manifest's own DisplayName, else the game's folder (the FINAL FANTASY titles spell the config MicrosoftGame.Config)");
}
[Fact]
public async Task TheApplicationThatRunsTheLaunchHelperIsTheOneActivated()
{
string manifest = _valorManifest.Replace(
"]>" + _valorConfig[(_valorConfig.IndexOf("
/// beta.33 (D80): the agent's test before a LaunchGame — an executable whose install root is a GDK package root is
/// a game Windows starts through its package, never by its path. The Unreal shipping executable three levels down is in
/// its package; a Steam game's is not, even beside a stray package file.
///
[Fact]
public void AnExecutableAnywhereInAPackageIsInItAndAStoresGameIsNot()
{
string content = Game("Valor Mortis", _valorConfig, _valorManifest);
string shipping = Path.Combine(content, "ValorMortis", "Binaries", "Win64", "ValorMortisMSG-Win64-Shipping.exe");
Directory.CreateDirectory(Path.GetDirectoryName(shipping)!);
GdkPackageFiles.IsInPackage(shipping).Should().BeTrue();
GdkPackageFiles.IsInPackage(Path.Combine(content, "ValorMortisMSG.exe")).Should().BeTrue();
string steam = Path.Combine(_root, "steamapps", "common", "FINAL FANTASY");
Directory.CreateDirectory(steam);
File.WriteAllText(Path.Combine(steam, GdkPackageFiles.ManifestFileName), _valorManifest);
GdkPackageFiles.IsInPackage(Path.Combine(steam, "FINAL FANTASY.exe")).Should().BeFalse("a Steam root is the boundary, and it holds no package config");
GdkPackageFiles.IsInPackage(Path.Combine(_root, "Loose", "game.exe")).Should().BeFalse();
}
[Fact]
public void WhatIsNotAPackageCostsNothing()
{
GdkPackageFiles.IsPackageRoot(" ").Should().BeFalse();
GdkPackageFiles.NearestPackageRoot(" ").Should().BeNull();
GdkPackageFiles.NearestPackageRoot(_root).Should().BeNull("no folder at or above it holds a package's two files");
GdkPackageFiles.IsInPackage(Path.Combine(_root, "a\0b", "game.exe")).Should().BeFalse("a path that is no path is in no package");
string big = Game("Big", _valorConfig + new string(' ', (int)GdkPackageFiles.MaxFileBytes), _valorManifest);
GdkPackageFiles.Read(big).Should().BeNull("a description file over the cap is not read");
new XboxLibrarySource([_root], new Packages()).Platform.Should().Be("xbox");
var art = new XboxArtCache();
art.Platform.Should().Be("xbox");
art.Find(new StoreGame("xbox", "Gone.Game_0000000000000", "Gone", Path.Combine(_root, "Gone", "Content"), null, null))
.Should().Be(Application.Art.GameArtFiles.None, "a folder that is not a package has no pictures to show");
XboxArtCache.Image(_root, " ").Should().BeNull();
XboxArtCache.Image(_root, "a\0b.png").Should().BeNull();
}
/// Windows' package answers, stubbed: the family name is the one listed whose name part matches; installed = listed.
private sealed class Packages(params string[] installed) : IInstalledPackages
{
public List<(string Name, string Publisher)> Asked { get; } = [];
public string? FamilyName(string name, string publisher)
{
Asked.Add((name, publisher));
return installed.FirstOrDefault(f => f.StartsWith(name + "_", StringComparison.Ordinal)) ?? name + "_0000000000000";
}
public bool IsInstalled(string familyName) => installed.Contains(familyName, StringComparer.Ordinal);
}
}