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

presentation.test.ts

238 lines9.8 KB

1import { describe, expect, it } from "@effect/vitest";
2import { EnvironmentId, type ServerConfig } from "@t3tools/contracts";
3import * as Option from "effect/Option";
4import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
5​
6import { AVAILABLE_CONNECTION_STATE, PrimaryConnectionTarget } from "../connection/model.ts";
7import {
8 createEnvironmentPresentationAtoms,
9 createEnvironmentSummaryAtoms,
10} from "./presentation.ts";
11import type { EnvironmentCatalogState } from "./connections.ts";
12​
13const FIRST = EnvironmentId.make("first");
14const SECOND = EnvironmentId.make("second");
15function entry(environmentId: EnvironmentId, label = environmentId as string) {
16 return {
17 target: new PrimaryConnectionTarget({
18 environmentId,
19 label,
20 httpBaseUrl: "https://example.test",
21 wsBaseUrl: "wss://example.test",
22 }),
23 enabled: true,
24 profile: Option.none(),
25 };
27function config(pullRequests = false, cwd = "/workspace"): ServerConfig {
28 return {
29 cwd,
30 environment: { capabilities: { pullRequests }, platform: { machine: "desktop" } },
31 } as ServerConfig;
33function harness() {
34 const catalog = Atom.make<EnvironmentCatalogState>({
35 isReady: true,
36 entries: new Map([
37 [FIRST, entry(FIRST)],
38 [SECOND, entry(SECOND)],
39 ]),
40 });
41 const configs = Atom.family((_id: EnvironmentId) => Atom.make<ServerConfig | null>(config()));
42 const state = Atom.make(AsyncResult.success(AVAILABLE_CONNECTION_STATE));
43 const full = createEnvironmentPresentationAtoms({
44 catalogValueAtom: catalog,
45 stateAtom: () => state,
46 serverConfigValueAtom: configs,
47 });
48 const summaries = createEnvironmentSummaryAtoms({
49 catalogValueAtom: catalog,
50 presentationAtom: full.presentationAtom,
51 });
52 return { catalog, configs, state, full, ...summaries, registry: AtomRegistry.make() };
54​
55describe("environment summary subscriptions", () => {
56 it("publishes full config updates without notifying membership, labels, connections or capability consumers", () => {
57 const h = harness();
58 h.registry.get(h.full.presentationsAtom);
59 h.registry.get(h.environmentIdsAtom);
60 h.registry.get(h.identitiesAtom);
61 h.registry.get(h.environmentsAtom);
62 h.registry.get(h.pullRequestsSupportedAtom);
63 h.registry.get(h.machineByIdAtom);
64 const counts = { full: 0, ids: 0, labels: 0, connections: 0, capability: 0, machines: 0 };
65 const stops = [
66 h.registry.subscribe(h.machineByIdAtom, () => counts.machines++),
67 h.registry.subscribe(h.full.presentationsAtom, () => counts.full++),
68 h.registry.subscribe(h.environmentIdsAtom, () => counts.ids++),
69 h.registry.subscribe(h.identitiesAtom, () => counts.labels++),
70 h.registry.subscribe(h.environmentsAtom, () => counts.connections++),
71 h.registry.subscribe(h.pullRequestsSupportedAtom, () => counts.capability++),
72 ];
73 const initialIds = h.registry.get(h.environmentIdsAtom);
74 const initialLabels = h.registry.get(h.identitiesAtom);
75 try {
76 for (let i = 0; i < 20; i++) {
77 const next = config(false, `/workspace-${i}`);
78 h.registry.set(h.configs(FIRST), next);
79 expect(h.registry.get(h.full.presentationsAtom).get(FIRST)?.serverConfig?.cwd).toBe(
80 next.cwd,
81 );
82 expect(h.registry.get(h.environmentIdsAtom)).toBe(initialIds);
83 expect(h.registry.get(h.identitiesAtom)).toBe(initialLabels);
84 h.registry.get(h.environmentsAtom);
85 expect(h.registry.get(h.machineByIdAtom).get(FIRST)).toBe("desktop");
86 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(false);
87 }
88 expect(counts).toEqual({
89 full: 20,
90 ids: 0,
91 labels: 0,
92 connections: 0,
93 capability: 0,
94 machines: 0,
95 });
96 h.registry.set(h.catalog, {
97 isReady: true,
98 entries: new Map([
99 [FIRST, entry(FIRST, "Renamed")],
100 [SECOND, entry(SECOND)],
101 ]),
102 });
103 expect(h.registry.get(h.identitiesAtom)[0]?.label).toBe("Renamed");
104 expect(h.registry.get(h.environmentIdsAtom)).toBe(initialIds);
105 expect(h.registry.get(h.environmentsAtom)[0]?.environmentLabel).toBe("Renamed");
106 expect(counts.labels).toBe(1);
107 expect(counts.ids).toBe(0);
108 } finally {
109 stops.forEach((stop) => stop());
110 h.registry.dispose();
111 }
112 });
113​
114 it("keeps presentations identical when only provider statuses change", () => {
115 const h = harness();
116 const pushes = { presentations: 0, providers: 0, configs: 0 };
117 const stops = [
118 h.registry.subscribe(h.full.presentationsAtom, () => pushes.presentations++),
119 h.registry.subscribe(h.full.providersByEnvironmentAtom, () => pushes.providers++),
120 h.registry.subscribe(h.full.serverConfigsAtom, () => pushes.configs++),
121 ];
122 try {
123 const base = config();
124 h.registry.set(h.configs(FIRST), { ...base, providers: [] } as ServerConfig);
125 const presentations = h.registry.get(h.full.presentationsAtom);
126 const configs = h.registry.get(h.full.serverConfigsAtom);
127 const before = { ...pushes };
128 const providers = [{ instanceId: "codex" }] as unknown as ServerConfig["providers"];
129 h.registry.set(h.configs(FIRST), { ...base, providers } as ServerConfig);
130​
131 expect(h.registry.get(h.full.presentationsAtom)).toBe(presentations);
132 expect(h.registry.get(h.full.providersByEnvironmentAtom).get(FIRST)).toBe(providers);
133 expect(
134 h.registry.get(h.full.presentationsWithProvidersAtom).get(FIRST)?.serverConfig,
135 ).toEqual({ ...base, providers });
136 expect(h.registry.get(h.full.serverConfigsAtom)).toBe(configs);
137 expect(pushes.presentations).toBe(before.presentations);
138 expect(pushes.configs).toBe(before.configs);
139 expect(pushes.providers).toBe(before.providers + 1);
140​
141 h.registry.set(h.configs(FIRST), { ...base, cwd: "/moved", providers } as ServerConfig);
142 expect(h.registry.get(h.full.presentationsAtom).get(FIRST)?.serverConfig?.cwd).toBe("/moved");
143 expect(h.registry.get(h.full.serverConfigsAtom).get(FIRST)?.cwd).toBe("/moved");
144 expect(h.registry.get(h.full.providersByEnvironmentAtom).get(FIRST)).toBe(providers);
145 } finally {
146 stops.forEach((stop) => stop());
147 h.registry.dispose();
148 }
149 });
150​
151 it("keeps connected search targets stable through config refreshes and updates them on disconnect", () => {
152 const h = harness();
153 let changes = 0;
154 const stop = h.registry.subscribe(h.connectedEnvironmentIdsAtom, () => changes++);
155 try {
156 expect(h.registry.get(h.connectedEnvironmentIdsAtom)).toEqual([]);
157 h.registry.set(
158 h.state,
159 AsyncResult.success({
160 ...AVAILABLE_CONNECTION_STATE,
161 phase: "connected",
162 generation: 1,
163 }),
164 );
165 const connected = h.registry.get(h.connectedEnvironmentIdsAtom);
166 expect(connected).toEqual([FIRST, SECOND]);
167 const connectionChanges = changes;
168 expect(connectionChanges).toBeGreaterThan(0);
169 for (let index = 0; index < 20; index++) {
170 h.registry.set(h.configs(FIRST), config(false, `/workspace-${index}`));
171 expect(h.registry.get(h.connectedEnvironmentIdsAtom)).toBe(connected);
172 }
173 expect(changes).toBe(connectionChanges);
174 h.registry.set(h.state, AsyncResult.success(AVAILABLE_CONNECTION_STATE));
175 expect(h.registry.get(h.connectedEnvironmentIdsAtom)).toEqual([]);
176 expect(changes).toBeGreaterThan(connectionChanges);
177 } finally {
178 stop();
179 h.registry.dispose();
180 }
181 });
182​
183 it("updates machine icons and preserves cached icons for disabled environments", () => {
184 const h = harness();
185 try {
186 expect(h.registry.get(h.machineByIdAtom).get(FIRST)).toBe("desktop");
187 h.registry.set(h.configs(FIRST), {
188 ...config(),
189 settings: { environmentIcon: "laptop" },
190 } as ServerConfig);
191 expect(h.registry.get(h.machineByIdAtom).get(FIRST)).toBe("laptop");
192 h.registry.set(h.catalog, {
193 isReady: true,
194 entries: new Map([[FIRST, { ...entry(FIRST), enabled: false }]]),
195 });
196 expect(h.registry.get(h.machineByIdAtom)).toEqual(new Map([[FIRST, "laptop"]]));
197 h.registry.set(h.configs(FIRST), null);
198 expect(h.registry.get(h.machineByIdAtom).get(FIRST)).toBe("server");
199 } finally {
200 h.registry.dispose();
201 }
202 });
203​
204 it("tracks capabilities across environments, config loss and removal", () => {
205 const h = harness();
206 const stop = h.registry.subscribe(h.pullRequestsSupportedAtom, () => {});
207 try {
208 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(false);
209 h.registry.set(h.configs(FIRST), config(true));
210 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(true);
211 h.registry.set(h.configs(SECOND), config(true));
212 h.registry.set(h.configs(FIRST), null);
213 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(true);
214 h.registry.set(h.catalog, { isReady: true, entries: new Map([[FIRST, entry(FIRST)]]) });
215 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(false);
216 expect(h.registry.get(h.environmentIdsAtom)).toEqual([FIRST]);
217 expect(h.registry.get(h.identitiesAtom)).toEqual([{ environmentId: FIRST, label: "first" }]);
218 h.registry.set(h.catalog, {
219 isReady: true,
220 entries: new Map([
221 [SECOND, entry(SECOND)],
222 [FIRST, entry(FIRST)],
223 ]),
224 });
225 expect(h.registry.get(h.environmentIdsAtom)).toEqual([SECOND, FIRST]);
226 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(true);
227 h.registry.set(h.configs(SECOND), config(false));
228 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(false);
229 h.registry.set(h.catalog, { isReady: true, entries: new Map() });
230 expect(h.registry.get(h.environmentIdsAtom)).toEqual([]);
231 expect(h.registry.get(h.identitiesAtom)).toEqual([]);
232 expect(h.registry.get(h.environmentsAtom)).toEqual([]);
233 } finally {
234 stop();
235 h.registry.dispose();
236 }
237 });
238});