|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO.Abstractions; |
| 4 | +using System.IO.Abstractions.TestingHelpers; |
| 5 | +using System.Linq; |
| 6 | +using EawModinfo.File; |
| 7 | +using EawModinfo.Model; |
| 8 | +using EawModinfo.Spec; |
| 9 | +using EawModinfo.Utilities; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace EawModinfo.Tests; |
| 13 | + |
| 14 | +public class ModReferenceBuilderTest |
| 15 | +{ |
| 16 | + private readonly IFileSystem _fileSystem = new MockFileSystem(); |
| 17 | + |
| 18 | + [Fact] |
| 19 | + public void CreateVirtualModIdentifier_ReturnsCorrectModReference() |
| 20 | + { |
| 21 | + var modinfo = ModinfoData.Parse(TestUtilities.MainModinfoData); |
| 22 | + var result = ModReferenceBuilder.CreateVirtualModIdentifier(modinfo); |
| 23 | + |
| 24 | + Assert.Equal(modinfo.ToJson(), result.Identifier); |
| 25 | + Assert.Equal(ModType.Virtual, result.Type); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void CreateVirtualModIdentifier_ThrowsArgumentNullException_WhenModinfoIsNull() |
| 30 | + { |
| 31 | + Assert.Throws<ArgumentNullException>(() => ModReferenceBuilder.CreateVirtualModIdentifier(null!)); |
| 32 | + } |
| 33 | + |
| 34 | + [Theory] |
| 35 | + [InlineData(ModLocationKind.GameModsDirectory)] |
| 36 | + [InlineData(ModLocationKind.SteamWorkshops)] |
| 37 | + [InlineData(ModLocationKind.External)] |
| 38 | + public void ThrowsArgumentNullException_WhenInputIsNull(ModLocationKind modLocation) |
| 39 | + { |
| 40 | + Assert.Throws<ArgumentNullException>(() => |
| 41 | + ModReferenceBuilder.CreateIdentifiers(null!, modLocation)); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public void CreateIdentifiers_ThrowsModinfoException_WhenModIsSteamWorkshopButNotDirectory() |
| 46 | + { |
| 47 | + var modDirectoryPath = "Game/Mods/NoWorkshopsId"; |
| 48 | + var modDirectory = _fileSystem.DirectoryInfo.New(modDirectoryPath); |
| 49 | + |
| 50 | + var modinfoFinderCollection = new ModinfoFinderCollection(modDirectory, null, []); |
| 51 | + |
| 52 | + Assert.Throws<ModinfoException>(() => |
| 53 | + ModReferenceBuilder.CreateIdentifiers(modinfoFinderCollection, ModLocationKind.SteamWorkshops)); |
| 54 | + } |
| 55 | + |
| 56 | + [Theory] |
| 57 | + [MemberData(nameof(ModDirectoryTestData))] |
| 58 | + public void CreateIdentifiers_NoModinfoFiles_ReturnsCorrectIdentifier( |
| 59 | + ModLocationKind locationKind, |
| 60 | + string modDirectoryPath, |
| 61 | + string expectedIdentifier) |
| 62 | + { |
| 63 | + var modDirectory = _fileSystem.DirectoryInfo.New(modDirectoryPath); |
| 64 | + |
| 65 | + var modinfoFinderCollection = new ModinfoFinderCollection(modDirectory, null, new List<ModinfoVariantFile>()); |
| 66 | + |
| 67 | + var modReferences = ModReferenceBuilder.CreateIdentifiers(modinfoFinderCollection, locationKind) |
| 68 | + .ToList(); |
| 69 | + |
| 70 | + var modRef = Assert.Single(modReferences); |
| 71 | + if (locationKind is ModLocationKind.External) |
| 72 | + expectedIdentifier = _fileSystem.Path.GetFullPath(expectedIdentifier); |
| 73 | + Assert.Equal(expectedIdentifier, modRef.ModReference.Identifier); |
| 74 | + Assert.Equal(locationKind is ModLocationKind.SteamWorkshops ? ModType.Workshops : ModType.Default, modRef.ModReference.Type); |
| 75 | + Assert.Null(modRef.Modinfo); |
| 76 | + } |
| 77 | + |
| 78 | + [Theory] |
| 79 | + [MemberData(nameof(GetCombinedModinfoFilesTestData))] |
| 80 | + public void CreateIdentifiers_TestScenario( |
| 81 | + string modPath, |
| 82 | + ModLocationKind locationKind, |
| 83 | + bool? hasValidMainModinfo, |
| 84 | + ICollection<string> validVariants, |
| 85 | + ICollection<string> malformedVariants, |
| 86 | + string expectedBaseIdentifier) |
| 87 | + { |
| 88 | + var modDir = _fileSystem.DirectoryInfo.New(modPath); |
| 89 | + |
| 90 | + // Simulate the creation of a main modinfo file |
| 91 | + var mainModinfo = hasValidMainModinfo switch |
| 92 | + { |
| 93 | + true => CreateValidMainFile(modDir, "ValidMain.json"), |
| 94 | + false => CreateInvalidMainModinfoFile(modDir), |
| 95 | + _ => null |
| 96 | + }; |
| 97 | + |
| 98 | + // Simulate the creation of variant files |
| 99 | + var variantFiles = validVariants.Select(x => CreateValidVariantFile(modDir, x)) |
| 100 | + .Concat(malformedVariants.Select(x => CreateInvalidVariantFile(modDir))) |
| 101 | + .ToList(); |
| 102 | + |
| 103 | + var modinfoFinderResult = new ModinfoFinderCollection(modDir, mainModinfo, variantFiles); |
| 104 | + |
| 105 | + var result = ModReferenceBuilder.CreateIdentifiers(modinfoFinderResult, locationKind).ToList(); |
| 106 | + |
| 107 | + // Adjust expected identifier if it's external |
| 108 | + if (locationKind is ModLocationKind.External) |
| 109 | + expectedBaseIdentifier = _fileSystem.Path.GetFullPath(expectedBaseIdentifier); |
| 110 | + |
| 111 | + var expectedCount = 1 + validVariants.Count; |
| 112 | + Assert.Equal(expectedCount, result.Count); |
| 113 | + |
| 114 | + var currentIndex = 0; |
| 115 | + |
| 116 | + // Validate main modinfo if it exists |
| 117 | + if (hasValidMainModinfo == true) |
| 118 | + { |
| 119 | + Assert.Equal(expectedBaseIdentifier, result[currentIndex].ModReference.Identifier); |
| 120 | + Assert.Equal(locationKind is ModLocationKind.SteamWorkshops ? ModType.Workshops : ModType.Default, result[currentIndex].ModReference.Type); |
| 121 | + Assert.NotNull(result[currentIndex].Modinfo); |
| 122 | + Assert.Equal("ValidMain.json", result[currentIndex].Modinfo.Name); |
| 123 | + currentIndex++; |
| 124 | + } |
| 125 | + |
| 126 | + // Validate valid variants |
| 127 | + var validVariantsList = validVariants.ToList(); // Ensure we can index into validVariants |
| 128 | + for (int i = 0; i < validVariantsList.Count; i++) |
| 129 | + { |
| 130 | + var expectedVariant = validVariantsList[i]; |
| 131 | + var modReference = result[currentIndex]; |
| 132 | + Assert.Equal($"{expectedBaseIdentifier}:{expectedVariant}", modReference.ModReference.Identifier); |
| 133 | + Assert.Equal(locationKind is ModLocationKind.SteamWorkshops ? ModType.Workshops : ModType.Default, modReference.ModReference.Type); |
| 134 | + Assert.NotNull(modReference.Modinfo); |
| 135 | + Assert.Equal(expectedVariant, modReference.Modinfo.Name); |
| 136 | + currentIndex++; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + public static IEnumerable<object[]> GetCombinedModinfoFilesTestData() |
| 141 | + { |
| 142 | + foreach (var pathData in ModDirectoryTestData()) |
| 143 | + { |
| 144 | + var locationKind = (ModLocationKind)pathData[0]; |
| 145 | + var modPath = (string)pathData[1]; |
| 146 | + var expectedIdentifier = (string)pathData[2]; |
| 147 | + |
| 148 | + yield return |
| 149 | + [ |
| 150 | + modPath, |
| 151 | + locationKind, |
| 152 | + null!, // No main modinfo |
| 153 | + Array.Empty<string>(), // No main modinfo |
| 154 | + Array.Empty<string>(), |
| 155 | + expectedIdentifier |
| 156 | + ]; |
| 157 | + |
| 158 | + yield return |
| 159 | + [ |
| 160 | + modPath, |
| 161 | + locationKind, |
| 162 | + false, // Invalid main file |
| 163 | + Array.Empty<string>(), // No main modinfo |
| 164 | + Array.Empty<string>(), |
| 165 | + expectedIdentifier |
| 166 | + ]; |
| 167 | + |
| 168 | + //yield return |
| 169 | + //[ |
| 170 | + // modPath, |
| 171 | + // isSteamWorkshopMod, |
| 172 | + // true, // Valid main modinfo |
| 173 | + // new[] { "ValidVariant2" }, |
| 174 | + // Enumerable.Empty<string>(), |
| 175 | + // expectedIdentifier, |
| 176 | + // isExternalMod |
| 177 | + //]; |
| 178 | + |
| 179 | + //yield return |
| 180 | + //[ |
| 181 | + // modPath, |
| 182 | + // isSteamWorkshopMod, |
| 183 | + // false, // Malformed main modinfo |
| 184 | + // Enumerable.Empty<string>(), |
| 185 | + // new[] { "MalformedVariant2" }, |
| 186 | + // expectedIdentifier, |
| 187 | + // isExternalMod |
| 188 | + //]; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + public static IEnumerable<object[]> ModDirectoryTestData() |
| 193 | + { |
| 194 | + yield return [ModLocationKind.SteamWorkshops, "Game/Mods/1234567890", "1234567890"]; |
| 195 | + yield return [ModLocationKind.GameModsDirectory, "Game/Mods/DefaultMod", "DefaultMod"]; |
| 196 | + yield return [ModLocationKind.External, "path/ExternalMod", "ExternalMod"]; |
| 197 | + } |
| 198 | + |
| 199 | + private MainModinfoFile? CreateValidMainFile(IDirectoryInfo dir, string mainmodinfoJson) |
| 200 | + { |
| 201 | + throw new NotImplementedException(); |
| 202 | + } |
| 203 | + |
| 204 | + private MainModinfoFile CreateInvalidMainModinfoFile(IDirectoryInfo dir) |
| 205 | + { |
| 206 | + dir.Create(); |
| 207 | + var file = _fileSystem.FileInfo.New(_fileSystem.Path.Combine(dir.FullName, "modinfo.json")); |
| 208 | + using var sw = file.CreateText(); |
| 209 | + sw.Write("This is not a valid modinfo file"); |
| 210 | + file.Refresh(); |
| 211 | + return new MainModinfoFile(file); |
| 212 | + } |
| 213 | + |
| 214 | + private ModinfoVariantFile CreateInvalidVariantFile(IDirectoryInfo dir) |
| 215 | + { |
| 216 | + dir.Create(); |
| 217 | + var file = _fileSystem.FileInfo.New(_fileSystem.Path.Combine(dir.FullName, "modinfo.json")); |
| 218 | + using var sw = file.CreateText(); |
| 219 | + sw.Write("This is not a valid modinfo file"); |
| 220 | + file.Refresh(); |
| 221 | + return new ModinfoVariantFile(file); |
| 222 | + } |
| 223 | + |
| 224 | + private ModinfoVariantFile CreateValidVariantFile(IDirectoryInfo dir, string name) |
| 225 | + { |
| 226 | + throw new NotImplementedException(); |
| 227 | + } |
| 228 | +} |
0 commit comments