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

workspace-connection-atoms.test.ts

99 lines3.6 KB

1import { describe, expect, it } from "@effect/vitest";
2import {
3 PrimaryConnectionTarget,
4 type EnvironmentPresentation,
5 type NetworkStatus,
6} from "@t3tools/client-runtime/connection";
7import type { EnvironmentCatalogState } from "@t3tools/client-runtime/state/connections";
8import type { EnvironmentShellSummary } from "@t3tools/client-runtime/state/shell";
9import { EnvironmentId, type ServerConfig } from "@t3tools/contracts";
10import * as Option from "effect/Option";
11import { Atom, AtomRegistry } from "effect/reactivity";
12​
13import { createWorkspaceConnectionAtoms } from "./workspace-connection-atoms";
14import { projectWorkspaceState } from "./workspaceModel";
15import { projectEnvironmentConnectionSummary as projectWorkspaceEnvironment } from "@t3tools/client-runtime/state/presentation";
16​
17const ID = EnvironmentId.make("environment-1");
18function presentation(): EnvironmentPresentation {
19 return {
20 entry: {
21 target: new PrimaryConnectionTarget({
22 environmentId: ID,
23 label: "Mac",
24 httpBaseUrl: "https://example.test",
25 wsBaseUrl: "wss://example.test",
26 }),
27 enabled: true,
28 profile: Option.none(),
29 },
30 connection: { phase: "connected", error: null, traceId: null },
31 serverConfig: null,
32 refusedRouteIds: [],
33 };
35function harness() {
36 const source = Atom.make<EnvironmentPresentation | null>(presentation());
37 const catalog = Atom.make<EnvironmentCatalogState>({
38 isReady: true,
39 entries: new Map([[ID, presentation().entry]]),
40 });
41 const network = Atom.make<NetworkStatus>("online");
42 const atoms = createWorkspaceConnectionAtoms({
43 catalogValueAtom: catalog,
44 networkStatusValueAtom: network,
45 presentationAtom: () => source,
46 });
47 const registry = AtomRegistry.make();
48 return { source, catalog, network, registry, ...atoms };
50​
51describe("workspace connection subscriptions", () => {
52 it("keeps connection consumers stable across shell and provider updates", () => {
53 const h = harness();
54 const shell = Atom.make<EnvironmentShellSummary>({
55 hasSnapshot: true,
56 hasSynchronizingShell: false,
57 firstError: null,
58 });
59 const broadState = Atom.make((get) =>
60 projectWorkspaceState({
61 isReady: get(h.catalog).isReady,
62 networkStatus: get(h.network),
63 environments: [projectWorkspaceEnvironment(ID, get(h.source)!)],
64 shellSummary: get(shell),
65 hasSyncingConnectedEnvironment: false,
66 }),
67 );
68 const initial = h.registry.get(h.stateAtom);
69 const initialEnvironments = h.registry.get(h.environmentsAtom);
70 let broadChanges = 0;
71 let connectionChanges = 0;
72 const stopBroad = h.registry.subscribe(broadState, () => broadChanges++);
73 const stopNarrow = h.registry.subscribe(h.stateAtom, () => connectionChanges++);
74 try {
75 for (let index = 0; index < 20; index++) {
76 h.registry.set(shell, {
77 ...h.registry.get(shell),
78 hasSynchronizingShell: index % 2 === 0,
79 });
80 h.registry.get(broadState);
81 h.registry.get(h.stateAtom);
82 }
83 expect(broadChanges).toBe(20);
84 expect(connectionChanges).toBe(0);
85 for (let index = 0; index < 4; index++) {
86 const config = { cwd: `/workspace-${index}` } as ServerConfig;
87 h.registry.set(h.source, { ...presentation(), serverConfig: config });
88 expect(h.registry.get(h.source)?.serverConfig).toBe(config);
89 expect(h.registry.get(h.stateAtom)).toBe(initial);
90 expect(h.registry.get(h.environmentsAtom)).toBe(initialEnvironments);
91 }
92 expect(connectionChanges).toBe(0);
93 } finally {
94 stopBroad();
95 stopNarrow();
96 h.registry.dispose();
97 }
98 });
99});