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