Open History to see the last commit
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 type { ConnectionCatalogEntry } from "../connection/catalog.ts";
7import { AVAILABLE_CONNECTION_STATE, PrimaryConnectionTarget } from "../connection/model.ts";
8import {
9 createEnvironmentPresentationAtoms,
10 createEnvironmentSummaryAtoms,
11} from "./presentation.ts";
12import type { EnvironmentCatalogState } from "./connections.ts";
13
14const FIRST = EnvironmentId.make("first");
15const SECOND = EnvironmentId.make("second");
16function entry(environmentId: EnvironmentId, label = environmentId as string) {
17 return {
18 target: new PrimaryConnectionTarget({
19 environmentId,
20 label,
21 httpBaseUrl: "https://example.test",
22 wsBaseUrl: "wss://example.test",
23 }),
24 enabled: true,
25 profile: Option.none(),
26 };
27}
28function config(pullRequests = false, cwd = "/workspace"): ServerConfig {
29 return {
30 cwd,
31 environment: { capabilities: { pullRequests }, platform: { machine: "desktop" } },
32 } as ServerConfig;
33}
34function harness() {
35 const catalog = Atom.make<EnvironmentCatalogState>({
36 isReady: true,
37 entries: new Map([
38 [FIRST, entry(FIRST)],
39 [SECOND, entry(SECOND)],
40 ]),
41 });
42 const configs = Atom.family((_id: EnvironmentId) => Atom.make<ServerConfig | null>(config()));
43 const state = Atom.make(AsyncResult.success(AVAILABLE_CONNECTION_STATE));
44 const full = createEnvironmentPresentationAtoms({
45 catalogValueAtom: catalog,
46 stateAtom: () => state,
47 serverConfigValueAtom: configs,
48 });
49 const summaries = createEnvironmentSummaryAtoms({
50 catalogValueAtom: catalog,
51 presentationAtom: full.presentationAtom,
52 });
53 return { catalog, configs, state, full, ...summaries, registry: AtomRegistry.make() };
54}
55
56describe("environment summary subscriptions", () => {
57 it("tracks capabilities across environments, config loss and removal", () => {
58 const h = harness();
59 const stop = h.registry.subscribe(h.pullRequestsSupportedAtom, () => {});
60 try {
61 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(false);
62 h.registry.set(h.configs(FIRST), config(true));
63 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(true);
64 h.registry.set(h.configs(SECOND), config(true));
65 h.registry.set(h.configs(FIRST), null);
66 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(true);
67 h.registry.set(h.catalog, { isReady: true, entries: new Map([[FIRST, entry(FIRST)]]) });
68 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(false);
69 expect(h.registry.get(h.environmentIdsAtom)).toEqual([FIRST]);
70 expect(h.registry.get(h.identitiesAtom)).toEqual([{ environmentId: FIRST, label: "first" }]);
71 h.registry.set(h.catalog, {
72 isReady: true,
73 entries: new Map([
74 [SECOND, entry(SECOND)],
75 [FIRST, entry(FIRST)],
76 ]),
77 });
78 expect(h.registry.get(h.environmentIdsAtom)).toEqual([SECOND, FIRST]);
79 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(true);
80 h.registry.set(h.configs(SECOND), config(false));
81 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(false);
82 h.registry.set(h.catalog, { isReady: true, entries: new Map() });
83 expect(h.registry.get(h.environmentIdsAtom)).toEqual([]);
84 expect(h.registry.get(h.identitiesAtom)).toEqual([]);
85 expect(h.registry.get(h.environmentsAtom)).toEqual([]);
86 } finally {
87 stop();
88 h.registry.dispose();
89 }
90 });
91});
92
93describe("presentations with providers", () => {
94 it("leaves out disabled connections until they are re-enabled", () => {
95 const h = harness();
96 const atom = h.full.presentationsWithProvidersAtom;
97 const withSecond = (enabled: boolean): EnvironmentCatalogState => ({
98 isReady: true,
99 entries: new Map([
100 [FIRST, entry(FIRST)],
101 [SECOND, { ...entry(SECOND), enabled }],
102 ]),
103 });
104 const stop = h.registry.subscribe(atom, () => {});
105 try {
106 h.registry.set(h.configs(SECOND), config(false, "/second"));
107 h.registry.set(h.catalog, withSecond(false));
108 expect([...h.registry.get(atom).keys()]).toEqual([FIRST]);
109 expect(h.registry.get(h.full.presentationsAtom).has(SECOND)).toBe(true);
110 h.registry.set(h.catalog, withSecond(true));
111 expect([...h.registry.get(atom).keys()]).toEqual([FIRST, SECOND]);
112 expect(h.registry.get(atom).get(SECOND)?.serverConfig?.cwd).toBe("/second");
113 } finally {
114 stop();
115 h.registry.dispose();
116 }
117 });
118
119 it("leaves out unsupported environments", () => {
120 const h = harness();
121 const atom = h.full.presentationsWithProvidersAtom;
122 const stop = h.registry.subscribe(atom, () => {});
123 try {
124 h.registry.set(h.catalog, {
125 isReady: true,
126 entries: new Map<EnvironmentId, ConnectionCatalogEntry>([
127 [FIRST, entry(FIRST)],
128 [SECOND, { ...entry(SECOND), unsupportedReason: "Server is too old." }],
129 ]),
130 });
131 expect([...h.registry.get(atom).keys()]).toEqual([FIRST]);
132 } finally {
133 stop();
134 h.registry.dispose();
135 }
136 });
137});