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

threadShell.test.ts

200 lines7.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/unstable/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 },
38 ]),
39 ),
40 });
41 return {
42 registry: AtomRegistry.make(),
43 snapshotAtom,
44 catalogValueAtom,
45 threads: createEnvironmentThreadShellAtoms({ catalogValueAtom, snapshotAtom }),
46 };
48​
49describe("v2 thread shell lists", () => {
50 it("preserves ordered reference arrays when a middle thread changes", () => {
51 const { registry, threads, snapshotAtom } = makeHarness();
52 const snapshot = {
53 ...v2ShellSnapshot,
54 threads: ["a", "b", "c"].map((id) => ({ ...v2ThreadShell, id: ThreadId.make(id) })),
55 };
56 registry.set(snapshotAtom(environmentId), snapshot);
57 const dispose = registry.mount(threads.threadRefsAtom);
58 const before = registry.get(threads.threadRefsAtom);
59 registry.set(
60 snapshotAtom(environmentId),
61 applyShellStreamEvent(snapshot, {
62 kind: "thread.updated",
63 location: "active",
64 sequence: 1,
65 thread: { ...snapshot.threads[1]!, title: "Updated" },
66 }),
67 );
68 expect(registry.get(threads.threadRefsAtom)).toBe(before);
69 dispose();
70 registry.dispose();
71 });
72​
73 it("keeps navigation stable on hidden subagent updates and retains user forks", () => {
74 const { registry, threads, snapshotAtom } = makeHarness();
75 const root = v2ThreadShell;
76 const child = {
77 ...root,
78 id: ThreadId.make("child"),
79 lineage: {
80 ...root.lineage,
81 parentThreadId: root.id,
82 relationshipToParent: "subagent" as const,
83 },
84 };
85 const fork = {
86 ...child,
87 id: ThreadId.make("fork"),
88 lineage: { ...child.lineage, relationshipToParent: "fork" as const },
89 };
90 const snapshot = { ...v2ShellSnapshot, threads: [root, child, fork] };
91 registry.set(snapshotAtom(environmentId), snapshot);
92 const dispose = registry.mount(threads.navigationThreadShellsAtom);
93 const before = registry.get(threads.navigationThreadShellsAtom);
94 expect(before.map((thread) => thread.id)).toEqual([root.id, fork.id]);
95 registry.set(snapshotAtom(environmentId), {
96 ...snapshot,
97 threads: [root, { ...child, title: "Child streaming" }, fork],
98 });
99 expect(registry.get(threads.navigationThreadShellsAtom)).toBe(before);
100 expect(registry.get(threads.threadShellsAtom)).toHaveLength(3);
101 dispose();
102 registry.dispose();
103 });
104​
105 it("shares point and list values without retaining an atom for every listed thread", () => {
106 const harness = makeHarness();
107 const snapshot = {
108 ...v2ShellSnapshot,
109 threads: Array.from({ length: 200 }, (_, index) => ({
110 ...v2ThreadShell,
111 id: ThreadId.make(`thread-${index}`),
112 })),
113 };
114 harness.registry.set(harness.snapshotAtom(environmentId), snapshot);
115 const listAtom = harness.threads.threadShellsAtom;
116 const projectListAtom = harness.threads.threadShellsForProjectRefsAtom([
117 { environmentId, projectId: v2ThreadShell.projectId },
118 ]);
119 const disposeList = harness.registry.mount(listAtom);
120 const disposeProjectList = harness.registry.mount(projectListAtom);
121 try {
122 const before = harness.registry.get(listAtom);
123 expect(before).toHaveLength(200);
124 expect(harness.registry.get(projectListAtom)).toEqual(before);
125 expect(harness.registry.getNodes().size).toBeLessThan(20);
126 const firstAtom = harness.threads.threadShellAtom({
127 environmentId,
128 threadId: snapshot.threads[0]!.id,
129 });
130 expect(harness.registry.get(firstAtom)).toBe(before[0]);
131​
132 harness.registry.set(harness.snapshotAtom(environmentId), {
133 ...snapshot,
134 snapshotSequence: 1,
135 threads: snapshot.threads.map((thread, index) =>
136 index === 199 ? { ...thread, title: "Updated last thread" } : thread,
137 ),
138 });
139 const after = harness.registry.get(listAtom);
140 expect(after[0]).toBe(before[0]);
141 expect(after.at(-1)).not.toBe(before.at(-1));
142 expect(after.at(-1)?.title).toBe("Updated last thread");
143 expect(harness.registry.get(projectListAtom).at(-1)).toBe(after.at(-1));
144 expect(harness.registry.get(firstAtom)).toBe(after[0]);
145 expect(harness.registry.getNodes().size).toBeLessThan(20);
146 } finally {
147 disposeProjectList();
148 disposeList();
149 harness.registry.dispose();
150 }
151 });
152​
153 it("preserves project memberships on updates and keeps the same thread separate per environment", () => {
154 const harness = makeHarness([environmentId, remoteEnvironmentId]);
155 const otherThread = {
156 ...v2ThreadShell,
157 id: ThreadId.make("other-thread"),
158 projectId: otherProjectId,
159 };
160 const snapshot = { ...v2ShellSnapshot, threads: [v2ThreadShell, otherThread] };
161 harness.registry.set(harness.snapshotAtom(environmentId), snapshot);
162 const membershipAtom = harness.threads.environmentThreadRefsByProjectAtom(environmentId);
163 const listAtom = harness.threads.threadShellsForProjectRefsAtom([
164 { environmentId: remoteEnvironmentId, projectId: v2ThreadShell.projectId },
165 { environmentId, projectId: v2ThreadShell.projectId },
166 ]);
167 const disposeList = harness.registry.mount(listAtom);
168 try {
169 const membership = harness.registry.get(membershipAtom);
170 const before = harness.registry.get(listAtom);
171 expect(before).toHaveLength(2);
172 expect(before[0]).not.toBe(before[1]);
173 expect(before.map((thread) => thread.environmentId)).toEqual([
174 remoteEnvironmentId,
175 environmentId,
176 ]);
177​
178 harness.registry.set(harness.snapshotAtom(environmentId), {
179 ...snapshot,
180 threads: [v2ThreadShell, { ...otherThread, title: "Changed elsewhere" }],
181 });
182 expect(harness.registry.get(membershipAtom)).toBe(membership);
183 expect(harness.registry.get(listAtom)).toBe(before);
184​
185 harness.registry.set(harness.snapshotAtom(environmentId), {
186 ...snapshot,
187 threads: [{ ...v2ThreadShell, projectId: otherProjectId }, otherThread],
188 });
189 expect(harness.registry.get(listAtom)).toEqual([before[0]]);
190 harness.registry.set(harness.snapshotAtom(remoteEnvironmentId), {
191 ...v2ShellSnapshot,
192 threads: [],
193 });
194 expect(harness.registry.get(listAtom)).toEqual([]);
195 } finally {
196 disposeList();
197 harness.registry.dispose();
198 }
199 });
200});