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 subscribeServerConfig: (input) => client.subscribeServerConfig(input),
61 ready: Effect.void,
62 probe: Effect.void,
63 closed: Effect.never,
64 };
65 return EnvironmentSupervisor.EnvironmentSupervisor.of({
66 target: TARGET,
67 state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
68 session: yield* SubscriptionRef.make(Option.some(session)),
69 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
70 connect: Effect.void,
71 disconnect: Effect.void,
72 retryNow: Effect.void,
73 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
74});
75
76describe("environment commands", () => {
77 it.effect("adds generated command metadata", () =>
78 Effect.gen(function* () {
79 const dispatched: ClientOrchestrationCommand[] = [];
80 const supervisor = yield* makeSupervisor(dispatched);
81
82 const result = yield* createProject({
83 projectId: ProjectId.make("project-1"),
84 title: "Project",
85 workspaceRoot: "/workspace/project",
86 createdAt: "2026-06-06T00:00:00.000Z",
87 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
88
89 expect(result).toEqual({ sequence: 1 });
90 expect(dispatched).toEqual([
91 {
92 type: "project.create",
93 commandId: "00000000-0000-4000-8000-000000000000",
94 projectId: "project-1",
95 title: "Project",
96 workspaceRoot: "/workspace/project",
97 createdAt: "2026-06-06T00:00:00.000Z",
98 },
99 ]);
100 }).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
101 );
102
103 it.effect("preserves caller metadata for idempotent queued commands", () =>
104 Effect.gen(function* () {
105 const dispatched: ClientOrchestrationCommand[] = [];
106 const supervisor = yield* makeSupervisor(dispatched);
107
108 yield* stopThreadSession({
109 commandId: CommandId.make("queued-command"),
110 threadId: ThreadId.make("thread-1"),
111 createdAt: "2026-06-06T00:01:00.000Z",
112 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
113
114 expect(dispatched).toEqual([
115 {
116 type: "thread.session.stop",
117 commandId: "queued-command",
118 threadId: "thread-1",
119 createdAt: "2026-06-06T00:01:00.000Z",
120 },
121 ]);
122 }).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
123 );
124
125 it.effect("does not add timestamps to commands without createdAt", () =>
126 Effect.gen(function* () {
127 const dispatched: ClientOrchestrationCommand[] = [];
128 const supervisor = yield* makeSupervisor(dispatched);
129
130 yield* archiveThread({
131 commandId: CommandId.make("archive-command"),
132 threadId: ThreadId.make("thread-1"),
133 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
134
135 expect(dispatched).toEqual([
136 {
137 type: "thread.archive",
138 commandId: "archive-command",
139 threadId: "thread-1",
140 },
141 ]);
142 }).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
143 );
144
145 it.effect("dispatches settle and unsettle commands without timestamps", () =>
146 Effect.gen(function* () {
147 const dispatched: ClientOrchestrationCommand[] = [];
148 const supervisor = yield* makeSupervisor(dispatched);
149
150 yield* settleThread({
151 commandId: CommandId.make("settle-command"),
152 threadId: ThreadId.make("thread-1"),
153 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
154 yield* unsettleThread({
155 commandId: CommandId.make("unsettle-command"),
156 threadId: ThreadId.make("thread-1"),
157 reason: "user",
158 }).pipe(Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor));
159
160 expect(dispatched).toEqual([
161 {
162 type: "thread.settle",
163 commandId: "settle-command",
164 threadId: "thread-1",
165 },
166 {
167 type: "thread.unsettle",
168 commandId: "unsettle-command",
169 threadId: "thread-1",
170 reason: "user",
171 },
172 ]);
173 }).pipe(Effect.provide(TEST_CRYPTO_LAYER)),
174 );
175});