|
| 1 | +using Packer.Core.Model; |
| 2 | +using Packer.Core.Model.Abstract; |
| 3 | +using Packer.Core.Model.Configuration; |
| 4 | +using Packer.Core.Model.ModProvider; |
| 5 | +using Packer.Core.Model.ResourceFile; |
| 6 | +using Serilog; |
| 7 | +using System.IO.Compression; |
| 8 | +using System.Text.Json; |
| 9 | +using System.Text.Json.Serialization.Metadata; |
| 10 | + |
| 11 | +namespace Packer.Core.Tests; |
| 12 | + |
| 13 | +public class IntegrationTests |
| 14 | +{ |
| 15 | + private static readonly Lazy<HashSet<string>> _dests = new(() => RunPack("1.21")); |
| 16 | + private static readonly HashSet<string> _empty = []; |
| 17 | + |
| 18 | + static IntegrationTests() |
| 19 | + { |
| 20 | + Environment.CurrentDirectory = @"C:\Users\16229\source\OpenSourceLibrary\Minecraft-Mod-Language-Package"; |
| 21 | + Log.Logger = new LoggerConfiguration() |
| 22 | + .WriteTo.Console() |
| 23 | + .MinimumLevel.Warning() |
| 24 | + .CreateLogger(); |
| 25 | + } |
| 26 | + |
| 27 | + private static HashSet<string> RunPack(string version) |
| 28 | + { |
| 29 | + var config = JsonSerializer.Deserialize<Config>( |
| 30 | + File.ReadAllText($"config/packer/{version}.json"), |
| 31 | + new JsonSerializerOptions |
| 32 | + { |
| 33 | + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| 34 | + TypeInfoResolver = new DefaultJsonTypeInfoResolver() |
| 35 | + })!; |
| 36 | + |
| 37 | + var provider = new AssetsModProvider(); |
| 38 | + var namespaces = provider.GetModsByVersion(version); |
| 39 | + var filtered = namespaces.Where(g => !config.Base.ExclusionMods.Contains(g.Key)); |
| 40 | + var allProviders = filtered |
| 41 | + .SelectMany(g => g) |
| 42 | + .Where(ns => !config.Base.ExclusionNamespaces.Contains(ns.NamespaceName)) |
| 43 | + .SelectMany(ns => ns.PackerPolicies |
| 44 | + .SelectMany(p => p.CreateProviders(ns, config.Floating.Merge(ns.LocalConfig)))) |
| 45 | + .ToList(); |
| 46 | + |
| 47 | + var groups = allProviders.ToLookup(p => p.Destination); |
| 48 | + var merged = groups.Select(group => |
| 49 | + { |
| 50 | + if (!group.Skip(1).Any()) return group.First(); |
| 51 | + if (group.All(p => p is KVPFile)) |
| 52 | + { |
| 53 | + var r = group.Cast<KVPFile>().Aggregate((a, n) => a.Merge(n)); |
| 54 | + if (r is ResourceFileProvider rfp) rfp.Namespace = group.Cast<KVPFile>().First().Namespace; |
| 55 | + return r; |
| 56 | + } |
| 57 | + return group.First(); |
| 58 | + }).ToList(); |
| 59 | + |
| 60 | + foreach (var p in merged.OfType<TextFile>()) |
| 61 | + p.EffectiveConfig = config.Floating; |
| 62 | + |
| 63 | + var initial = new List<IResourceFileProvider> |
| 64 | + { |
| 65 | + new RawFile("./projects/templates/pack.png", "pack.png"), |
| 66 | + new TextFile(File.ReadAllText("./projects/templates/LICENSE"), "LICENSE"), |
| 67 | + config.Base.LoadReadmeTemp(), |
| 68 | + config.Base.LoadMetaTemp() |
| 69 | + }; |
| 70 | + |
| 71 | + var dests = new HashSet<string>(); |
| 72 | + using var ms = new MemoryStream(); |
| 73 | + using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, leaveOpen: true)) |
| 74 | + { |
| 75 | + foreach (var p in merged.Concat(initial)) |
| 76 | + { |
| 77 | + using var content = p.GetContentStream(); |
| 78 | + var entry = archive.CreateEntry(p.Destination); |
| 79 | + using var es = entry.Open(); |
| 80 | + content.CopyTo(es); |
| 81 | + dests.Add(p.Destination); |
| 82 | + } |
| 83 | + } |
| 84 | + return dests; |
| 85 | + } |
| 86 | + |
| 87 | + private HashSet<string> Dests => _dests.Value; |
| 88 | + |
| 89 | + [Fact] |
| 90 | + public void Createdeco_Composition_Output() |
| 91 | + => Assert.Contains("assets/createdeco/lang/zh_cn.json", Dests); |
| 92 | + |
| 93 | + [Fact] |
| 94 | + public void Justhammers_Indirect_Output() |
| 95 | + => Assert.Contains("assets/justhammers/lang/zh_cn.json", Dests); |
| 96 | + |
| 97 | + [Fact] |
| 98 | + public void Minecraft_Font_Assets() |
| 99 | + { |
| 100 | + Assert.Contains("assets/minecraft/font/default.json", Dests); |
| 101 | + Assert.Contains("assets/minecraft/font/uniform.json", Dests); |
| 102 | + Assert.Contains("assets/minecraft/textures/font/2em_dash.png", Dests); |
| 103 | + } |
| 104 | + |
| 105 | + [Fact] |
| 106 | + public void No_Composition_Source_Files() |
| 107 | + { |
| 108 | + Assert.DoesNotContain("assets/createdeco/lang/zh_cn-composition.json", Dests); |
| 109 | + Assert.DoesNotContain("assets/bibliobiomes/lang/zh_cn-composition.json", Dests); |
| 110 | + } |
| 111 | +} |
0 commit comments