Skip to content
0.2.0-dev Download ZIP
148 lines ·5.7 KB Raw
1using EvilEngine.Adapters.Steam;
2using EvilEngine.Mounts;
3
4namespace EvilEngine.Adapters.Tests;
5
6public sealed class SteamFinderTests : IDisposable
7{
8 private readonly string _folder = Path.Combine(Path.GetTempPath(), $"evil-steam-{Guid.NewGuid():N}");
9
10 public SteamFinderTests()
11 {
12 Directory.CreateDirectory(_folder);
13 }
14
15 private string Root => Path.Combine(_folder, "Steam");
16
17 private string Second => Path.Combine(_folder, "Games", "SteamLibrary");
18
19 public void Dispose()
20 {
21 Directory.Delete(_folder, true);
22 }
23
24 private static void App(string library, string id, string name, string folder, string state = "4",
25 bool installed = true)
26 {
27 var steamapps = Path.Combine(library, "steamapps");
28 Directory.CreateDirectory(steamapps);
29 File.WriteAllText(Path.Combine(steamapps, $"appmanifest_{id}.acf"), $$"""
30 "AppState"
31 {
32 "appid" "{{id}}"
33 "name" "{{name}}"
34 "StateFlags" "{{state}}"
35 "installdir" "{{folder}}"
36 }
37 """);
38 if (installed)
39 Directory.CreateDirectory(Path.Combine(steamapps, "common", folder));
40 }
41
42 private void Libraries()
43 {
44 Directory.CreateDirectory(Path.Combine(Root, "steamapps"));
45 File.WriteAllText(Path.Combine(Root, "steamapps", "libraryfolders.vdf"), $$"""
46 "libraryfolders"
47 {
48 "0"
49 {
50 "path" "{{Root}}"
51 "apps" { "4000" "1" "105600" "1" }
52 }
53 "1"
54 {
55 "path" "{{Second}}"
56 }
57 }
58 """);
59 App(Root, "4000", "Garry's Mod", "GarrysMod");
60 App(Root, "220200", "Kerbal Space Program", "Kerbal Space Program", "1026");
61 App(Second, "1944430", "Amnesia: The Bunker", "Amnesia The Bunker");
62 App(Second, "264710", "Subnautica", "Subnautica", installed: false);
63 }
64
65 [Fact]
66 public void OffersEveryFullyInstalledAppInEveryLibrary()
67 {
68 Libraries();
69
70 var installs = new SteamFinder([Root]).Installs();
71
72 Assert.Equal(
73 [
74 ("app/1944430", "Amnesia: The Bunker",
75 Path.Combine(Second, "steamapps", "common", "Amnesia The Bunker")),
76 ("app/4000", "Garry's Mod", Path.Combine(Root, "steamapps", "common", "GarrysMod"))
77 ],
78 installs.Select(install => (install.Key, install.Title, install.Path)));
79 Assert.All(installs, install => Assert.Equal("steam", install.Store));
80 }
81
82 [Fact]
83 public void ReadsALibraryOnceThoughTwoRootsReachIt()
84 {
85 Libraries();
86 var link = Path.Combine(_folder, "dot-steam");
87 Directory.CreateSymbolicLink(link, Root);
88
89 Assert.Equal(2, new SteamFinder([Root, link]).Installs().Count);
90 }
91
92 [Fact]
93 public void FindsAnAppByItsId()
94 {
95 Libraries();
96 var finder = new SteamFinder([Root]);
97
98 Assert.True(finder.TryFind("app/1944430", out var bunker));
99 Assert.Equal(Path.Combine(Second, "steamapps", "common", "Amnesia The Bunker"), bunker);
100 Assert.False(finder.TryFind("app/264710", out _));
101 Assert.False(finder.TryFind("app/105600", out _));
102 }
103
104 [Fact]
105 public void HandsBackTheArtSteamCachedForEachApp()
106 {
107 Libraries();
108 var gmod = Path.Combine(Root, "appcache", "librarycache", "4000");
109 Directory.CreateDirectory(gmod);
110 foreach (var name in new[]
111 {
112 "library_600x900.jpg", "library_header.jpg", "library_hero.jpg", "logo.png",
113 "4a6f25cfa2426445d0d9d6e233408de4d371ce8b.jpg"
114 })
115 File.WriteAllBytes(Path.Combine(gmod, name), [1]);
116 var bunker = Path.Combine(Root, "appcache", "librarycache", "1944430", "0f3c");
117 Directory.CreateDirectory(bunker);
118 File.WriteAllBytes(Path.Combine(bunker, "header.jpg"), [1]);
119
120 var installs = new SteamFinder([Root]).Installs().ToDictionary(install => install.Key);
121
122 Assert.Equal(new FoundArt(Path.Combine(gmod, "library_600x900.jpg"), Path.Combine(gmod, "library_header.jpg"),
123 Path.Combine(gmod, "library_hero.jpg"), Path.Combine(gmod, "logo.png"))
124 {
125 Icon = Path.Combine(gmod, "4a6f25cfa2426445d0d9d6e233408de4d371ce8b.jpg")
126 }, installs["app/4000"].Art);
127 Assert.Equal(new FoundArt(null, Path.Combine(bunker, "header.jpg"), null, null), installs["app/1944430"].Art);
128 }
129
130 [Theory]
131 [InlineData("4", InstallState.Installed)]
132 [InlineData("6", InstallState.UpdateQueued)]
133 [InlineData("68", InstallState.Installed)]
134 [InlineData("1030", InstallState.Updating)]
135 public void SaysWhetherSteamHasAnUpdateQueuedOrRunning(string flags, InstallState state)
136 {
137 Libraries();
138 App(Root, "252950", "Rocket League", "rocketleague", flags);
139
140 Assert.Equal(state, new SteamFinder([Root]).Installs().Single(install => install.Key == "app/252950").State);
141 }
142
143 [Fact]
144 public void OffersNothingWithoutSteam()
145 {
146 Assert.Empty(new SteamFinder([Path.Combine(_folder, "nowhere")]).Installs());
147 }
148}