Skip to content

Commit 789f0d0

Browse files
committed
add LLMCharacter save unit test
1 parent 0c7555f commit 789f0d0

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

Tests/Runtime/TestLLM.cs

+47-4
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,9 @@ public virtual async Task Tests()
246246

247247
public void TestInitParameters(int nkeep, int chats)
248248
{
249-
Assert.That(llmCharacter.nKeep == nkeep);
249+
Assert.AreEqual(llmCharacter.nKeep, nkeep);
250250
Assert.That(ChatTemplate.GetTemplate(llm.chatTemplate).GetStop(llmCharacter.playerName, llmCharacter.AIName).Length > 0);
251-
Assert.That(llmCharacter.chat.Count == chats);
251+
Assert.AreEqual(llmCharacter.chat.Count, chats);
252252
}
253253

254254
public void TestTokens(List<int> tokens)
@@ -410,7 +410,7 @@ public override async Task Tests()
410410
public class TestLLM_Double : TestLLM
411411
{
412412
LLM llm1;
413-
LLMCharacter lLMCharacter1;
413+
LLMCharacter llmCharacter1;
414414

415415
public override async Task Init()
416416
{
@@ -421,8 +421,51 @@ public override async Task Init()
421421
llm = CreateLLM();
422422
llmCharacter = CreateLLMCharacter();
423423
llm1 = CreateLLM();
424-
lLMCharacter1 = CreateLLMCharacter();
424+
llmCharacter1 = CreateLLMCharacter();
425425
gameObject.SetActive(true);
426426
}
427427
}
428+
429+
public class TestLLMCharacter_Save : TestLLM
430+
{
431+
string saveName = "TestLLMCharacter_Save";
432+
433+
public override LLMCharacter CreateLLMCharacter()
434+
{
435+
LLMCharacter llmCharacter = base.CreateLLMCharacter();
436+
llmCharacter.save = saveName;
437+
llmCharacter.saveCache = true;
438+
return llmCharacter;
439+
}
440+
441+
public override async Task Tests()
442+
{
443+
await base.Tests();
444+
TestSave();
445+
}
446+
447+
public void TestSave()
448+
{
449+
string jsonPath = llmCharacter.GetJsonSavePath(saveName);
450+
string cachePath = llmCharacter.GetCacheSavePath(saveName);
451+
Assert.That(File.Exists(jsonPath));
452+
Assert.That(File.Exists(cachePath));
453+
string json = File.ReadAllText(jsonPath);
454+
File.Delete(jsonPath);
455+
File.Delete(cachePath);
456+
457+
List<ChatMessage> chatHistory = JsonUtility.FromJson<ChatListWrapper>(json).chat;
458+
Assert.AreEqual(chatHistory.Count, 2);
459+
Assert.AreEqual(chatHistory[0].role, llmCharacter.playerName);
460+
Assert.AreEqual(chatHistory[0].content, "hi");
461+
Assert.AreEqual(chatHistory[1].role, llmCharacter.AIName);
462+
463+
Assert.AreEqual(llmCharacter.chat.Count, chatHistory.Count + 1);
464+
for (int i = 0; i < chatHistory.Count; i++)
465+
{
466+
Assert.AreEqual(chatHistory[i].role, llmCharacter.chat[i + 1].role);
467+
Assert.AreEqual(chatHistory[i].content, llmCharacter.chat[i + 1].content);
468+
}
469+
}
470+
}
428471
}

0 commit comments

Comments
 (0)