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

shell-sync.test.ts

120 lines4.0 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 } 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 LIVE_SHELL_SNAPSHOT: OrchestrationShellSnapshot = {
33 snapshotSequence: 1,
34 projects: [],
35 threads: [],
36 updatedAt: "2026-06-06T00:00:00.000Z",
37};
38​
39function session(client: WsRpcProtocolClient): RpcSession.RpcSession {
40 return {
41 client,
42 initialConfig: Effect.never,
43 ready: Effect.void,
44 probe: Effect.void,
45 closed: Effect.never,
46 };
48​
49describe("environment shell synchronization", () => {
50 it.effect("publishes live state before persistence and preserves it when ready", () =>
51 Effect.gen(function* () {
52 const events = yield* Queue.unbounded<OrchestrationShellStreamItem>();
53 const client = {
54 [ORCHESTRATION_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events),
55 } as unknown as WsRpcProtocolClient;
56 const supervisorState = yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE);
57 const activeSession = yield* SubscriptionRef.make<Option.Option<RpcSession.RpcSession>>(
58 Option.some(session(client)),
59 );
60 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
61 target: TARGET,
62 state: supervisorState,
63 session: activeSession,
64 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
65 connect: Effect.void,
66 disconnect: Effect.void,
67 retryNow: Effect.void,
68 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
69 const cache = Persistence.EnvironmentCacheStore.of({
70 loadShell: () => Effect.succeed(Option.none()),
71 saveShell: () => Effect.never,
72 loadThread: () => Effect.succeed(Option.none()),
73 saveThread: () => Effect.void,
74 removeThread: () => Effect.void,
75 clear: () => Effect.void,
76 });
77 const shellState = yield* makeEnvironmentShellState().pipe(
78 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
79 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
80 );
81​
82 yield* SubscriptionRef.set(supervisorState, {
83 desired: true,
84 network: "online",
85 phase: "connecting",
86 stage: "synchronizing",
87 attempt: 1,
88 generation: 0,
89 lastFailure: null,
90 retryAt: null,
91 });
92 yield* Queue.offer(events, {
93 kind: "snapshot",
94 snapshot: LIVE_SHELL_SNAPSHOT,
95 });
96 yield* SubscriptionRef.changes(shellState).pipe(
97 Stream.filter((state) => state.status === "live"),
98 Stream.runHead,
99 );
100​
101 yield* SubscriptionRef.set(supervisorState, {
102 desired: true,
103 network: "online",
104 phase: "connected",
105 stage: null,
106 attempt: 1,
107 generation: 1,
108 lastFailure: null,
109 retryAt: null,
110 });
111 for (let index = 0; index < 10; index += 1) {
112 yield* Effect.yieldNow;
113 }
114​
115 const state = yield* SubscriptionRef.get(shellState);
116 expect(state.status).toBe("live");
117 expect(Option.getOrThrow(state.snapshot)).toEqual(LIVE_SHELL_SNAPSHOT);
118 }),
119 );
120});