using EvilEngine.Mounts; namespace EvilEngine.Host.Tests; public sealed class MountLibraryTests { private static readonly AdapterManifest[] Adapters = [ AdapterManifest.Parse(""" id = "gmod" name = "Garry's Mod" version = "0.1.0" api = 1 code = "Code/gmod.dll" games = ["gmod"] engine = "Source" [titles] gmod = "Garry's Mod" [stores] gmod = { steam = 4000 } """, "gmod/adapter.toml"), AdapterManifest.Parse(""" id = "sourcegames" name = "Source games" version = "0.1.0" api = 1 code = "Code/sourcegames.dll" games = ["portal", "hl2", "episodic", "lostcoast"] engine = "Source" [titles] portal = "Portal" hl2 = "Half-Life 2" episodic = "Half-Life 2: Episode One" lostcoast = "Half-Life 2: Lost Coast" [stores] portal = { steam = 400 } hl2 = { steam = 220 } episodic = { steam = 380 } lostcoast = { steam = 340 } """, "sourcegames/adapter.toml"), AdapterManifest.Parse(""" id = "quake" name = "Quake" version = "0.1.0" api = 1 code = "Code/quake.dll" games = ["quake"] engine = "Quake" [titles] quake = "Quake" [stores] quake = { steam = 2310 } """, "quake/adapter.toml"), AdapterManifest.Parse(""" id = "skate3" name = "Skate 3" version = "0.1.0" api = 1 code = "Code/skate3.dll" games = ["skate3"] engine = "EA Pegasus" [titles] skate3 = "Skate 3" """, "skate3/adapter.toml") ]; private const string Steamapps = "/games/steamapps/common"; private static FoundInstall Steam(string app, string title, string folder, InstallState state = InstallState.Installed) { return new FoundInstall("steam", $"app/{app}", title, $"{Steamapps}/{folder}") { State = state }; } private static PointerReport Pointer(string path, PointerStatus status, params string[] ids) { return new PointerReport(new StoredPointer($"/mounts/{Path.GetFileName(path)}.toml", new MountPointer(path) { Enabled = status != PointerStatus.Disabled, LastProbed = ids.Select(Id).ToArray() }), status); } private static MountId Id(string id) { var parts = id.Split('/'); return new MountId(parts[0], parts[1], parts[2]); } private static FoundArt Art(string store, string key) { return new FoundArt(null, $"/art/{store}/{key}/header.jpg", null, null); } [Fact] public void ListsEachInstallOnceTitledByItsFirstGameWithEveryGameItHolds() { FoundInstall[] installs = [ Steam("220", "Half-Life 2", "Half-Life 2"), Steam("340", "Half-Life 2: Lost Coast", "Half-Life 2"), Steam("380", "Half-Life 2: Episode One", "Half-Life 2") ]; MountId[] ids = [Id("lostcoast/pc/1"), Id("hl2/pc/1"), Id("episodic/pc/1")]; var offers = installs.Select(install => new InstallOffer(install, ids)).ToArray(); var library = MountLibrary.Build(Adapters, [], installs, offers, Art); var hl2 = Assert.Single(library.Entries, entry => entry.Path is not null); Assert.Equal("Half-Life 2", hl2.Title); Assert.Equal(["Half-Life 2", "Half-Life 2: Episode One", "Half-Life 2: Lost Coast"], hl2.Games); Assert.Equal("app/220", hl2.Install!.Key); Assert.Equal(new MountLicence("steam", "220"), hl2.Licence); Assert.Equal("/art/steam/app/220/header.jpg", hl2.Art.Header); Assert.Equal((LibraryState.Off, false, true), (hl2.State, hl2.Ticked, hl2.CanTick)); } [Fact] public void FoldsByEngineSourceFirstAndCountsWhatsMounted() { PointerReport[] pointers = [ Pointer($"{Steamapps}/GarrysMod", PointerStatus.Mounted, "gmod/pc/1"), Pointer("/discs/skate3.iso", PointerStatus.Mounted, "skate3/ps3/BLUS30464-01.00"), Pointer($"{Steamapps}/Portal", PointerStatus.Disabled, "portal/pc/1") ]; var library = MountLibrary.Build(Adapters, pointers, [Steam("4000", "Garry's Mod", "GarrysMod"), Steam("400", "Portal", "Portal")], [], Art); var folds = library.Folds(LibraryFilter.Library); Assert.Equal(["Source", "EA Pegasus"], folds.Select(fold => fold.Engine)); Assert.Equal(["Garry's Mod", "Portal"], folds[0].Entries.Select(entry => entry.Title)); Assert.Equal((1, 2), (folds[0].Mounted, folds[0].Entries.Count)); Assert.True(library.Folds(LibraryFilter.Library)[1].Entries[0].AddedByHand); Assert.Equal(["Garry's Mod", "Skate 3"], library.Folds(LibraryFilter.Mounted).SelectMany(fold => fold.Entries).Select(entry => entry.Title)); Assert.Equal(["Portal"], library.Folds(LibraryFilter.Library, "port").SelectMany(fold => fold.Entries).Select(entry => entry.Title)); } [Fact] public void ListsCatalogueGamesNoInstallHoldsOnlyUnderEverything() { var library = MountLibrary.Build(Adapters, [Pointer($"{Steamapps}/GarrysMod", PointerStatus.Mounted, "gmod/pc/1")], [Steam("4000", "Garry's Mod", "GarrysMod")], [], Art); Assert.DoesNotContain(library.Folds(LibraryFilter.Library).SelectMany(fold => fold.Entries), entry => entry.State == LibraryState.NotInstalled); var everything = library.Folds(LibraryFilter.Everything).SelectMany(fold => fold.Entries).ToArray(); Assert.Equal(["Garry's Mod", "Half-Life 2", "Half-Life 2: Episode One", "Half-Life 2: Lost Coast", "Portal", "Quake"], everything.Select(entry => entry.Title)); var quake = everything.Single(entry => entry.Title == "Quake"); Assert.Equal(("store:steam/2310", "Not installed", LibraryAction.InstallInSteam, false), (quake.Key, quake.Status, quake.Action, quake.CanTick)); Assert.DoesNotContain(everything, entry => entry.Title == "Skate 3"); } [Fact] public void LeavesOutEveryGameTheBuildDoesntSwitchOn() { var library = MountLibrary.Build(Adapters, [ Pointer($"{Steamapps}/GarrysMod", PointerStatus.Mounted, "gmod/pc/1"), Pointer("/games/Quake", PointerStatus.Hidden, "quake/pc/1.06") ], [Steam("4000", "Garry's Mod", "GarrysMod")], [], Art, game => game is "gmod" or "portal" or "hl2"); Assert.Equal(["Garry's Mod", "Half-Life 2", "Portal"], library.Folds(LibraryFilter.Everything).SelectMany(fold => fold.Entries).Select(entry => entry.Title)); } [Theory] [InlineData(PointerStatus.Offline, "Offline", LibraryAction.Locate, false)] [InlineData(PointerStatus.Changed, "Changed build", LibraryAction.AcceptBuild, false)] [InlineData(PointerStatus.Unrecognised, "Unrecognised", LibraryAction.Remove, false)] [InlineData(PointerStatus.Duplicate, "Duplicate", LibraryAction.None, true)] [InlineData(PointerStatus.Mounted, null, LibraryAction.None, true)] public void SaysWhatStandsInAPointersWay(PointerStatus status, string? said, LibraryAction action, bool tick) { var library = MountLibrary.Build(Adapters, [Pointer("/games/Quake", status, "quake/pc/1.06")], [], [], Art); var quake = library.Entries.Single(entry => entry.Report is not null); Assert.Equal((said, action, tick), (quake.Status, quake.Action, quake.CanTick)); } [Fact] public void StopsTickingWhileSteamUpdatesAndSaysAQueuedUpdate() { var library = MountLibrary.Build(Adapters, [ Pointer($"{Steamapps}/Portal", PointerStatus.Disabled, "portal/pc/1"), Pointer($"{Steamapps}/GarrysMod", PointerStatus.Mounted, "gmod/pc/1") ], [ Steam("400", "Portal", "Portal", InstallState.Updating), Steam("4000", "Garry's Mod", "GarrysMod", InstallState.UpdateQueued) ], [], Art); var portal = library.Entries.Single(entry => entry.Title == "Portal"); Assert.Equal((LibraryState.Blocked, false, "Updating"), (portal.State, portal.CanTick, portal.Status)); var gmod = library.Entries.Single(entry => entry.Title == "Garry's Mod"); Assert.Equal((true, "Update queued"), (gmod.CanTick, gmod.Status)); } }