-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoreAssemblyEditModeTests.cs
More file actions
110 lines (99 loc) · 4.2 KB
/
CoreAssemblyEditModeTests.cs
File metadata and controls
110 lines (99 loc) · 4.2 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
using System.Threading.Tasks;
using CoreAI;
using CoreAI.Ai;
using CoreAI.Sandbox;
using CoreAI.Session;
using MoonSharp.Interpreter;
using NUnit.Framework;
namespace CoreAI.Tests.EditMode
{
public sealed class CoreAssemblyEditModeTests
{
[Test]
public void CoreAssemblyMarker_IsDefined()
{
Assert.AreEqual("CoreAI.Core", CoreAssemblyMarker.AssemblyName);
}
[Test]
public void StubLlmClient_ReturnsJson()
{
StubLlmClient client = new();
Task<LlmCompletionResult> task = client.CompleteAsync(new LlmCompletionRequest { UserPayload = "x" });
LlmCompletionResult result = task.GetAwaiter().GetResult();
Assert.IsTrue(result.Ok);
StringAssert.Contains("ApplyWaveModifier", result.Content);
StringAssert.Contains("\"agentRole\":\"Creator\"", result.Content);
}
[Test]
public void StubLlmClient_SmartChat_IsConversational()
{
StubLlmClient client = new();
LlmCompletionResult r = client.CompleteAsync(new LlmCompletionRequest
{
AgentRoleId = BuiltInAgentRoleIds.SmartChat,
UserPayload = "hello"
}).GetAwaiter().GetResult();
Assert.IsTrue(r.Ok);
StringAssert.Contains("stub", r.Content.ToLowerInvariant());
}
[Test]
public void StubLlmClient_Teacher_IsShortOfflineMessage()
{
StubLlmClient client = new();
LlmCompletionResult r = client.CompleteAsync(new LlmCompletionRequest
{
AgentRoleId = "Teacher",
UserPayload = "{\"telemetry\":{},\"hint\":\"test\"}"
}).GetAwaiter().GetResult();
Assert.IsTrue(r.Ok);
StringAssert.StartsWith("[stub]", r.Content);
StringAssert.DoesNotContain("telemetry", r.Content);
}
[Test]
public void AiPromptComposer_UsesSystemProviderAndTemplates()
{
BuiltInDefaultAgentSystemPromptProvider sys = new();
NoAgentUserPromptTemplateProvider user = new();
AiPromptComposer composer = new(sys, user, new NullLuaScriptVersionStore());
string s = composer.GetSystemPrompt(BuiltInAgentRoleIds.Programmer);
StringAssert.Contains("Programmer", s);
GameSessionSnapshot snap = new();
snap.Telemetry["wave"] = "2";
string u = composer.BuildUserPayload(snap,
new AiTaskRequest { RoleId = BuiltInAgentRoleIds.Creator, Hint = "h" });
StringAssert.Contains("\"wave\":\"2\"", u);
StringAssert.Contains("\"hint\":\"h\"", u);
}
[Test]
public void SecureLuaEnvironment_AllowsWhitelistedApi()
{
SecureLuaEnvironment env = new();
LuaApiRegistry reg = new();
reg.Register("add", new System.Func<double, double, double>((a, b) => a + b));
Script script = env.CreateScript(reg);
DynValue r = script.DoString("return add(2,3)");
Assert.AreEqual(5, (int)r.Number);
}
[Test]
public void SecureLuaEnvironment_StripGlobals_RemovesRequire()
{
SecureLuaEnvironment env = new();
Script script = env.CreateScript(new LuaApiRegistry());
Assert.Throws<ScriptRuntimeException>(() => script.DoString("return require('x')"));
}
[Test]
public void SecureLuaEnvironment_InfiniteLoop_IsInterrupted()
{
SecureLuaEnvironment env = new();
Script script = env.CreateScript(new LuaApiRegistry());
ScriptRuntimeException ex = Assert.Throws<ScriptRuntimeException>(() =>
env.RunChunk(script, "while true do local x = 1 end")
);
// Может прерваться по лимиту шагов ИЛИ по wall-clock timeout — оба валидны
bool isStepLimit = ex.Message.Contains("EXCEEDED_HARD_LIMIT_STEPS");
bool isTimeout = ex.Message.Contains("Lua exceeded");
Assert.IsTrue(isStepLimit || isTimeout,
$"Expected step limit or timeout, but got: {ex.Message}");
}
}
}