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

shell.test.ts

130 lines4.1 KB

1import type { ServerConfig } from "@t3tools/contracts";
2import { EnvironmentId } from "@t3tools/contracts";
3import { describe, expect, it } from "@effect/vitest";
4import * as Option from "effect/Option";
5import { Atom, AtomRegistry } from "effect/unstable/reactivity";
6​
7import { PrimaryConnectionTarget } from "../connection/model.ts";
8import type { EnvironmentShellState } from "./shell.ts";
9import { createEnvironmentServerConfigsAtom, createEnvironmentShellSummaryAtom } from "./shell.ts";
10​
11const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
12const OTHER_ENVIRONMENT_ID = EnvironmentId.make("environment-2");
13​
14function environmentEntry(environmentId: EnvironmentId, label: string) {
15 return {
16 target: new PrimaryConnectionTarget({
17 environmentId,
18 label,
19 httpBaseUrl: `https://${environmentId}.example.test`,
20 wsBaseUrl: `wss://${environmentId}.example.test`,
21 }),
22 profile: Option.none(),
23 };
25​
26function shellState(input: {
27 readonly status: EnvironmentShellState["status"];
28 readonly updatedAt?: string;
29 readonly error?: string;
30 readonly snapshotSequence?: number;
31}): EnvironmentShellState {
32 return {
33 snapshot:
34 input.updatedAt === undefined
35 ? Option.none()
36 : Option.some({
37 snapshotSequence: input.snapshotSequence ?? 1,
38 updatedAt: input.updatedAt,
39 projects: [],
40 threads: [],
41 }),
42 status: input.status,
43 error: input.error === undefined ? Option.none() : Option.some(input.error),
44 };
46​
47function makeHarness() {
48 const shellStateAtoms = Atom.family((environmentId: EnvironmentId) =>
49 Atom.make<EnvironmentShellState>(
50 environmentId === ENVIRONMENT_ID
51 ? shellState({
52 status: "cached",
53 updatedAt: "2026-06-01T00:00:00.000Z",
54 })
55 : shellState({
56 status: "synchronizing",
57 updatedAt: "2026-06-02T00:00:00.000Z",
58 error: "Retrying.",
59 }),
60 ),
61 );
62 const configAtoms = Atom.family((_environmentId: EnvironmentId) =>
63 Atom.make<ServerConfig | null>(null),
64 );
65 const catalogValueAtom = Atom.make({
66 isReady: true,
67 entries: new Map([
68 [ENVIRONMENT_ID, environmentEntry(ENVIRONMENT_ID, "Environment")],
69 [OTHER_ENVIRONMENT_ID, environmentEntry(OTHER_ENVIRONMENT_ID, "Other environment")],
70 ]),
71 });
72 const summaryAtom = createEnvironmentShellSummaryAtom({
73 catalogValueAtom,
74 shellStateValueAtom: shellStateAtoms,
75 });
76 const serverConfigsAtom = createEnvironmentServerConfigsAtom({
77 catalogValueAtom,
78 serverConfigValueAtom: configAtoms,
79 });
80​
81 return {
82 registry: AtomRegistry.make(),
83 shellStateAtom: shellStateAtoms,
84 configAtom: configAtoms,
85 summaryAtom,
86 serverConfigsAtom,
87 };
89​
90describe("environment shell projections", () => {
91 it("summarizes shell state and preserves identity when only irrelevant snapshot data changes", () => {
92 const harness = makeHarness();
93 const summary = harness.registry.get(harness.summaryAtom);
94​
95 expect(summary).toEqual({
96 hasSnapshot: true,
97 hasSynchronizingShell: true,
98 hasCachedShell: true,
99 hasLiveShell: false,
100 firstError: "Retrying.",
101 latestSnapshotUpdatedAt: "2026-06-02T00:00:00.000Z",
102 });
103​
104 harness.registry.set(
105 harness.shellStateAtom(ENVIRONMENT_ID),
106 shellState({
107 status: "cached",
108 updatedAt: "2026-06-01T00:00:00.000Z",
109 snapshotSequence: 2,
110 }),
111 );
112​
113 expect(harness.registry.get(harness.summaryAtom)).toBe(summary);
114 });
115​
116 it("preserves server-config map identity until a config reference changes", () => {
117 const harness = makeHarness();
118 const empty = harness.registry.get(harness.serverConfigsAtom);
119 const config = { cwd: "/repo" } as ServerConfig;
120​
121 harness.registry.set(harness.configAtom(ENVIRONMENT_ID), config);
122 const withConfig = harness.registry.get(harness.serverConfigsAtom);
123​
124 expect(withConfig).not.toBe(empty);
125 expect(withConfig.get(ENVIRONMENT_ID)).toBe(config);
126​
127 harness.registry.set(harness.configAtom(ENVIRONMENT_ID), config);
128 expect(harness.registry.get(harness.serverConfigsAtom)).toBe(withConfig);
129 });
130});