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

decider.active-order.test.ts

200 lines6.7 KB

1import {
2 CommandId,
3 ProjectId,
4 ProviderInstanceId,
5 ThreadId,
6 type OrchestrationCommand,
7 type OrchestrationReadModel,
8 type OrchestrationThread,
9} from "@t3tools/contracts";
10import * as NodeServices from "@effect/platform-node/NodeServices";
11import { expect, it } from "@effect/vitest";
12import * as Effect from "effect/Effect";
13​
14import { decideOrchestrationCommand } from "./decider.ts";
15import { projectEvent } from "./projector.ts";
16​
17const NOW = "2026-01-01T00:00:00.000Z";
18// The Effect test clock starts at the epoch.
19const BEFORE_NOW = "1969-12-30T00:00:00.000Z";
20const SNOOZED_AT = "1969-12-31T00:00:00.000Z";
21const FUTURE_WAKE = "1970-01-02T00:00:00.000Z";
22const THREAD_ID = ThreadId.make("thread-1");
23​
24function makeReadModel(overrides: Partial<OrchestrationThread> = {}): OrchestrationReadModel {
25 return {
26 snapshotSequence: 0,
27 projects: [],
28 threads: [
29 {
30 id: THREAD_ID,
31 projectId: ProjectId.make("project-1"),
32 title: "Thread",
33 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
34 runtimeMode: "full-access",
35 interactionMode: "default",
36 branch: null,
37 worktreePath: null,
38 latestTurn: null,
39 createdAt: NOW,
40 updatedAt: NOW,
41 archivedAt: null,
42 settledOverride: null,
43 settledAt: null,
44 unsettledAt: null,
45 activeOrderKey: null,
46 snoozedUntil: null,
47 snoozedAt: null,
48 pinnedAt: null,
49 pinOrderKey: null,
50 deletedAt: null,
51 messages: [],
52 proposedPlans: [],
53 activities: [],
54 checkpoints: [],
55 session: null,
56 ...overrides,
57 },
58 ],
59 updatedAt: NOW,
60 };
62​
63const reorderCommand = {
64 type: "thread.active.reorder",
65 commandId: CommandId.make("cmd-active-reorder"),
66 threadId: THREAD_ID,
67 orderKey: "m",
68} as const;
69​
70it.layer(NodeServices.layer)("active thread ordering", (it) => {
71 it.effect("persists changed and repeated slots without changing thread activity timestamps", () =>
72 Effect.gen(function* () {
73 let readModel = makeReadModel({ unsettledAt: BEFORE_NOW });
74 for (const orderKey of ["m", "m", "g"]) {
75 const decided = yield* decideOrchestrationCommand({
76 command: { ...reorderCommand, orderKey },
77 readModel,
78 });
79 const events = Array.isArray(decided) ? decided : [decided];
80 expect(events).toHaveLength(1);
81 expect(events[0]).toMatchObject({
82 type: "thread.meta-updated",
83 payload: { threadId: THREAD_ID, activeOrderKey: orderKey, updatedAt: NOW },
84 });
85 for (const event of events) {
86 readModel = yield* projectEvent(readModel, {
87 ...event,
88 sequence: readModel.snapshotSequence + 1,
89 });
90 }
91 expect(readModel.threads[0]).toMatchObject({
92 activeOrderKey: orderKey,
93 updatedAt: NOW,
94 createdAt: NOW,
95 unsettledAt: BEFORE_NOW,
96 });
97 }
98 }),
99 );
100​
101 for (const [label, overrides] of [
102 ["archived", { archivedAt: NOW }],
103 ["deleted", { deletedAt: NOW }],
104 ["pinned", { pinnedAt: NOW }],
105 ["settled", { settledOverride: "settled", settledAt: NOW }],
106 ] satisfies ReadonlyArray<readonly [string, Partial<OrchestrationThread>]>) {
107 it.effect(`rejects reordering a ${label} thread`, () =>
108 Effect.gen(function* () {
109 const error = yield* decideOrchestrationCommand({
110 command: reorderCommand,
111 readModel: makeReadModel(overrides),
112 }).pipe(Effect.flip);
113 expect(error._tag).toBe("OrchestrationCommandInvariantError");
114 }),
115 );
116 }
117​
118 it.effect("reorders a running thread without affecting its session", () =>
119 Effect.gen(function* () {
120 const readModel = makeReadModel({
121 session: {
122 threadId: THREAD_ID,
123 status: "running",
124 providerName: "codex",
125 runtimeMode: "full-access",
126 activeTurnId: null,
127 lastError: null,
128 updatedAt: NOW,
129 },
130 });
131 const decided = yield* decideOrchestrationCommand({ command: reorderCommand, readModel });
132 const events = Array.isArray(decided) ? decided : [decided];
133 expect(events).toHaveLength(1);
134 for (const event of events) {
135 const projected = yield* projectEvent(readModel, { ...event, sequence: 1 });
136 expect(projected.threads[0]).toEqual({ ...readModel.threads[0], activeOrderKey: "m" });
137 }
138 }),
139 );
140​
141 it.effect(
142 "changes a snoozed thread's retained slot without waking it or changing timestamps",
143 () =>
144 Effect.gen(function* () {
145 const readModel = makeReadModel({
146 activeOrderKey: "g",
147 snoozedAt: SNOOZED_AT,
148 snoozedUntil: FUTURE_WAKE,
149 unsettledAt: BEFORE_NOW,
150 });
151 const decided = yield* decideOrchestrationCommand({ command: reorderCommand, readModel });
152 const events = Array.isArray(decided) ? decided : [decided];
153 expect(events).toHaveLength(1);
154 for (const event of events) {
155 const projected = yield* projectEvent(readModel, { ...event, sequence: 1 });
156 expect(projected.threads[0]).toEqual({ ...readModel.threads[0], activeOrderKey: "m" });
157 }
158 }),
159 );
160​
161 it.effect("keeps placement through metadata, pin and snooze, then resets it on settlement", () =>
162 Effect.gen(function* () {
163 let readModel = makeReadModel();
164 const steps = [
165 [reorderCommand, "m"],
166 [{ type: "thread.meta.update", title: "Renamed" }, "m"],
167 [{ type: "thread.pin", orderKey: "g" }, "m"],
168 [{ type: "thread.snooze", snoozedUntil: FUTURE_WAKE }, "m"],
169 [{ type: "thread.unsnooze", reason: "user" }, "m"],
170 [{ type: "thread.unpin" }, "m"],
171 [{ type: "thread.settle" }, null],
172 [{ type: "thread.unsettle", reason: "user" }, null],
173 [{ type: "thread.active.reorder", orderKey: "s" }, "s"],
174 ] as const;
175 for (const [index, [step, expectedKey]] of steps.entries()) {
176 const command: OrchestrationCommand = {
177 ...step,
178 commandId: CommandId.make(`lifecycle-${index}`),
179 threadId: THREAD_ID,
180 };
181 const decided = yield* decideOrchestrationCommand({ command, readModel });
182 const events = Array.isArray(decided) ? decided : [decided];
183 for (const event of events) {
184 readModel = yield* projectEvent(readModel, {
185 ...event,
186 sequence: readModel.snapshotSequence + 1,
187 });
188 }
189 expect(readModel.threads[0]?.activeOrderKey, command.type).toBe(expectedKey);
190 }
191 expect(readModel.threads[0]).toMatchObject({
192 title: "Renamed",
193 settledOverride: "active",
194 settledAt: null,
195 snoozedUntil: null,
196 pinnedAt: null,
197 });
198 }),
199 );
200});