nandi/oripublic Fork 0
55d4c9e2f7a9027b52846558166f42e50851523a
Commits
Clone
git clone https://git.rickub.com/nandi/ori.git
git clone ssh://git@rickub.com/nandi/ori.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

forked from bots-garden/ori

reducer.test.ts · 322 lines · 8.4 KBTypeScript Blame HistoryRaw
  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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import { describe, expect, it } from "vitest";
import { initialState, reduce, type ChatState } from "./reducer";
import type { ServerMessage } from "./protocol";

function afterServer(
	state: ChatState,
	...messages: ServerMessage[]
): ChatState {
	return messages.reduce(
		(s, message) => reduce(s, { type: "server", message }),
		state,
	);
}

describe("reduce", () => {
	it("stores the session and goes online on hello", () => {
		const state = afterServer(initialState, {
			type: "hello",
			sessionId: "s1",
			turnActive: true,
		});
		expect(state.sessionId).toBe("s1");
		expect(state.connection).toBe("online");
		expect(state.turnActive).toBe(true);
	});

	it("resets the thread on hello so reconnect replays are idempotent", () => {
		let state = afterServer(
			initialState,
			{ type: "hello", sessionId: "s1" },
			{ type: "user_message", text: "hi" },
		);
		expect(state.thread).toHaveLength(1);
		state = afterServer(
			state,
			{ type: "hello", sessionId: "s1" },
			{ type: "user_message", text: "hi" },
		);
		expect(state.thread).toHaveLength(1);
	});

	it("appends user messages to the thread", () => {
		const state = afterServer(initialState, {
			type: "user_message",
			text: "do the thing",
		});
		expect(state.thread).toEqual([{ kind: "user", text: "do the thing" }]);
	});

	it("accumulates agent chunks into one open item", () => {
		const chunk = (text: string): ServerMessage => ({
			type: "session_update",
			update: {
				sessionUpdate: "agent_message_chunk",
				content: { type: "text", text },
			},
		});
		const state = afterServer(initialState, chunk("Hello "), chunk("world"));
		expect(state.thread).toEqual([
			{ kind: "agent", text: "Hello world", closed: false },
		]);
	});

	it("starts a fresh agent item after the turn ends", () => {
		const chunk = (text: string): ServerMessage => ({
			type: "session_update",
			update: {
				sessionUpdate: "agent_message_chunk",
				content: { type: "text", text },
			},
		});
		const state = afterServer(
			initialState,
			chunk("first answer"),
			{ type: "turn_ended", stopReason: "end_turn" },
			chunk("second answer"),
		);
		expect(state.thread).toHaveLength(2);
		expect(state.thread[0]).toMatchObject({
			kind: "agent",
			text: "first answer",
			closed: true,
		});
		expect(state.thread[1]).toMatchObject({
			kind: "agent",
			text: "second answer",
			closed: false,
		});
	});

	it("tracks the turn flag through markers", () => {
		let state = afterServer(initialState, { type: "turn_started" });
		expect(state.turnActive).toBe(true);
		state = afterServer(state, { type: "turn_ended", stopReason: "end_turn" });
		expect(state.turnActive).toBe(false);
	});

	it("renders server errors as thread items", () => {
		const state = afterServer(initialState, { type: "error", message: "boom" });
		expect(state.thread).toEqual([{ kind: "error", text: "boom" }]);
	});

	it("ignores unknown update kinds without breaking", () => {
		const state = afterServer(initialState, {
			type: "session_update",
			update: { sessionUpdate: "something_new" },
		});
		expect(state.thread).toEqual([]);
	});

	it("accumulates thought chunks separately from agent messages", () => {
		const state = afterServer(
			initialState,
			{
				type: "session_update",
				update: {
					sessionUpdate: "agent_thought_chunk",
					content: { type: "text", text: "hmm " },
				},
			},
			{
				type: "session_update",
				update: {
					sessionUpdate: "agent_thought_chunk",
					content: { type: "text", text: "ok" },
				},
			},
			{
				type: "session_update",
				update: {
					sessionUpdate: "agent_message_chunk",
					content: { type: "text", text: "Answer" },
				},
			},
		);
		expect(state.thread).toHaveLength(2);
		expect(state.thread[0]).toMatchObject({ kind: "thought", text: "hmm ok" });
		expect(state.thread[1]).toMatchObject({ kind: "agent", text: "Answer" });
	});

	it("creates a tool call and merges its updates", () => {
		let state = afterServer(initialState, {
			type: "session_update",
			update: {
				sessionUpdate: "tool_call",
				toolCallId: "call-1",
				title: "Reading files",
				kind: "read",
				status: "pending",
				locations: [{ path: "/p/main.go" }],
			},
		});
		expect(state.thread).toEqual([
			{
				kind: "tool",
				call: {
					toolCallId: "call-1",
					title: "Reading files",
					toolKind: "read",
					status: "pending",
					contents: [],
					locations: [{ path: "/p/main.go" }],
				},
			},
		]);

		state = afterServer(state, {
			type: "session_update",
			update: {
				sessionUpdate: "tool_call_update",
				toolCallId: "call-1",
				status: "completed",
				content: [
					{ type: "content", content: { type: "text", text: "42 lines" } },
				],
			},
		});
		expect(state.thread).toHaveLength(1);
		const item = state.thread[0];
		if (item.kind !== "tool") throw new Error("expected a tool item");
		expect(item.call.status).toBe("completed");
		expect(item.call.title).toBe("Reading files");
		expect(item.call.contents).toHaveLength(1);
	});

	it("creates a tool item when an update arrives for an unknown id", () => {
		const state = afterServer(initialState, {
			type: "session_update",
			update: {
				sessionUpdate: "tool_call_update",
				toolCallId: "ghost",
				status: "in_progress",
			},
		});
		expect(state.thread).toHaveLength(1);
		expect(state.thread[0]).toMatchObject({
			kind: "tool",
			call: { toolCallId: "ghost", status: "in_progress" },
		});
	});

	it("replaces the plan on each plan update", () => {
		let state = afterServer(initialState, {
			type: "session_update",
			update: {
				sessionUpdate: "plan",
				entries: [{ content: "step 1", priority: "high", status: "pending" }],
			},
		});
		expect(state.plan).toHaveLength(1);
		state = afterServer(state, {
			type: "session_update",
			update: {
				sessionUpdate: "plan",
				entries: [
					{ content: "step 1", priority: "high", status: "completed" },
					{ content: "step 2", priority: "low", status: "pending" },
				],
			},
		});
		expect(state.plan).toHaveLength(2);
		expect(state.plan[0].status).toBe("completed");
	});

	it("tracks permission requests until they are resolved", () => {
		let state = afterServer(initialState, {
			type: "permission_request",
			requestId: "perm-1",
			request: {
				toolCall: { title: "Run make test" },
				options: [{ optionId: "allow", name: "Allow", kind: "allow_once" }],
			},
		});
		expect(state.permissions).toEqual([
			{
				requestId: "perm-1",
				title: "Run make test",
				options: [{ optionId: "allow", name: "Allow", kind: "allow_once" }],
			},
		]);

		// replayed duplicates are ignored
		state = afterServer(state, {
			type: "permission_request",
			requestId: "perm-1",
			request: { options: [] },
		});
		expect(state.permissions).toHaveLength(1);

		state = afterServer(state, {
			type: "permission_resolved",
			requestId: "perm-1",
		});
		expect(state.permissions).toEqual([]);
	});

	it("clears plan and permissions on hello", () => {
		let state = afterServer(
			initialState,
			{
				type: "permission_request",
				requestId: "perm-1",
				request: { options: [] },
			},
			{
				type: "session_update",
				update: {
					sessionUpdate: "plan",
					entries: [{ content: "x", priority: "low", status: "pending" }],
				},
			},
		);
		state = afterServer(state, { type: "hello", sessionId: "s1" });
		expect(state.permissions).toEqual([]);
		expect(state.plan).toEqual([]);
	});

	it("stores the agent's available commands and clears them on hello", () => {
		let state = reduce(initialState, {
			type: "server",
			message: {
				type: "session_update",
				update: {
					sessionUpdate: "available_commands_update",
					availableCommands: [
						{ name: "review", description: "Review changes" },
						{ name: "compact", description: "Summarise", input: { hint: "x" } },
					],
				},
			},
		});
		expect(state.commands.map((c) => c.name)).toEqual(["review", "compact"]);

		state = reduce(state, {
			type: "server",
			message: { type: "hello", sessionId: "s2" },
		});
		expect(state.commands).toEqual([]);
	});

	it("tracks connection status changes", () => {
		const state = reduce(initialState, {
			type: "connection",
			status: "offline",
		});
		expect(state.connection).toBe("offline");
	});

	it("never mutates the previous state", () => {
		const before = afterServer(initialState, {
			type: "user_message",
			text: "a",
		});
		const snapshot = JSON.parse(JSON.stringify(before));
		afterServer(
			before,
			{ type: "user_message", text: "b" },
			{ type: "turn_started" },
		);
		expect(before).toEqual(snapshot);
	});
});