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

queuedRunEdit.test.ts

274 lines9.5 KB

1import { RunId } from "@t3tools/contracts";
2import { Atom, AtomRegistry } from "effect/reactivity";
3import { describe, expect, it } from "vite-plus/test";
4​
5import {
6 makeQueuedRunEditStore,
7 makeQueuedRunsAtom,
8 resolveQueuedEditExit,
9 type QueuedRunStatus,
10} from "./queuedRunEdit.ts";
11​
12const RUN = RunId.make("run-1");
13const queued = (status = "queued"): ReadonlyArray<QueuedRunStatus> => [{ id: RUN, status }];
14​
15function setup(initialRuns: ReadonlyArray<QueuedRunStatus> | null = queued()) {
16 const registry = AtomRegistry.make();
17 const runs = Atom.make<ReadonlyArray<QueuedRunStatus> | null>(initialRuns);
18 const store = makeQueuedRunEditStore<{ readonly runId: RunId; readonly text: string }>({
19 registry,
20 label: "test-queued-run-edits",
21 });
22 const exits: Array<{ threadKey: string; text: string }> = [];
23 const begin = () =>
24 store.begin(
25 "env:thread",
26 { runId: RUN, text: "draft" },
27 { runs, onRunLeftQueue: (threadKey, edit) => exits.push({ threadKey, text: edit.text }) },
28 );
29 return { registry, runs, store, exits, begin };
31​
32describe("resolveQueuedEditExit", () => {
33 it("closes an unchanged edit without touching drafts", () => {
34 expect(resolveQueuedEditExit({ editChanged: false, threadDraftEmpty: true })).toBe("clean");
35 });
36​
37 it("keeps a changed edit only when the thread draft is empty", () => {
38 expect(resolveQueuedEditExit({ editChanged: true, threadDraftEmpty: true })).toBe("kept");
39 expect(resolveQueuedEditExit({ editChanged: true, threadDraftEmpty: false })).toBe("discarded");
40 });
41});
42​
43describe("makeQueuedRunEditStore", () => {
44 it("fires once when the edited run leaves the queue, with the latest edit", () => {
45 const { registry, runs, store, exits, begin } = setup();
46 begin();
47 store.update("env:thread", (edit) => ({ ...edit, text: "changed" }));
48 registry.set(runs, queued());
49 expect(exits).toEqual([]);
50 registry.set(runs, queued("running"));
51 registry.set(runs, []);
52 expect(exits).toEqual([{ threadKey: "env:thread", text: "changed" }]);
53 });
54​
55 it("waits while the runs are unknown and treats a vanished run as left", () => {
56 const { registry, runs, exits, begin } = setup(null);
57 begin();
58 expect(exits).toEqual([]);
59 registry.set(runs, []);
60 expect(exits).toHaveLength(1);
61 });
62​
63 it("fires immediately when the run already left the queue", () => {
64 const { exits, begin } = setup(queued("running"));
65 begin();
66 expect(exits).toHaveLength(1);
67 });
68​
69 it("stops watching when the edit ends", () => {
70 const { registry, runs, store, exits, begin } = setup();
71 begin();
72 store.end("env:thread");
73 registry.set(runs, []);
74 expect(exits).toEqual([]);
75 expect(store.get("env:thread")).toBeNull();
76 });
77​
78 it("holds the exit during a save and replays it when the save fails", () => {
79 const { registry, runs, store, exits, begin } = setup();
80 begin();
81 const finishSave = store.startSave("env:thread");
82 registry.set(runs, []);
83 expect(exits).toEqual([]);
84 expect(finishSave?.(false)).toBe(true);
85 expect(exits).toHaveLength(1);
86 });
87​
88 it("allows one save at a time and ends the edit when it succeeds", () => {
89 const { registry, store, begin } = setup();
90 begin();
91 const finishSave = store.startSave("env:thread");
92 expect(registry.get(store.savingAtom)).toEqual({ "env:thread": true });
93 expect(store.startSave("env:thread")).toBeNull();
94 expect(finishSave?.(true)).toBe(true);
95 expect(store.get("env:thread")).toBeNull();
96 expect(registry.get(store.savingAtom)).toEqual({});
97 });
98​
99 it("leaves an edit begun again during a save to that edit", () => {
100 const { registry, runs, store, exits, begin } = setup();
101 begin();
102 const finishStale = store.startSave("env:thread");
103 begin();
104 store.update("env:thread", (edit) => ({ ...edit, text: "newer" }));
105​
106 expect(finishStale?.(true)).toBe(false);
107 expect(finishStale?.(false)).toBe(false);
108 expect(store.get("env:thread")).toEqual({ runId: RUN, text: "newer" });
109 registry.set(runs, []);
110 expect(exits).toEqual([{ threadKey: "env:thread", text: "newer" }]);
111 });
112​
113 // The server orders saves by arrival, not by edit age, so a second save could
114 // land before the first and then be overwritten by it.
115 it("keeps the save claimed until it settles, even when the edit is begun again or ended", () => {
116 const { registry, store, begin } = setup();
117 begin();
118 const finishStale = store.startSave("env:thread");
119 begin();
120 expect(registry.get(store.savingAtom)).toEqual({ "env:thread": true });
121 expect(store.startSave("env:thread")).toBeNull();
122 store.end("env:thread");
123 begin();
124 expect(store.startSave("env:thread")).toBeNull();
125​
126 expect(finishStale?.(true)).toBe(false);
127 expect(registry.get(store.savingAtom)).toEqual({});
128 expect(finishStale?.(false)).toBe(false);
129 const finishNewer = store.startSave("env:thread");
130 expect(finishNewer?.(true)).toBe(true);
131 expect(store.get("env:thread")).toBeNull();
132 });
133​
134 it("does not revive a cancelled edit when its save fails", () => {
135 const { registry, runs, store, exits, begin } = setup();
136 begin();
137 const finishSave = store.startSave("env:thread");
138 store.end("env:thread");
139 expect(finishSave?.(false)).toBe(false);
140 registry.set(runs, []);
141 expect(exits).toEqual([]);
142 expect(registry.get(store.savingAtom)).toEqual({});
143 });
144​
145 // The runs read the thread's detail, a live server subscription; a thread
146 // with no edit open must not keep it.
147 it("holds the thread's runs only while an edit is open and watching", () => {
148 // The registry removes idle nodes in a scheduled task; the test runs those tasks itself.
149 const tasks: Array<() => void> = [];
150 const registry = AtomRegistry.make({
151 scheduleTask: (task) => {
152 tasks.push(task);
153 return () => {};
154 },
155 });
156 const settle = () => {
157 for (const task of tasks.splice(0)) task();
158 };
159 const source = Atom.make<ReadonlyArray<QueuedRunStatus> | null>(queued());
160 let mounted = 0;
161 const runs = Atom.make((get) => {
162 mounted += 1;
163 get.addFinalizer(() => {
164 mounted -= 1;
165 });
166 return get(source);
167 });
168 const store = makeQueuedRunEditStore<{ readonly runId: RunId }>({
169 registry,
170 label: "test-queued-run-edits",
171 });
172 const begin = () =>
173 store.begin("env:thread", { runId: RUN }, { runs, onRunLeftQueue: () => {} });
174​
175 begin();
176 expect(mounted).toBe(1);
177 store.end("env:thread");
178 settle();
179 expect(mounted).toBe(0);
180​
181 begin();
182 const finishSave = store.startSave("env:thread");
183 settle();
184 expect(mounted).toBe(0);
185 finishSave?.(false);
186 expect(mounted).toBe(1);
187​
188 registry.set(source, []);
189 expect(store.get("env:thread")).toBeNull();
190 settle();
191 expect(mounted).toBe(0);
192 });
193});
194​
195describe("makeQueuedRunsAtom", () => {
196 const thread = (overrides: { archivedAt?: string | null; deletedAt?: string | null } = {}) => ({
197 id: "thread-1",
198 archivedAt: null,
199 deletedAt: null,
200 ...overrides,
201 });
202​
203 function setupWatched() {
204 const registry = AtomRegistry.make();
205 const snapshot = Atom.make<{ threads: ReadonlyArray<ReturnType<typeof thread>> } | null>({
206 threads: [thread()],
207 });
208 let detailReads = 0;
209 const detailStatus = Atom.make("live");
210 const detail = Atom.make<ReadonlyArray<QueuedRunStatus> | null>(queued());
211 const runs = makeQueuedRunsAtom({
212 threadId: "thread-1",
213 snapshot,
214 detailStatus,
215 runs: Atom.make((get) => {
216 detailReads += 1;
217 return get(detail);
218 }),
219 });
220 const store = makeQueuedRunEditStore<{ readonly runId: RunId }>({
221 registry,
222 label: "test-queued-run-edits",
223 });
224 const exits: Array<string> = [];
225 store.begin("env:thread", { runId: RUN }, { runs, onRunLeftQueue: (key) => exits.push(key) });
226 return {
227 registry,
228 snapshot,
229 detailStatus,
230 detail,
231 store,
232 exits,
233 detailReads: () => detailReads,
234 };
235 }
236​
237 it("ends the edit when the shell marks the thread deleted, without reading its detail again", () => {
238 const { registry, snapshot, detail, exits, detailReads } = setupWatched();
239 expect(exits).toEqual([]);
240 registry.set(snapshot, { threads: [thread({ deletedAt: "2026-10-24T00:00:00Z" })] });
241 expect(exits).toEqual(["env:thread"]);
242 const reads = detailReads();
243 registry.set(detail, []);
244 expect(detailReads()).toBe(reads);
245 });
246​
247 it("ends the edit when the detail reports the thread gone", () => {
248 const { registry, snapshot, detailStatus, exits } = setupWatched();
249 registry.set(snapshot, { threads: [] });
250 expect(exits).toEqual([]);
251 registry.set(detailStatus, "deleted");
252 expect(exits).toEqual(["env:thread"]);
253 });
254​
255 it("keeps an archived thread's edit open, ending it only when the run leaves the queue", () => {
256 const { registry, snapshot, detail, store, exits } = setupWatched();
257 // The shell drops archived threads, but their queue stays editable.
258 registry.set(snapshot, { threads: [] });
259 registry.set(snapshot, { threads: [thread({ archivedAt: "2026-10-24T00:00:00Z" })] });
260 expect(exits).toEqual([]);
261 expect(store.get("env:thread")).not.toBeNull();
262 registry.set(detail, queued("running"));
263 expect(exits).toEqual(["env:thread"]);
264 });
265​
266 it("waits while the thread detail is unknown", () => {
267 const { registry, snapshot, detail, exits } = setupWatched();
268 registry.set(snapshot, null);
269 registry.set(detail, null);
270 expect(exits).toEqual([]);
271 registry.set(detail, []);
272 expect(exits).toEqual(["env:thread"]);
273 });
274});