-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameConfigEditModeTests.cs
More file actions
217 lines (175 loc) · 7.87 KB
/
GameConfigEditModeTests.cs
File metadata and controls
217 lines (175 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using NUnit.Framework;
using CoreAI.Config;
using CoreAI.Ai;
namespace CoreAI.Tests.EditMode
{
/// <summary>
/// EditMode coverage for <see cref="GameConfigTool"/> and <see cref="GameConfigPolicy"/>
/// JSON configuration read/write behavior.
/// </summary>
[TestFixture]
public class GameConfigEditModeTests
{
private InMemoryConfigStore _store;
private GameConfigPolicy _policy;
[SetUp]
public void SetUp()
{
_store = new InMemoryConfigStore();
_policy = new GameConfigPolicy();
}
#region GameConfigPolicy Tests
[Test]
public void Policy_ConfigureRole_CanReadWrite()
{
_policy.ConfigureRole("Creator", new[] { "session" }, new[] { "session" });
_policy.SetKnownKeys(new[] { "session", "crafting" });
Assert.IsTrue(_policy.CanRead("Creator", "session"));
Assert.IsTrue(_policy.CanWrite("Creator", "session"));
Assert.IsFalse(_policy.CanRead("Creator", "crafting"));
}
[Test]
public void Policy_GrantFullAccess_CanReadWriteAll()
{
_policy.GrantFullAccess("Creator");
_policy.SetKnownKeys(new[] { "session", "crafting" });
Assert.IsTrue(_policy.CanRead("Creator", "session"));
Assert.IsTrue(_policy.CanWrite("Creator", "crafting"));
CollectionAssert.AreEquivalent(new[] { "session", "crafting" }, _policy.GetAllowedKeys("Creator"));
}
[Test]
public void Policy_RevokeAccess_CannotReadWrite()
{
_policy.GrantFullAccess("Creator");
_policy.RevokeAccess("Creator");
Assert.IsFalse(_policy.CanRead("Creator", "session"));
Assert.IsFalse(_policy.CanWrite("Creator", "session"));
Assert.IsEmpty(_policy.GetAllowedKeys("Creator"));
}
#endregion
#region GameConfigTool Read Tests
[Test]
public void ConfigTool_Read_ReturnsConfigJson()
{
_store.TrySave("session", "{\"difficulty\":2,\"enemy_hp_mult\":1.5}");
_policy.ConfigureRole("Creator", new[] { "session" }, new[] { "session" });
GameConfigTool tool = new(_store, _policy, "Creator");
GameConfigTool.GameConfigResult result =
JsonConvert.DeserializeObject<GameConfigTool.GameConfigResult>(tool.ExecuteAsync("read").Result);
Assert.IsTrue(result.Success);
StringAssert.Contains("difficulty", result.ConfigJson);
StringAssert.Contains("2", result.ConfigJson);
StringAssert.Contains("enemy_hp_mult", result.ConfigJson);
}
[Test]
public void ConfigTool_ReadUnknownRole_ReturnsError()
{
_policy.SetKnownKeys(new[] { "session" });
GameConfigTool tool = new(_store, _policy, "UnknownRole");
GameConfigTool.GameConfigResult result =
JsonConvert.DeserializeObject<GameConfigTool.GameConfigResult>(tool.ExecuteAsync("read").Result);
Assert.IsFalse(result.Success);
StringAssert.Contains("no allowed config", result.Error);
}
#endregion
#region GameConfigTool Update Tests
[Test]
public void ConfigTool_Update_SavesNewJson()
{
_store.TrySave("session", "{\"difficulty\":1,\"enemy_hp_mult\":1.0}");
_policy.ConfigureRole("Creator", new[] { "session" }, new[] { "session" });
GameConfigTool tool = new(_store, _policy, "Creator");
string newConfig = "{\"difficulty\":3,\"enemy_hp_mult\":2.5}";
GameConfigTool.GameConfigResult result =
JsonConvert.DeserializeObject<GameConfigTool.GameConfigResult>(tool.ExecuteAsync("update", newConfig)
.Result);
Assert.IsTrue(result.Success);
_store.TryLoad("session", out string savedJson);
StringAssert.Contains("difficulty", savedJson);
StringAssert.Contains("3", savedJson);
StringAssert.Contains("2.5", savedJson);
}
[Test]
public void ConfigTool_UpdateWithoutContent_ReturnsError()
{
_policy.GrantFullAccess("Creator");
GameConfigTool tool = new(_store, _policy, "Creator");
GameConfigTool.GameConfigResult result =
JsonConvert.DeserializeObject<GameConfigTool.GameConfigResult>(tool.ExecuteAsync("update").Result);
Assert.IsFalse(result.Success);
StringAssert.Contains("Content", result.Error);
}
[Test]
public void ConfigTool_UpdateInvalidJson_ReturnsError()
{
_policy.GrantFullAccess("Creator");
GameConfigTool tool = new(_store, _policy, "Creator");
GameConfigTool.GameConfigResult result =
JsonConvert.DeserializeObject<GameConfigTool.GameConfigResult>(tool.ExecuteAsync("update", "not json")
.Result);
Assert.IsFalse(result.Success);
StringAssert.Contains("JSON", result.Error);
}
[Test]
public void ConfigTool_UnknownAction_ReturnsError()
{
GameConfigTool tool = new(_store, _policy, "Creator");
GameConfigTool.GameConfigResult result =
JsonConvert.DeserializeObject<GameConfigTool.GameConfigResult>(tool.ExecuteAsync("delete").Result);
Assert.IsFalse(result.Success);
StringAssert.Contains("Unknown action", result.Error);
}
#endregion
#region Integration: Read → Modify → Write
[Test]
public void ConfigTool_ReadModifyWrite_RoundTrip()
{
// Начальный конфиг
_store.TrySave("session", "{\"difficulty\":1,\"enemy_hp_mult\":1.0,\"max_enemies\":50}");
_policy.GrantFullAccess("Creator");
_policy.SetKnownKeys(new[] { "session" });
GameConfigTool tool = new(_store, _policy, "Creator");
// Шаг 1: Читаем
GameConfigTool.GameConfigResult readResult =
JsonConvert.DeserializeObject<GameConfigTool.GameConfigResult>(tool.ExecuteAsync("read").Result);
Assert.IsTrue(readResult.Success);
StringAssert.Contains("difficulty", readResult.ConfigJson);
// Шаг 2: Модифицируем (имитация что AI изменил JSON)
string modifiedJson = "{\"difficulty\":2,\"enemy_hp_mult\":1.5,\"max_enemies\":80}";
// Шаг 3: Сохраняем
GameConfigTool.GameConfigResult writeResult =
JsonConvert.DeserializeObject<GameConfigTool.GameConfigResult>(tool.ExecuteAsync("update", modifiedJson)
.Result);
Assert.IsTrue(writeResult.Success);
// Шаг 4: Проверяем что сохранилось
_store.TryLoad("session", out string finalJson);
StringAssert.Contains("difficulty", finalJson);
StringAssert.Contains("2", finalJson);
StringAssert.Contains("1.5", finalJson);
StringAssert.Contains("80", finalJson);
}
#endregion
#region InMemory Config Store (для тестов)
private sealed class InMemoryConfigStore : IGameConfigStore
{
private readonly Dictionary<string, string> _configs = new();
public bool TryLoad(string key, out string json)
{
return _configs.TryGetValue(key, out json);
}
public bool TrySave(string key, string json)
{
_configs[key] = json;
return true;
}
public string[] GetKnownKeys()
{
return _configs.Keys.ToArray();
}
}
#endregion
}
}