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

threadShell.test.ts

144 lines5.4 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 { createEnvironmentThreadShellAtoms } from "./threadShell.ts";
14​
15const environmentId = EnvironmentId.make("environment-v2");
16const remoteEnvironmentId = EnvironmentId.make("remote-environment-v2");
17const otherProjectId = ProjectId.make("other-project");
18​
19function makeHarness(environmentIds: ReadonlyArray<EnvironmentId> = [environmentId]) {
20 const snapshotAtom = Atom.family((_environmentId: EnvironmentId) =>
21 Atom.make<OrchestrationV2ShellSnapshot | null>(v2ShellSnapshot),
22 );
23 const catalogValueAtom = Atom.make({
24 isReady: true,
25 entries: new Map(
26 environmentIds.map((id) => [
27 id,
28 {
29 target: new PrimaryConnectionTarget({
30 environmentId: id,
31 label: "Environment",
32 httpBaseUrl: "https://example.test",
33 wsBaseUrl: "wss://example.test",
34 }),
35 profile: Option.none(),
36 },
37 ]),
38 ),
39 });
40 return {
41 registry: AtomRegistry.make(),
42 snapshotAtom,
43 catalogValueAtom,
44 threads: createEnvironmentThreadShellAtoms({ catalogValueAtom, snapshotAtom }),
45 };
47​
48describe("v2 thread shell lists", () => {
49 it("shares point and list values without retaining an atom for every listed thread", () => {
50 const harness = makeHarness();
51 const snapshot = {
52 ...v2ShellSnapshot,
53 threads: Array.from({ length: 200 }, (_, index) => ({
54 ...v2ThreadShell,
55 id: ThreadId.make(`thread-${index}`),
56 })),
57 };
58 harness.registry.set(harness.snapshotAtom(environmentId), snapshot);
59 const listAtom = harness.threads.threadShellsAtom;
60 const projectListAtom = harness.threads.threadShellsForProjectRefsAtom([
61 { environmentId, projectId: v2ThreadShell.projectId },
62 ]);
63 const disposeList = harness.registry.mount(listAtom);
64 const disposeProjectList = harness.registry.mount(projectListAtom);
65 try {
66 const before = harness.registry.get(listAtom);
67 expect(before).toHaveLength(200);
68 expect(harness.registry.get(projectListAtom)).toEqual(before);
69 expect(harness.registry.getNodes().size).toBeLessThan(20);
70 const firstAtom = harness.threads.threadShellAtom({
71 environmentId,
72 threadId: snapshot.threads[0]!.id,
73 });
74 expect(harness.registry.get(firstAtom)).toBe(before[0]);
75​
76 harness.registry.set(harness.snapshotAtom(environmentId), {
77 ...snapshot,
78 snapshotSequence: 1,
79 threads: snapshot.threads.map((thread, index) =>
80 index === 199 ? { ...thread, title: "Updated last thread" } : thread,
81 ),
82 });
83 const after = harness.registry.get(listAtom);
84 expect(after[0]).toBe(before[0]);
85 expect(after.at(-1)).not.toBe(before.at(-1));
86 expect(after.at(-1)?.title).toBe("Updated last thread");
87 expect(harness.registry.get(projectListAtom).at(-1)).toBe(after.at(-1));
88 expect(harness.registry.get(firstAtom)).toBe(after[0]);
89 expect(harness.registry.getNodes().size).toBeLessThan(20);
90 } finally {
91 disposeProjectList();
92 disposeList();
93 harness.registry.dispose();
94 }
95 });
96​
97 it("preserves project memberships on updates and keeps the same thread separate per environment", () => {
98 const harness = makeHarness([environmentId, remoteEnvironmentId]);
99 const otherThread = {
100 ...v2ThreadShell,
101 id: ThreadId.make("other-thread"),
102 projectId: otherProjectId,
103 };
104 const snapshot = { ...v2ShellSnapshot, threads: [v2ThreadShell, otherThread] };
105 harness.registry.set(harness.snapshotAtom(environmentId), snapshot);
106 const membershipAtom = harness.threads.environmentThreadRefsByProjectAtom(environmentId);
107 const listAtom = harness.threads.threadShellsForProjectRefsAtom([
108 { environmentId: remoteEnvironmentId, projectId: v2ThreadShell.projectId },
109 { environmentId, projectId: v2ThreadShell.projectId },
110 ]);
111 const disposeList = harness.registry.mount(listAtom);
112 try {
113 const membership = harness.registry.get(membershipAtom);
114 const before = harness.registry.get(listAtom);
115 expect(before).toHaveLength(2);
116 expect(before[0]).not.toBe(before[1]);
117 expect(before.map((thread) => thread.environmentId)).toEqual([
118 remoteEnvironmentId,
119 environmentId,
120 ]);
121​
122 harness.registry.set(harness.snapshotAtom(environmentId), {
123 ...snapshot,
124 threads: [v2ThreadShell, { ...otherThread, title: "Changed elsewhere" }],
125 });
126 expect(harness.registry.get(membershipAtom)).toBe(membership);
127 expect(harness.registry.get(listAtom)).toBe(before);
128​
129 harness.registry.set(harness.snapshotAtom(environmentId), {
130 ...snapshot,
131 threads: [{ ...v2ThreadShell, projectId: otherProjectId }, otherThread],
132 });
133 expect(harness.registry.get(listAtom)).toEqual([before[0]]);
134 harness.registry.set(harness.snapshotAtom(remoteEnvironmentId), {
135 ...v2ShellSnapshot,
136 threads: [],
137 });
138 expect(harness.registry.get(listAtom)).toEqual([]);
139 } finally {
140 disposeList();
141 harness.registry.dispose();
142 }
143 });
144});