- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/server/test/projected-queue.test.ts?format=json
189 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 164, 2 versions changed it
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { answer } from "../../worker/project/http.ts";
4import type { Project } from "../../worker/project/project.ts";
5import { DiskFirstRuntime } from "../src/disk-first-runtime.ts";
6import { memoryStorage } from "./helpers.ts";
8test("disk queue preserves a dropped base without restoring dropped changes", (t) => {
9 const storage = memoryStorage();
10 t.after(() => storage.db.close());
11 const runtime = () => new DiskFirstRuntime(storage, "queue-drop", "queue-drop");
12 runtime().initialize(
13 (c) => answer(c, "init", undefined, { files: { "avcs.json": '{"queue":true}', "a.txt": "before\n" } }),
14 () => {},
15 );
16 while (!runtime().preparePage(() => {}).ready) {}
17 runtime().write(
18 (c) => {
19 assert.equal(answer(c, "record", "alice", { files: { "a.txt": "temporary\n" } }).status, 200);
20 assert.equal(answer(c, "publish", "alice", { message: "Temporary" }).status, 200);
21 c.sync("old");
22 assert.equal(c.drop("alice", [1], "Undo temporary").ok, true);
23 },
24 () => {},
25 );
26 for (const [agent, file] of [
27 ["ahead", "ahead.txt"],
28 ["old", "old.txt"],
29 ] as const) {
30 runtime().write(
31 (c) => {
32 c.setCheckerReady(true);
33 assert.equal(answer(c, "record", agent, { files: { [file]: `${agent}\n` } }).status, 200);
34 const result = answer(c, "publish", agent, { message: agent });
35 assert.equal(result.status, 200, JSON.stringify(result));
36 assert.equal((result.body as { queued: number }).queued, agent === "ahead" ? 1 : 2);
37 },
38 () => {},
39 );
40 }
41 const candidate = runtime().read(
42 (c) => (c as unknown as { project: Project }).project.queueCandidate(2),
43 () => {},
44 );
45 assert.equal(candidate.files["a.txt"], "before\n");
46 assert.equal(candidate.files["ahead.txt"], "ahead\n");
47 assert.equal(candidate.files["old.txt"], "old\n");
48});
50test("disk runtime builds and lands a queued candidate", (t) => {
51 const storage = memoryStorage();
52 t.after(() => storage.db.close());
53 const runtime = () => new DiskFirstRuntime(storage, "queue", "queue");
54 runtime().initialize(
55 (c) => answer(c, "init", undefined, { files: { "avcs.json": '{"queue":true}', "a.txt": "before\n" } }),
56 () => {},
57 );
58 while (!runtime().preparePage(() => {}).ready) {}
59 runtime().write(
60 (c) => {
61 c.setCheckerReady(true);
62 assert.equal(answer(c, "record", "alice", { files: { "a.txt": "after\n" } }).status, 200);
63 const result = answer(c, "publish", "alice", { message: "Queued" });
64 assert.equal((result.body as { queued: number }).queued, 1);
65 },
66 () => {},
67 );
68 // Queue-driver methods are internal to the Project adapter, not public HTTP commands.
69 const candidate = runtime().read(
70 (c) => (c as unknown as { project: Project }).project.queueCandidate(1),
71 () => {},
72 );
73 assert.equal(candidate.files["a.txt"], "after\n");
74 const landed = runtime().write(
75 (c) => (c as unknown as { project: Project }).project.queueLand(1, 10),
76 () => {},
77 );
78 assert.deepEqual(landed.versions, [{ id: 1, version: 1 }]);
79 const empty = runtime().read(
80 (c) => (c as unknown as { project: Project }).project.queueCandidate(4),
81 () => {},
82 );
83 assert.deepEqual(empty.ids, []);
84 assert.deepEqual(empty.files, candidate.files);
85 assert.equal(empty.tree, candidate.tree);
86 assert.equal(
87 runtime().read(
88 (c) => c.shared().files["a.txt"],
89 () => {},
90 ),
91 "after\n",
92 );
93});
95test("disk runtime builds and lands two queued candidates in order", (t) => {
96 const storage = memoryStorage();
97 t.after(() => storage.db.close());
98 const runtime = () => new DiskFirstRuntime(storage, "queue", "queue");
99 runtime().initialize(
100 (c) => answer(c, "init", undefined, { files: { "avcs.json": '{"queue":true}', "a.txt": "before\n" } }),
101 () => {},
102 );
103 while (!runtime().preparePage(() => {}).ready) {}
104 runtime().write(
105 (c) => {
106 c.setCheckerReady(true);
107 assert.equal(answer(c, "record", "alice", { files: { "a.txt": "after\n" } }).status, 200);
108 const result = answer(c, "publish", "alice", { message: "Queued" });
109 assert.equal((result.body as { queued: number }).queued, 1);
110 },
111 () => {},
112 );
113 runtime().write(
114 (c) => {
115 c.setCheckerReady(true);
116 assert.equal(answer(c, "record", "bob", { files: { "b.txt": "second\n" } }).status, 200);
117 const result = answer(c, "publish", "bob", { message: "Second" });
118 assert.equal(result.status, 200, JSON.stringify(result));
119 assert.equal((result.body as { queued: number }).queued, 2);
120 },
121 () => {},
122 );
123 // Queue-driver methods are internal to the Project adapter, not public HTTP commands.
124 const candidate = runtime().read(
125 (c) => (c as unknown as { project: Project }).project.queueCandidate(2),
126 () => {},
127 );
128 assert.equal(candidate.files["a.txt"], "after\n");
129 assert.equal(candidate.files["b.txt"], "second\n");
130 const landed = runtime().write(
131 (c) => (c as unknown as { project: Project }).project.queueLand(2, 10),
132 () => {},
133 );
134 assert.deepEqual(landed.versions, [
135 { id: 1, version: 1 },
136 { id: 2, version: 2 },
137 ]);
138 assert.equal(
139 runtime().read(
140 (c) => c.shared().files["a.txt"],
141 () => {},
142 ),
143 "after\n",
144 );
145});
147test("disk queue refuses overlapping edits and accepts an older nonconflicting base", (t) => {
148 const storage = memoryStorage();
149 t.after(() => storage.db.close());
150 const runtime = () => new DiskFirstRuntime(storage, "queue-edge", "queue-edge");
151 runtime().initialize(
152 (c) => answer(c, "init", undefined, { files: { "avcs.json": '{"queue":true}', "a.txt": "before\n" } }),
153 () => {},
154 );
155 while (!runtime().preparePage(() => {}).ready) {}
156 const call = (op: string, agent: string, body: unknown) =>
157 runtime().write(
158 (c) => {
159 c.setCheckerReady(true);
160 return answer(c, op, agent, body);
161 },
162 () => {},
163 );
164 for (const agent of ["alice", "bob", "old"]) assert.equal(call("sync", agent, {}).status, 200);
165 assert.equal(call("record", "alice", { files: { "a.txt": "alice\n" } }).status, 200);
166 assert.equal((call("publish", "alice", { message: "Alice" }).body as { queued: number }).queued, 1);
167 assert.equal(call("record", "bob", { files: { "a.txt": "bob\n" } }).status, 200);
168 const refused = call("publish", "bob", { message: "Bob conflict" });
169 assert.equal(refused.status, 200, JSON.stringify(refused));
170 assert.equal((refused.body as { ok: boolean }).ok, false);
171 assert.ok((refused.body as { collisions: unknown[] }).collisions.length > 0);
172 runtime().write(
173 (c) => (c as unknown as { project: Project }).project.queueLand(1, 20),
174 () => {},
175 );
176 assert.equal(call("record", "alice", { files: { "next.txt": "next\n" } }).status, 200);
177 assert.equal((call("publish", "alice", { message: "Next" }).body as { queued: number }).queued, 2);
178 assert.equal(call("record", "old", { files: { "old.txt": "old base\n" } }).status, 200);
179 const queued = call("publish", "old", { message: "Old base" });
180 assert.equal(queued.status, 200, JSON.stringify(queued));
181 assert.equal((queued.body as { queued: number }).queued, 3);
182 const candidate = runtime().read(
183 (c) => (c as unknown as { project: Project }).project.queueCandidate(2),
184 () => {},
185 );
186 assert.equal(candidate.files["a.txt"], "alice\n");
187 assert.equal(candidate.files["next.txt"], "next\n");
188 assert.equal(candidate.files["old.txt"], "old base\n");
189});