-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentRolesAndPromptsTests.cs
More file actions
142 lines (131 loc) · 5.66 KB
/
AgentRolesAndPromptsTests.cs
File metadata and controls
142 lines (131 loc) · 5.66 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
using System.Collections.Generic;
using System.Linq;
using CoreAI.Ai;
using CoreAI.Session;
using NUnit.Framework;
namespace CoreAI.Tests.EditMode
{
public sealed class AgentRolesAndPromptsTests
{
[Test]
public void BuiltInAgentRoleIds_AllBuiltInRoles_MatchesConstants()
{
CollectionAssert.AreEquivalent(
new[]
{
BuiltInAgentRoleIds.Creator,
BuiltInAgentRoleIds.Analyzer,
BuiltInAgentRoleIds.Programmer,
BuiltInAgentRoleIds.AiNpc,
BuiltInAgentRoleIds.CoreMechanic,
BuiltInAgentRoleIds.PlainChat,
BuiltInAgentRoleIds.SmartChat,
BuiltInAgentRoleIds.Merchant
},
BuiltInAgentRoleIds.AllBuiltInRoles.ToArray());
}
[Test]
public void BuiltInDefaultSystemPrompts_AllRoles_NonEmptyAndDistinct()
{
BuiltInDefaultAgentSystemPromptProvider provider = new();
List<string> texts = new();
foreach (string role in BuiltInAgentRoleIds.AllBuiltInRoles)
{
Assert.IsTrue(provider.TryGetSystemPrompt(role, out string sys), role);
Assert.IsFalse(string.IsNullOrWhiteSpace(sys), role);
StringAssert.Contains("You are", sys, role);
texts.Add(sys.Trim());
}
Assert.AreEqual(texts.Count, texts.Distinct().Count(), "Промпты ролей должны различаться.");
}
[Test]
public void AiPromptComposer_AllBuiltInRoles_GetSystemPrompt()
{
BuiltInDefaultAgentSystemPromptProvider provider = new();
NoAgentUserPromptTemplateProvider noUser = new();
AiPromptComposer composer = new(provider, noUser, new NullLuaScriptVersionStore());
foreach (string role in BuiltInAgentRoleIds.AllBuiltInRoles)
{
string s = composer.GetSystemPrompt(role);
Assert.IsFalse(string.IsNullOrWhiteSpace(s), role);
}
}
[Test]
public void AiPromptComposer_AllBuiltInRoles_BuildUserPayload()
{
BuiltInDefaultAgentSystemPromptProvider provider = new();
NoAgentUserPromptTemplateProvider noUser = new();
AiPromptComposer composer = new(provider, noUser, new NullLuaScriptVersionStore());
GameSessionSnapshot snap = new();
snap.Telemetry["wave"] = "3";
snap.Telemetry["mode"] = "arena";
snap.Telemetry["party"] = "2";
foreach (string role in BuiltInAgentRoleIds.AllBuiltInRoles)
{
string u = composer.BuildUserPayload(snap, new AiTaskRequest { RoleId = role, Hint = "test" });
StringAssert.Contains("\"telemetry\":{", u);
StringAssert.Contains("\"wave\":\"3\"", u);
StringAssert.Contains("\"mode\":\"arena\"", u);
StringAssert.Contains("\"party\":\"2\"", u);
StringAssert.Contains("\"hint\":\"test\"", u);
}
}
[Test]
public void AiPromptComposer_UserTemplate_ReplacesTelemetryPlaceholders()
{
BuiltInDefaultAgentSystemPromptProvider sys = new();
FixedUserTemplateProvider user = new("w={wave} m={mode} hint={hint}");
AiPromptComposer composer = new(sys, user, new NullLuaScriptVersionStore());
GameSessionSnapshot snap = new();
snap.Telemetry["wave"] = "7";
snap.Telemetry["mode"] = "solo";
string u = composer.BuildUserPayload(snap,
new AiTaskRequest { RoleId = BuiltInAgentRoleIds.Creator, Hint = "go" });
Assert.AreEqual("w=7 m=solo hint=go", u);
}
private sealed class FixedUserTemplateProvider : IAgentUserPromptTemplateProvider
{
private readonly string _template;
public FixedUserTemplateProvider(string template)
{
_template = template;
}
public bool TryGetUserTemplate(string roleId, out string template)
{
template = _template;
return true;
}
}
private static readonly string[] AllRoleIdCases = BuiltInAgentRoleIds.AllBuiltInRoles.ToArray();
[TestCaseSource(nameof(AllRoleIdCases))]
public void StubLlmClient_EachBuiltInRole_ReturnsOk(string roleId)
{
StubLlmClient client = new();
LlmCompletionResult r = client.CompleteAsync(new LlmCompletionRequest
{
AgentRoleId = roleId,
SystemPrompt = "sys",
UserPayload = "payload"
}).GetAwaiter().GetResult();
Assert.IsTrue(r.Ok, roleId);
Assert.IsFalse(string.IsNullOrEmpty(r.Content), roleId);
if (roleId == BuiltInAgentRoleIds.PlainChat ||
roleId == BuiltInAgentRoleIds.SmartChat ||
roleId == BuiltInAgentRoleIds.AiNpc)
{
StringAssert.StartsWith("[stub]", r.Content);
StringAssert.Contains("offline", r.Content.ToLowerInvariant());
}
else if (roleId == BuiltInAgentRoleIds.Programmer)
{
StringAssert.Contains("```lua", r.Content, roleId);
StringAssert.Contains("report('stub:", r.Content, roleId);
}
else
{
StringAssert.Contains("ApplyWaveModifier", r.Content);
StringAssert.Contains("\"agentRole\":\"" + roleId + "\"", r.Content);
}
}
}
}