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