Open History to see the last commit
1import { EnvironmentId, type OrchestrationShellSnapshot } from "@t3tools/contracts";
2import { AtomRegistry } from "effect/unstable/reactivity";
3import { afterEach, describe, expect, it, vi } from "vitest";
4
5import {
6 type ArchivedThreadsClient,
7 createArchivedThreadsManager,
8 makeArchivedThreadsEnvironmentKey,
9 parseArchivedThreadsEnvironmentKey,
10 readArchivedThreadsSnapshotState,
11} from "./archivedThreadsState.ts";
12
13let registry = AtomRegistry.make();
14
15function resetAtomRegistry() {
16 registry.dispose();
17 registry = AtomRegistry.make();
18}
19
20function createSnapshot(id: string): OrchestrationShellSnapshot {
21 return {
22 snapshotSequence: 1,
23 projects: [],
24 threads: [],
25 updatedAt: `2026-05-08T00:00:00.000Z`,
26 id,
27 } as OrchestrationShellSnapshot;
28}
29
30describe("createArchivedThreadsManager", () => {
31 afterEach(() => {
32 resetAtomRegistry();
33 });
34
35 it("loads archived snapshots for configured environment clients", async () => {
36 const envA = EnvironmentId.make("env-a");
37 const envB = EnvironmentId.make("env-b");
38 const clients = new Map<EnvironmentId, ArchivedThreadsClient>([
39 [
40 envA,
41 {
42 getArchivedShellSnapshot: vi.fn(async () => createSnapshot("a")),
43 },
44 ],
45 [
46 envB,
47 {
48 getArchivedShellSnapshot: vi.fn(async () => createSnapshot("b")),
49 },
50 ],
51 ]);
52 const manager = createArchivedThreadsManager({
53 getRegistry: () => registry,
54 getClient: (environmentId) => clients.get(environmentId) ?? null,
55 });
56
57 const result = registry.get(manager.getAtom(makeArchivedThreadsEnvironmentKey([envB, envA])));
58
59 await vi.waitFor(() => {
60 const state = readArchivedThreadsSnapshotState(
61 registry.get(manager.getAtom(makeArchivedThreadsEnvironmentKey([envA, envB]))),
62 );
63 expect(state.snapshots.map((snapshot) => snapshot.environmentId)).toEqual([envA, envB]);
64 });
65 expect(readArchivedThreadsSnapshotState(result).isLoading).toBe(true);
66 });
67
68 it("refreshes known snapshot groups that include an environment", async () => {
69 const envA = EnvironmentId.make("env-a");
70 const envB = EnvironmentId.make("env-b");
71 const getArchivedShellSnapshot = vi.fn(async () =>
72 createSnapshot(`a-${getArchivedShellSnapshot.mock.calls.length}`),
73 );
74 const manager = createArchivedThreadsManager({
75 getRegistry: () => registry,
76 getClient: (environmentId) => (environmentId === envA ? { getArchivedShellSnapshot } : null),
77 staleTimeMs: 60_000,
78 });
79
80 const atom = manager.getAtom(makeArchivedThreadsEnvironmentKey([envA, envB]));
81 registry.get(atom);
82 await vi.waitFor(() => expect(getArchivedShellSnapshot).toHaveBeenCalledTimes(1));
83
84 manager.refreshForEnvironment(envA);
85
86 await vi.waitFor(() => expect(getArchivedShellSnapshot).toHaveBeenCalledTimes(2));
87 });
88
89 it("round-trips environment keys in sorted order", () => {
90 const envA = EnvironmentId.make("env-a");
91 const envB = EnvironmentId.make("env-b");
92 const key = makeArchivedThreadsEnvironmentKey([envB, envA]);
93
94 expect(parseArchivedThreadsEnvironmentKey(key)).toEqual([envA, envB]);
95 });
96});