0.2.0-dev Download ZIP
142 lines ·6.4 KB Raw
| 1 | using System.Reflection; |
| 2 | using EvilEngine.Mounts; |
| 3 | |
| 4 | namespace EvilEngine.Host.Tests; |
| 5 | |
| 6 | public sealed class KspWorkshopTests : IDisposable |
| 7 | { |
| 8 | private static readonly string Installed = typeof(KspWorkshopTests).Assembly |
| 9 | .GetCustomAttributes<AssemblyMetadataAttribute>() |
| 10 | .Single(attribute => attribute.Key == "AdapterOutputRoot").Value!; |
| 11 | |
| 12 | private readonly string _library = Path.Combine(Path.GetTempPath(), $"evil-steam-{Guid.NewGuid():N}"); |
| 13 | |
| 14 | private string Install => Path.Combine(_library, "steamapps", "common", "Kerbal Space Program"); |
| 15 | |
| 16 | private string Workshop => Path.Combine(_library, "steamapps", "workshop"); |
| 17 | |
| 18 | public void Dispose() |
| 19 | { |
| 20 | if (Directory.Exists(_library)) |
| 21 | Directory.Delete(_library, true); |
| 22 | } |
| 23 | |
| 24 | private void Write(string path, string text) |
| 25 | { |
| 26 | Directory.CreateDirectory(Path.GetDirectoryName(path)!); |
| 27 | File.WriteAllText(path, text); |
| 28 | } |
| 29 | |
| 30 | private void Library(bool workshop = true) |
| 31 | { |
| 32 | Write(Path.Combine(Install, "readme.txt"), |
| 33 | "Thank you for downloading the Kerbal Space Program!\n\nVersion 1.12.5\n"); |
| 34 | Write(Path.Combine(Install, "buildID.txt"), "build id = 03190\ndistribution name = Steam\n"); |
| 35 | Write(Path.Combine(Install, "KSP_Data", "globalgamemanagers"), "2019.4.18f1"); |
| 36 | Write(Path.Combine(Install, "GameData", "Squad", "Parts", "Aero", "airbrake", "Airbrake.cfg"), |
| 37 | "PART { name = airbrake1 }"); |
| 38 | if (!workshop) |
| 39 | return; |
| 40 | Write(Path.Combine(Workshop, "appworkshop_220200.acf"), """ |
| 41 | "AppWorkshop" |
| 42 | { |
| 43 | "appid" "220200" |
| 44 | "WorkshopItemsInstalled" |
| 45 | { |
| 46 | "1417407681" { "size" "222735" "manifest" "8022348588880630668" } |
| 47 | "1428795716" { "size" "459811" "manifest" "7418085109009680491" } |
| 48 | "1632011785" { "size" "0" "manifest" "1529321936858015522" } |
| 49 | } |
| 50 | "WorkshopItemDetails" |
| 51 | { |
| 52 | "1417407681" { "manifest" "8022348588880630669" "subscribedby" "312094376" } |
| 53 | } |
| 54 | } |
| 55 | """); |
| 56 | Write(Path.Combine(Workshop, "content", "220200", "1417407681", "Rigel 1.craft"), "ship = Rigel 1"); |
| 57 | Write(Path.Combine(Workshop, "content", "220200", "1417407681", "Rigel 1.png"), "png"); |
| 58 | Write(Path.Combine(Workshop, "content", "220200", "1428795716", "Millennium Falcon.craft"), |
| 59 | "ship = Millennium Falcon"); |
| 60 | Directory.CreateDirectory(Path.Combine(Workshop, "content", "220200", "1632011785")); |
| 61 | } |
| 62 | |
| 63 | private static EngineHost Boot() |
| 64 | { |
| 65 | var host = new EngineHost(); |
| 66 | host.Adapters.LoadAll([Installed]); |
| 67 | return host; |
| 68 | } |
| 69 | |
| 70 | private Mount Open(EngineHost host, Dictionary<string, ExternalSetting>? settings = null) |
| 71 | { |
| 72 | return host.Mounts.Open(Assert.Single(host.Mounts.Probe(Install)), settings); |
| 73 | } |
| 74 | |
| 75 | [Fact] |
| 76 | public void PlacesEachCraftItemAtItsOwnPathBetweenGameDataAndTheInstall() |
| 77 | { |
| 78 | Library(); |
| 79 | var mount = Open(Boot()); |
| 80 | |
| 81 | Assert.Equal(Workshop, mount.Externals["workshop"]!.Location); |
| 82 | Assert.Equal(["GameData/Squad", "workshop/1417407681", "workshop/1428795716", "install"], |
| 83 | mount.Layers.Select(layer => layer.Name)); |
| 84 | Assert.Equal("ship = Rigel 1", mount.Files.ReadAllText("workshop/1417407681/Rigel 1.craft")); |
| 85 | Assert.Equal("workshop/1428795716", mount.Origin("workshop/1428795716/Millennium Falcon.craft")!.Layer.Name); |
| 86 | Assert.Equal("workshop", mount.Layers[1].Role); |
| 87 | Assert.False(mount.Files.Exists("content/220200/1417407681/Rigel 1.craft")); |
| 88 | } |
| 89 | |
| 90 | [Fact] |
| 91 | public void MakesEachItemAPartWithItsManifestAsItsVersion() |
| 92 | { |
| 93 | Library(); |
| 94 | var mount = Open(Boot()); |
| 95 | |
| 96 | Assert.Equal(["parts", "interiors", "workshop/1417407681", "workshop/1428795716"], |
| 97 | mount.Parts.Select(part => part.Name)); |
| 98 | Assert.Equal("8022348588880630669", mount.Part("workshop/1417407681")!.Version); |
| 99 | Assert.Equal("7418085109009680491", mount.Part("workshop/1428795716")!.Version); |
| 100 | Assert.Equal(2, mount.Part("workshop/1417407681")!.DefiningFiles.Count); |
| 101 | Assert.Equal(["workshop/1632011785 has no files in content/220200/1632011785."], mount.Notes); |
| 102 | } |
| 103 | |
| 104 | [Fact] |
| 105 | public void LetsThePointerTurnItemsOff() |
| 106 | { |
| 107 | Library(); |
| 108 | var mount = Open(Boot(), new Dictionary<string, ExternalSetting> |
| 109 | { |
| 110 | ["workshop/1417407681"] = ExternalSetting.Disabled, |
| 111 | ["workshop/42"] = ExternalSetting.Disabled |
| 112 | }); |
| 113 | |
| 114 | Assert.Equal(["GameData/Squad", "workshop/1428795716", "install"], mount.Layers.Select(layer => layer.Name)); |
| 115 | Assert.Contains("workshop/1417407681 is off: the pointer turns it off.", mount.Notes); |
| 116 | Assert.Contains("workshop/42 is set in the pointer but isn't installed.", mount.Notes); |
| 117 | Assert.Empty(mount.Externals.Unknown); |
| 118 | } |
| 119 | |
| 120 | [Fact] |
| 121 | public void LeavesTheBuildAndTheGamesPartsAloneWithoutTheWorkshop() |
| 122 | { |
| 123 | Library(); |
| 124 | var with = Open(Boot()); |
| 125 | var without = Open(Boot(), new Dictionary<string, ExternalSetting> { ["workshop"] = ExternalSetting.Disabled }); |
| 126 | |
| 127 | Assert.Equal(["GameData/Squad", "install"], without.Layers.Select(layer => layer.Name)); |
| 128 | Assert.Equal(with.Build, without.Build); |
| 129 | Assert.Equal(with.Part("parts")!.Fingerprint, without.Part("parts")!.Fingerprint); |
| 130 | } |
| 131 | |
| 132 | [Fact] |
| 133 | public void MountsWithNoWorkshopBesideIt() |
| 134 | { |
| 135 | Library(false); |
| 136 | var mount = Open(Boot()); |
| 137 | |
| 138 | Assert.Equal(ExternalState.Missing, mount.Externals["workshop"]!.State); |
| 139 | Assert.Equal(["GameData/Squad", "install"], mount.Layers.Select(layer => layer.Name)); |
| 140 | Assert.Empty(mount.Notes); |
| 141 | } |
| 142 | } |