Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadShell.test.ts

225 lines8.5 KB

1import {
2 EnvironmentId,
3 ProjectId,
4 ThreadId,
5 type OrchestrationV2ShellSnapshot,
6} from "@t3tools/contracts";
7import * as Option from "effect/Option";
8import { Atom, AtomRegistry } from "effect/reactivity";
9import { describe, expect, it } from "vite-plus/test";
10​
11import { PrimaryConnectionTarget } from "../connection/model.ts";
12import { v2ShellSnapshot, v2ThreadShell } from "./orchestrationV2TestFixtures.ts";
13import { applyShellStreamEvent } from "./shellReducer.ts";
14import { createEnvironmentThreadShellAtoms } from "./threadShell.ts";
15​
16const environmentId = EnvironmentId.make("environment-v2");
17const remoteEnvironmentId = EnvironmentId.make("remote-environment-v2");
18const otherProjectId = ProjectId.make("other-project");
19​
20function makeHarness(environmentIds: ReadonlyArray<EnvironmentId> = [environmentId]) {
21 const snapshotAtom = Atom.family((_environmentId: EnvironmentId) =>
22 Atom.make<OrchestrationV2ShellSnapshot | null>(v2ShellSnapshot),
23 );
24 const catalogValueAtom = Atom.make({
25 isReady: true,
26 entries: new Map(
27 environmentIds.map((id) => [
28 id,
29 {
30 target: new PrimaryConnectionTarget({
31 environmentId: id,
32 label: "Environment",
33 httpBaseUrl: "https://example.test",
34 wsBaseUrl: "wss://example.test",
35 }),
36 profile: Option.none(),
37 enabled: true,
38 },
39 ]),
40 ),
41 });
42 return {
43 registry: AtomRegistry.make(),
44 snapshotAtom,
45 catalogValueAtom,
46 threads: createEnvironmentThreadShellAtoms({ catalogValueAtom, snapshotAtom }),
47 };
49​
50describe("v2 thread shell lists", () => {
51 it("preserves ordered reference arrays when a middle thread changes", () => {
52 const { registry, threads, snapshotAtom } = makeHarness();
53 const snapshot = {
54 ...v2ShellSnapshot,
55 threads: ["a", "b", "c"].map((id) => ({ ...v2ThreadShell, id: ThreadId.make(id) })),
56 };
57 registry.set(snapshotAtom(environmentId), snapshot);
58 const dispose = registry.mount(threads.threadRefsAtom);
59 const before = registry.get(threads.threadRefsAtom);
60 registry.set(
61 snapshotAtom(environmentId),
62 applyShellStreamEvent(snapshot, {
63 kind: "thread.updated",
64 location: "active",
65 sequence: 1,
66 thread: { ...snapshot.threads[1]!, title: "Updated" },
67 }),
68 );
69 expect(registry.get(threads.threadRefsAtom)).toBe(before);
70 dispose();
71 registry.dispose();
72 });
73​
74 it("keeps navigation stable on hidden subagent updates and retains user forks", () => {
75 const { registry, threads, snapshotAtom } = makeHarness();
76 const root = v2ThreadShell;
77 const child = {
78 ...root,
79 id: ThreadId.make("child"),
80 lineage: {
81 ...root.lineage,
82 parentThreadId: root.id,
83 relationshipToParent: "subagent" as const,
84 },
85 };
86 const fork = {
87 ...child,
88 id: ThreadId.make("fork"),
89 lineage: { ...child.lineage, relationshipToParent: "fork" as const },
90 };
91 const snapshot = { ...v2ShellSnapshot, threads: [root, child, fork] };
92 registry.set(snapshotAtom(environmentId), snapshot);
93 const dispose = registry.mount(threads.navigationThreadShellsAtom);
94 const before = registry.get(threads.navigationThreadShellsAtom);
95 expect(before.map((thread) => thread.id)).toEqual([root.id, fork.id]);
96 registry.set(snapshotAtom(environmentId), {
97 ...snapshot,
98 threads: [root, { ...child, title: "Child streaming" }, fork],
99 });
100 expect(registry.get(threads.navigationThreadShellsAtom)).toBe(before);
101 expect(registry.get(threads.threadShellsAtom)).toHaveLength(3);
102 dispose();
103 registry.dispose();
104 });
105​
106 it("leaves switched-off environments out of navigation", () => {
107 const { registry, threads, catalogValueAtom } = makeHarness([
108 environmentId,
109 remoteEnvironmentId,
110 ]);
111 const dispose = registry.mount(threads.navigationThreadShellsAtom);
112 const environmentsOf = () =>
113 new Set(registry.get(threads.navigationThreadShellsAtom).map((t) => t.environmentId));
114 expect(environmentsOf()).toEqual(new Set([environmentId, remoteEnvironmentId]));
115​
116 const setRemoteEnabled = (enabled: boolean) => {
117 const catalog = registry.get(catalogValueAtom);
118 const entries = new Map(catalog.entries);
119 entries.set(remoteEnvironmentId, { ...entries.get(remoteEnvironmentId)!, enabled });
120 registry.set(catalogValueAtom, { ...catalog, entries });
121 };
122 setRemoteEnabled(false);
123 expect(environmentsOf()).toEqual(new Set([environmentId]));
124 setRemoteEnabled(true);
125 expect(environmentsOf()).toEqual(new Set([environmentId, remoteEnvironmentId]));
126 dispose();
127 registry.dispose();
128 });
129​
130 it("shares point and list values without retaining an atom for every listed thread", () => {
131 const harness = makeHarness();
132 const snapshot = {
133 ...v2ShellSnapshot,
134 threads: Array.from({ length: 200 }, (_, index) => ({
135 ...v2ThreadShell,
136 id: ThreadId.make(`thread-${index}`),
137 })),
138 };
139 harness.registry.set(harness.snapshotAtom(environmentId), snapshot);
140 const listAtom = harness.threads.threadShellsAtom;
141 const projectListAtom = harness.threads.threadShellsForProjectRefsAtom([
142 { environmentId, projectId: v2ThreadShell.projectId },
143 ]);
144 const disposeList = harness.registry.mount(listAtom);
145 const disposeProjectList = harness.registry.mount(projectListAtom);
146 try {
147 const before = harness.registry.get(listAtom);
148 expect(before).toHaveLength(200);
149 expect(harness.registry.get(projectListAtom)).toEqual(before);
150 expect(harness.registry.getNodes().size).toBeLessThan(20);
151 const firstAtom = harness.threads.threadShellAtom({
152 environmentId,
153 threadId: snapshot.threads[0]!.id,
154 });
155 expect(harness.registry.get(firstAtom)).toBe(before[0]);
156​
157 harness.registry.set(harness.snapshotAtom(environmentId), {
158 ...snapshot,
159 snapshotSequence: 1,
160 threads: snapshot.threads.map((thread, index) =>
161 index === 199 ? { ...thread, title: "Updated last thread" } : thread,
162 ),
163 });
164 const after = harness.registry.get(listAtom);
165 expect(after[0]).toBe(before[0]);
166 expect(after.at(-1)).not.toBe(before.at(-1));
167 expect(after.at(-1)?.title).toBe("Updated last thread");
168 expect(harness.registry.get(projectListAtom).at(-1)).toBe(after.at(-1));
169 expect(harness.registry.get(firstAtom)).toBe(after[0]);
170 expect(harness.registry.getNodes().size).toBeLessThan(20);
171 } finally {
172 disposeProjectList();
173 disposeList();
174 harness.registry.dispose();
175 }
176 });
177​
178 it("preserves project memberships on updates and keeps the same thread separate per environment", () => {
179 const harness = makeHarness([environmentId, remoteEnvironmentId]);
180 const otherThread = {
181 ...v2ThreadShell,
182 id: ThreadId.make("other-thread"),
183 projectId: otherProjectId,
184 };
185 const snapshot = { ...v2ShellSnapshot, threads: [v2ThreadShell, otherThread] };
186 harness.registry.set(harness.snapshotAtom(environmentId), snapshot);
187 const membershipAtom = harness.threads.environmentThreadRefsByProjectAtom(environmentId);
188 const listAtom = harness.threads.threadShellsForProjectRefsAtom([
189 { environmentId: remoteEnvironmentId, projectId: v2ThreadShell.projectId },
190 { environmentId, projectId: v2ThreadShell.projectId },
191 ]);
192 const disposeList = harness.registry.mount(listAtom);
193 try {
194 const membership = harness.registry.get(membershipAtom);
195 const before = harness.registry.get(listAtom);
196 expect(before).toHaveLength(2);
197 expect(before[0]).not.toBe(before[1]);
198 expect(before.map((thread) => thread.environmentId)).toEqual([
199 remoteEnvironmentId,
200 environmentId,
201 ]);
202​
203 harness.registry.set(harness.snapshotAtom(environmentId), {
204 ...snapshot,
205 threads: [v2ThreadShell, { ...otherThread, title: "Changed elsewhere" }],
206 });
207 expect(harness.registry.get(membershipAtom)).toBe(membership);
208 expect(harness.registry.get(listAtom)).toBe(before);
209​
210 harness.registry.set(harness.snapshotAtom(environmentId), {
211 ...snapshot,
212 threads: [{ ...v2ThreadShell, projectId: otherProjectId }, otherThread],
213 });
214 expect(harness.registry.get(listAtom)).toEqual([before[0]]);
215 harness.registry.set(harness.snapshotAtom(remoteEnvironmentId), {
216 ...v2ShellSnapshot,
217 threads: [],
218 });
219 expect(harness.registry.get(listAtom)).toEqual([]);
220 } finally {
221 disposeList();
222 harness.registry.dispose();
223 }
224 });
225});