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