Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
blame on this site's /mcp
CLI
abso blame src/engine/test/moves.test.ts --json
JSON
/christopher/abso-core/blame/src/engine/test/moves.test.ts?format=json
364 linesFirst import 94% · codex-capacity-chair 5% · pinch 1%Last change codex-capacity-chair in version 126,

364 lines from 3 changes in 3 versions.

init0Start the project1// Moves (#28): a line is its id, not its place. A move gives lines a new
2// parent, possibly in another file, and edits name ids, so an edit made
3// before or after a move follows the moved lines.
4// PROP_SEED=<seed> PROP_CASES=1 node --test src/engine/test/moves.test.ts
5
6import assert from "node:assert/strict";
7import { describe, it } from "node:test";
codex-capacity-chair126Identify suppressed relocation cycles across persistent readsCompare8import { type Atom, type AtomId, conflicts, Graph, View } from "../index.ts";
9import { StableOverlaySet } from "../state-source.ts";
init0Start the project10import { lines, Repo, Rng } from "./helpers.ts";
codex-capacity-chair126Identify suppressed relocation cycles across persistent readsCompare11import { SqlState } from "./storage-helper.ts";
init0Start the project12
pinch78Biome is silent: about 90 hints cleared. String joins as templates, dotted keys, unused imports, variables and a private method removed, optional chains (one kept boolean with === true), the preset field instead of the deprecated recommended. Two rules off where they don't fit: noExplicitAny in the hook modules, whose engine types come from the host, and noTemplateCurlyInString in tests, which hold source text as data.Compare13const BASE_SEED = Number(process.env.PROP_SEED ?? 20261004);
14const CASES = Number(process.env.PROP_CASES ?? 200);
init0Start the project15
16const FN = ["function area(r) {", " const pi = 3.14;", " return pi * r * r;", "}"];
17const START = {
18 "shapes.js": lines("// shapes", ...FN, "", "function other() {", " return 1;", "}"),
19 "geometry.js": lines("// geometry", "export {};"),
20};
21/** shapes.js with area() moved out. */
22const SHAPES_AFTER = lines("// shapes", "", "function other() {", " return 1;", "}");
24/** Move area() (shapes.js lines 1-4) to the end of geometry.js. */
25function moveArea(repo: Repo, against?: Iterable<AtomId>): Atom {
26 return repo.draft({ move: { file: "shapes.js", start: 1, end: 5, to: "geometry.js", at: 2 } }, against);
29function accept(repo: Repo, atom: Atom): Atom {
30 repo.graph.add(atom);
31 repo.shared.add(atom.id);
32 return atom;
35/** Two atoms built against the same base, both accepted: the merge. */
36function concurrently(
37 repo: Repo,
38 a: (base: AtomId[]) => Atom,
39 b: (base: AtomId[]) => Atom,
40): { first: Atom; second: Atom } {
41 const base = [...repo.shared];
42 const first = accept(repo, a(base));
43 const second = accept(repo, b(base));
44 return { first, second };
47describe("moves", () => {
48 it("moves lines to another file, keeping their ids", () => {
49 const repo = Repo.init(START);
50 const before = repo
51 .view()
52 .lines("shapes.js")
53 .slice(1, 5)
54 .map((l) => l.id);
55 accept(repo, moveArea(repo));
56 assert.equal(repo.text("shapes.js"), SHAPES_AFTER);
57 assert.equal(repo.text("geometry.js"), lines("// geometry", "export {};", ...FN));
58 const view = repo.view();
59 assert.deepEqual(
60 view
61 .lines("geometry.js")
62 .slice(2)
63 .map((l) => l.id),
64 before,
65 );
66 assert.equal(view.fileOf(before[2]!), "geometry.js");
67 });
69 it("an edit made before the move, to the body, the first line or the last line, follows the moved lines", () => {
70 for (const [old, now] of [
71 [" return pi * r * r;", " return pi * r ** 2;"],
72 ["function area(r) {", "function area(r = 1) {"],
73 ["}\n\nfunction other", "} // area\n\nfunction other"],
74 ] as const) {
75 const repo = Repo.init(START);
76 concurrently(
77 repo,
78 (base) => repo.draft({ edits: [{ file: "shapes.js", old, new: now }] }, base),
79 (base) => moveArea(repo, base),
80 );
81 assert.deepEqual(conflicts(repo.graph, repo.shared), [], old);
82 assert.equal(repo.text("shapes.js"), SHAPES_AFTER, old);
83 const fn = FN.map((l) => (l === old.split("\n")[0] ? now.split("\n")[0]! : l));
84 assert.equal(repo.text("geometry.js"), lines("// geometry", "export {};", ...fn), old);
85 }
86 });
88 it("an edit made after the move, against a view without it, follows too (either order is the same set)", () => {
89 const repo = Repo.init(START);
90 concurrently(
91 repo,
92 (base) => moveArea(repo, base),
93 (base) =>
94 repo.draft({ edits: [{ file: "shapes.js", old: " const pi = 3.14;", new: " const pi = Math.PI;" }] }, base),
95 );
96 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
97 assert.equal(
98 repo.text("geometry.js"),
99 lines("// geometry", "export {};", "function area(r) {", " const pi = Math.PI;", " return pi * r * r;", "}"),
100 );
101 });
103 it("lines inserted inside the moved block go with it", () => {
104 const repo = Repo.init(START);
105 concurrently(
106 repo,
107 (base) =>
108 repo.draft(
109 {
110 edits: [
111 { file: "shapes.js", old: " const pi = 3.14;\n", new: " const pi = 3.14;\n // area of a circle\n" },
112 ],
113 },
114 base,
115 ),
116 (base) => moveArea(repo, base),
117 );
118 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
119 assert.equal(repo.text("shapes.js"), SHAPES_AFTER);
120 assert.match(repo.text("geometry.js"), /const pi = 3\.14;\n {2}\/\/ area of a circle\n/);
121 });
123 it("inserting just above the moved block collides with the move: it is unclear which place the line meant", () => {
124 const repo = Repo.init(START);
125 concurrently(
126 repo,
127 (base) =>
128 repo.draft({ edits: [{ file: "shapes.js", start: 1, end: 1, lines: ["/** The area of a circle. */"] }] }, base),
129 (base) => moveArea(repo, base),
130 );
131 assert.equal(conflicts(repo.graph, repo.shared).length, 1);
132 });
134 it("a line deleted at its old place is deleted at its new one, with no collision", () => {
135 const repo = Repo.init(START);
136 concurrently(
137 repo,
pinch78Biome is silent: about 90 hints cleared. String joins as templates, dotted keys, unused imports, variables and a private method removed, optional chains (one kept boolean with === true), the preset field instead of the deprecated recommended. Two rules off where they don't fit: noExplicitAny in the hook modules, whose engine types come from the host, and noTemplateCurlyInString in tests, which hold source text as data.Compare138 (base) => repo.draft({ edits: [{ file: "shapes.js", old: `${FN.join("\n")}\n`, new: "" }] }, base),
init0Start the project139 (base) => moveArea(repo, base),
140 );
141 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
142 assert.equal(repo.text("shapes.js"), SHAPES_AFTER);
143 assert.equal(repo.text("geometry.js"), lines("// geometry", "export {};"));
144 });
146 it("two moves of the same lines collide", () => {
147 const repo = Repo.init(START);
148 concurrently(
149 repo,
150 (base) => moveArea(repo, base),
151 (base) => repo.draft({ move: { file: "shapes.js", start: 1, end: 5, to: "shapes.js", at: 9 } }, base),
152 );
153 const found = conflicts(repo.graph, repo.shared);
154 assert.equal(found.length, 1);
155 assert.ok(found[0]!.keys.some((k) => k.key.startsWith("P:")));
156 });
158 it("a whole file renamed: every line moves, and an edit to it lands in the new file", () => {
159 const repo = Repo.init(START);
160 concurrently(
161 repo,
162 (base) => repo.draft({ edits: [{ file: "shapes.js", old: " return 1;", new: " return 2;" }] }, base),
163 (base) => repo.draft({ move: { file: "shapes.js", start: 0, end: 9, to: "circle.js", at: 0 } }, base),
164 );
165 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
166 const view = repo.view();
167 assert.equal(view.hasFile("shapes.js"), false);
168 assert.equal(view.text("circle.js"), START["shapes.js"].replace("return 1;", "return 2;"));
169 assert.deepEqual(view.paths(), ["circle.js", "geometry.js"]);
170 });
172 it("a block moved within a file, up or down, with a concurrent edit inside it", () => {
173 for (const at of [0, 9]) {
174 const repo = Repo.init(START);
175 concurrently(
176 repo,
177 (base) =>
178 repo.draft(
179 { edits: [{ file: "shapes.js", old: " return pi * r * r;", new: " return pi * r * r; // m^2" }] },
180 base,
181 ),
182 (base) => repo.draft({ move: { file: "shapes.js", start: 1, end: 5, to: "shapes.js", at } }, base),
183 );
184 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
185 const fn = lines(...FN).replace("r * r;", "r * r; // m^2");
186 assert.equal(repo.text("shapes.js"), at === 0 ? fn + SHAPES_AFTER : SHAPES_AFTER + fn);
187 }
188 });
190 it("a file split in two: edits to either part, made apart, land in the right file", () => {
191 const repo = Repo.init(START);
192 const base = [...repo.shared];
193 accept(repo, repo.draft({ move: { file: "shapes.js", start: 5, end: 9, to: "other.js", at: 0 } }, base));
194 accept(repo, repo.draft({ edits: [{ file: "shapes.js", old: "3.14", new: "Math.PI" }] }, base));
195 accept(repo, repo.draft({ edits: [{ file: "shapes.js", old: "return 1;", new: "return 2;" }] }, base));
196 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
197 assert.equal(repo.text("shapes.js"), lines("// shapes", ...FN).replace("3.14", "Math.PI"));
198 assert.equal(repo.text("other.js"), lines("", "function other() {", " return 2;", "}"));
199 });
201 it("two files merged into one: the second file's lines move under the end of the first, and edits follow", () => {
202 const repo = Repo.init(START);
203 const base = [...repo.shared];
204 accept(repo, repo.draft({ move: { file: "geometry.js", start: 0, end: 2, to: "shapes.js", at: 9 } }, base));
205 accept(repo, repo.draft({ edits: [{ file: "geometry.js", old: "export {};", new: "export { area };" }] }, base));
206 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
207 assert.equal(repo.view().hasFile("geometry.js"), false);
208 assert.equal(repo.text("shapes.js"), START["shapes.js"] + lines("// geometry", "export { area };"));
209 });
211 it("moves stack: a moved block can move again, and a later edit follows both", () => {
212 const repo = Repo.init(START);
213 accept(repo, moveArea(repo));
214 repo.edit({ move: { file: "geometry.js", start: 2, end: 6, to: "circle.js", at: 0 } });
215 repo.edit({ edits: [{ file: "circle.js", old: "3.14", new: "3.14159" }] });
216 assert.equal(repo.text("circle.js"), lines(...FN).replace("3.14", "3.14159"));
217 assert.equal(repo.text("geometry.js"), lines("// geometry", "export {};"));
218 assert.equal(repo.text("shapes.js"), SHAPES_AFTER);
219 });
221 it("moves that loop lines into each other lose no lines", () => {
222 // Hand-made: two moves made apart, each putting one line under the other.
223 const repo = Repo.init({ "f.txt": lines("x", "m", "y", "n") });
224 const mk = (line: string, parent: string): Atom => {
225 const id = repo.graph.nextId();
226 return {
227 id,
228 seq: repo.graph.size,
229 author: "a",
230 message: "loop",
231 time: 0,
232 inserts: [],
233 deletes: [],
234 writes: [`G:${parent}`, `P:${line}`],
235 reads: [],
236 holds: [],
237 needs: [],
238 resolves: [],
239 reasons: [],
240 deps: ["a0"],
241 moves: [{ line, parent }],
242 };
243 };
244 accept(repo, mk("a0.0", "a0.2"));
245 accept(repo, mk("a0.2", "a0.0"));
246 const text = repo.text("f.txt");
247 assert.deepEqual(text.split("\n").filter(Boolean).sort(), ["m", "n", "x", "y"]);
codex-capacity-chair126Identify suppressed relocation cycles across persistent readsCompare248 class BoundedReads extends SqlState {
249 override read(collection: string, key: string): unknown {
250 if (this.reads > 10_000) throw new Error("relocation cycle did not terminate");
251 return super.read(collection, key);
252 }
253 }
254 const storage = new BoundedReads();
255 try {
256 const stored = new Graph({ storage });
257 for (let seq = 0; seq < repo.graph.size; seq++) stored.add(repo.graph.atom(repo.graph.idOfSeq(seq)));
258 const reopened = new Graph({ storage });
259 assert.equal(new View(reopened, repo.shared).text("f.txt"), text);
260 assert.equal(new View(reopened, new StableOverlaySet(repo.shared)).text("f.txt"), text);
261 } finally {
262 storage.db.close();
263 }
init0Start the project264 });
266 it("rejects a move onto itself or out of range", () => {
267 const repo = Repo.init(START);
268 assert.throws(() => repo.draft({ move: { file: "shapes.js", start: 1, end: 5, to: "shapes.js", at: 3 } }), {
269 code: "bad-range",
270 });
271 assert.throws(() => repo.draft({ move: { file: "shapes.js", start: 1, end: 5, to: "shapes.js", at: 5 } }), {
272 code: "bad-range",
273 });
274 assert.throws(() => repo.draft({ move: { file: "shapes.js", start: 4, end: 4, to: "x.js", at: 0 } }), {
275 code: "bad-range",
276 });
277 assert.throws(() => repo.draft({ move: { file: "shapes.js", start: 1, end: 2, to: "geometry.js", at: 3 } }), {
278 code: "bad-range",
279 });
280 assert.throws(
281 () =>
282 repo.draft({
283 move: { file: "shapes.js", start: 1, end: 2, to: "x.js", at: 0 },
284 edits: [{ file: "geometry.js", old: "export", new: "x" }],
285 }),
286 { code: "bad-edit" },
287 );
288 });
289});
291describe("moves, property", () => {
292 const word = (rng: Rng): string =>
293 rng.pick(["a", "b", "c", "x", "y", "}", "", "let q = 1;", "return z;"]) +
294 (rng.chance(0.5) ? String(rng.int(9)) : "");
296 it("random edits and moves, one after another, agree with editing plain text", () => {
297 for (let c = 0; c < CASES; c++) {
298 const seed = BASE_SEED + c;
299 const rng = new Rng(seed);
300 const names = ["a.txt", "b.txt", "c.txt"];
301 const plain: Record<string, string[]> = {};
302 const files: Record<string, string> = {};
303 for (const f of names.slice(0, 2)) {
304 plain[f] = Array.from({ length: rng.range(1, 8) }, () => word(rng));
305 files[f] = lines(...plain[f]!);
306 }
307 plain["c.txt"] = [];
308 const repo = Repo.init(files);
309 for (let step = 0; step < 12; step++) {
310 const from = rng.pick(names.filter((f) => plain[f]!.length > 0));
311 const n = plain[from]!.length;
312 if (rng.chance(0.5)) {
313 const start = rng.int(n);
314 const end = rng.range(start + 1, n);
315 const to = rng.pick(names);
316 const len = plain[to]!.length;
317 let at = rng.int(len + 1);
318 if (to === from && at >= start && at <= end)
319 at = start > 0 ? rng.int(start) : end < len ? rng.range(end + 1, len) : -1;
320 if (at < 0) continue;
321 repo.edit({ move: { file: from, start, end, to, at } });
322 const block = plain[from]!.splice(start, end - start);
323 const where = to === from && at > end ? at - block.length : at;
324 plain[to]!.splice(where, 0, ...block);
325 } else {
326 const start = rng.int(n + 1);
327 const end = rng.range(start, Math.min(n, start + 2));
328 const add = Array.from({ length: rng.int(3) }, () => word(rng));
329 if (end - start === add.length && plain[from]!.slice(start, end).every((l, i) => l === add[i])) continue;
330 repo.edit({ edits: [{ file: from, start, end, lines: add }] });
331 plain[from]!.splice(start, end - start, ...add);
332 }
333 for (const f of names) assert.equal(repo.text(f), lines(...plain[f]!), `seed ${seed}, step ${step}, ${f}`);
334 }
335 }
336 });
338 it("a move and an edit inside the moved block, made apart, compose: the edited block lands at the new place", () => {
339 for (let c = 0; c < CASES; c++) {
340 const seed = BASE_SEED + c;
341 const rng = new Rng(seed);
342 const a = Array.from({ length: rng.range(3, 10) }, (_, i) => `a${i}`);
343 const b = Array.from({ length: rng.range(0, 4) }, (_, i) => `b${i}`);
344 const repo = Repo.init(b.length ? { "a.txt": lines(...a), "b.txt": lines(...b) } : { "a.txt": lines(...a) });
345 const start = rng.int(a.length);
346 const end = rng.range(start + 1, a.length);
347 const at = rng.int(b.length + 1);
348 // An edit that replaces at least one line of the block, staying inside it.
349 const es = rng.range(start, end - 1);
350 const ee = rng.range(es + 1, end);
351 const add = Array.from({ length: rng.int(3) }, (_, i) => `new${i}`);
352 concurrently(
353 repo,
354 (base) => repo.draft({ move: { file: "a.txt", start, end, to: "b.txt", at } }, base),
355 (base) => repo.draft({ edits: [{ file: "a.txt", start: es, end: ee, lines: add }] }, base),
356 );
357 const msg = `seed ${seed}`;
358 assert.deepEqual(conflicts(repo.graph, repo.shared), [], msg);
359 const block = [...a.slice(start, es), ...add, ...a.slice(ee, end)];
360 assert.equal(repo.text("a.txt"), lines(...a.slice(0, start), ...a.slice(end)), msg);
361 assert.equal(repo.text("b.txt"), lines(...b.slice(0, at), ...block, ...b.slice(at)), msg);
362 }
363 });
364});