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