Open History to see the last commit
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 reorderActiveThread,
28 settleThread,
29 stopThreadSession,
30 unsettleThread,
31} from "./commands.ts";
32
33const TEST_CRYPTO_LAYER = Layer.succeed(
34 Crypto.Crypto,
35 Crypto.make({
36 randomBytes: (size) => new Uint8Array(size),
37 digest: (_algorithm, data) => Effect.succeed(data),
38 }),
39);
40
41const TARGET = new PrimaryConnectionTarget({
42 environmentId: EnvironmentId.make("environment-1"),
43 label: "Test environment",
44 httpBaseUrl: "https://environment.example.test",
45 wsBaseUrl: "wss://environment.example.test",
46});
47
48const makeSupervisor = Effect.fn("TestEnvironmentCommands.makeSupervisor")(function* (
49 dispatched: ClientOrchestrationCommand[],
50) {
51 const client = {
52 [ORCHESTRATION_WS_METHODS.dispatchCommand]: (command: ClientOrchestrationCommand) =>
53 Effect.sync(() => {
54 dispatched.push(command);
55 return { sequence: dispatched.length };
56 }),
57 } as unknown as WsRpcProtocolClient;
58 const session: RpcSession.RpcSession = {
59 client,
60 initialConfig: Effect.never,
61 subscribeServerConfig: (input) => client.subscribeServerConfig(input),
62 ready: Effect.void,
63 probe: Effect.void,
64 closed: Effect.never,
65 };
66 return EnvironmentSupervisor.EnvironmentSupervisor.of({
67 target: TARGET,
68 state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
69 session: yield* SubscriptionRef.make(Option.some(session)),
70 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
71 connect: Effect.void,
72 disconnect: Effect.void,
73 retryNow: Effect.void,
74 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
75});
76
77describe("environment commands", () => {
78 it.effect("adds generated command metadata", () =>
79 Effect.gen(function* () {
80 const dispatched: ClientOrchestrationCommand[] = [];
81 const supervisor = yield* makeSupervisor(dispatched);
82
83 const result = yield* createProject({
84 projectId: ProjectId.make("project-1"),
85 title: "Project",
86 workspaceRoot: "/workspace/project",
87 createdAt: "2026-06-06T00:00:00.000Z",
88 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
89
90 expect(result).toEqual({ sequence: 1 });
91 expect(dispatched).toEqual([
92 {
93 type: "project.create",
94 commandId: "00000000-0000-4000-8000-000000000000",
95 projectId: "project-1",
96 title: "Project",
97 workspaceRoot: "/workspace/project",
98 createdAt: "2026-06-06T00:00:00.000Z",
99 },
100 ]);
101 }).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
102 );
103
104 it.effect("preserves caller metadata for idempotent queued commands", () =>
105 Effect.gen(function* () {
106 const dispatched: ClientOrchestrationCommand[] = [];
107 const supervisor = yield* makeSupervisor(dispatched);
108
109 yield* stopThreadSession({
110 commandId: CommandId.make("queued-command"),
111 threadId: ThreadId.make("thread-1"),
112 createdAt: "2026-06-06T00:01:00.000Z",
113 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
114
115 expect(dispatched).toEqual([
116 {
117 type: "thread.session.stop",
118 commandId: "queued-command",
119 threadId: "thread-1",
120 createdAt: "2026-06-06T00:01:00.000Z",
121 },
122 ]);
123 }).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
124 );
125
126 it.effect("does not add timestamps to commands without createdAt", () =>
127 Effect.gen(function* () {
128 const dispatched: ClientOrchestrationCommand[] = [];
129 const supervisor = yield* makeSupervisor(dispatched);
130
131 yield* archiveThread({
132 commandId: CommandId.make("archive-command"),
133 threadId: ThreadId.make("thread-1"),
134 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
135
136 expect(dispatched).toEqual([
137 {
138 type: "thread.archive",
139 commandId: "archive-command",
140 threadId: "thread-1",
141 },
142 ]);
143 }).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
144 );
145
146 it.effect("dispatches settle and unsettle commands without timestamps", () =>
147 Effect.gen(function* () {
148 const dispatched: ClientOrchestrationCommand[] = [];
149 const supervisor = yield* makeSupervisor(dispatched);
150
151 yield* settleThread({
152 commandId: CommandId.make("settle-command"),
153 threadId: ThreadId.make("thread-1"),
154 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
155 yield* unsettleThread({
156 commandId: CommandId.make("unsettle-command"),
157 threadId: ThreadId.make("thread-1"),
158 reason: "user",
159 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
160
161 expect(dispatched).toEqual([
162 {
163 type: "thread.settle",
164 commandId: "settle-command",
165 threadId: "thread-1",
166 },
167 {
168 type: "thread.unsettle",
169 commandId: "unsettle-command",
170 threadId: "thread-1",
171 reason: "user",
172 },
173 ]);
174 }).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
175 );
176
177 it.effect("sends an active order key without changing activity timestamps", () =>
178 Effect.gen(function* () {
179 const dispatched: ClientOrchestrationCommand[] = [];
180 const supervisor = yield* makeSupervisor(dispatched);
181 yield* reorderActiveThread({
182 commandId: CommandId.make("reorder-command"),
183 threadId: ThreadId.make("thread-1"),
184 orderKey: "mf",
185 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
186 expect(dispatched).toEqual([
187 {
188 type: "thread.active.reorder",
189 commandId: "reorder-command",
190 threadId: "thread-1",
191 orderKey: "mf",
192 },
193 ]);
194 }).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
195 );
196});