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

commands.test.ts

218 lines7.4 KB

1import {
2 CommandId,
3 EnvironmentId,
4 ORCHESTRATION_WS_METHODS,
5 ProjectId,
6 ThreadId,
7 type ClientOrchestrationCommand,
8} from "@t3tools/contracts";
9import { describe, expect, it } from "@effect/vitest";
10import * as Crypto from "effect/Crypto";
11import * as Effect from "effect/Effect";
12import * as Layer from "effect/Layer";
13import * as Option from "effect/Option";
14import * as SubscriptionRef from "effect/SubscriptionRef";
15​
16import {
17 AVAILABLE_CONNECTION_STATE,
18 PrimaryConnectionTarget,
19 type PreparedConnection,
20} from "../connection/model.ts";
21import * as EnvironmentSupervisor from "../connection/supervisor.ts";
22import * as RpcSession from "../rpc/session.ts";
23import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
24import {
25 archiveThread,
26 createProject,
27 revertThreadCheckpoint,
28 reorderActiveThread,
29 settleThread,
30 stopThreadSession,
31 unsettleThread,
32} from "./commands.ts";
33​
34const TEST_CRYPTO_LAYER = Layer.succeed(
35 Crypto.Crypto,
36 Crypto.make({
37 randomBytes: (size) => new Uint8Array(size),
38 digest: (_algorithm, data) => Effect.succeed(data),
39 }),
40);
41​
42const TARGET = new PrimaryConnectionTarget({
43 environmentId: EnvironmentId.make("environment-1"),
44 label: "Test environment",
45 httpBaseUrl: "https://environment.example.test",
46 wsBaseUrl: "wss://environment.example.test",
47});
48​
49const makeSupervisor = Effect.fn("TestEnvironmentCommands.makeSupervisor")(function* (
50 dispatched: ClientOrchestrationCommand[],
51) {
52 const client = {
53 [ORCHESTRATION_WS_METHODS.dispatchCommand]: (command: ClientOrchestrationCommand) =>
54 Effect.sync(() => {
55 dispatched.push(command);
56 return { sequence: dispatched.length };
57 }),
58 } as unknown as WsRpcProtocolClient;
59 const session: RpcSession.RpcSession = {
60 client,
61 initialConfig: Effect.never,
62 subscribeServerConfig: (input) => client.subscribeServerConfig(input),
63 ready: Effect.void,
64 probe: Effect.void,
65 closed: Effect.never,
66 };
67 return EnvironmentSupervisor.EnvironmentSupervisor.of({
68 target: TARGET,
69 state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
70 session: yield* SubscriptionRef.make(Option.some(session)),
71 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
72 connect: Effect.void,
73 disconnect: Effect.void,
74 retryNow: Effect.void,
75 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
76});
77​
78describe("environment commands", () => {
79 it.effect("adds generated command metadata", () =>
80 Effect.gen(function* () {
81 const dispatched: ClientOrchestrationCommand[] = [];
82 const supervisor = yield* makeSupervisor(dispatched);
83​
84 const result = yield* createProject({
85 projectId: ProjectId.make("project-1"),
86 title: "Project",
87 workspaceRoot: "/workspace/project",
88 createdAt: "2026-06-06T00:00:00.000Z",
89 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
90​
91 expect(result).toEqual({ sequence: 1 });
92 expect(dispatched).toEqual([
93 {
94 type: "project.create",
95 commandId: "00000000-0000-4000-8000-000000000000",
96 projectId: "project-1",
97 title: "Project",
98 workspaceRoot: "/workspace/project",
99 createdAt: "2026-06-06T00:00:00.000Z",
100 },
101 ]);
102 }).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
103 );
104​
105 it.effect("uses a distinct command when keeping workspace changes", () =>
106 Effect.gen(function* () {
107 const dispatched: ClientOrchestrationCommand[] = [];
108 const supervisor = yield* makeSupervisor(dispatched);
109 for (const restoreFiles of [undefined, true, false]) {
110 yield* revertThreadCheckpoint({
111 commandId: CommandId.make("rewind-command"),
112 threadId: ThreadId.make("thread-1"),
113 turnCount: 0,
114 ...(restoreFiles !== undefined ? { restoreFiles } : {}),
115 createdAt: "2026-06-06T00:01:00.000Z",
116 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
117 }
118 expect(dispatched.map((command) => command.type)).toEqual([
119 "thread.checkpoint.revert",
120 "thread.checkpoint.revert",
121 "thread.conversation.revert",
122 ]);
123 }).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
124 );
125​
126 it.effect("preserves caller metadata for idempotent queued commands", () =>
127 Effect.gen(function* () {
128 const dispatched: ClientOrchestrationCommand[] = [];
129 const supervisor = yield* makeSupervisor(dispatched);
130​
131 yield* stopThreadSession({
132 commandId: CommandId.make("queued-command"),
133 threadId: ThreadId.make("thread-1"),
134 createdAt: "2026-06-06T00:01:00.000Z",
135 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
136​
137 expect(dispatched).toEqual([
138 {
139 type: "thread.session.stop",
140 commandId: "queued-command",
141 threadId: "thread-1",
142 createdAt: "2026-06-06T00:01:00.000Z",
143 },
144 ]);
145 }).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
146 );
147​
148 it.effect("does not add timestamps to commands without createdAt", () =>
149 Effect.gen(function* () {
150 const dispatched: ClientOrchestrationCommand[] = [];
151 const supervisor = yield* makeSupervisor(dispatched);
152​
153 yield* archiveThread({
154 commandId: CommandId.make("archive-command"),
155 threadId: ThreadId.make("thread-1"),
156 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
157​
158 expect(dispatched).toEqual([
159 {
160 type: "thread.archive",
161 commandId: "archive-command",
162 threadId: "thread-1",
163 },
164 ]);
165 }).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
166 );
167​
168 it.effect("dispatches settle and unsettle commands without timestamps", () =>
169 Effect.gen(function* () {
170 const dispatched: ClientOrchestrationCommand[] = [];
171 const supervisor = yield* makeSupervisor(dispatched);
172​
173 yield* settleThread({
174 commandId: CommandId.make("settle-command"),
175 threadId: ThreadId.make("thread-1"),
176 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
177 yield* unsettleThread({
178 commandId: CommandId.make("unsettle-command"),
179 threadId: ThreadId.make("thread-1"),
180 reason: "user",
181 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
182​
183 expect(dispatched).toEqual([
184 {
185 type: "thread.settle",
186 commandId: "settle-command",
187 threadId: "thread-1",
188 },
189 {
190 type: "thread.unsettle",
191 commandId: "unsettle-command",
192 threadId: "thread-1",
193 reason: "user",
194 },
195 ]);
196 }).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
197 );
198​
199 it.effect("sends an active order key without changing activity timestamps", () =>
200 Effect.gen(function* () {
201 const dispatched: ClientOrchestrationCommand[] = [];
202 const supervisor = yield* makeSupervisor(dispatched);
203 yield* reorderActiveThread({
204 commandId: CommandId.make("reorder-command"),
205 threadId: ThreadId.make("thread-1"),
206 orderKey: "mf",
207 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
208 expect(dispatched).toEqual([
209 {
210 type: "thread.active.reorder",
211 commandId: "reorder-command",
212 threadId: "thread-1",
213 orderKey: "mf",
214 },
215 ]);
216 }).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
217 );
218});