Open History to see the last commit
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 enabled: true,
24 };
25}
26
27function shellState(input: {
28 readonly status: EnvironmentShellState["status"];
29 readonly updatedAt?: string;
30 readonly error?: string;
31 readonly snapshotSequence?: number;
32}): EnvironmentShellState {
33 return {
34 snapshot:
35 input.updatedAt === undefined
36 ? Option.none()
37 : Option.some({
38 snapshotSequence: input.snapshotSequence ?? 1,
39 updatedAt: input.updatedAt,
40 projects: [],
41 threads: [],
42 }),
43 status: input.status,
44 error: input.error === undefined ? Option.none() : Option.some(input.error),
45 };
46}
47
48function makeHarness() {
49 const shellStateAtoms = Atom.family((environmentId: EnvironmentId) =>
50 Atom.make<EnvironmentShellState>(
51 environmentId === ENVIRONMENT_ID
52 ? shellState({
53 status: "cached",
54 updatedAt: "2026-06-01T00:00:00.000Z",
55 })
56 : shellState({
57 status: "synchronizing",
58 updatedAt: "2026-06-02T00:00:00.000Z",
59 error: "Retrying.",
60 }),
61 ),
62 );
63 const configAtoms = Atom.family((_environmentId: EnvironmentId) =>
64 Atom.make<ServerConfig | null>(null),
65 );
66 const catalogValueAtom = Atom.make({
67 isReady: true,
68 entries: new Map([
69 [ENVIRONMENT_ID, environmentEntry(ENVIRONMENT_ID, "Environment")],
70 [OTHER_ENVIRONMENT_ID, environmentEntry(OTHER_ENVIRONMENT_ID, "Other environment")],
71 ]),
72 });
73 const summaryAtom = createEnvironmentShellSummaryAtom({
74 catalogValueAtom,
75 shellStateValueAtom: shellStateAtoms,
76 });
77 const serverConfigsAtom = createEnvironmentServerConfigsAtom({
78 catalogValueAtom,
79 serverConfigValueAtom: configAtoms,
80 });
81
82 return {
83 registry: AtomRegistry.make(),
84 shellStateAtom: shellStateAtoms,
85 configAtom: configAtoms,
86 summaryAtom,
87 serverConfigsAtom,
88 };
89}
90
91describe("environment shell projections", () => {
92 it("summarizes shell state and preserves identity when only irrelevant snapshot data changes", () => {
93 const harness = makeHarness();
94 const summary = harness.registry.get(harness.summaryAtom);
95
96 expect(summary).toEqual({
97 hasSnapshot: true,
98 hasSynchronizingShell: true,
99 hasCachedShell: true,
100 hasLiveShell: false,
101 firstError: "Retrying.",
102 latestSnapshotUpdatedAt: "2026-06-02T00:00:00.000Z",
103 });
104
105 harness.registry.set(
106 harness.shellStateAtom(ENVIRONMENT_ID),
107 shellState({
108 status: "cached",
109 updatedAt: "2026-06-01T00:00:00.000Z",
110 snapshotSequence: 2,
111 }),
112 );
113
114 expect(harness.registry.get(harness.summaryAtom)).toBe(summary);
115 });
116
117 it("preserves server-config map identity until a config reference changes", () => {
118 const harness = makeHarness();
119 const empty = harness.registry.get(harness.serverConfigsAtom);
120 const config = { cwd: "/repo" } as ServerConfig;
121
122 harness.registry.set(harness.configAtom(ENVIRONMENT_ID), config);
123 const withConfig = harness.registry.get(harness.serverConfigsAtom);
124
125 expect(withConfig).not.toBe(empty);
126 expect(withConfig.get(ENVIRONMENT_ID)).toBe(config);
127
128 harness.registry.set(harness.configAtom(ENVIRONMENT_ID), config);
129 expect(harness.registry.get(harness.serverConfigsAtom)).toBe(withConfig);
130 });
131});