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

shell-sync.test.ts

204 lines7.6 KB

1import {
2 EnvironmentId,
3 ORCHESTRATION_WS_METHODS,
4 type OrchestrationShellSnapshot,
5 type OrchestrationShellStreamItem,
6} from "@t3tools/contracts";
7import { describe, expect, it } from "@effect/vitest";
8import * as Effect from "effect/Effect";
9import * as Option from "effect/Option";
10import * as Queue from "effect/Queue";
11import * as Stream from "effect/Stream";
12import * as SubscriptionRef from "effect/SubscriptionRef";
13​
14import {
15 AVAILABLE_CONNECTION_STATE,
16 PrimaryConnectionTarget,
17 type PreparedConnection,
18} from "../connection/model.ts";
19import * as EnvironmentSupervisor from "../connection/supervisor.ts";
20import * as Persistence from "../platform/persistence.ts";
21import * as RpcSession from "../rpc/session.ts";
22import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
23import { makeEnvironmentShellState, ShellSnapshotLoader } from "./shell.ts";
24​
25const TARGET = new PrimaryConnectionTarget({
26 environmentId: EnvironmentId.make("environment-1"),
27 label: "Test environment",
28 httpBaseUrl: "https://environment.example.test",
29 wsBaseUrl: "wss://environment.example.test",
30});
31​
32const PREPARED: PreparedConnection = {
33 environmentId: TARGET.environmentId,
34 label: TARGET.label,
35 httpBaseUrl: TARGET.httpBaseUrl,
36 socketUrl: TARGET.wsBaseUrl,
37 httpAuthorization: null,
38 target: TARGET,
39};
40​
41const LIVE_SHELL_SNAPSHOT: OrchestrationShellSnapshot = {
42 snapshotSequence: 1,
43 projects: [],
44 threads: [],
45 updatedAt: "2026-06-06T00:00:00.000Z",
46};
47​
48function session(client: WsRpcProtocolClient): RpcSession.RpcSession {
49 return {
50 client,
51 initialConfig: Effect.never,
52 ready: Effect.void,
53 probe: Effect.void,
54 closed: Effect.never,
55 };
57​
58describe("environment shell synchronization", () => {
59 it.effect("publishes live state before persistence and preserves it when ready", () =>
60 Effect.gen(function* () {
61 const events = yield* Queue.unbounded<OrchestrationShellStreamItem>();
62 const client = {
63 [ORCHESTRATION_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events),
64 } as unknown as WsRpcProtocolClient;
65 const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE);
66 const activeSession = yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>(
67 Option.some(session(client)),
68 );
69 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
70 target: TARGET,
71 state: supervisorState,
72 session: activeSession,
73 prepared: yield* SubscriptionRef.make(Option.some(PREPARED)),
74 connect: Effect.void,
75 disconnect: Effect.void,
76 retryNow: Effect.void,
77 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
78 const cache = Persistence.EnvironmentCacheStore.of({
79 loadShell: () => Effect.succeed(Option.none()),
80 saveShell: () => Effect.never,
81 loadThread: () => Effect.succeed(Option.none()),
82 saveThread: () => Effect.void,
83 removeThread: () => Effect.void,
84 loadServerConfig: () => Effect.succeed(Option.none()),
85 saveServerConfig: () => Effect.void,
86 loadVcsRefs: () => Effect.succeed(Option.none()),
87 saveVcsRefs: () => Effect.void,
88 clear: () => Effect.void,
89 });
90 // Cold cache with no HTTP snapshot available → falls back to the
91 // socket-embedded snapshot.
92 const snapshotLoader = ShellSnapshotLoader.of({
93 load: () => Effect.succeed(Option.none()),
94 });
95 const shellState = yield* makeEnvironmentShellState().pipe(
96 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
97 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
98 Effect.provideService(ShellSnapshotLoader, snapshotLoader),
99 );
100​
101 yield* SubscriptionRef.set(supervisorState, {
102 desired: true,
103 network: "online",
104 phase: "connecting",
105 stage: "synchronizing",
106 attempt: 1,
107 generation: 0,
108 lastFailure: null,
109 retryAt: null,
110 });
111 yield* Queue.offer(events, {
112 kind: "snapshot",
113 snapshot: LIVE_SHELL_SNAPSHOT,
114 });
115 yield* SubscriptionRef.changes(shellState).pipe(
116 Stream.filter((state) => state.status === "live"),
117 Stream.runHead,
118 );
119​
120 yield* SubscriptionRef.set(supervisorState, {
121 desired: true,
122 network: "online",
123 phase: "connected",
124 stage: null,
125 attempt: 1,
126 generation: 1,
127 lastFailure: null,
128 retryAt: null,
129 });
130 for (let index = 0; index < 10; index += 1) {
131 yield* Effect.yieldNow;
132 }
133​
134 const state = yield* SubscriptionRef.get(shellState);
135 expect(state.status).toBe("live");
136 expect(Option.getOrThrow(state.snapshot)).toEqual(LIVE_SHELL_SNAPSHOT);
137 }),
138 );
139​
140 it.effect("resumes a warm shell cache via afterSequence without an HTTP fetch", () =>
141 Effect.gen(function* () {
142 const cachedSnapshot: OrchestrationShellSnapshot = {
143 snapshotSequence: 5,
144 projects: [],
145 threads: [],
146 updatedAt: "2026-06-06T00:00:00.000Z",
147 };
148 const events = yield* Queue.unbounded<OrchestrationShellStreamItem>();
149 const capturedAfterSequence = yield* SubscriptionRef.make<number | undefined>(undefined);
150 const loaderCalls = yield* SubscriptionRef.make(0);
151 const client = {
152 [ORCHESTRATION_WS_METHODS.subscribeShell]: (input: { readonly afterSequence?: number }) =>
153 Stream.unwrap(
154 SubscriptionRef.set(capturedAfterSequence, input.afterSequence).pipe(
155 Effect.as(Stream.fromQueue(events)),
156 ),
157 ),
158 } as unknown as WsRpcProtocolClient;
159 const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE);
160 const activeSession = yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>(
161 Option.some(session(client)),
162 );
163 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
164 target: TARGET,
165 state: supervisorState,
166 session: activeSession,
167 prepared: yield* SubscriptionRef.make(Option.some(PREPARED)),
168 connect: Effect.void,
169 disconnect: Effect.void,
170 retryNow: Effect.void,
171 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
172 const cache = Persistence.EnvironmentCacheStore.of({
173 loadShell: () => Effect.succeed(Option.some(cachedSnapshot)),
174 saveShell: () => Effect.void,
175 loadThread: () => Effect.succeed(Option.none()),
176 saveThread: () => Effect.void,
177 removeThread: () => Effect.void,
178 loadServerConfig: () => Effect.succeed(Option.none()),
179 saveServerConfig: () => Effect.void,
180 loadVcsRefs: () => Effect.succeed(Option.none()),
181 saveVcsRefs: () => Effect.void,
182 clear: () => Effect.void,
183 });
184 const snapshotLoader = ShellSnapshotLoader.of({
185 load: () =>
186 SubscriptionRef.update(loaderCalls, (count) => count + 1).pipe(Effect.as(Option.none())),
187 });
188 yield* makeEnvironmentShellState().pipe(
189 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
190 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
191 Effect.provideService(ShellSnapshotLoader, snapshotLoader),
192 );
193​
194 // Wait until the subscription is established from the warm cache.
195 yield* SubscriptionRef.changes(capturedAfterSequence).pipe(
196 Stream.filter((value) => value !== undefined),
197 Stream.runHead,
198 );
199​
200 expect(yield* SubscriptionRef.get(capturedAfterSequence)).toBe(5);
201 expect(yield* SubscriptionRef.get(loaderCalls)).toBe(0);
202 }),
203 );
204});