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
|
import { beforeEach, describe, expect, it } from "vitest";
import { act, fireEvent, render, screen } from "@testing-library/react";
import { App } from "./App";
import { dispatch, resetStore } from "./store";
import type { OriSocket } from "./ws";
import type { ClientMessage } from "./protocol";
function fakeSocket() {
const sent: ClientMessage[] = [];
const socket: OriSocket = {
send: (message) => sent.push(message),
close: () => {},
};
return { socket, sent };
}
describe("App", () => {
beforeEach(() => {
resetStore();
});
it("sends a prompt typed in the composer", () => {
const { socket, sent } = fakeSocket();
render(<App socket={socket} />);
fireEvent.change(screen.getByLabelText("prompt"), {
target: { value: "hello agent" },
});
fireEvent.click(screen.getByRole("button", { name: "Send" }));
expect(sent).toEqual([{ type: "prompt", text: "hello agent" }]);
});
it("shows the streamed conversation from the store", () => {
const { socket } = fakeSocket();
render(<App socket={socket} />);
act(() => {
dispatch({
type: "server",
message: { type: "user_message", text: "my question" },
});
dispatch({
type: "server",
message: {
type: "session_update",
update: {
sessionUpdate: "agent_message_chunk",
content: { type: "text", text: "my **answer**" },
},
},
});
});
expect(screen.getByText("my question")).toBeDefined();
expect(screen.getByText("answer")).toBeDefined(); // bold inner text
});
it("offers Stop instead of Send during a turn, and Stop cancels", () => {
const { socket, sent } = fakeSocket();
render(<App socket={socket} />);
act(() => {
dispatch({ type: "server", message: { type: "turn_started" } });
});
expect(screen.queryByRole("button", { name: "Send" })).toBeNull();
fireEvent.click(screen.getByRole("button", { name: "Stop" }));
expect(sent).toEqual([{ type: "cancel" }]);
});
it("shows the connection status and session id from hello", () => {
const { socket } = fakeSocket();
render(<App socket={socket} />);
act(() => {
dispatch({
type: "server",
message: { type: "hello", sessionId: "sess-42" },
});
});
expect(screen.getByText(/online/)).toBeDefined();
expect(screen.getByText(/sess-42/)).toBeDefined();
});
it("renders tool calls, plan and thoughts from updates", () => {
const { socket } = fakeSocket();
render(<App socket={socket} />);
act(() => {
dispatch({
type: "server",
message: {
type: "session_update",
update: {
sessionUpdate: "tool_call",
toolCallId: "c1",
title: "Running tests",
kind: "execute",
status: "in_progress",
},
},
});
dispatch({
type: "server",
message: {
type: "session_update",
update: {
sessionUpdate: "plan",
entries: [
{
content: "write the fix",
priority: "high",
status: "in_progress",
},
],
},
},
});
dispatch({
type: "server",
message: {
type: "session_update",
update: {
sessionUpdate: "agent_thought_chunk",
content: { type: "text", text: "let me think" },
},
},
});
});
expect(screen.getByText("Running tests")).toBeDefined();
expect(screen.getByText(/write the fix/)).toBeDefined();
expect(screen.getByText("let me think")).toBeDefined();
});
it("answers a permission request through the socket", () => {
const { socket, sent } = fakeSocket();
render(<App socket={socket} />);
act(() => {
dispatch({
type: "server",
message: {
type: "permission_request",
requestId: "perm-9",
request: {
toolCall: { title: "Write main.go" },
options: [
{
optionId: "allow-once",
name: "Allow once",
kind: "allow_once",
},
{ optionId: "reject-once", name: "Reject", kind: "reject_once" },
],
},
},
});
});
expect(screen.getByText(/Write main\.go/)).toBeDefined();
fireEvent.click(screen.getByRole("button", { name: "Allow once" }));
expect(sent).toEqual([
{
type: "permission_response",
requestId: "perm-9",
optionId: "allow-once",
},
]);
});
it("does not send empty prompts", () => {
const { socket, sent } = fakeSocket();
render(<App socket={socket} />);
fireEvent.change(screen.getByLabelText("prompt"), {
target: { value: " " },
});
fireEvent.keyDown(screen.getByLabelText("prompt"), { key: "Enter" });
expect(sent).toEqual([]);
});
});
|