- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/test/view.test.ts?format=json
265 linescodex-capacity-chair 35% · First import 35% · codex-capacity-policy 29% · pinch 1%Last change codex-capacity-chair in version 125, 9 versions changed it
1// Projection: the line tree, tombstones, sibling priority, and long files.
3import assert from "node:assert/strict";
4import { describe, it } from "node:test";
5import { buildBase, conflicts, Graph, project, View } from "../index.ts";
6import { StableOverlaySet, StableSet } from "../state-source.ts";
7import type { AtomId } from "../types.ts";
8import { lines, Repo } from "./helpers.ts";
10describe("View", () => {
11 it("bounds file-location memo growth across long ancestry walks with moves", () => {
12 const retained = (count: number) => {
13 const repo = Repo.init({ "long.txt": "line\n".repeat(count), "a.txt": "move\n", "b.txt": "base\n" });
14 repo.edit({ move: { file: "a.txt", start: 0, end: 1, to: "b.txt", at: 1 } });
15 const view = new View(repo.graph, new StableOverlaySet<AtomId>(["a0", "a1"]));
16 const oracle = repo.view();
17 // Resolve the deepest ancestry first, then force repeated cache eviction.
18 const sequence = oracle.sequence("long.txt");
19 assert.equal(sequence.length, count);
20 assert.equal(view.fileOf(sequence[count - 1]!.id), "long.txt");
21 for (const line of sequence) assert.equal(view.fileOf(line.id), "long.txt");
22 for (const line of oracle.lines("b.txt")) assert.equal(view.fileOf(line.id), "b.txt");
23 assert.equal(view.fileOf(sequence[count - 1]!.id), "long.txt");
24 return view.weight();
25 };
26 assert.equal(retained(2048), retained(512), "file-location retained state must not grow with ancestry depth");
27 });
28 it("current indexed paths do not retain deleted historical file layouts", () => {
29 const retained = (count: number) => {
30 const files: Record<string, string> = { "live.txt": "live\n" };
31 for (let i = 0; i < count; i++) files[`old-${i}.txt`] = "gone\n";
32 const repo = Repo.init(files);
33 repo.edit({ edits: Array.from({ length: count }, (_, i) => ({ file: `old-${i}.txt`, old: "gone\n", new: "" })) });
34 const view = new View(repo.graph, new StableOverlaySet<AtomId>(["a0", "a1"]));
35 assert.deepEqual(view.files(), { "live.txt": "live\n" });
36 const weight = view.weight();
37 assert.equal(view.knownPaths().length, count + 1, "explicit historical enumeration is preserved");
38 return weight;
39 };
40 assert.equal(retained(200), retained(20), "deleted path count must not grow current-view retained state");
41 });
42 it("reuses indexed membership across a long run of lines without a history-sized memo", () => {
43 const repo = Repo.init({ "long.txt": "line\n".repeat(1000) });
44 class CountingSet extends StableOverlaySet<AtomId> {
45 calls = 0;
46 override has(id: AtomId): boolean {
47 this.calls++;
48 return super.has(id);
49 }
50 }
51 const set = new CountingSet(["a0"]);
52 const view = new View(repo.graph, set);
53 set.calls = 0;
54 assert.equal(view.text("long.txt"), "line\n".repeat(1000));
55 assert.ok(set.calls <= 2, `membership was queried ${set.calls} times for one atom`);
56 });
57 it("immutable indexed membership avoids a history-sized bitmap and remains a snapshot", () => {
58 class RangeMembership extends StableSet<AtomId> {
59 readonly retainedBytes = 64;
60 readonly count: number;
61 constructor(count: number) {
62 super();
63 this.count = count;
64 }
65 has(id: AtomId): boolean {
66 return /^a[0-9]+$/.test(id) && Number(id.slice(1)) < this.count;
67 }
68 *values(): SetIterator<AtomId> {
69 for (let i = 0; i < this.count; i++) yield `a${i}`;
70 }
71 }
72 const repo = Repo.init({ "a.txt": "0\n" });
73 for (let i = 1; i <= 40; i++) repo.edit({ edits: [{ file: "a.txt", old: `${i - 1}\n`, new: `${i}\n` }] });
74 const first = new View(repo.graph, new RangeMembership(repo.graph.size));
75 const weight = first.weight();
76 for (let i = 41; i <= 80; i++) repo.edit({ edits: [{ file: "a.txt", old: `${i - 1}\n`, new: `${i}\n` }] });
77 const second = new View(repo.graph, new RangeMembership(repo.graph.size));
78 assert.equal(second.weight(), weight, "membership resident size does not increase with historical atom count");
79 assert.equal(first.text("a.txt"), "40\n");
80 assert.equal(second.text("a.txt"), "80\n");
81 assert.equal(second.weight(), first.weight(), "current text retains visible lines, not historical tombstones");
82 const currentWeight = second.weight();
83 assert.equal(second.sequence("a.txt").length, 81);
84 assert.equal(second.sequence("a.txt").filter((line) => line.visible).length, 1);
85 assert.ok(second.weight() > currentWeight, "explicit history reads may retain their requested sequence");
86 assert.equal(second.text("a.txt"), "80\n");
87 assert.equal(first.set.has("a80"), false);
88 assert.throws(() => new View(repo.graph, new StableOverlaySet<AtomId>(["a1"])), /not closed/);
89 });
90 it("membership memo collisions preserve both included and excluded historical atoms", () => {
91 const repo = Repo.init({ "f.txt": "0\n" });
92 for (let i = 1; i <= 257; i++) repo.edit({ edits: [{ file: "f.txt", old: `${i - 1}\n`, new: `${i}\n` }] });
93 const old = new View(repo.graph, new StableOverlaySet<AtomId>(["a0"]));
94 assert.equal(old.text("f.txt"), "0\n");
95 assert.deepEqual(
96 old.sequence("f.txt").map((line) => line.text),
97 ["0"],
98 );
99 assert.equal(repo.view().text("f.txt"), "257\n");
100 });
101 it("keeps deleted lines as tombstones in the full sequence", () => {
102 const repo = Repo.init({ "f.txt": lines("one", "two", "three") });
103 const del = repo.edit({ edits: [{ file: "f.txt", old: "two\n", new: "" }] });
104 const view = repo.view();
105 assert.deepEqual(
106 view.sequence("f.txt").map((l) => [l.id, l.text, l.visible, l.deletedBy]),
107 [
108 ["a0.0", "one", true, []],
109 ["a0.1", "two", false, [del.id]],
110 ["a0.2", "three", true, []],
111 ],
112 );
113 assert.deepEqual(
114 view.lines("f.txt").map((l) => l.id),
115 ["a0.0", "a0.2"],
116 );
117 assert.equal(view.lineNumber("f.txt", "a0.2"), 1);
118 assert.equal(view.lineNumber("f.txt", "a0.1"), -1);
119 assert.equal(view.sequenceIndex("f.txt", "a0.1"), 1);
120 assert.equal(view.text("f.txt"), "one\nthree\n");
121 assert.equal(view.text("missing.txt"), "");
122 assert.deepEqual(view.lines("missing.txt"), []);
123 });
125 it("orders siblings by descending priority: higher lamport first, then higher seq", () => {
126 const repo = Repo.init({ "f.txt": lines("top", "bottom") });
127 // Three inserts after "top", all written against the base: same lamport, seq decides.
128 const p = repo.edit({ edits: [{ file: "f.txt", start: 1, end: 1, lines: ["p"] }] }, ["a0"]);
129 const q = repo.edit({ edits: [{ file: "f.txt", start: 1, end: 1, lines: ["q"] }] }, ["a0"]);
130 assert.equal(repo.text("f.txt"), lines("top", "q", "p", "bottom"));
131 // One written later, against a view with both: higher lamport, lands right after "top".
132 const r = repo.edit({ edits: [{ file: "f.txt", start: 1, end: 1, lines: ["r"] }] });
133 assert.ok(repo.graph.lamport(r.id) > repo.graph.lamport(q.id));
134 assert.equal(repo.text("f.txt"), lines("top", "r", "q", "p", "bottom"));
135 assert.deepEqual(
136 r.deps,
137 [p.id, q.id].sort((a, b) => Number(a.slice(1)) - Number(b.slice(1))),
138 );
139 assert.deepEqual(repo.graph.closureOf(r.deps), repo.graph.closureOf(["a0", p.id, q.id]));
140 });
142 it("puts a line's replacements ahead of the lines inserted after it, whatever their priority", () => {
143 const repo = Repo.init({ "f.txt": lines("x", "y") });
144 const y1 = repo.edit({ edits: [{ file: "f.txt", old: "y", new: "y1" }] });
145 const y2 = repo.edit({ edits: [{ file: "f.txt", old: "y1", new: "y2" }] });
146 // A before-mode insert above y2: it hangs off x and depends on y2's writer (lamport 3).
147 const before = repo.edit({ edits: [{ file: "f.txt", start: 1, end: 1, lines: ["n"], mode: "before" }] });
148 assert.equal(repo.graph.lamport(before.id), 3);
149 // A replacement of x written against the base (lamport 1); the two don't conflict.
150 const replace = repo.edit({ edits: [{ file: "f.txt", old: "x", new: "X" }] }, ["a0"]);
151 assert.equal(repo.graph.lamport(replace.id), 1);
152 assert.deepEqual(conflicts(repo.graph, repo.shared), []);
153 assert.equal(repo.text("f.txt"), lines("X", "n", "y2"));
154 assert.deepEqual(
155 repo
156 .view()
157 .sequence("f.txt")
158 .map((l) => l.id),
159 ["a0.0", `${replace.id}.0`, `${before.id}.0`, "a0.1", `${y1.id}.0`, `${y2.id}.0`],
160 );
161 });
163 it("refuses sets that are not closed or name unknown atoms", () => {
164 const repo = Repo.init({ "f.txt": lines("one") });
165 const x = repo.edit({ edits: [{ file: "f.txt", old: "one", new: "ONE" }] });
166 assert.throws(() => new View(repo.graph, [x.id]), { code: "not-closed" });
167 assert.throws(() => new View(repo.graph, ["a0", "a9"]), { code: "unknown-atom" });
168 assert.deepEqual(new View(repo.graph, []).files(), {});
169 });
171 it("projects a long file (a deep chain) without recursion limits", () => {
172 const big = Array.from({ length: 30_000 }, (_, i) => `line ${i}`);
173 const graph = new Graph();
174 graph.add(buildBase({ "big.txt": `${big.join("\n")}\n` }, { author: "import", message: "big" }));
175 const files = project(graph, ["a0"]);
176 assert.equal(files["big.txt"]!.split("\n").length, 30_001);
177 });
179 it("lists known paths separately from existing files", () => {
180 const repo = Repo.init({ "a.txt": lines("a"), "b.txt": lines("b") });
181 repo.edit({ edits: [{ file: "b.txt", old: "b\n", new: "" }] });
182 const view = repo.view();
183 assert.deepEqual(view.paths(), ["a.txt"]);
184 assert.deepEqual(view.knownPaths(), ["a.txt", "b.txt"]);
185 assert.deepEqual(view.files(), { "a.txt": "a\n" });
186 });
188 it("accounts for the membership bitmap before layout and stays observational", () => {
189 const small = Repo.init({ "f.txt": lines("same") });
190 const larger = Repo.init({ "f.txt": lines("same") });
191 for (let i = 0; i < 8; i++)
192 larger.edit({ attrs: [{ file: "f.txt", name: "exec", value: i % 2 === 0 ? "1" : null }] });
194 const smallView = new View(small.graph, small.shared);
195 const largerView = new View(larger.graph, larger.shared);
196 const smallBeforeLayout = smallView.weight();
197 const largerBeforeLayout = largerView.weight();
198 assert.ok(smallBeforeLayout >= small.graph.size);
199 assert.ok(largerBeforeLayout >= larger.graph.size);
200 assert.equal(largerBeforeLayout - smallBeforeLayout, larger.graph.size - small.graph.size);
201 assert.equal(largerView.weight(), largerBeforeLayout);
203 assert.deepEqual(smallView.files(), largerView.files());
204 const afterLayout = largerView.weight();
205 assert.equal(largerView.weight(), afterLayout);
206 assert.deepEqual(largerView.files(), { "f.txt": "same\n" });
207 });
209 it("accounts for lazy view-owned containers and relocation/index growth", () => {
210 const repo = Repo.init({ "a.txt": lines("one", "two"), "b.txt": lines("base") });
211 repo.edit({ move: { file: "a.txt", start: 0, end: 1, to: "b.txt", at: 1 } });
212 const deletion = repo.edit({ edits: [{ file: "b.txt", old: "base\n", new: "" }] });
213 const view = repo.view();
214 let weight = view.weight();
215 assert.equal(view.weight(), weight);
217 assert.equal(view.set.size, 3);
218 assert.ok(view.weight() > weight);
219 weight = view.weight();
220 assert.deepEqual(view.knownPaths(), ["a.txt", "b.txt"]);
221 assert.ok(view.weight() > weight);
222 weight = view.weight();
223 assert.equal(view.fileOf("a0.0"), "b.txt");
224 assert.ok(view.weight() > weight);
226 const expectedFiles = { "a.txt": "two\n", "b.txt": "one\n" };
227 assert.deepEqual(view.files(), expectedFiles);
228 weight = view.weight();
229 assert.deepEqual(
230 view.sequence("b.txt").map((line) => [line.text, line.visible, line.deletedBy]),
231 [
232 ["base", false, [deletion.id]],
233 ["one", true, []],
234 ],
235 );
236 assert.ok(view.weight() > weight);
237 weight = view.weight();
238 assert.equal(view.sequenceIndex("b.txt", "a0.0"), 1);
239 assert.ok(view.weight() > weight);
240 weight = view.weight();
241 assert.equal(view.lineNumber("b.txt", "a0.0"), 0);
242 assert.ok(view.weight() > weight);
243 weight = view.weight();
244 assert.deepEqual(
245 view.lines("b.txt").map((line) => line.text),
246 ["one"],
247 );
248 assert.ok(view.weight() > weight);
249 const finalWeight = view.weight();
250 assert.equal(view.weight(), finalWeight);
251 assert.deepEqual(view.files(), expectedFiles);
252 });
254 it("counts line-cache references without charging Graph-owned text per view", () => {
255 const short = Repo.init({ "f.txt": lines("x") });
256 const long = Repo.init({ "f.txt": lines("x".repeat(10_000)) });
257 const shortView = short.view();
258 const longView = long.view();
259 shortView.lines("f.txt");
260 longView.lines("f.txt");
261 assert.equal(shortView.weight(), longView.weight());
262 const longWeight = longView.weight();
263 assert.equal(longView.weight(), longWeight);
264 });
265});