Skip to content

Commit c4a7fed

Browse files
committed
minor #204 [Agent] Add unit tests for Chat (OskarStark)
This PR was squashed before being merged into the main branch. Discussion ---------- [Agent] Add unit tests for `Chat` | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | no | Issues | -- | License | MIT Commits ------- 64b9dfc [Agent] Add unit tests for `Chat`
2 parents f38317f + 64b9dfc commit c4a7fed

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed

src/agent/tests/ChatTest.php

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Agent\Tests;
13+
14+
use PHPUnit\Framework\Attributes\CoversClass;
15+
use PHPUnit\Framework\Attributes\Small;
16+
use PHPUnit\Framework\Attributes\Test;
17+
use PHPUnit\Framework\Attributes\UsesClass;
18+
use PHPUnit\Framework\TestCase;
19+
use Symfony\AI\Agent\AgentInterface;
20+
use Symfony\AI\Agent\Chat;
21+
use Symfony\AI\Agent\Chat\MessageStoreInterface;
22+
use Symfony\AI\Platform\Message\AssistantMessage;
23+
use Symfony\AI\Platform\Message\Message;
24+
use Symfony\AI\Platform\Message\MessageBag;
25+
use Symfony\AI\Platform\Message\MessageBagInterface;
26+
use Symfony\AI\Platform\Result\TextResult;
27+
28+
#[CoversClass(Chat::class)]
29+
#[UsesClass(Message::class)]
30+
#[UsesClass(MessageBag::class)]
31+
#[UsesClass(TextResult::class)]
32+
#[Small]
33+
final class ChatTest extends TestCase
34+
{
35+
private AgentInterface&\PHPUnit\Framework\MockObject\MockObject $agent;
36+
private MessageStoreInterface&\PHPUnit\Framework\MockObject\MockObject $store;
37+
private Chat $chat;
38+
39+
protected function setUp(): void
40+
{
41+
$this->agent = $this->createMock(AgentInterface::class);
42+
$this->store = $this->createMock(MessageStoreInterface::class);
43+
$this->chat = new Chat($this->agent, $this->store);
44+
}
45+
46+
#[Test]
47+
public function itInitiatesChatByClearingAndSavingMessages(): void
48+
{
49+
$messages = $this->createMock(MessageBagInterface::class);
50+
51+
$this->store->expects($this->once())
52+
->method('clear');
53+
54+
$this->store->expects($this->once())
55+
->method('save')
56+
->with($messages);
57+
58+
$this->chat->initiate($messages);
59+
}
60+
61+
#[Test]
62+
public function itSubmitsUserMessageAndReturnsAssistantMessage(): void
63+
{
64+
$userMessage = Message::ofUser('Hello, how are you?');
65+
$existingMessages = new MessageBag();
66+
$assistantContent = 'I am doing well, thank you!';
67+
68+
$textResult = new TextResult($assistantContent);
69+
70+
$this->store->expects($this->once())
71+
->method('load')
72+
->willReturn($existingMessages);
73+
74+
$this->agent->expects($this->once())
75+
->method('call')
76+
->with($this->callback(function (MessageBagInterface $messages) use ($userMessage) {
77+
$messagesArray = $messages->getMessages();
78+
79+
return end($messagesArray) === $userMessage;
80+
}))
81+
->willReturn($textResult);
82+
83+
$this->store->expects($this->once())
84+
->method('save')
85+
->with($this->callback(function (MessageBagInterface $messages) use ($userMessage, $assistantContent) {
86+
$messagesArray = $messages->getMessages();
87+
$lastTwo = \array_slice($messagesArray, -2);
88+
89+
return 2 === \count($lastTwo)
90+
&& $lastTwo[0] === $userMessage
91+
&& $lastTwo[1] instanceof AssistantMessage
92+
&& $lastTwo[1]->content === $assistantContent;
93+
}));
94+
95+
$result = $this->chat->submit($userMessage);
96+
97+
$this->assertInstanceOf(AssistantMessage::class, $result);
98+
$this->assertSame($assistantContent, $result->content);
99+
}
100+
101+
#[Test]
102+
public function itAppendsMessagesToExistingConversation(): void
103+
{
104+
$existingUserMessage = Message::ofUser('What is the weather?');
105+
$existingAssistantMessage = Message::ofAssistant('I cannot provide weather information.');
106+
107+
$existingMessages = new MessageBag();
108+
$existingMessages->add($existingUserMessage);
109+
$existingMessages->add($existingAssistantMessage);
110+
111+
$newUserMessage = Message::ofUser('Can you help with programming?');
112+
$newAssistantContent = 'Yes, I can help with programming!';
113+
114+
$textResult = new TextResult($newAssistantContent);
115+
116+
$this->store->expects($this->once())
117+
->method('load')
118+
->willReturn($existingMessages);
119+
120+
$this->agent->expects($this->once())
121+
->method('call')
122+
->with($this->callback(function (MessageBagInterface $messages) {
123+
$messagesArray = $messages->getMessages();
124+
125+
return 3 === \count($messagesArray);
126+
}))
127+
->willReturn($textResult);
128+
129+
$this->store->expects($this->once())
130+
->method('save')
131+
->with($this->callback(function (MessageBagInterface $messages) {
132+
$messagesArray = $messages->getMessages();
133+
134+
return 4 === \count($messagesArray);
135+
}));
136+
137+
$result = $this->chat->submit($newUserMessage);
138+
139+
$this->assertInstanceOf(AssistantMessage::class, $result);
140+
$this->assertSame($newAssistantContent, $result->content);
141+
}
142+
143+
#[Test]
144+
public function itHandlesEmptyMessageStore(): void
145+
{
146+
$userMessage = Message::ofUser('First message');
147+
$emptyMessages = new MessageBag();
148+
$assistantContent = 'First response';
149+
150+
$textResult = new TextResult($assistantContent);
151+
152+
$this->store->expects($this->once())
153+
->method('load')
154+
->willReturn($emptyMessages);
155+
156+
$this->agent->expects($this->once())
157+
->method('call')
158+
->with($this->callback(function (MessageBagInterface $messages) {
159+
$messagesArray = $messages->getMessages();
160+
161+
return 1 === \count($messagesArray);
162+
}))
163+
->willReturn($textResult);
164+
165+
$this->store->expects($this->once())
166+
->method('save');
167+
168+
$result = $this->chat->submit($userMessage);
169+
170+
$this->assertInstanceOf(AssistantMessage::class, $result);
171+
$this->assertSame($assistantContent, $result->content);
172+
}
173+
}

0 commit comments

Comments
 (0)