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

shell-sync.test.ts

119 lines4.1 KB

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