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

threadShell.test.ts

201 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/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("shares point and list values without retaining an atom for every listed thread", () => {
107 const harness = makeHarness();
108 const snapshot = {
109 ...v2ShellSnapshot,
110 threads: Array.from({ length: 200 }, (_, index) => ({
111 ...v2ThreadShell,
112 id: ThreadId.make(`thread-${index}`),
113 })),
114 };
115 harness.registry.set(harness.snapshotAtom(environmentId), snapshot);
116 const listAtom = harness.threads.threadShellsAtom;
117 const projectListAtom = harness.threads.threadShellsForProjectRefsAtom([
118 { environmentId, projectId: v2ThreadShell.projectId },
119 ]);
120 const disposeList = harness.registry.mount(listAtom);
121 const disposeProjectList = harness.registry.mount(projectListAtom);
122 try {
123 const before = harness.registry.get(listAtom);
124 expect(before).toHaveLength(200);
125 expect(harness.registry.get(projectListAtom)).toEqual(before);
126 expect(harness.registry.getNodes().size).toBeLessThan(20);
127 const firstAtom = harness.threads.threadShellAtom({
128 environmentId,
129 threadId: snapshot.threads[0]!.id,
130 });
131 expect(harness.registry.get(firstAtom)).toBe(before[0]);
132​
133 harness.registry.set(harness.snapshotAtom(environmentId), {
134 ...snapshot,
135 snapshotSequence: 1,
136 threads: snapshot.threads.map((thread, index) =>
137 index === 199 ? { ...thread, title: "Updated last thread" } : thread,
138 ),
139 });
140 const after = harness.registry.get(listAtom);
141 expect(after[0]).toBe(before[0]);
142 expect(after.at(-1)).not.toBe(before.at(-1));
143 expect(after.at(-1)?.title).toBe("Updated last thread");
144 expect(harness.registry.get(projectListAtom).at(-1)).toBe(after.at(-1));
145 expect(harness.registry.get(firstAtom)).toBe(after[0]);
146 expect(harness.registry.getNodes().size).toBeLessThan(20);
147 } finally {
148 disposeProjectList();
149 disposeList();
150 harness.registry.dispose();
151 }
152 });
153​
154 it("preserves project memberships on updates and keeps the same thread separate per environment", () => {
155 const harness = makeHarness([environmentId, remoteEnvironmentId]);
156 const otherThread = {
157 ...v2ThreadShell,
158 id: ThreadId.make("other-thread"),
159 projectId: otherProjectId,
160 };
161 const snapshot = { ...v2ShellSnapshot, threads: [v2ThreadShell, otherThread] };
162 harness.registry.set(harness.snapshotAtom(environmentId), snapshot);
163 const membershipAtom = harness.threads.environmentThreadRefsByProjectAtom(environmentId);
164 const listAtom = harness.threads.threadShellsForProjectRefsAtom([
165 { environmentId: remoteEnvironmentId, projectId: v2ThreadShell.projectId },
166 { environmentId, projectId: v2ThreadShell.projectId },
167 ]);
168 const disposeList = harness.registry.mount(listAtom);
169 try {
170 const membership = harness.registry.get(membershipAtom);
171 const before = harness.registry.get(listAtom);
172 expect(before).toHaveLength(2);
173 expect(before[0]).not.toBe(before[1]);
174 expect(before.map((thread) => thread.environmentId)).toEqual([
175 remoteEnvironmentId,
176 environmentId,
177 ]);
178​
179 harness.registry.set(harness.snapshotAtom(environmentId), {
180 ...snapshot,
181 threads: [v2ThreadShell, { ...otherThread, title: "Changed elsewhere" }],
182 });
183 expect(harness.registry.get(membershipAtom)).toBe(membership);
184 expect(harness.registry.get(listAtom)).toBe(before);
185​
186 harness.registry.set(harness.snapshotAtom(environmentId), {
187 ...snapshot,
188 threads: [{ ...v2ThreadShell, projectId: otherProjectId }, otherThread],
189 });
190 expect(harness.registry.get(listAtom)).toEqual([before[0]]);
191 harness.registry.set(harness.snapshotAtom(remoteEnvironmentId), {
192 ...v2ShellSnapshot,
193 threads: [],
194 });
195 expect(harness.registry.get(listAtom)).toEqual([]);
196 } finally {
197 disposeList();
198 harness.registry.dispose();
199 }
200 });
201});