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

presentation.test.ts

168 lines6.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/unstable/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).toBe(next);
80 expect(h.registry.get(h.environmentIdsAtom)).toBe(initialIds);
81 expect(h.registry.get(h.identitiesAtom)).toBe(initialLabels);
82 h.registry.get(h.environmentsAtom);
83 expect(h.registry.get(h.machineByIdAtom).get(FIRST)).toBe("desktop");
84 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(false);
85 }
86 expect(counts).toEqual({
87 full: 20,
88 ids: 0,
89 labels: 0,
90 connections: 0,
91 capability: 0,
92 machines: 0,
93 });
94 h.registry.set(h.catalog, {
95 isReady: true,
96 entries: new Map([
97 [FIRST, entry(FIRST, "Renamed")],
98 [SECOND, entry(SECOND)],
99 ]),
100 });
101 expect(h.registry.get(h.identitiesAtom)[0]?.label).toBe("Renamed");
102 expect(h.registry.get(h.environmentIdsAtom)).toBe(initialIds);
103 expect(h.registry.get(h.environmentsAtom)[0]?.environmentLabel).toBe("Renamed");
104 expect(counts.labels).toBe(1);
105 expect(counts.ids).toBe(0);
106 } finally {
107 stops.forEach((stop) => stop());
108 h.registry.dispose();
109 }
110 });
111​
112 it("updates machine icons and preserves cached icons for disabled environments", () => {
113 const h = harness();
114 try {
115 expect(h.registry.get(h.machineByIdAtom).get(FIRST)).toBe("desktop");
116 h.registry.set(h.configs(FIRST), {
117 ...config(),
118 settings: { environmentIcon: "laptop" },
119 } as ServerConfig);
120 expect(h.registry.get(h.machineByIdAtom).get(FIRST)).toBe("laptop");
121 h.registry.set(h.catalog, {
122 isReady: true,
123 entries: new Map([[FIRST, { ...entry(FIRST), enabled: false }]]),
124 });
125 expect(h.registry.get(h.machineByIdAtom)).toEqual(new Map([[FIRST, "laptop"]]));
126 h.registry.set(h.configs(FIRST), null);
127 expect(h.registry.get(h.machineByIdAtom).get(FIRST)).toBe("server");
128 } finally {
129 h.registry.dispose();
130 }
131 });
132​
133 it("tracks capabilities across environments, config loss and removal", () => {
134 const h = harness();
135 const stop = h.registry.subscribe(h.pullRequestsSupportedAtom, () => {});
136 try {
137 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(false);
138 h.registry.set(h.configs(FIRST), config(true));
139 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(true);
140 // The first true result short-circuits: the second must be read when the first stops supporting it.
141 h.registry.set(h.configs(SECOND), config(true));
142 h.registry.set(h.configs(FIRST), null);
143 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(true);
144 h.registry.set(h.catalog, { isReady: true, entries: new Map([[FIRST, entry(FIRST)]]) });
145 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(false);
146 expect(h.registry.get(h.environmentIdsAtom)).toEqual([FIRST]);
147 expect(h.registry.get(h.identitiesAtom)).toEqual([{ environmentId: FIRST, label: "first" }]);
148 h.registry.set(h.catalog, {
149 isReady: true,
150 entries: new Map([
151 [SECOND, entry(SECOND)],
152 [FIRST, entry(FIRST)],
153 ]),
154 });
155 expect(h.registry.get(h.environmentIdsAtom)).toEqual([SECOND, FIRST]);
156 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(true);
157 h.registry.set(h.configs(SECOND), config(false));
158 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(false);
159 h.registry.set(h.catalog, { isReady: true, entries: new Map() });
160 expect(h.registry.get(h.environmentIdsAtom)).toEqual([]);
161 expect(h.registry.get(h.identitiesAtom)).toEqual([]);
162 expect(h.registry.get(h.environmentsAtom)).toEqual([]);
163 } finally {
164 stop();
165 h.registry.dispose();
166 }
167 });
168});