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
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/worker/project/test/trail.test.ts?format=json
136 lineswhytrail 100%Last change whytrail in version 194, 2 versions changed it
1// A line's earlier changes (abso-core issue 20): why follows a line back
2// through the changes that rewrote it, and marks the ones that only changed
3// its formatting.
4
5import assert from "node:assert/strict";
6import { test } from "node:test";
7import { why } from "../blame.ts";
8import { MemoryStore, Project } from "../project.ts";
9import { normalize, pairIn, trail } from "../trail.ts";
11function setup(files: Record<string, string>) {
12 const project = new Project(new MemoryStore());
13 project.init(files, 0);
14 for (const a of ["ann", "bob", "fmt"]) project.sync(a);
15 return project;
18function push(p: Project, agent: string, file: string, old: string, now: string, message: string, time: number) {
19 p.sync(agent);
20 p.edit(agent, { file, old, new: now }, time);
21 return p.publish(agent, message, time + 1);
24test("normalized text ignores whitespace, wrapping, quotes, trailing commas, line-end semicolons and lone arrow parentheses", () => {
25 assert.equal(normalize(["const a = f('x', 1);"]), normalize(['const a = f("x", 1)']));
26 assert.equal(normalize(["f(a, b)"]), normalize(["f(", "\ta,", "\tb,", ")"]));
27 assert.equal(normalize(["xs.map((x) => x)"]), normalize(["xs.map(x => x)"]));
28 assert.notEqual(normalize(["const n = 8;"]), normalize(["const n = 9;"]));
29});
31test("pairing: a rewrapped run pairs by its characters, a real edit by similarity, a new line with nothing", () => {
32 assert.deepEqual(pairIn(["f(a, b);"], ["f(", " a,", " b,", ");"], 1), { index: 0, formatting: true });
33 assert.deepEqual(pairIn(["const n = Math.min(8, w);"], ["const n = Math.min(cap ?? 8, w);"], 0), {
34 index: 0,
35 formatting: false,
36 });
37 assert.equal(pairIn(["const n = 8;"], ["// nothing like it at all, really"], 0), undefined);
38 assert.equal(pairIn([], ["new"], 0), undefined);
39});
41test("why follows a line back past a reformat to the change that said what it does", () => {
42 const p = setup({ "q.ts": "export function take(w: number) {\n\tconst n = Math.min(8, w);\n\treturn n;\n}\n" });
43 push(p, "ann", "q.ts", "Math.min(8, w)", "Math.min(cap ?? 8, w)", "Cap the batch at 8, tunable per project", 10); // v1
44 push(p, "bob", "q.ts", "\treturn n;\n", "\treturn n; // the count\n", "Say what n is", 20); // v2, beside it
45 push(
46 p,
47 "fmt",
48 "q.ts",
49 "\tconst n = Math.min(cap ?? 8, w);\n",
50 " const n = Math.min(cap ?? 8, w)\n",
51 "Reformat",
52 30,
53 ); // v3
55 const w = why(p, "q.ts", 2);
56 assert.equal(w.text, " const n = Math.min(cap ?? 8, w)");
57 assert.deepEqual(
58 w.trail.map((s) => [s.publish?.version, s.formatting, s.text]),
59 [
60 [3, true, " const n = Math.min(cap ?? 8, w)"],
61 [1, false, "\tconst n = Math.min(cap ?? 8, w);"],
62 [0, false, "\tconst n = Math.min(8, w);"],
63 ],
64 );
65 assert.equal(w.trail[1]!.publish?.message, "Cap the batch at 8, tunable per project");
66 assert.equal(w.trailMore, undefined);
67});
69test("a reformat that wraps one line onto several leads each piece back to it", () => {
70 const p = setup({ "a.ts": "call(alpha, beta);\n" });
71 push(p, "fmt", "a.ts", "call(alpha, beta);\n", "call(\n\talpha,\n\tbeta,\n);\n", "Wrap", 10); // v1
72 const w = why(p, "a.ts", 3);
73 assert.deepEqual(
74 w.trail.map((s) => [s.publish?.version, s.formatting, s.text]),
75 [
76 [1, true, "\tbeta,"],
77 [0, false, "call(alpha, beta);"],
78 ],
79 );
80});
82test("why --at reads the line as shared had it at that version", () => {
83 const p = setup({ "a.ts": "one\ntwo\n" });
84 push(p, "ann", "a.ts", "one\n", "zero\none\n", "Add zero", 10); // v1
85 assert.equal(why(p, "a.ts", 1, { version: 0 }).text, "one");
86 assert.equal(why(p, "a.ts", 2).text, "one");
87});
89test("the trail stops after the most steps it may take, and says so", () => {
90 const p = setup({ "a.ts": "v = 0\n" });
91 for (let i = 1; i <= 4; i++) push(p, "ann", "a.ts", `v = ${i - 1}`, `v = ${i}`, `to ${i}`, i * 10);
92 const view = p.sharedView();
93 const line = view.lines("a.ts")[0]!.id;
94 const t = trail(p.graph, view, "a.ts", line, 2);
95 assert.equal(t.steps.length, 2);
96 assert.equal(t.more, true);
97 assert.equal(trail(p.graph, view, "a.ts", line).steps.length, 5);
98});
100test("code moved to another file in one push is followed back into the file it came from", () => {
101 const p = setup({
102 "big.ts": "export const keep = 1;\nexport function tide(level: number) {\n\treturn level * 2;\n}\n",
103 "small.ts": "export const other = 1;\n",
104 });
105 push(p, "ann", "big.ts", "level * 2", "level * 3", "Tides run higher", 10); // v1
106 // Moved as it is: the engine keeps the lines themselves, so their history comes along.
107 p.sync("bob");
108 const tide = "export function tide(level: number) {\n\treturn level * 3;\n}\n";
109 p.edit("bob", { file: "big.ts", old: tide, new: "" }, 20);
110 p.edit("bob", { file: "small.ts", old: "export const other = 1;\n", new: `export const other = 1;\n${tide}` }, 21);
111 p.publish("bob", "Move tide to small.ts", 22); // v2
112 assert.deepEqual(
113 why(p, "small.ts", 3).trail.map((s) => [s.publish?.version, s.text]),
114 [
115 [1, "\treturn level * 3;"],
116 [0, "\treturn level * 2;"],
117 ],
118 );
119 // Moved and reindented in one push: new lines, followed back by their code.
120 p.sync("ann");
121 const moved = tide.replaceAll("\t", " ");
122 p.edit("ann", { file: "small.ts", old: tide, new: "" }, 30);
123 p.edit("ann", { file: "big.ts", old: "export const keep = 1;\n", new: `export const keep = 1;\n${moved}` }, 31);
124 p.publish("ann", "Move tide back, two-space indent", 32); // v3
125 const w = why(p, "big.ts", 3);
126 assert.deepEqual(
127 w.trail.map((s) => [s.publish?.version, s.formatting, s.movedFrom, s.text]),
128 [
129 [3, true, "small.ts", " return level * 3;"],
130 [1, false, undefined, "\treturn level * 3;"],
131 [0, false, undefined, "\treturn level * 2;"],
132 ],
133 );
134 // A short line (a brace) is too common to follow.
135 assert.equal(why(p, "big.ts", 4).trail.length, 1);
136});