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