- MCP
- blame on this site's /mcp
- CLI
- abso blame src/engine/test/moves.test.ts --version 78 --json
- JSON
- /christopher/abso-core/blame/src/engine/test/moves.test.ts?version=78&format=json
346 linesFirst import 99% · pinch 1%Last change pinch in version 78,
346 lines from 2 changes in 2 versions.
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
6import assert from "node:assert/strict";
7import { describe, it } from "node:test";
8import { type Atom, type AtomId, conflicts } from "../index.ts";
9import { lines, Repo, Rng } from "./helpers.ts";
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.Compare11const BASE_SEED = Number(process.env.PROP_SEED ?? 20261004);
12const CASES = Number(process.env.PROP_CASES ?? 200);
14const FN = ["function area(r) {", " const pi = 3.14;", " return pi * r * r;", "}"];
15const START = {
16 "shapes.js": lines("// shapes", ...FN, "", "function other() {", " return 1;", "}"),
17 "geometry.js": lines("// geometry", "export {};"),
18};
19/** shapes.js with area() moved out. */
20const SHAPES_AFTER = lines("// shapes", "", "function other() {", " return 1;", "}");
22/** Move area() (shapes.js lines 1-4) to the end of geometry.js. */
23function moveArea(repo: Repo, against?: Iterable<AtomId>): Atom {
24 return repo.draft({ move: { file: "shapes.js", start: 1, end: 5, to: "geometry.js", at: 2 } }, against);
25}
27function accept(repo: Repo, atom: Atom): Atom {
28 repo.graph.add(atom);
29 repo.shared.add(atom.id);
30 return atom;
31}
33/** Two atoms built against the same base, both accepted: the merge. */
34function concurrently(
35 repo: Repo,
36 a: (base: AtomId[]) => Atom,
37 b: (base: AtomId[]) => Atom,
38): { first: Atom; second: Atom } {
39 const base = [...repo.shared];
40 const first = accept(repo, a(base));
41 const second = accept(repo, b(base));
42 return { first, second };
43}
45describe("moves", () => {
46 it("moves lines to another file, keeping their ids", () => {
47 const repo = Repo.init(START);
48 const before = repo
49 .view()
50 .lines("shapes.js")
51 .slice(1, 5)
52 .map((l) => l.id);
53 accept(repo, moveArea(repo));
54 assert.equal(repo.text("shapes.js"), SHAPES_AFTER);
55 assert.equal(repo.text("geometry.js"), lines("// geometry", "export {};", ...FN));
56 const view = repo.view();
57 assert.deepEqual(
58 view
59 .lines("geometry.js")
60 .slice(2)
61 .map((l) => l.id),
62 before,
63 );
64 assert.equal(view.fileOf(before[2]!), "geometry.js");
65 });
67 it("an edit made before the move, to the body, the first line or the last line, follows the moved lines", () => {
68 for (const [old, now] of [
69 [" return pi * r * r;", " return pi * r ** 2;"],
70 ["function area(r) {", "function area(r = 1) {"],
71 ["}\n\nfunction other", "} // area\n\nfunction other"],
72 ] as const) {
73 const repo = Repo.init(START);
74 concurrently(
75 repo,
76 (base) => repo.draft({ edits: [{ file: "shapes.js", old, new: now }] }, base),
77 (base) => moveArea(repo, base),
78 );
79 assert.deepEqual(conflicts(repo.graph, repo.shared), [], old);
80 assert.equal(repo.text("shapes.js"), SHAPES_AFTER, old);
81 const fn = FN.map((l) => (l === old.split("\n")[0] ? now.split("\n")[0]! : l));
82 assert.equal(repo.text("geometry.js"), lines("// geometry", "export {};", ...fn), old);
83 }
84 });
86 it("an edit made after the move, against a view without it, follows too (either order is the same set)", () => {
87 const repo = Repo.init(START);
88 concurrently(
89 repo,
90 (base) => moveArea(repo, base),
91 (base) =>
92 repo.draft({ edits: [{ file: "shapes.js", old: " const pi = 3.14;", new: " const pi = Math.PI;" }] }, base),
93 );
94 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
95 assert.equal(
96 repo.text("geometry.js"),
97 lines("// geometry", "export {};", "function area(r) {", " const pi = Math.PI;", " return pi * r * r;", "}"),
98 );
99 });
101 it("lines inserted inside the moved block go with it", () => {
102 const repo = Repo.init(START);
103 concurrently(
104 repo,
105 (base) =>
106 repo.draft(
107 {
108 edits: [
109 { file: "shapes.js", old: " const pi = 3.14;\n", new: " const pi = 3.14;\n // area of a circle\n" },
110 ],
111 },
112 base,
113 ),
114 (base) => moveArea(repo, base),
115 );
116 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
117 assert.equal(repo.text("shapes.js"), SHAPES_AFTER);
118 assert.match(repo.text("geometry.js"), /const pi = 3\.14;\n {2}\/\/ area of a circle\n/);
119 });
121 it("inserting just above the moved block collides with the move: it is unclear which place the line meant", () => {
122 const repo = Repo.init(START);
123 concurrently(
124 repo,
125 (base) =>
126 repo.draft({ edits: [{ file: "shapes.js", start: 1, end: 1, lines: ["/** The area of a circle. */"] }] }, base),
127 (base) => moveArea(repo, base),
128 );
129 assert.equal(conflicts(repo.graph, repo.shared).length, 1);
130 });
132 it("a line deleted at its old place is deleted at its new one, with no collision", () => {
133 const repo = Repo.init(START);
134 concurrently(
135 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.Compare136 (base) => repo.draft({ edits: [{ file: "shapes.js", old: `${FN.join("\n")}\n`, new: "" }] }, base),
138 );
139 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
140 assert.equal(repo.text("shapes.js"), SHAPES_AFTER);
141 assert.equal(repo.text("geometry.js"), lines("// geometry", "export {};"));
142 });
144 it("two moves of the same lines collide", () => {
145 const repo = Repo.init(START);
146 concurrently(
147 repo,
148 (base) => moveArea(repo, base),
149 (base) => repo.draft({ move: { file: "shapes.js", start: 1, end: 5, to: "shapes.js", at: 9 } }, base),
150 );
151 const found = conflicts(repo.graph, repo.shared);
152 assert.equal(found.length, 1);
153 assert.ok(found[0]!.keys.some((k) => k.key.startsWith("P:")));
154 });
156 it("a whole file renamed: every line moves, and an edit to it lands in the new file", () => {
157 const repo = Repo.init(START);
158 concurrently(
159 repo,
160 (base) => repo.draft({ edits: [{ file: "shapes.js", old: " return 1;", new: " return 2;" }] }, base),
161 (base) => repo.draft({ move: { file: "shapes.js", start: 0, end: 9, to: "circle.js", at: 0 } }, base),
162 );
163 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
164 const view = repo.view();
165 assert.equal(view.hasFile("shapes.js"), false);
166 assert.equal(view.text("circle.js"), START["shapes.js"].replace("return 1;", "return 2;"));
167 assert.deepEqual(view.paths(), ["circle.js", "geometry.js"]);
168 });
170 it("a block moved within a file, up or down, with a concurrent edit inside it", () => {
171 for (const at of [0, 9]) {
172 const repo = Repo.init(START);
173 concurrently(
174 repo,
175 (base) =>
176 repo.draft(
177 { edits: [{ file: "shapes.js", old: " return pi * r * r;", new: " return pi * r * r; // m^2" }] },
178 base,
179 ),
180 (base) => repo.draft({ move: { file: "shapes.js", start: 1, end: 5, to: "shapes.js", at } }, base),
181 );
182 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
183 const fn = lines(...FN).replace("r * r;", "r * r; // m^2");
184 assert.equal(repo.text("shapes.js"), at === 0 ? fn + SHAPES_AFTER : SHAPES_AFTER + fn);
185 }
186 });
188 it("a file split in two: edits to either part, made apart, land in the right file", () => {
189 const repo = Repo.init(START);
190 const base = [...repo.shared];
191 accept(repo, repo.draft({ move: { file: "shapes.js", start: 5, end: 9, to: "other.js", at: 0 } }, base));
192 accept(repo, repo.draft({ edits: [{ file: "shapes.js", old: "3.14", new: "Math.PI" }] }, base));
193 accept(repo, repo.draft({ edits: [{ file: "shapes.js", old: "return 1;", new: "return 2;" }] }, base));
194 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
195 assert.equal(repo.text("shapes.js"), lines("// shapes", ...FN).replace("3.14", "Math.PI"));
196 assert.equal(repo.text("other.js"), lines("", "function other() {", " return 2;", "}"));
197 });
199 it("two files merged into one: the second file's lines move under the end of the first, and edits follow", () => {
200 const repo = Repo.init(START);
201 const base = [...repo.shared];
202 accept(repo, repo.draft({ move: { file: "geometry.js", start: 0, end: 2, to: "shapes.js", at: 9 } }, base));
203 accept(repo, repo.draft({ edits: [{ file: "geometry.js", old: "export {};", new: "export { area };" }] }, base));
204 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
205 assert.equal(repo.view().hasFile("geometry.js"), false);
206 assert.equal(repo.text("shapes.js"), START["shapes.js"] + lines("// geometry", "export { area };"));
207 });
209 it("moves stack: a moved block can move again, and a later edit follows both", () => {
210 const repo = Repo.init(START);
211 accept(repo, moveArea(repo));
212 repo.edit({ move: { file: "geometry.js", start: 2, end: 6, to: "circle.js", at: 0 } });
213 repo.edit({ edits: [{ file: "circle.js", old: "3.14", new: "3.14159" }] });
214 assert.equal(repo.text("circle.js"), lines(...FN).replace("3.14", "3.14159"));
215 assert.equal(repo.text("geometry.js"), lines("// geometry", "export {};"));
216 assert.equal(repo.text("shapes.js"), SHAPES_AFTER);
217 });
219 it("moves that loop lines into each other lose no lines", () => {
220 // Hand-made: two moves made apart, each putting one line under the other.
221 const repo = Repo.init({ "f.txt": lines("x", "m", "y", "n") });
222 const mk = (line: string, parent: string): Atom => {
223 const id = repo.graph.nextId();
224 return {
225 id,
226 seq: repo.graph.size,
227 author: "a",
228 message: "loop",
229 time: 0,
230 inserts: [],
231 deletes: [],
232 writes: [`G:${parent}`, `P:${line}`],
233 reads: [],
234 holds: [],
235 needs: [],
236 resolves: [],
237 reasons: [],
238 deps: ["a0"],
239 moves: [{ line, parent }],
240 };
241 };
242 accept(repo, mk("a0.0", "a0.2"));
243 accept(repo, mk("a0.2", "a0.0"));
244 const text = repo.text("f.txt");
245 assert.deepEqual(text.split("\n").filter(Boolean).sort(), ["m", "n", "x", "y"]);
246 });
248 it("rejects a move onto itself or out of range", () => {
249 const repo = Repo.init(START);
250 assert.throws(() => repo.draft({ move: { file: "shapes.js", start: 1, end: 5, to: "shapes.js", at: 3 } }), {
251 code: "bad-range",
252 });
253 assert.throws(() => repo.draft({ move: { file: "shapes.js", start: 1, end: 5, to: "shapes.js", at: 5 } }), {
254 code: "bad-range",
255 });
256 assert.throws(() => repo.draft({ move: { file: "shapes.js", start: 4, end: 4, to: "x.js", at: 0 } }), {
257 code: "bad-range",
258 });
259 assert.throws(() => repo.draft({ move: { file: "shapes.js", start: 1, end: 2, to: "geometry.js", at: 3 } }), {
260 code: "bad-range",
261 });
262 assert.throws(
263 () =>
264 repo.draft({
265 move: { file: "shapes.js", start: 1, end: 2, to: "x.js", at: 0 },
266 edits: [{ file: "geometry.js", old: "export", new: "x" }],
267 }),
268 { code: "bad-edit" },
269 );
270 });
271});
273describe("moves, property", () => {
274 const word = (rng: Rng): string =>
275 rng.pick(["a", "b", "c", "x", "y", "}", "", "let q = 1;", "return z;"]) +
276 (rng.chance(0.5) ? String(rng.int(9)) : "");
278 it("random edits and moves, one after another, agree with editing plain text", () => {
279 for (let c = 0; c < CASES; c++) {
280 const seed = BASE_SEED + c;
281 const rng = new Rng(seed);
282 const names = ["a.txt", "b.txt", "c.txt"];
283 const plain: Record<string, string[]> = {};
284 const files: Record<string, string> = {};
285 for (const f of names.slice(0, 2)) {
286 plain[f] = Array.from({ length: rng.range(1, 8) }, () => word(rng));
287 files[f] = lines(...plain[f]!);
288 }
289 plain["c.txt"] = [];
290 const repo = Repo.init(files);
291 for (let step = 0; step < 12; step++) {
292 const from = rng.pick(names.filter((f) => plain[f]!.length > 0));
293 const n = plain[from]!.length;
294 if (rng.chance(0.5)) {
295 const start = rng.int(n);
296 const end = rng.range(start + 1, n);
297 const to = rng.pick(names);
298 const len = plain[to]!.length;
299 let at = rng.int(len + 1);
300 if (to === from && at >= start && at <= end)
301 at = start > 0 ? rng.int(start) : end < len ? rng.range(end + 1, len) : -1;
302 if (at < 0) continue;
303 repo.edit({ move: { file: from, start, end, to, at } });
304 const block = plain[from]!.splice(start, end - start);
305 const where = to === from && at > end ? at - block.length : at;
306 plain[to]!.splice(where, 0, ...block);
307 } else {
308 const start = rng.int(n + 1);
309 const end = rng.range(start, Math.min(n, start + 2));
310 const add = Array.from({ length: rng.int(3) }, () => word(rng));
311 if (end - start === add.length && plain[from]!.slice(start, end).every((l, i) => l === add[i])) continue;
312 repo.edit({ edits: [{ file: from, start, end, lines: add }] });
313 plain[from]!.splice(start, end - start, ...add);
314 }
315 for (const f of names) assert.equal(repo.text(f), lines(...plain[f]!), `seed ${seed}, step ${step}, ${f}`);
316 }
317 }
318 });
320 it("a move and an edit inside the moved block, made apart, compose: the edited block lands at the new place", () => {
321 for (let c = 0; c < CASES; c++) {
322 const seed = BASE_SEED + c;
323 const rng = new Rng(seed);
324 const a = Array.from({ length: rng.range(3, 10) }, (_, i) => `a${i}`);
325 const b = Array.from({ length: rng.range(0, 4) }, (_, i) => `b${i}`);
326 const repo = Repo.init(b.length ? { "a.txt": lines(...a), "b.txt": lines(...b) } : { "a.txt": lines(...a) });
327 const start = rng.int(a.length);
328 const end = rng.range(start + 1, a.length);
329 const at = rng.int(b.length + 1);
330 // An edit that replaces at least one line of the block, staying inside it.
331 const es = rng.range(start, end - 1);
332 const ee = rng.range(es + 1, end);
333 const add = Array.from({ length: rng.int(3) }, (_, i) => `new${i}`);
334 concurrently(
335 repo,
336 (base) => repo.draft({ move: { file: "a.txt", start, end, to: "b.txt", at } }, base),
337 (base) => repo.draft({ edits: [{ file: "a.txt", start: es, end: ee, lines: add }] }, base),
338 );
339 const msg = `seed ${seed}`;
340 assert.deepEqual(conflicts(repo.graph, repo.shared), [], msg);
341 const block = [...a.slice(start, es), ...add, ...a.slice(ee, end)];
342 assert.equal(repo.text("a.txt"), lines(...a.slice(0, start), ...a.slice(end)), msg);
343 assert.equal(repo.text("b.txt"), lines(...b.slice(0, at), ...block, ...b.slice(at)), msg);
344 }
345 });
346});