-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyzerEditModeTests.cs
More file actions
225 lines (186 loc) · 7.17 KB
/
AnalyzerEditModeTests.cs
File metadata and controls
225 lines (186 loc) · 7.17 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
218
219
220
221
222
223
224
225
using NUnit.Framework;
using System.Collections.Generic;
using CoreAI.Ai;
using CoreAI.Infrastructure.Ai;
using CoreAI.Session;
using CoreAI.Authority;
using CoreAI.Messaging;
namespace CoreAI.Tests.EditMode
{
/// <summary>
/// EditMode coverage for Analyzer role prompts, telemetry, and response format.
/// </summary>
[TestFixture]
public class AnalyzerEditModeTests
{
private AiPromptComposer _promptComposer;
private StubLlmClient _stubLlm;
private SessionTelemetryCollector _telemetry;
[SetUp]
public void SetUp()
{
_promptComposer = new AiPromptComposer(
new BuiltInDefaultAgentSystemPromptProvider(),
new StubUserTemplateProvider(),
new NullLuaScriptVersionStore(),
null);
_stubLlm = new StubLlmClient();
_telemetry = new SessionTelemetryCollector();
}
#region System Prompt Tests
[Test]
public void Analyzer_SystemPrompt_IsNotEmpty()
{
string prompt = _promptComposer.GetSystemPrompt("Analyzer");
Assert.IsNotNull(prompt);
Assert.Greater(prompt.Length, 50, "Analyzer system prompt should be substantial");
}
[Test]
public void Analyzer_SystemPrompt_ContainsAnalysisKeywords()
{
string prompt = _promptComposer.GetSystemPrompt("Analyzer").ToLowerInvariant();
StringAssert.Contains("analy", prompt); // analyze/analysis
StringAssert.Contains("telemetry", prompt); // reads telemetry
}
[Test]
public void Analyzer_SystemPrompt_DifferentFromCreator()
{
string analyzerPrompt = _promptComposer.GetSystemPrompt("Analyzer");
string creatorPrompt = _promptComposer.GetSystemPrompt("Creator");
Assert.AreNotEqual(analyzerPrompt, creatorPrompt);
}
#endregion
#region Telemetry Tests
[Test]
public void Analyzer_ReceivesTelemetry_InUserPayload()
{
_telemetry.SetTelemetry("wave", 3);
GameSessionSnapshot snapshot = _telemetry.BuildSnapshot();
string userPayload = _promptComposer.BuildUserPayload(snapshot, new AiTaskRequest
{
RoleId = "Analyzer",
Hint = "Analyze player death rate"
});
Assert.IsNotNull(userPayload);
Assert.Greater(userPayload.Length, 10);
}
[Test]
public void Analyzer_EmptyTelemetry_HandlesGracefully()
{
GameSessionSnapshot snapshot = _telemetry.BuildSnapshot();
string userPayload = _promptComposer.BuildUserPayload(snapshot, new AiTaskRequest
{
RoleId = "Analyzer",
Hint = ""
});
Assert.IsNotNull(userPayload);
}
#endregion
#region Response Validation Tests
[Test]
public void Analyzer_ResponsePolicy_ValidJson_ReturnsTrue()
{
AnalyzerResponsePolicy policy = new();
string content = @"{""metric"": ""player_death_rate"", ""value"": 0.35, ""status"": ""balanced""}";
Assert.IsTrue(policy.ShouldValidate("Analyzer"));
Assert.IsTrue(policy.TryValidate("Analyzer", content, out _));
}
[Test]
public void Analyzer_ResponsePolicy_InvalidText_ReturnsFalse()
{
AnalyzerResponsePolicy policy = new();
string content = "The game seems balanced enough.";
Assert.IsFalse(policy.TryValidate("Analyzer", content, out string reason));
StringAssert.Contains("Expected JSON", reason);
}
[Test]
public void Analyzer_ResponsePolicy_RecommendationsJson_ReturnsTrue()
{
AnalyzerResponsePolicy policy = new();
string content =
@"{""recommendation"": ""increase enemy HP by 10%"", ""analysis"": ""players die too fast""}";
Assert.IsTrue(policy.TryValidate("Analyzer", content, out _));
}
#endregion
#region Stub LLM Tests
[Test]
public void Analyzer_StubLlm_ReturnsJsonResponse()
{
LlmCompletionResult result = _stubLlm.CompleteAsync(new LlmCompletionRequest
{
AgentRoleId = "Analyzer",
SystemPrompt = _promptComposer.GetSystemPrompt("Analyzer"),
UserPayload = "Analyze wave 3",
TraceId = "test123"
}).Result;
Assert.IsNotNull(result);
Assert.IsTrue(result.Ok);
Assert.IsNotNull(result.Content);
}
#endregion
#region Orchestrator Integration Tests
[Test]
public void Analyzer_Orchestrator_PublishesEnvelope()
{
// Arrange
TestCommandSink commandSink = new();
TestAuthorityHost authority = new() { CanRunAiTasks = true };
NullAgentMemoryStore memoryStore = new();
AgentMemoryPolicy memoryPolicy = new();
NoOpRoleStructuredResponsePolicy structuredPolicy = new();
NullAiOrchestrationMetrics metrics = new();
AiOrchestrator orchestrator = new(
authority,
_stubLlm,
commandSink,
_telemetry,
_promptComposer,
memoryStore,
memoryPolicy,
structuredPolicy,
metrics, UnityEngine.ScriptableObject.CreateInstance<Infrastructure.Llm.CoreAISettingsAsset>());
// Act
orchestrator.RunTaskAsync(new AiTaskRequest
{
RoleId = "Analyzer",
Hint = "Analyze current session balance",
TraceId = "test_analyzer"
}).Wait();
// Assert
Assert.IsTrue(commandSink.PublishedCommands.Count > 0, "Should publish at least one command");
ApplyAiGameCommand envelope = commandSink.PublishedCommands[0];
Assert.AreEqual("Analyzer", envelope.SourceRoleId);
Assert.IsNotNull(envelope.JsonPayload);
}
#endregion
#region Helper Classes
private sealed class TestCommandSink : IAiGameCommandSink
{
public List<ApplyAiGameCommand> PublishedCommands { get; } = new();
public void Publish(ApplyAiGameCommand command)
{
PublishedCommands.Add(command);
}
}
private sealed class TestAuthorityHost : IAuthorityHost
{
public bool CanRunAiTasks { get; set; } = true;
}
private sealed class StubUserTemplateProvider : IAgentUserPromptTemplateProvider
{
public bool TryGetUserTemplate(string roleId, out string template)
{
template = "{hint}\n\nTelemetry:\n{telemetry}";
return true;
}
}
private sealed class StubSessionTelemetryProvider : ISessionTelemetryProvider
{
public GameSessionSnapshot BuildSnapshot()
{
return new GameSessionSnapshot();
}
}
#endregion
}
}