Open History to see the last commit
1import {
2 AVAILABLE_CONNECTION_STATE,
3 BearerConnectionTarget,
4 PrimaryConnectionTarget,
5} from "@t3tools/client-runtime/connection";
6import type { EnvironmentCatalogState } from "@t3tools/client-runtime/state/connections";
7import { EMPTY_SHELL_STATE, type EnvironmentShellState } from "@t3tools/client-runtime/state/shell";
8import { EnvironmentId } from "@t3tools/contracts";
9import * as Option from "effect/Option";
10import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
11import { describe, expect, it, vi } from "vite-plus/test";
12
13import {
14 createAllEnvironmentProjectSnapshotsReadyAtom,
15 expectsPrimaryEnvironment,
16 environmentShell,
17} from "./shell";
18import { environmentCatalog } from "../connection/catalog";
19import { environmentUnreachableAtom } from "./presentation";
20import { defaultProjectFallbackReadyAtom } from "./projects";
21
22const LOCAL = EnvironmentId.make("local");
23const REMOTE = EnvironmentId.make("remote");
24
25function shellState(status: EnvironmentShellState["status"]): EnvironmentShellState {
26 return {
27 ...EMPTY_SHELL_STATE,
28 status,
29 snapshot:
30 status === "empty"
31 ? Option.none()
32 : Option.some({
33 schemaVersion: 1,
34 snapshotSequence: 1,
35 projects: [],
36 threads: [],
37 archivedThreads: [],
38 }),
39 error: Option.none(),
40 };
41}
42
43function catalogState(environmentIds: readonly EnvironmentId[]): EnvironmentCatalogState {
44 return {
45 isReady: true,
46 entries: new Map(
47 environmentIds.map((environmentId) => [
48 environmentId,
49 {
50 target:
51 environmentId === LOCAL
52 ? new PrimaryConnectionTarget({
53 environmentId,
54 label: environmentId,
55 httpBaseUrl: `https://${environmentId}.example.test`,
56 wsBaseUrl: `wss://${environmentId}.example.test`,
57 })
58 : new BearerConnectionTarget({
59 environmentId,
60 connectionId: environmentId,
61 label: environmentId,
62 }),
63 profile: Option.none(),
64 enabled: true,
65 },
66 ]),
67 ),
68 };
69}
70
71function makeHarness(requiresPrimaryEnvironment = true) {
72 const catalog = Atom.make<EnvironmentCatalogState>({ isReady: false, entries: new Map() });
73 const shells = Atom.family((_environmentId: EnvironmentId) =>
74 Atom.make<EnvironmentShellState>(shellState("empty")),
75 );
76 const ready = createAllEnvironmentProjectSnapshotsReadyAtom({
77 catalogValueAtom: catalog,
78 shellStateValueAtom: shells,
79 requiresPrimaryEnvironment,
80 });
81 const registry = AtomRegistry.make();
82 return { catalog, shells, ready, registry };
83}
84
85describe("project snapshot readiness", () => {
86 it("does not clear a saved scope while the ready catalog is still empty", () => {
87 const { catalog, ready, registry } = makeHarness();
88 expect(registry.get(ready)).toBe(false);
89 registry.set(catalog, catalogState([]));
90 expect(registry.get(ready)).toBe(false);
91 registry.dispose();
92 });
93
94 it("waits for primary discovery even if a persisted remote is already live", () => {
95 const { catalog, shells, ready, registry } = makeHarness();
96 registry.set(catalog, catalogState([REMOTE]));
97 registry.set(shells(REMOTE), shellState("live"));
98 expect(registry.get(ready)).toBe(false);
99
100 registry.set(catalog, catalogState([LOCAL, REMOTE]));
101 expect(registry.get(ready)).toBe(false);
102 registry.set(shells(LOCAL), shellState("live"));
103 expect(registry.get(ready)).toBe(true);
104 registry.dispose();
105 });
106
107 it("allows a hosted client to load projects without a primary environment", () => {
108 const { catalog, shells, ready, registry } = makeHarness(false);
109 registry.set(catalog, catalogState([REMOTE]));
110 expect(registry.get(ready)).toBe(false);
111 registry.set(shells(REMOTE), shellState("live"));
112 expect(registry.get(ready)).toBe(true);
113 registry.dispose();
114 });
115
116 it("waits for live snapshots through offline startup and reconnect", () => {
117 const { catalog, shells, ready, registry } = makeHarness();
118 registry.set(catalog, catalogState([LOCAL, REMOTE]));
119 registry.set(shells(LOCAL), shellState("live"));
120 expect(registry.get(ready)).toBe(false);
121
122 registry.set(shells(REMOTE), shellState("cached"));
123 expect(registry.get(ready)).toBe(false);
124 registry.set(shells(REMOTE), shellState("synchronizing"));
125 expect(registry.get(ready)).toBe(false);
126 registry.set(shells(REMOTE), shellState("live"));
127 expect(registry.get(ready)).toBe(true);
128
129 registry.set(shells(REMOTE), shellState("cached"));
130 expect(registry.get(ready)).toBe(false);
131 registry.set(shells(REMOTE), shellState("live"));
132 expect(registry.get(ready)).toBe(true);
133 registry.dispose();
134 });
135
136 it("stops waiting for an environment only when it leaves the catalog", () => {
137 const { catalog, shells, ready, registry } = makeHarness();
138 registry.set(catalog, catalogState([LOCAL, REMOTE]));
139 registry.set(shells(LOCAL), shellState("live"));
140 expect(registry.get(ready)).toBe(false);
141 registry.set(catalog, catalogState([LOCAL]));
142 expect(registry.get(ready)).toBe(true);
143 registry.dispose();
144 });
145});
146
147it("calls an environment unreachable only once primary discovery can prove it absent", () => {
148 const UNKNOWN = EnvironmentId.make("unknown");
149 const unreachable = (catalog: EnvironmentCatalogState, environmentId: EnvironmentId) => {
150 const registry = AtomRegistry.make({
151 initialValues: [[environmentCatalog.catalogValueAtom, catalog]],
152 });
153 try {
154 return registry.get(environmentUnreachableAtom(environmentId));
155 } finally {
156 registry.dispose();
157 }
158 };
159 vi.stubGlobal("window", { desktopBridge: { getLocalEnvironmentEnabled: () => true } });
160 try {
161 expect(unreachable(catalogState([REMOTE]), LOCAL)).toBe(false);
162 expect(unreachable(catalogState([LOCAL, REMOTE]), UNKNOWN)).toBe(true);
163 expect(unreachable(catalogState([LOCAL, REMOTE]), REMOTE)).toBe(false);
164 } finally {
165 vi.unstubAllGlobals();
166 }
167});
168
169it("expects a primary environment only on desktops with the local environment enabled", () => {
170 const desktop = (enabled: boolean) => {
171 vi.stubGlobal("window", { desktopBridge: { getLocalEnvironmentEnabled: () => enabled } });
172 try {
173 return expectsPrimaryEnvironment();
174 } finally {
175 vi.unstubAllGlobals();
176 }
177 };
178 expect(desktop(true)).toBe(true);
179 expect(desktop(false)).toBe(false);
180});
181
182it.each([undefined, "", "Server update required"])(
183 "settles fallback for an unsupported remembered environment: %s",
184 (unsupportedReason) => {
185 const catalog = catalogState([LOCAL, REMOTE]);
186 const entries = new Map(catalog.entries);
187 entries.set(REMOTE, {
188 ...entries.get(REMOTE)!,
189 ...(unsupportedReason === undefined ? {} : { unsupportedReason }),
190 });
191 const registry = AtomRegistry.make({
192 initialValues: [
193 [environmentCatalog.catalogValueAtom, { ...catalog, entries }],
194 [environmentShell.statusAtom(REMOTE), "empty"],
195 [environmentCatalog.stateAtom(REMOTE), AsyncResult.success(AVAILABLE_CONNECTION_STATE)],
196 ],
197 });
198 try {
199 expect(registry.get(defaultProjectFallbackReadyAtom("remote:project"))).toBe(
200 unsupportedReason !== undefined,
201 );
202 } finally {
203 registry.dispose();
204 }
205 },
206);