using EvilEngine.Adapters.Steam; using EvilEngine.Mounts; namespace EvilEngine.Adapters.Tests; public sealed class SteamFinderTests : IDisposable { private readonly string _folder = Path.Combine(Path.GetTempPath(), $"evil-steam-{Guid.NewGuid():N}"); public SteamFinderTests() { Directory.CreateDirectory(_folder); } private string Root => Path.Combine(_folder, "Steam"); private string Second => Path.Combine(_folder, "Games", "SteamLibrary"); public void Dispose() { Directory.Delete(_folder, true); } private static void App(string library, string id, string name, string folder, string state = "4", bool installed = true) { var steamapps = Path.Combine(library, "steamapps"); Directory.CreateDirectory(steamapps); File.WriteAllText(Path.Combine(steamapps, $"appmanifest_{id}.acf"), $$""" "AppState" { "appid" "{{id}}" "name" "{{name}}" "StateFlags" "{{state}}" "installdir" "{{folder}}" } """); if (installed) Directory.CreateDirectory(Path.Combine(steamapps, "common", folder)); } private void Libraries() { Directory.CreateDirectory(Path.Combine(Root, "steamapps")); File.WriteAllText(Path.Combine(Root, "steamapps", "libraryfolders.vdf"), $$""" "libraryfolders" { "0" { "path" "{{Root}}" "apps" { "4000" "1" "105600" "1" } } "1" { "path" "{{Second}}" } } """); App(Root, "4000", "Garry's Mod", "GarrysMod"); App(Root, "220200", "Kerbal Space Program", "Kerbal Space Program", "1026"); App(Second, "1944430", "Amnesia: The Bunker", "Amnesia The Bunker"); App(Second, "264710", "Subnautica", "Subnautica", installed: false); } [Fact] public void OffersEveryFullyInstalledAppInEveryLibrary() { Libraries(); var installs = new SteamFinder([Root]).Installs(); Assert.Equal( [ ("app/1944430", "Amnesia: The Bunker", Path.Combine(Second, "steamapps", "common", "Amnesia The Bunker")), ("app/4000", "Garry's Mod", Path.Combine(Root, "steamapps", "common", "GarrysMod")) ], installs.Select(install => (install.Key, install.Title, install.Path))); Assert.All(installs, install => Assert.Equal("steam", install.Store)); } [Fact] public void ReadsALibraryOnceThoughTwoRootsReachIt() { Libraries(); var link = Path.Combine(_folder, "dot-steam"); Directory.CreateSymbolicLink(link, Root); Assert.Equal(2, new SteamFinder([Root, link]).Installs().Count); } [Fact] public void FindsAnAppByItsId() { Libraries(); var finder = new SteamFinder([Root]); Assert.True(finder.TryFind("app/1944430", out var bunker)); Assert.Equal(Path.Combine(Second, "steamapps", "common", "Amnesia The Bunker"), bunker); Assert.False(finder.TryFind("app/264710", out _)); Assert.False(finder.TryFind("app/105600", out _)); } [Fact] public void HandsBackTheArtSteamCachedForEachApp() { Libraries(); var gmod = Path.Combine(Root, "appcache", "librarycache", "4000"); Directory.CreateDirectory(gmod); foreach (var name in new[] { "library_600x900.jpg", "library_header.jpg", "library_hero.jpg", "logo.png", "4a6f25cfa2426445d0d9d6e233408de4d371ce8b.jpg" }) File.WriteAllBytes(Path.Combine(gmod, name), [1]); var bunker = Path.Combine(Root, "appcache", "librarycache", "1944430", "0f3c"); Directory.CreateDirectory(bunker); File.WriteAllBytes(Path.Combine(bunker, "header.jpg"), [1]); var installs = new SteamFinder([Root]).Installs().ToDictionary(install => install.Key); Assert.Equal(new FoundArt(Path.Combine(gmod, "library_600x900.jpg"), Path.Combine(gmod, "library_header.jpg"), Path.Combine(gmod, "library_hero.jpg"), Path.Combine(gmod, "logo.png")) { Icon = Path.Combine(gmod, "4a6f25cfa2426445d0d9d6e233408de4d371ce8b.jpg") }, installs["app/4000"].Art); Assert.Equal(new FoundArt(null, Path.Combine(bunker, "header.jpg"), null, null), installs["app/1944430"].Art); } [Theory] [InlineData("4", InstallState.Installed)] [InlineData("6", InstallState.UpdateQueued)] [InlineData("68", InstallState.Installed)] [InlineData("1030", InstallState.Updating)] public void SaysWhetherSteamHasAnUpdateQueuedOrRunning(string flags, InstallState state) { Libraries(); App(Root, "252950", "Rocket League", "rocketleague", flags); Assert.Equal(state, new SteamFinder([Root]).Installs().Single(install => install.Key == "app/252950").State); } [Fact] public void OffersNothingWithoutSteam() { Assert.Empty(new SteamFinder([Path.Combine(_folder, "nowhere")]).Installs()); } }