|
| 1 | +import { PrimaryConnectionTarget } from "@t3tools/client-runtime/connection"; |
| 2 | +import type { EnvironmentCatalogState } from "@t3tools/client-runtime/state/connections"; |
| 3 | +import type { EnvironmentShellState } from "@t3tools/client-runtime/state/shell"; |
| 4 | +import { EnvironmentId } from "@t3tools/contracts"; |
| 5 | +import * as Option from "effect/Option"; |
| 6 | +import { Atom, AtomRegistry } from "effect/unstable/reactivity"; |
| 7 | +import { describe, expect, it } from "vite-plus/test"; |
| 8 | + |
| 9 | +import { createAllEnvironmentProjectSnapshotsReadyAtom } from "./shell"; |
| 10 | + |
| 11 | +const LOCAL = EnvironmentId.make("local"); |
| 12 | +const REMOTE = EnvironmentId.make("remote"); |
| 13 | + |
| 14 | +function shellState(status: EnvironmentShellState["status"]): EnvironmentShellState { |
| 15 | + return { |
| 16 | + status, |
| 17 | + snapshot: |
| 18 | + status === "empty" |
| 19 | + ? Option.none() |
| 20 | + : Option.some({ |
| 21 | + snapshotSequence: 1, |
| 22 | + updatedAt: "2026-09-04T00:00:00.000Z", |
| 23 | + projects: [], |
| 24 | + threads: [], |
| 25 | + }), |
| 26 | + error: Option.none(), |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +function catalogState(environmentIds: readonly EnvironmentId[]): EnvironmentCatalogState { |
| 31 | + return { |
| 32 | + isReady: true, |
| 33 | + entries: new Map( |
| 34 | + environmentIds.map((environmentId) => [ |
| 35 | + environmentId, |
| 36 | + { |
| 37 | + target: new PrimaryConnectionTarget({ |
| 38 | + environmentId, |
| 39 | + label: environmentId, |
| 40 | + httpBaseUrl: `https://${environmentId}.example.test`, |
| 41 | + wsBaseUrl: `wss://${environmentId}.example.test`, |
| 42 | + }), |
| 43 | + profile: Option.none(), |
| 44 | + }, |
| 45 | + ]), |
| 46 | + ), |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +function makeHarness() { |
| 51 | + const catalog = Atom.make<EnvironmentCatalogState>({ isReady: false, entries: new Map() }); |
| 52 | + const shells = Atom.family((_environmentId: EnvironmentId) => |
| 53 | + Atom.make<EnvironmentShellState>(shellState("empty")), |
| 54 | + ); |
| 55 | + const ready = createAllEnvironmentProjectSnapshotsReadyAtom({ |
| 56 | + catalogValueAtom: catalog, |
| 57 | + shellStateValueAtom: shells, |
| 58 | + }); |
| 59 | + const registry = AtomRegistry.make(); |
| 60 | + return { catalog, shells, ready, registry }; |
| 61 | +} |
| 62 | + |
| 63 | +describe("project snapshot readiness", () => { |
| 64 | + it("does not clear a saved scope before catalog loading finishes", () => { |
| 65 | + const { catalog, ready, registry } = makeHarness(); |
| 66 | + expect(registry.get(ready)).toBe(false); |
| 67 | + registry.set(catalog, catalogState([])); |
| 68 | + expect(registry.get(ready)).toBe(true); |
| 69 | + registry.dispose(); |
| 70 | + }); |
| 71 | + |
| 72 | + it("waits for live snapshots through offline startup and reconnect", () => { |
| 73 | + const { catalog, shells, ready, registry } = makeHarness(); |
| 74 | + registry.set(catalog, catalogState([LOCAL, REMOTE])); |
| 75 | + registry.set(shells(LOCAL), shellState("live")); |
| 76 | + expect(registry.get(ready)).toBe(false); |
| 77 | + |
| 78 | + // An old cache and a reconnect in progress can both omit a real project. |
| 79 | + registry.set(shells(REMOTE), shellState("cached")); |
| 80 | + expect(registry.get(ready)).toBe(false); |
| 81 | + registry.set(shells(REMOTE), shellState("synchronizing")); |
| 82 | + expect(registry.get(ready)).toBe(false); |
| 83 | + registry.set(shells(REMOTE), shellState("live")); |
| 84 | + expect(registry.get(ready)).toBe(true); |
| 85 | + |
| 86 | + registry.set(shells(REMOTE), shellState("cached")); |
| 87 | + expect(registry.get(ready)).toBe(false); |
| 88 | + registry.set(shells(REMOTE), shellState("live")); |
| 89 | + expect(registry.get(ready)).toBe(true); |
| 90 | + registry.dispose(); |
| 91 | + }); |
| 92 | + |
| 93 | + it("stops waiting for an environment only when it leaves the catalog", () => { |
| 94 | + const { catalog, shells, ready, registry } = makeHarness(); |
| 95 | + registry.set(catalog, catalogState([LOCAL, REMOTE])); |
| 96 | + registry.set(shells(LOCAL), shellState("live")); |
| 97 | + expect(registry.get(ready)).toBe(false); |
| 98 | + registry.set(catalog, catalogState([LOCAL])); |
| 99 | + expect(registry.get(ready)).toBe(true); |
| 100 | + registry.dispose(); |
| 101 | + }); |
| 102 | +}); |
0 commit comments