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

decider.queuedTurns.test.ts

164 lines4.9 KB

1import {
2 CommandId,
3 MessageId,
4 ProjectId,
5 ProviderInstanceId,
6 ThreadId,
7 type OrchestrationReadModel,
8} from "@t3tools/contracts";
9import * as NodeServices from "@effect/platform-node/NodeServices";
10import { expect, it } from "@effect/vitest";
11import * as Effect from "effect/Effect";
12​
13import { decideOrchestrationCommand } from "./decider.ts";
14​
15const NOW = "2026-08-16T12:00:00.000Z";
16const THREAD_ID = ThreadId.make("thread-queue-test");
17const MESSAGE_ID = MessageId.make("message-queue-test");
18​
19function makeReadModel(deliveryState?: "queued"): OrchestrationReadModel {
20 return {
21 snapshotSequence: 0,
22 projects: [],
23 threads: [
24 {
25 id: THREAD_ID,
26 projectId: ProjectId.make("project-queue-test"),
27 title: "Queue test",
28 modelSelection: {
29 instanceId: ProviderInstanceId.make("codex"),
30 model: "gpt-5.4",
31 },
32 runtimeMode: "full-access",
33 interactionMode: "default",
34 branch: null,
35 worktreePath: null,
36 latestTurn: null,
37 createdAt: NOW,
38 updatedAt: NOW,
39 archivedAt: null,
40 settledOverride: null,
41 settledAt: null,
42 snoozedUntil: null,
43 snoozedAt: null,
44 deletedAt: null,
45 messages:
46 deliveryState === undefined
47 ? []
48 : [
49 {
50 id: MESSAGE_ID,
51 role: "user",
52 text: "Run this next",
53 turnId: null,
54 streaming: false,
55 createdAt: NOW,
56 updatedAt: NOW,
57 deliveryState,
58 },
59 ],
60 proposedPlans: [],
61 activities: [],
62 checkpoints: [],
63 session: null,
64 },
65 ],
66 updatedAt: NOW,
67 };
69​
70it.layer(NodeServices.layer)("queued turn decider", (it) => {
71 it.effect("persists after-current delivery instead of starting the provider immediately", () =>
72 Effect.gen(function* () {
73 const result = yield* decideOrchestrationCommand({
74 command: {
75 type: "thread.turn.start",
76 commandId: CommandId.make("cmd-queue-turn"),
77 threadId: THREAD_ID,
78 message: {
79 messageId: MESSAGE_ID,
80 role: "user",
81 text: "Run this next",
82 attachments: [],
83 },
84 runtimeMode: "full-access",
85 interactionMode: "default",
86 deliveryMode: "after-current",
87 createdAt: NOW,
88 },
89 readModel: makeReadModel(),
90 });
91​
92 const events = Array.isArray(result) ? result : [result];
93 expect(events.map((event) => event.type)).toEqual([
94 "thread.message-sent",
95 "thread.turn-queued",
96 ]);
97 }),
98 );
99​
100 it.effect("dispatches a queued message into the normal provider start path", () =>
101 Effect.gen(function* () {
102 const result = yield* decideOrchestrationCommand({
103 command: {
104 type: "thread.queued-turn.dispatch",
105 commandId: CommandId.make("cmd-dispatch-queued-turn"),
106 threadId: THREAD_ID,
107 messageId: MESSAGE_ID,
108 modelSelection: {
109 instanceId: ProviderInstanceId.make("codex"),
110 model: "gpt-5.4",
111 },
112 runtimeMode: "full-access",
113 interactionMode: "default",
114 queuedAt: NOW,
115 createdAt: "2026-08-16T12:01:00.000Z",
116 },
117 readModel: makeReadModel("queued"),
118 });
119​
120 const events = Array.isArray(result) ? result : [result];
121 expect(events.map((event) => event.type)).toEqual([
122 "thread.queued-turn-dispatched",
123 "thread.turn-start-requested",
124 ]);
125 const start = events[1];
126 expect(start?.type).toBe("thread.turn-start-requested");
127 if (start?.type === "thread.turn-start-requested") {
128 expect(start.payload.messageId).toBe(MESSAGE_ID);
129 expect(start.payload.createdAt).toBe(NOW);
130 }
131 }),
132 );
133​
134 it.effect("cancels only messages that are still queued", () =>
135 Effect.gen(function* () {
136 const result = yield* decideOrchestrationCommand({
137 command: {
138 type: "thread.queued-turn.cancel",
139 commandId: CommandId.make("cmd-cancel-queued-turn"),
140 threadId: THREAD_ID,
141 messageId: MESSAGE_ID,
142 createdAt: NOW,
143 },
144 readModel: makeReadModel("queued"),
145 });
146 const events = Array.isArray(result) ? result : [result];
147 expect(events.map((event) => event.type)).toEqual(["thread.queued-turn-cancelled"]);
148​
149 const failure = yield* Effect.result(
150 decideOrchestrationCommand({
151 command: {
152 type: "thread.queued-turn.cancel",
153 commandId: CommandId.make("cmd-cancel-delivered-turn"),
154 threadId: THREAD_ID,
155 messageId: MESSAGE_ID,
156 createdAt: NOW,
157 },
158 readModel: makeReadModel(),
159 }),
160 );
161 expect(failure._tag).toBe("Failure");
162 }),
163 );
164});