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

commands.test.ts

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