Skip to content
0.2.0-dev Download ZIP
206 lines ·9.5 KB Raw
1using EvilEngine.Mounts;
2
3namespace EvilEngine.Host.Tests;
4
5public sealed class MountLibraryTests
6{
7 private static readonly AdapterManifest[] Adapters =
8 [
9 AdapterManifest.Parse("""
10 id = "gmod"
11 name = "Garry's Mod"
12 version = "0.1.0"
13 api = 1
14 code = "Code/gmod.dll"
15 games = ["gmod"]
16 engine = "Source"
17 [titles]
18 gmod = "Garry's Mod"
19 [stores]
20 gmod = { steam = 4000 }
21 """, "gmod/adapter.toml"),
22 AdapterManifest.Parse("""
23 id = "sourcegames"
24 name = "Source games"
25 version = "0.1.0"
26 api = 1
27 code = "Code/sourcegames.dll"
28 games = ["portal", "hl2", "episodic", "lostcoast"]
29 engine = "Source"
30 [titles]
31 portal = "Portal"
32 hl2 = "Half-Life 2"
33 episodic = "Half-Life 2: Episode One"
34 lostcoast = "Half-Life 2: Lost Coast"
35 [stores]
36 portal = { steam = 400 }
37 hl2 = { steam = 220 }
38 episodic = { steam = 380 }
39 lostcoast = { steam = 340 }
40 """, "sourcegames/adapter.toml"),
41 AdapterManifest.Parse("""
42 id = "quake"
43 name = "Quake"
44 version = "0.1.0"
45 api = 1
46 code = "Code/quake.dll"
47 games = ["quake"]
48 engine = "Quake"
49 [titles]
50 quake = "Quake"
51 [stores]
52 quake = { steam = 2310 }
53 """, "quake/adapter.toml"),
54 AdapterManifest.Parse("""
55 id = "skate3"
56 name = "Skate 3"
57 version = "0.1.0"
58 api = 1
59 code = "Code/skate3.dll"
60 games = ["skate3"]
61 engine = "EA Pegasus"
62 [titles]
63 skate3 = "Skate 3"
64 """, "skate3/adapter.toml")
65 ];
66
67 private const string Steamapps = "/games/steamapps/common";
68
69 private static FoundInstall Steam(string app, string title, string folder,
70 InstallState state = InstallState.Installed)
71 {
72 return new FoundInstall("steam", $"app/{app}", title, $"{Steamapps}/{folder}") { State = state };
73 }
74
75 private static PointerReport Pointer(string path, PointerStatus status, params string[] ids)
76 {
77 return new PointerReport(new StoredPointer($"/mounts/{Path.GetFileName(path)}.toml",
78 new MountPointer(path)
79 {
80 Enabled = status != PointerStatus.Disabled,
81 LastProbed = ids.Select(Id).ToArray()
82 }), status);
83 }
84
85 private static MountId Id(string id)
86 {
87 var parts = id.Split('/');
88 return new MountId(parts[0], parts[1], parts[2]);
89 }
90
91 private static FoundArt Art(string store, string key)
92 {
93 return new FoundArt(null, $"/art/{store}/{key}/header.jpg", null, null);
94 }
95
96 [Fact]
97 public void ListsEachInstallOnceTitledByItsFirstGameWithEveryGameItHolds()
98 {
99 FoundInstall[] installs =
100 [
101 Steam("220", "Half-Life 2", "Half-Life 2"), Steam("340", "Half-Life 2: Lost Coast", "Half-Life 2"),
102 Steam("380", "Half-Life 2: Episode One", "Half-Life 2")
103 ];
104 MountId[] ids = [Id("lostcoast/pc/1"), Id("hl2/pc/1"), Id("episodic/pc/1")];
105 var offers = installs.Select(install => new InstallOffer(install, ids)).ToArray();
106
107 var library = MountLibrary.Build(Adapters, [], installs, offers, Art);
108
109 var hl2 = Assert.Single(library.Entries, entry => entry.Path is not null);
110 Assert.Equal("Half-Life 2", hl2.Title);
111 Assert.Equal(["Half-Life 2", "Half-Life 2: Episode One", "Half-Life 2: Lost Coast"], hl2.Games);
112 Assert.Equal("app/220", hl2.Install!.Key);
113 Assert.Equal(new MountLicence("steam", "220"), hl2.Licence);
114 Assert.Equal("/art/steam/app/220/header.jpg", hl2.Art.Header);
115 Assert.Equal((LibraryState.Off, false, true), (hl2.State, hl2.Ticked, hl2.CanTick));
116 }
117
118 [Fact]
119 public void FoldsByEngineSourceFirstAndCountsWhatsMounted()
120 {
121 PointerReport[] pointers =
122 [
123 Pointer($"{Steamapps}/GarrysMod", PointerStatus.Mounted, "gmod/pc/1"),
124 Pointer("/discs/skate3.iso", PointerStatus.Mounted, "skate3/ps3/BLUS30464-01.00"),
125 Pointer($"{Steamapps}/Portal", PointerStatus.Disabled, "portal/pc/1")
126 ];
127
128 var library = MountLibrary.Build(Adapters, pointers,
129 [Steam("4000", "Garry's Mod", "GarrysMod"), Steam("400", "Portal", "Portal")], [], Art);
130
131 var folds = library.Folds(LibraryFilter.Library);
132 Assert.Equal(["Source", "EA Pegasus"], folds.Select(fold => fold.Engine));
133 Assert.Equal(["Garry's Mod", "Portal"], folds[0].Entries.Select(entry => entry.Title));
134 Assert.Equal((1, 2), (folds[0].Mounted, folds[0].Entries.Count));
135 Assert.True(library.Folds(LibraryFilter.Library)[1].Entries[0].AddedByHand);
136 Assert.Equal(["Garry's Mod", "Skate 3"],
137 library.Folds(LibraryFilter.Mounted).SelectMany(fold => fold.Entries).Select(entry => entry.Title));
138 Assert.Equal(["Portal"],
139 library.Folds(LibraryFilter.Library, "port").SelectMany(fold => fold.Entries).Select(entry => entry.Title));
140 }
141
142 [Fact]
143 public void ListsCatalogueGamesNoInstallHoldsOnlyUnderEverything()
144 {
145 var library = MountLibrary.Build(Adapters,
146 [Pointer($"{Steamapps}/GarrysMod", PointerStatus.Mounted, "gmod/pc/1")],
147 [Steam("4000", "Garry's Mod", "GarrysMod")], [], Art);
148
149 Assert.DoesNotContain(library.Folds(LibraryFilter.Library).SelectMany(fold => fold.Entries),
150 entry => entry.State == LibraryState.NotInstalled);
151 var everything = library.Folds(LibraryFilter.Everything).SelectMany(fold => fold.Entries).ToArray();
152 Assert.Equal(["Garry's Mod", "Half-Life 2", "Half-Life 2: Episode One", "Half-Life 2: Lost Coast", "Portal", "Quake"],
153 everything.Select(entry => entry.Title));
154 var quake = everything.Single(entry => entry.Title == "Quake");
155 Assert.Equal(("store:steam/2310", "Not installed", LibraryAction.InstallInSteam, false),
156 (quake.Key, quake.Status, quake.Action, quake.CanTick));
157 Assert.DoesNotContain(everything, entry => entry.Title == "Skate 3");
158 }
159
160 [Fact]
161 public void LeavesOutEveryGameTheBuildDoesntSwitchOn()
162 {
163 var library = MountLibrary.Build(Adapters,
164 [
165 Pointer($"{Steamapps}/GarrysMod", PointerStatus.Mounted, "gmod/pc/1"),
166 Pointer("/games/Quake", PointerStatus.Hidden, "quake/pc/1.06")
167 ],
168 [Steam("4000", "Garry's Mod", "GarrysMod")], [], Art, game => game is "gmod" or "portal" or "hl2");
169
170 Assert.Equal(["Garry's Mod", "Half-Life 2", "Portal"],
171 library.Folds(LibraryFilter.Everything).SelectMany(fold => fold.Entries).Select(entry => entry.Title));
172 }
173
174 [Theory]
175 [InlineData(PointerStatus.Offline, "Offline", LibraryAction.Locate, false)]
176 [InlineData(PointerStatus.Changed, "Changed build", LibraryAction.AcceptBuild, false)]
177 [InlineData(PointerStatus.Unrecognised, "Unrecognised", LibraryAction.Remove, false)]
178 [InlineData(PointerStatus.Duplicate, "Duplicate", LibraryAction.None, true)]
179 [InlineData(PointerStatus.Mounted, null, LibraryAction.None, true)]
180 public void SaysWhatStandsInAPointersWay(PointerStatus status, string? said, LibraryAction action, bool tick)
181 {
182 var library = MountLibrary.Build(Adapters, [Pointer("/games/Quake", status, "quake/pc/1.06")], [], [], Art);
183
184 var quake = library.Entries.Single(entry => entry.Report is not null);
185 Assert.Equal((said, action, tick), (quake.Status, quake.Action, quake.CanTick));
186 }
187
188 [Fact]
189 public void StopsTickingWhileSteamUpdatesAndSaysAQueuedUpdate()
190 {
191 var library = MountLibrary.Build(Adapters,
192 [
193 Pointer($"{Steamapps}/Portal", PointerStatus.Disabled, "portal/pc/1"),
194 Pointer($"{Steamapps}/GarrysMod", PointerStatus.Mounted, "gmod/pc/1")
195 ],
196 [
197 Steam("400", "Portal", "Portal", InstallState.Updating),
198 Steam("4000", "Garry's Mod", "GarrysMod", InstallState.UpdateQueued)
199 ], [], Art);
200
201 var portal = library.Entries.Single(entry => entry.Title == "Portal");
202 Assert.Equal((LibraryState.Blocked, false, "Updating"), (portal.State, portal.CanTick, portal.Status));
203 var gmod = library.Entries.Single(entry => entry.Title == "Garry's Mod");
204 Assert.Equal((true, "Update queued"), (gmod.CanTick, gmod.Status));
205 }
206}