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