Open History to see the last commit
1import { AtomRegistry } from "effect/unstable/reactivity";
2import { afterEach, describe, expect, it } from "vitest";
3
4import {
5 EnvironmentId,
6 ProjectId,
7 ProviderInstanceId,
8 ThreadId,
9 type OrchestrationShellSnapshot,
10} from "@t3tools/contracts";
11
12import { createShellSnapshotManager } from "./shellSnapshotState.ts";
13
14let atomRegistry = AtomRegistry.make();
15
16function resetAtomRegistry() {
17 atomRegistry.dispose();
18 atomRegistry = AtomRegistry.make();
19}
20
21const BASE_SNAPSHOT: OrchestrationShellSnapshot = {
22 snapshotSequence: 1,
23 updatedAt: "2026-04-01T00:00:00.000Z",
24 projects: [
25 {
26 id: ProjectId.make("project-1"),
27 title: "Project",
28 workspaceRoot: "/repo",
29 repositoryIdentity: null,
30 defaultModelSelection: null,
31 scripts: [],
32 createdAt: "2026-04-01T00:00:00.000Z",
33 updatedAt: "2026-04-01T00:00:00.000Z",
34 },
35 ],
36 threads: [
37 {
38 id: ThreadId.make("thread-1"),
39 projectId: ProjectId.make("project-1"),
40 title: "Thread",
41 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
42 runtimeMode: "full-access",
43 interactionMode: "default",
44 branch: null,
45 worktreePath: null,
46 latestTurn: null,
47 createdAt: "2026-04-01T00:00:00.000Z",
48 updatedAt: "2026-04-01T00:00:00.000Z",
49 archivedAt: null,
50 session: null,
51 latestUserMessageAt: null,
52 hasPendingApprovals: false,
53 hasPendingUserInput: false,
54 hasActionableProposedPlan: false,
55 },
56 ],
57};
58
59const TARGET = { environmentId: EnvironmentId.make("env-local") } as const;
60
61describe("createShellSnapshotManager", () => {
62 afterEach(() => {
63 resetAtomRegistry();
64 });
65
66 it("starts pending when marked pending", () => {
67 const manager = createShellSnapshotManager({
68 getRegistry: () => atomRegistry,
69 });
70
71 manager.markPending(TARGET);
72
73 expect(manager.getSnapshot(TARGET)).toEqual({
74 data: null,
75 error: null,
76 isPending: true,
77 });
78 });
79
80 it("stores snapshots", () => {
81 const manager = createShellSnapshotManager({
82 getRegistry: () => atomRegistry,
83 });
84
85 manager.syncSnapshot(TARGET, BASE_SNAPSHOT);
86
87 expect(manager.getSnapshot(TARGET)).toEqual({
88 data: BASE_SNAPSHOT,
89 error: null,
90 isPending: false,
91 });
92 });
93
94 it("applies incremental shell events", () => {
95 const manager = createShellSnapshotManager({
96 getRegistry: () => atomRegistry,
97 });
98 const existingThread = BASE_SNAPSHOT.threads[0]!;
99
100 manager.syncSnapshot(TARGET, BASE_SNAPSHOT);
101 manager.applyEvent(TARGET, {
102 kind: "thread-upserted",
103 sequence: 2,
104 thread: {
105 ...existingThread,
106 title: "Renamed thread",
107 },
108 });
109
110 expect(manager.getSnapshot(TARGET).data?.threads[0]?.title).toBe("Renamed thread");
111 expect(manager.getSnapshot(TARGET).data?.snapshotSequence).toBe(2);
112 });
113
114 it("invalidates per environment", () => {
115 const manager = createShellSnapshotManager({
116 getRegistry: () => atomRegistry,
117 });
118
119 manager.syncSnapshot(TARGET, BASE_SNAPSHOT);
120 manager.invalidate(TARGET);
121
122 expect(manager.getSnapshot(TARGET)).toEqual({
123 data: null,
124 error: null,
125 isPending: false,
126 });
127 });
128});