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

shell.test.ts

136 lines4.9 KB

1import {
2 BearerConnectionTarget,
3 PrimaryConnectionTarget,
4} from "@t3tools/client-runtime/connection";
5import type { EnvironmentCatalogState } from "@t3tools/client-runtime/state/connections";
6import type { EnvironmentShellState } from "@t3tools/client-runtime/state/shell";
7import { EnvironmentId } from "@t3tools/contracts";
8import * as Option from "effect/Option";
9import { Atom, AtomRegistry } from "effect/unstable/reactivity";
10import { describe, expect, it } from "vite-plus/test";
11​
12import { createAllEnvironmentProjectSnapshotsReadyAtom } from "./shell";
13​
14const LOCAL = EnvironmentId.make("local");
15const REMOTE = EnvironmentId.make("remote");
16​
17function shellState(status: EnvironmentShellState["status"]): EnvironmentShellState {
18 return {
19 status,
20 snapshot:
21 status === "empty"
22 ? Option.none()
23 : Option.some({
24 snapshotSequence: 1,
25 updatedAt: "2026-09-04T00:00:00.000Z",
26 projects: [],
27 threads: [],
28 }),
29 error: Option.none(),
30 };
32​
33function catalogState(environmentIds: readonly EnvironmentId[]): EnvironmentCatalogState {
34 return {
35 isReady: true,
36 entries: new Map(
37 environmentIds.map((environmentId) => [
38 environmentId,
39 {
40 target:
41 environmentId === LOCAL
42 ? new PrimaryConnectionTarget({
43 environmentId,
44 label: environmentId,
45 httpBaseUrl: `https://${environmentId}.example.test`,
46 wsBaseUrl: `wss://${environmentId}.example.test`,
47 })
48 : new BearerConnectionTarget({
49 environmentId,
50 connectionId: environmentId,
51 label: environmentId,
52 }),
53 profile: Option.none(),
54 enabled: true,
55 },
56 ]),
57 ),
58 };
60​
61function makeHarness(requiresPrimaryEnvironment = true) {
62 const catalog = Atom.make<EnvironmentCatalogState>({ isReady: false, entries: new Map() });
63 const shells = Atom.family((_environmentId: EnvironmentId) =>
64 Atom.make<EnvironmentShellState>(shellState("empty")),
65 );
66 const ready = createAllEnvironmentProjectSnapshotsReadyAtom({
67 catalogValueAtom: catalog,
68 shellStateValueAtom: shells,
69 requiresPrimaryEnvironment,
70 });
71 const registry = AtomRegistry.make();
72 return { catalog, shells, ready, registry };
74​
75describe("project snapshot readiness", () => {
76 it("does not clear a saved scope while the ready catalog is still empty", () => {
77 const { catalog, ready, registry } = makeHarness();
78 expect(registry.get(ready)).toBe(false);
79 registry.set(catalog, catalogState([]));
80 expect(registry.get(ready)).toBe(false);
81 registry.dispose();
82 });
83​
84 it("waits for primary discovery even if a persisted remote is already live", () => {
85 const { catalog, shells, ready, registry } = makeHarness();
86 registry.set(catalog, catalogState([REMOTE]));
87 registry.set(shells(REMOTE), shellState("live"));
88 expect(registry.get(ready)).toBe(false);
89​
90 registry.set(catalog, catalogState([LOCAL, REMOTE]));
91 expect(registry.get(ready)).toBe(false);
92 registry.set(shells(LOCAL), shellState("live"));
93 expect(registry.get(ready)).toBe(true);
94 registry.dispose();
95 });
96​
97 it("allows a hosted client to load projects without a primary environment", () => {
98 const { catalog, shells, ready, registry } = makeHarness(false);
99 registry.set(catalog, catalogState([REMOTE]));
100 expect(registry.get(ready)).toBe(false);
101 registry.set(shells(REMOTE), shellState("live"));
102 expect(registry.get(ready)).toBe(true);
103 registry.dispose();
104 });
105​
106 it("waits for live snapshots through offline startup and reconnect", () => {
107 const { catalog, shells, ready, registry } = makeHarness();
108 registry.set(catalog, catalogState([LOCAL, REMOTE]));
109 registry.set(shells(LOCAL), shellState("live"));
110 expect(registry.get(ready)).toBe(false);
111​
112 // An old cache and a reconnect in progress can both omit a real project.
113 registry.set(shells(REMOTE), shellState("cached"));
114 expect(registry.get(ready)).toBe(false);
115 registry.set(shells(REMOTE), shellState("synchronizing"));
116 expect(registry.get(ready)).toBe(false);
117 registry.set(shells(REMOTE), shellState("live"));
118 expect(registry.get(ready)).toBe(true);
119​
120 registry.set(shells(REMOTE), shellState("cached"));
121 expect(registry.get(ready)).toBe(false);
122 registry.set(shells(REMOTE), shellState("live"));
123 expect(registry.get(ready)).toBe(true);
124 registry.dispose();
125 });
126​
127 it("stops waiting for an environment only when it leaves the catalog", () => {
128 const { catalog, shells, ready, registry } = makeHarness();
129 registry.set(catalog, catalogState([LOCAL, REMOTE]));
130 registry.set(shells(LOCAL), shellState("live"));
131 expect(registry.get(ready)).toBe(false);
132 registry.set(catalog, catalogState([LOCAL]));
133 expect(registry.get(ready)).toBe(true);
134 registry.dispose();
135 });
136});