Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

orchestration.test.ts

361 lines11.1 KB

1import assert from "node:assert/strict";
2import { it } from "@effect/vitest";
3import { Effect, Schema } from "effect";
4​
5import {
6 DEFAULT_PROVIDER_INTERACTION_MODE,
7 DEFAULT_RUNTIME_MODE,
8 OrchestrationGetTurnDiffInput,
9 OrchestrationLatestTurn,
10 OrchestrationProposedPlan,
11 OrchestrationSession,
12 OrchestrationThread,
13 OrchestrationThreadActivity,
14 ProjectCreateCommand,
15 ThreadTurnStartCommand,
16 ThreadCreatedPayload,
17 ThreadTurnDiff,
18 ThreadTurnStartRequestedPayload,
19} from "./orchestration";
20​
21const decodeTurnDiffInput = Schema.decodeUnknownEffect(OrchestrationGetTurnDiffInput);
22const decodeThreadTurnDiff = Schema.decodeUnknownEffect(ThreadTurnDiff);
23const decodeProjectCreateCommand = Schema.decodeUnknownEffect(ProjectCreateCommand);
24const decodeThreadTurnStartCommand = Schema.decodeUnknownEffect(ThreadTurnStartCommand);
25const decodeThreadTurnStartRequestedPayload = Schema.decodeUnknownEffect(
26 ThreadTurnStartRequestedPayload,
27);
28const decodeOrchestrationLatestTurn = Schema.decodeUnknownEffect(OrchestrationLatestTurn);
29const decodeOrchestrationProposedPlan = Schema.decodeUnknownEffect(OrchestrationProposedPlan);
30const decodeOrchestrationSession = Schema.decodeUnknownEffect(OrchestrationSession);
31const decodeOrchestrationThread = Schema.decodeUnknownEffect(OrchestrationThread);
32const decodeOrchestrationThreadActivity = Schema.decodeUnknownEffect(OrchestrationThreadActivity);
33const decodeThreadCreatedPayload = Schema.decodeUnknownEffect(ThreadCreatedPayload);
34​
35it.effect("parses turn diff input when fromTurnCount <= toTurnCount", () =>
36 Effect.gen(function* () {
37 const parsed = yield* decodeTurnDiffInput({
38 threadId: "thread-1",
39 fromTurnCount: 1,
40 toTurnCount: 2,
41 });
42 assert.strictEqual(parsed.fromTurnCount, 1);
43 assert.strictEqual(parsed.toTurnCount, 2);
44 }),
45);
46​
47it.effect("rejects turn diff input when fromTurnCount > toTurnCount", () =>
48 Effect.gen(function* () {
49 const result = yield* Effect.exit(
50 decodeTurnDiffInput({
51 threadId: "thread-1",
52 fromTurnCount: 3,
53 toTurnCount: 2,
54 }),
55 );
56 assert.strictEqual(result._tag, "Failure");
57 }),
58);
59​
60it.effect("rejects thread turn diff when fromTurnCount > toTurnCount", () =>
61 Effect.gen(function* () {
62 const result = yield* Effect.exit(
63 decodeThreadTurnDiff({
64 threadId: "thread-1",
65 fromTurnCount: 3,
66 toTurnCount: 2,
67 diff: "patch",
68 }),
69 );
70 assert.strictEqual(result._tag, "Failure");
71 }),
72);
73​
74it.effect("trims branded ids and command string fields at decode boundaries", () =>
75 Effect.gen(function* () {
76 const parsed = yield* decodeProjectCreateCommand({
77 type: "project.create",
78 commandId: " cmd-1 ",
79 projectId: " project-1 ",
80 title: " Project Title ",
81 workspaceRoot: " /tmp/workspace ",
82 defaultModel: " gpt-5.2 ",
83 createdAt: "2026-01-01T00:00:00.000Z",
84 });
85 assert.strictEqual(parsed.commandId, "cmd-1");
86 assert.strictEqual(parsed.projectId, "project-1");
87 assert.strictEqual(parsed.title, "Project Title");
88 assert.strictEqual(parsed.workspaceRoot, "/tmp/workspace");
89 assert.strictEqual(parsed.defaultModel, "gpt-5.2");
90 }),
91);
92​
93it.effect("rejects command fields that become empty after trim", () =>
94 Effect.gen(function* () {
95 const result = yield* Effect.exit(
96 decodeProjectCreateCommand({
97 type: "project.create",
98 commandId: "cmd-1",
99 projectId: "project-1",
100 title: " ",
101 workspaceRoot: "/tmp/workspace",
102 createdAt: "2026-01-01T00:00:00.000Z",
103 }),
104 );
105 assert.strictEqual(result._tag, "Failure");
106 }),
107);
108​
109it.effect("decodes thread.turn.start defaults for provider and runtime mode", () =>
110 Effect.gen(function* () {
111 const parsed = yield* decodeThreadTurnStartCommand({
112 type: "thread.turn.start",
113 commandId: "cmd-turn-1",
114 threadId: "thread-1",
115 message: {
116 messageId: "msg-1",
117 role: "user",
118 text: "hello",
119 attachments: [],
120 },
121 createdAt: "2026-01-01T00:00:00.000Z",
122 });
123 assert.strictEqual(parsed.provider, undefined);
124 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
125 assert.strictEqual(parsed.interactionMode, DEFAULT_PROVIDER_INTERACTION_MODE);
126 }),
127);
128​
129it.effect("preserves explicit provider and runtime mode in thread.turn.start", () =>
130 Effect.gen(function* () {
131 const parsed = yield* decodeThreadTurnStartCommand({
132 type: "thread.turn.start",
133 commandId: "cmd-turn-2",
134 threadId: "thread-1",
135 message: {
136 messageId: "msg-2",
137 role: "user",
138 text: "hello",
139 attachments: [],
140 },
141 provider: "codex",
142 runtimeMode: "full-access",
143 createdAt: "2026-01-01T00:00:00.000Z",
144 });
145 assert.strictEqual(parsed.provider, "codex");
146 assert.strictEqual(parsed.runtimeMode, "full-access");
147 assert.strictEqual(parsed.interactionMode, DEFAULT_PROVIDER_INTERACTION_MODE);
148 }),
149);
150​
151it.effect("decodes thread.created runtime mode for historical events", () =>
152 Effect.gen(function* () {
153 const parsed = yield* decodeThreadCreatedPayload({
154 threadId: "thread-1",
155 projectId: "project-1",
156 title: "Thread title",
157 model: "gpt-5.4",
158 interactionMode: "default",
159 branch: null,
160 worktreePath: null,
161 createdAt: "2026-01-01T00:00:00.000Z",
162 updatedAt: "2026-01-01T00:00:00.000Z",
163 });
164​
165 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
166 }),
167);
168​
169it.effect("accepts provider-scoped model options in thread.turn.start", () =>
170 Effect.gen(function* () {
171 const parsed = yield* decodeThreadTurnStartCommand({
172 type: "thread.turn.start",
173 commandId: "cmd-turn-options",
174 threadId: "thread-1",
175 message: {
176 messageId: "msg-options",
177 role: "user",
178 text: "hello",
179 attachments: [],
180 },
181 provider: "codex",
182 model: "gpt-5.3-codex",
183 modelOptions: {
184 codex: {
185 reasoningEffort: "high",
186 fastMode: true,
187 },
188 },
189 createdAt: "2026-01-01T00:00:00.000Z",
190 });
191 assert.strictEqual(parsed.provider, "codex");
192 assert.strictEqual(parsed.modelOptions?.codex?.reasoningEffort, "high");
193 assert.strictEqual(parsed.modelOptions?.codex?.fastMode, true);
194 }),
195);
196​
197it.effect("accepts a source proposed plan reference in thread.turn.start", () =>
198 Effect.gen(function* () {
199 const parsed = yield* decodeThreadTurnStartCommand({
200 type: "thread.turn.start",
201 commandId: "cmd-turn-source-plan",
202 threadId: "thread-2",
203 message: {
204 messageId: "msg-source-plan",
205 role: "user",
206 text: "implement this",
207 attachments: [],
208 },
209 sourceProposedPlan: {
210 threadId: "thread-1",
211 planId: "plan-1",
212 },
213 createdAt: "2026-01-01T00:00:00.000Z",
214 });
215 assert.deepStrictEqual(parsed.sourceProposedPlan, {
216 threadId: "thread-1",
217 planId: "plan-1",
218 });
219 }),
220);
221​
222it.effect(
223 "decodes thread.turn-start-requested defaults for provider, runtime mode, and interaction mode",
224 () =>
225 Effect.gen(function* () {
226 const parsed = yield* decodeThreadTurnStartRequestedPayload({
227 threadId: "thread-1",
228 messageId: "msg-1",
229 createdAt: "2026-01-01T00:00:00.000Z",
230 });
231 assert.strictEqual(parsed.provider, undefined);
232 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
233 assert.strictEqual(parsed.interactionMode, DEFAULT_PROVIDER_INTERACTION_MODE);
234 assert.strictEqual(parsed.sourceProposedPlan, undefined);
235 }),
236);
237​
238it.effect("decodes thread.turn-start-requested source proposed plan metadata when present", () =>
239 Effect.gen(function* () {
240 const parsed = yield* decodeThreadTurnStartRequestedPayload({
241 threadId: "thread-2",
242 messageId: "msg-2",
243 sourceProposedPlan: {
244 threadId: "thread-1",
245 planId: "plan-1",
246 },
247 createdAt: "2026-01-01T00:00:00.000Z",
248 });
249 assert.deepStrictEqual(parsed.sourceProposedPlan, {
250 threadId: "thread-1",
251 planId: "plan-1",
252 });
253 }),
254);
255​
256it.effect("decodes latest turn source proposed plan metadata when present", () =>
257 Effect.gen(function* () {
258 const parsed = yield* decodeOrchestrationLatestTurn({
259 turnId: "turn-2",
260 state: "running",
261 requestedAt: "2026-01-01T00:00:00.000Z",
262 startedAt: "2026-01-01T00:00:01.000Z",
263 completedAt: null,
264 assistantMessageId: null,
265 sourceProposedPlan: {
266 threadId: "thread-1",
267 planId: "plan-1",
268 },
269 });
270 assert.deepStrictEqual(parsed.sourceProposedPlan, {
271 threadId: "thread-1",
272 planId: "plan-1",
273 });
274 }),
275);
276​
277it.effect("decodes orchestration session runtime mode defaults", () =>
278 Effect.gen(function* () {
279 const parsed = yield* decodeOrchestrationSession({
280 threadId: "thread-1",
281 status: "idle",
282 providerName: null,
283 providerSessionId: null,
284 providerThreadId: null,
285 activeTurnId: null,
286 lastError: null,
287 updatedAt: "2026-01-01T00:00:00.000Z",
288 });
289 assert.strictEqual(parsed.runtimeMode, DEFAULT_RUNTIME_MODE);
290 }),
291);
292​
293it.effect("defaults proposed plan implementation metadata for historical rows", () =>
294 Effect.gen(function* () {
295 const parsed = yield* decodeOrchestrationProposedPlan({
296 id: "plan-1",
297 turnId: "turn-1",
298 planMarkdown: "# Plan",
299 createdAt: "2026-01-01T00:00:00.000Z",
300 updatedAt: "2026-01-01T00:00:00.000Z",
301 });
302 assert.strictEqual(parsed.implementedAt, null);
303 assert.strictEqual(parsed.implementationThreadId, null);
304 }),
305);
306​
307it.effect("preserves proposed plan implementation metadata when present", () =>
308 Effect.gen(function* () {
309 const parsed = yield* decodeOrchestrationProposedPlan({
310 id: "plan-2",
311 turnId: "turn-2",
312 planMarkdown: "# Plan",
313 implementedAt: "2026-01-02T00:00:00.000Z",
314 implementationThreadId: "thread-2",
315 createdAt: "2026-01-01T00:00:00.000Z",
316 updatedAt: "2026-01-02T00:00:00.000Z",
317 });
318 assert.strictEqual(parsed.implementedAt, "2026-01-02T00:00:00.000Z");
319 assert.strictEqual(parsed.implementationThreadId, "thread-2");
320 }),
321);
322​
323it.effect("defaults activity work unit ids for historical rows", () =>
324 Effect.gen(function* () {
325 const parsed = yield* decodeOrchestrationThreadActivity({
326 id: "activity-1",
327 tone: "info",
328 kind: "runtime.note",
329 summary: "Runtime note",
330 payload: {},
331 turnId: null,
332 createdAt: "2026-01-01T00:00:00.000Z",
333 });
334 assert.strictEqual(parsed.workUnitId, null);
335 }),
336);
337​
338it.effect("defaults thread work units for historical rows", () =>
339 Effect.gen(function* () {
340 const parsed = yield* decodeOrchestrationThread({
341 id: "thread-1",
342 projectId: "project-1",
343 title: "Thread",
344 model: "gpt-5-codex",
345 runtimeMode: "full-access",
346 interactionMode: "default",
347 branch: null,
348 worktreePath: null,
349 latestTurn: null,
350 createdAt: "2026-01-01T00:00:00.000Z",
351 updatedAt: "2026-01-01T00:00:00.000Z",
352 deletedAt: null,
353 messages: [],
354 proposedPlans: [],
355 activities: [],
356 checkpoints: [],
357 session: null,
358 });
359 assert.deepStrictEqual(parsed.workUnits, []);
360 }),
361);