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 schemaVersion: 1,
25 snapshotSequence: 1,
26 projects: [],
27 threads: [],
28 archivedThreads: [],
29 }),
30 error: Option.none(),
31 };
32}
33
34function catalogState(environmentIds: readonly EnvironmentId[]): EnvironmentCatalogState {
35 return {
36 isReady: true,
37 entries: new Map(
38 environmentIds.map((environmentId) => [
39 environmentId,
40 {
41 target:
42 environmentId === LOCAL
43 ? new PrimaryConnectionTarget({
44 environmentId,
45 label: environmentId,
46 httpBaseUrl: `https://${environmentId}.example.test`,
47 wsBaseUrl: `wss://${environmentId}.example.test`,
48 })
49 : new BearerConnectionTarget({
50 environmentId,
51 connectionId: environmentId,
52 label: environmentId,
53 }),
54 profile: Option.none(),
55 },
56 ]),
57 ),
58 };
59}
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 };
73}
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});