|
| 1 | +using Packer.Core.Model; |
| 2 | +using Packer.Core.Model.Configuration; |
| 3 | +using Packer.Core.Model.ResourceFile; |
| 4 | +using System.Text.Json; |
| 5 | + |
| 6 | +namespace Packer.Core.Tests.ConfigurationTests; |
| 7 | + |
| 8 | +/// <summary>测试 1/2/3:配置与策略的解析与组合行为</summary> |
| 9 | +public class DeserializationTests |
| 10 | +{ |
| 11 | + static readonly JsonSerializerOptions Opts = new() |
| 12 | + { |
| 13 | + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| 14 | + TypeInfoResolver = new DefaultJsonTypeInfoResolver() |
| 15 | + }; |
| 16 | + |
| 17 | + // ── 测试 1:浮动配置和打包策略在命名空间文件夹里时可以解析 ── |
| 18 | + |
| 19 | + [Fact] |
| 20 | + public void NamespaceFolder_Loads_LocalConfig_And_Policy() |
| 21 | + { |
| 22 | + using var tmp = new TempDir(); |
| 23 | + // 造一个命名空间目录,带 local-config.json 和 packer-policy.json |
| 24 | + tmp.Write("local-config.json", """{ "inclusionDomains": ["font"] }"""); |
| 25 | + tmp.Write("packer-policy.json", """[{ "type": "singleton", "source": "a.json", "relativePath": "lang/zh_cn.json" }]"""); |
| 26 | + |
| 27 | + var ns = new AssetsNamespaceResource(tmp.Path, "myns", "mymod", "1.21"); |
| 28 | + |
| 29 | + // 验证 local-config 被加载 |
| 30 | + Assert.Contains("font", ns.LocalConfig.InclusionDomains); |
| 31 | + |
| 32 | + // 验证策略被加载(不会是 Shared) |
| 33 | + Assert.NotSame(PackerPolicy.Shared, ns.PackerPolicies); |
| 34 | + |
| 35 | + // 验证策略类型正确 |
| 36 | + var policy = Assert.Single(ns.PackerPolicies); |
| 37 | + Assert.IsType<SingletonPolicy>(policy); |
| 38 | + } |
| 39 | + |
| 40 | + // ── 测试 2:全局配置和浮动配置组合正确 ── |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void GlobalConfig_Merges_With_LocalConfig() |
| 44 | + { |
| 45 | + var global = new FloatingConfig( |
| 46 | + ["font"], [], [], ["README.md"], |
| 47 | + new Dictionary<string, string> { ["旧"] = "新" }, |
| 48 | + new Dictionary<string, string>()); |
| 49 | + |
| 50 | + var local = new FloatingConfig( |
| 51 | + ["gui"], [], [], ["packer-policy.json"], |
| 52 | + new Dictionary<string, string>(), |
| 53 | + new Dictionary<string, string>()); |
| 54 | + |
| 55 | + var merged = global.Merge(local); |
| 56 | + |
| 57 | + // domain 取并集 |
| 58 | + Assert.Contains("font", merged.InclusionDomains); |
| 59 | + Assert.Contains("gui", merged.InclusionDomains); |
| 60 | + |
| 61 | + // exclusionPaths 取并集 |
| 62 | + Assert.Contains("README.md", merged.ExclusionPaths); |
| 63 | + Assert.Contains("packer-policy.json", merged.ExclusionPaths); |
| 64 | + |
| 65 | + // 全局的 replacement 保留 |
| 66 | + Assert.Equal("新", merged.CharacterReplacement["旧"]); |
| 67 | + } |
| 68 | + |
| 69 | + // ── 测试 3:没有浮动配置和打包策略时结果正确 ── |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void NoLocalConfig_Returns_Shared() |
| 73 | + { |
| 74 | + using var tmp = new TempDir(); |
| 75 | + // 空目录,没有 local-config.json |
| 76 | + var ns = new AssetsNamespaceResource(tmp.Path, "ns", "mod", "1.21"); |
| 77 | + |
| 78 | + Assert.Same(FloatingConfig.Shared, ns.LocalConfig); |
| 79 | + Assert.Same(PackerPolicy.Shared, ns.PackerPolicies); |
| 80 | + } |
| 81 | + |
| 82 | + // ── 测试 8:TryAdd 和 ModifyOnly ── |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public void JsonFile_Merge_TryAdd_FirstWins() |
| 86 | + { |
| 87 | + var a = new JsonFile(new() { ["a"] = "1", ["b"] = "2" }, "x.json"); |
| 88 | + var b = new JsonFile(new() { ["a"] = "x", ["c"] = "3" }, "x.json"); |
| 89 | + |
| 90 | + var r = a.Merge(b); |
| 91 | + Assert.Equal("1", r.Entries["a"]); // 第一个赢 |
| 92 | + Assert.Equal("2", r.Entries["b"]); |
| 93 | + Assert.Equal("3", r.Entries["c"]); |
| 94 | + } |
| 95 | + |
| 96 | + [Fact] |
| 97 | + public void JsonFile_Merge_ModifyOnly_OverwritesExisting() |
| 98 | + { |
| 99 | + var a = new JsonFile(new() { ["a"] = "1", ["b"] = "2" }, "x.json"); |
| 100 | + var b = new JsonFile(new() { ["a"] = "x", ["c"] = "3" }, "x.json") |
| 101 | + { |
| 102 | + PolicyItem = new DirectPolicy { ModifyOnly = true } |
| 103 | + }; |
| 104 | + |
| 105 | + var r = a.Merge(b); |
| 106 | + Assert.Equal("x", r.Entries["a"]); // 覆盖已有 |
| 107 | + Assert.Equal("2", r.Entries["b"]); // 不动 |
| 108 | + Assert.False(r.Entries.ContainsKey("c")); // 不添加新键 |
| 109 | + } |
| 110 | + |
| 111 | + [Fact] |
| 112 | + public void LangFile_Merge_TryAdd_FirstWins() |
| 113 | + { |
| 114 | + var a = new LangFile(new() { ["k1"] = "v1", ["k2"] = "v2" }, "x.lang"); |
| 115 | + var b = new LangFile(new() { ["k1"] = "x", ["k3"] = "v3" }, "x.lang"); |
| 116 | + |
| 117 | + var r = a.Merge(b); |
| 118 | + Assert.Equal("v1", r.Entries["k1"]); |
| 119 | + Assert.Equal("v3", r.Entries["k3"]); |
| 120 | + } |
| 121 | + |
| 122 | + // ── 测试 11:全局配置的过滤 ── |
| 123 | + // 此测试需要在真实目录中放置文件,用 AssetsNamespaceResource + DirectPolicy |
| 124 | + // 验证 inclusionDomains / exclusionDomains / exclusionPaths 生效 |
| 125 | + |
| 126 | + [Fact] |
| 127 | + public void GlobalFilter_InclusionDomain_ForceIncludes() |
| 128 | + { |
| 129 | + using var dir = new TempDir(); |
| 130 | + dir.Write("font/extra.txt", "should be included via domain"); |
| 131 | + dir.Write("sounds/ambient.ogg", "should NOT be included"); |
| 132 | + |
| 133 | + var ns = new AssetsNamespaceResource(dir.Path, "testns", "testmod", "1.21"); |
| 134 | + var config = new FloatingConfig( |
| 135 | + InclusionDomains: ["font"], // font 之下无条件进包 |
| 136 | + ExclusionDomains: [], |
| 137 | + InclusionPaths: [], |
| 138 | + ExclusionPaths: ["packer-policy.json", "local-config.json"], |
| 139 | + CharacterReplacement: new Dictionary<string, string>(), |
| 140 | + DestinationReplacement: new Dictionary<string, string>()); |
| 141 | + |
| 142 | + // DirectPolicy 默认共享 |
| 143 | + var providers = new DirectPolicy().CreateProviders(ns, config).ToList(); |
| 144 | + |
| 145 | + Assert.Contains(providers, p => p.Destination.EndsWith("font/extra.txt")); |
| 146 | + Assert.DoesNotContain(providers, p => p.Destination.EndsWith("sounds/ambient.ogg")); |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +/// <summary>临时目录辅助类</summary> |
| 151 | +public class TempDir : IDisposable |
| 152 | +{ |
| 153 | + public string Path { get; } = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString()); |
| 154 | + |
| 155 | + public TempDir() |
| 156 | + { |
| 157 | + Directory.CreateDirectory(Path); |
| 158 | + } |
| 159 | + |
| 160 | + /// <summary>在临时目录下创建一个文件(自动创建父目录)</summary> |
| 161 | + public void Write(string relativePath, string content) |
| 162 | + { |
| 163 | + var full = System.IO.Path.Combine(Path, relativePath); |
| 164 | + Directory.CreateDirectory(System.IO.Path.GetDirectoryName(full)!); |
| 165 | + File.WriteAllText(full, content); |
| 166 | + } |
| 167 | + |
| 168 | + public void Dispose() |
| 169 | + { |
| 170 | + try { Directory.Delete(Path, recursive: true); } |
| 171 | + catch { /* 清理失败忽略 */ } |
| 172 | + } |
| 173 | +} |
0 commit comments