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/reactivity";
7
8import { PrimaryConnectionTarget } from "../connection/model.ts";
9import type { EnvironmentShellState } from "./shell.ts";
10import {
11 createEnvironmentServerConfigsAtom,
12 createEnvironmentShellProgressAtoms,
13 createEnvironmentShellSummaryAtom,
14} from "./shell.ts";
15import { v2ThreadShell } from "./orchestrationV2TestFixtures.ts";
16
17const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
18const OTHER_ENVIRONMENT_ID = EnvironmentId.make("environment-2");
19
20function environmentEntry(environmentId: EnvironmentId, label: string) {
21 return {
22 target: new PrimaryConnectionTarget({
23 environmentId,
24 label,
25 httpBaseUrl: `https://${environmentId}.example.test`,
26 wsBaseUrl: `wss://${environmentId}.example.test`,
27 }),
28 profile: Option.none(),
29 enabled: true,
30 };
31}
32
33function shellState(input: {
34 readonly status: EnvironmentShellState["status"];
35 readonly updatedAt?: string;
36 readonly error?: string;
37 readonly snapshotSequence?: number;
38}): EnvironmentShellState {
39 return {
40 snapshot:
41 input.updatedAt === undefined
42 ? Option.none()
43 : Option.some({
44 schemaVersion: 1,
45 snapshotSequence: input.snapshotSequence ?? 1,
46 projects: [],
47 threads: [
48 {
49 ...v2ThreadShell,
50 updatedAt: DateTime.makeUnsafe(input.updatedAt),
51 },
52 ],
53 archivedThreads: [],
54 }),
55 status: input.status,
56 error: input.error === undefined ? Option.none() : Option.some(input.error),
57 };
58}
59
60function makeHarness() {
61 const shellStateAtoms = Atom.family((environmentId: EnvironmentId) =>
62 Atom.make<EnvironmentShellState>(
63 environmentId === ENVIRONMENT_ID
64 ? shellState({
65 status: "cached",
66 updatedAt: "2026-06-01T00:00:00.000Z",
67 })
68 : shellState({
69 status: "synchronizing",
70 updatedAt: "2026-06-02T00:00:00.000Z",
71 error: "Retrying.",
72 }),
73 ),
74 );
75 const configAtoms = Atom.family((_environmentId: EnvironmentId) =>
76 Atom.make<ServerConfig | null>(null),
77 );
78 const catalogValueAtom = Atom.make({
79 isReady: true,
80 entries: new Map([
81 [ENVIRONMENT_ID, environmentEntry(ENVIRONMENT_ID, "Environment")],
82 [OTHER_ENVIRONMENT_ID, environmentEntry(OTHER_ENVIRONMENT_ID, "Other environment")],
83 ]),
84 });
85 const summaryAtom = createEnvironmentShellSummaryAtom({
86 catalogValueAtom,
87 shellStateValueAtom: shellStateAtoms,
88 });
89 const serverConfigsAtom = createEnvironmentServerConfigsAtom({
90 catalogValueAtom,
91 serverConfigValueAtom: configAtoms,
92 });
93
94 return {
95 catalogValueAtom,
96 registry: AtomRegistry.make(),
97 shellStateAtom: shellStateAtoms,
98 configAtom: configAtoms,
99 summaryAtom,
100 serverConfigsAtom,
101 };
102}
103
104describe("environment shell projections", () => {
105 it("summarizes shell state and preserves identity when only irrelevant snapshot data changes", () => {
106 const harness = makeHarness();
107 const summary = harness.registry.get(harness.summaryAtom);
108
109 expect(summary).toEqual({
110 hasSnapshot: true,
111 hasSynchronizingShell: true,
112 hasCachedShell: true,
113 hasLiveShell: false,
114 firstError: "Retrying.",
115 });
116
117 harness.registry.set(
118 harness.shellStateAtom(ENVIRONMENT_ID),
119 shellState({
120 status: "cached",
121 updatedAt: "2026-06-01T00:00:00.000Z",
122 snapshotSequence: 2,
123 }),
124 );
125
126 expect(harness.registry.get(harness.summaryAtom)).toBe(summary);
127 });
128
129 it("does not notify summary subscribers when thread timestamps advance", () => {
130 const harness = makeHarness();
131 const initial = harness.registry.get(harness.summaryAtom);
132 let changes = 0;
133 const unsubscribe = harness.registry.subscribe(harness.summaryAtom, () => changes++);
134 try {
135 for (let index = 0; index < 20; index++) {
136 harness.registry.set(
137 harness.shellStateAtom(ENVIRONMENT_ID),
138 shellState({
139 status: "cached",
140 updatedAt: `2026-07-01T00:00:${String(index).padStart(2, "0")}.000Z`,
141 snapshotSequence: index + 2,
142 }),
143 );
144 expect(harness.registry.get(harness.summaryAtom)).toBe(initial);
145 }
146 expect(changes).toBe(0);
147 } finally {
148 unsubscribe();
149 harness.registry.dispose();
150 }
151 });
152
153 it("notifies for shell availability, status, errors, and enabled environment changes", () => {
154 const harness = makeHarness();
155 let changes = 0;
156 const unsubscribe = harness.registry.subscribe(harness.summaryAtom, () => changes++);
157 const setCatalog = (entries: ReturnType<typeof environmentEntry>[]) => {
158 harness.registry.set(harness.catalogValueAtom, {
159 isReady: true,
160 entries: new Map(entries.map((entry) => [entry.target.environmentId, entry])),
161 });
162 return harness.registry.get(harness.summaryAtom);
163 };
164 try {
165 expect(setCatalog([])).toEqual({
166 hasSnapshot: false,
167 hasSynchronizingShell: false,
168 hasCachedShell: false,
169 hasLiveShell: false,
170 firstError: null,
171 });
172 expect(setCatalog([environmentEntry(ENVIRONMENT_ID, "Environment")])).toMatchObject({
173 hasSnapshot: true,
174 hasCachedShell: true,
175 });
176 harness.registry.set(harness.shellStateAtom(ENVIRONMENT_ID), shellState({ status: "empty" }));
177 expect(harness.registry.get(harness.summaryAtom)).toMatchObject({
178 hasSnapshot: false,
179 hasCachedShell: false,
180 });
181 harness.registry.set(
182 harness.shellStateAtom(ENVIRONMENT_ID),
183 shellState({ status: "synchronizing", error: "Retrying." }),
184 );
185 expect(harness.registry.get(harness.summaryAtom)).toMatchObject({
186 hasSnapshot: false,
187 hasSynchronizingShell: true,
188 firstError: "Retrying.",
189 });
190 harness.registry.set(
191 harness.shellStateAtom(ENVIRONMENT_ID),
192 shellState({ status: "live", updatedAt: "2026-07-01T00:00:00.000Z" }),
193 );
194 expect(harness.registry.get(harness.summaryAtom)).toMatchObject({
195 hasSnapshot: true,
196 hasSynchronizingShell: false,
197 hasLiveShell: true,
198 firstError: null,
199 });
200 harness.registry.set(
201 harness.shellStateAtom(ENVIRONMENT_ID),
202 shellState({
203 status: "live",
204 updatedAt: "2026-07-01T00:00:00.000Z",
205 error: "Disconnected.",
206 }),
207 );
208 expect(harness.registry.get(harness.summaryAtom).firstError).toBe("Disconnected.");
209 harness.registry.set(
210 harness.shellStateAtom(ENVIRONMENT_ID),
211 shellState({ status: "live", updatedAt: "2026-07-01T00:00:00.000Z" }),
212 );
213 expect(harness.registry.get(harness.summaryAtom).firstError).toBeNull();
214 expect(
215 setCatalog([{ ...environmentEntry(ENVIRONMENT_ID, "Environment"), enabled: false }]),
216 ).toMatchObject({ hasSnapshot: false, hasLiveShell: false });
217 expect(changes).toBe(8);
218 } finally {
219 unsubscribe();
220 harness.registry.dispose();
221 }
222 });
223
224 it("preserves server-config map identity until a config reference changes", () => {
225 const harness = makeHarness();
226 const empty = harness.registry.get(harness.serverConfigsAtom);
227 const config = { cwd: "/repo" } as ServerConfig;
228
229 harness.registry.set(harness.configAtom(ENVIRONMENT_ID), config);
230 const withConfig = harness.registry.get(harness.serverConfigsAtom);
231
232 expect(withConfig).not.toBe(empty);
233 expect(withConfig.get(ENVIRONMENT_ID)).toBe(config);
234
235 harness.registry.set(harness.configAtom(ENVIRONMENT_ID), config);
236 expect(harness.registry.get(harness.serverConfigsAtom)).toBe(withConfig);
237 });
238
239 it("keeps load progress quiet while threads stream and reports real progress", () => {
240 const stateAtom = Atom.make(
241 shellState({ status: "cached", updatedAt: "2026-06-01T00:00:00.000Z" }),
242 );
243 const registry = AtomRegistry.make();
244 const { statusAtom, hasSnapshotAtom } = createEnvironmentShellProgressAtoms(() => stateAtom);
245 let changes = 0;
246 const unsubscribeStatus = registry.subscribe(statusAtom(ENVIRONMENT_ID), () => changes++);
247 const unsubscribeSnapshot = registry.subscribe(
248 hasSnapshotAtom(ENVIRONMENT_ID),
249 () => changes++,
250 );
251
252 registry.set(
253 stateAtom,
254 shellState({ status: "cached", updatedAt: "2026-06-01T00:00:01.000Z" }),
255 );
256 expect(changes).toBe(0);
257
258 registry.set(stateAtom, shellState({ status: "live", updatedAt: "2026-06-01T00:00:02.000Z" }));
259 expect(registry.get(statusAtom(ENVIRONMENT_ID))).toBe("live");
260 expect(changes).toBe(1);
261
262 registry.set(stateAtom, shellState({ status: "live" }));
263 expect(registry.get(hasSnapshotAtom(ENVIRONMENT_ID))).toBe(false);
264 expect(changes).toBe(2);
265
266 unsubscribeStatus();
267 unsubscribeSnapshot();
268 });
269});