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

presentation.test.ts

90 lines3.5 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("tracks capabilities across environments, config loss and removal", () => {
57 const h = harness();
58 const stop = h.registry.subscribe(h.pullRequestsSupportedAtom, () => {});
59 try {
60 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(false);
61 h.registry.set(h.configs(FIRST), config(true));
62 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(true);
63 h.registry.set(h.configs(SECOND), config(true));
64 h.registry.set(h.configs(FIRST), null);
65 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(true);
66 h.registry.set(h.catalog, { isReady: true, entries: new Map([[FIRST, entry(FIRST)]]) });
67 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(false);
68 expect(h.registry.get(h.environmentIdsAtom)).toEqual([FIRST]);
69 expect(h.registry.get(h.identitiesAtom)).toEqual([{ environmentId: FIRST, label: "first" }]);
70 h.registry.set(h.catalog, {
71 isReady: true,
72 entries: new Map([
73 [SECOND, entry(SECOND)],
74 [FIRST, entry(FIRST)],
75 ]),
76 });
77 expect(h.registry.get(h.environmentIdsAtom)).toEqual([SECOND, FIRST]);
78 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(true);
79 h.registry.set(h.configs(SECOND), config(false));
80 expect(h.registry.get(h.pullRequestsSupportedAtom)).toBe(false);
81 h.registry.set(h.catalog, { isReady: true, entries: new Map() });
82 expect(h.registry.get(h.environmentIdsAtom)).toEqual([]);
83 expect(h.registry.get(h.identitiesAtom)).toEqual([]);
84 expect(h.registry.get(h.environmentsAtom)).toEqual([]);
85 } finally {
86 stop();
87 h.registry.dispose();
88 }
89 });
90});