Open History to see the last commit
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 };
31}
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 },
55 ]),
56 ),
57 };
58}
59
60function makeHarness(requiresPrimaryEnvironment = true) {
61 const catalog = Atom.make<EnvironmentCatalogState>({ isReady: false, entries: new Map() });
62 const shells = Atom.family((_environmentId: EnvironmentId) =>
63 Atom.make<EnvironmentShellState>(shellState("empty")),
64 );
65 const ready = createAllEnvironmentProjectSnapshotsReadyAtom({
66 catalogValueAtom: catalog,
67 shellStateValueAtom: shells,
68 requiresPrimaryEnvironment,
69 });
70 const registry = AtomRegistry.make();
71 return { catalog, shells, ready, registry };
72}
73
74describe("project snapshot readiness", () => {
75 it("does not clear a saved scope while the ready catalog is still empty", () => {
76 const { catalog, ready, registry } = makeHarness();
77 expect(registry.get(ready)).toBe(false);
78 registry.set(catalog, catalogState([]));
79 expect(registry.get(ready)).toBe(false);
80 registry.dispose();
81 });
82
83 it("waits for primary discovery even if a persisted remote is already live", () => {
84 const { catalog, shells, ready, registry } = makeHarness();
85 registry.set(catalog, catalogState([REMOTE]));
86 registry.set(shells(REMOTE), shellState("live"));
87 expect(registry.get(ready)).toBe(false);
88
89 registry.set(catalog, catalogState([LOCAL, REMOTE]));
90 expect(registry.get(ready)).toBe(false);
91 registry.set(shells(LOCAL), shellState("live"));
92 expect(registry.get(ready)).toBe(true);
93 registry.dispose();
94 });
95
96 it("allows a hosted client to load projects without a primary environment", () => {
97 const { catalog, shells, ready, registry } = makeHarness(false);
98 registry.set(catalog, catalogState([REMOTE]));
99 expect(registry.get(ready)).toBe(false);
100 registry.set(shells(REMOTE), shellState("live"));
101 expect(registry.get(ready)).toBe(true);
102 registry.dispose();
103 });
104
105 it("waits for live snapshots through offline startup and reconnect", () => {
106 const { catalog, shells, ready, registry } = makeHarness();
107 registry.set(catalog, catalogState([LOCAL, REMOTE]));
108 registry.set(shells(LOCAL), shellState("live"));
109 expect(registry.get(ready)).toBe(false);
110
111 // An old cache and a reconnect in progress can both omit a real project.
112 registry.set(shells(REMOTE), shellState("cached"));
113 expect(registry.get(ready)).toBe(false);
114 registry.set(shells(REMOTE), shellState("synchronizing"));
115 expect(registry.get(ready)).toBe(false);
116 registry.set(shells(REMOTE), shellState("live"));
117 expect(registry.get(ready)).toBe(true);
118
119 registry.set(shells(REMOTE), shellState("cached"));
120 expect(registry.get(ready)).toBe(false);
121 registry.set(shells(REMOTE), shellState("live"));
122 expect(registry.get(ready)).toBe(true);
123 registry.dispose();
124 });
125
126 it("stops waiting for an environment only when it leaves the catalog", () => {
127 const { catalog, shells, ready, registry } = makeHarness();
128 registry.set(catalog, catalogState([LOCAL, REMOTE]));
129 registry.set(shells(LOCAL), shellState("live"));
130 expect(registry.get(ready)).toBe(false);
131 registry.set(catalog, catalogState([LOCAL]));
132 expect(registry.get(ready)).toBe(true);
133 registry.dispose();
134 });
135});