- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/test/projection-marker.test.ts?format=json
189 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 165, 3 versions changed it
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { ExperimentalMarkerProjection as MarkerProjection } from "../experimental/projection-marker.ts";
4import { Graph } from "../graph.ts";
5import { CachedStateStorage } from "../state-source.ts";
6import { Repo } from "./helpers.ts";
7import { SqlState } from "./storage-helper.ts";
9test("projection apply and removal compare atom contents without whole-atom JSON", (t) => {
10 const storage = new SqlState();
11 t.after(() => storage.db.close());
12 const repo = Repo.init({ "f.txt": `${"x".repeat(40000)}\n` });
13 const atom = repo.graph.atom("a0");
14 const projection = new MarkerProjection(storage, repo.graph, "f.txt", "streamed-comparison");
15 const stringify = JSON.stringify;
16 t.mock.method(JSON, "stringify", (...args: Parameters<typeof JSON.stringify>) => {
17 assert.notEqual(args[0], atom, "projection validation must not serialize the full atom");
18 return stringify(...args);
19 });
20 const state = projection.apply(projection.empty(), atom);
21 const before = storage.writes;
22 const altered = { ...atom, message: "forged" };
23 assert.throws(() => projection.apply(state, altered), /canonical atom mismatch/);
24 assert.throws(() => projection.removeAtom(state, altered), /canonical atom mismatch/);
25 assert.equal(storage.writes, before);
26 const removed = projection.removeAtom(state, atom);
27 assert.deepEqual(projection.lines(removed, "f.txt"), []);
28 assert.equal(projection.lines(state, "f.txt").length, 1);
29});
31test("typed marker projection matches mixed multi-file edits and reverse removals", () => {
32 const db = new SqlState();
33 try {
34 const repo = Repo.init({ "f.txt": "a\nb\nc\n", "g.txt": "x\ny\n" }),
35 graph = new Graph({ storage: new CachedStateStorage(db) });
36 graph.add(repo.graph.atom("a0"));
37 const p = new MarkerProjection(db, graph, "f.txt", "typed-mixed");
38 let state = p.apply(p.empty(), repo.graph.atom("a0")),
39 checks = 0;
40 const atoms = [],
41 roots = [];
42 const check = () => {
43 const view = repo.view();
44 for (const path of ["f.txt", "g.txt"]) {
45 assert.deepEqual(
46 p.lines(state, path),
47 view.lines(path).map((x) => x.id),
48 );
49 assert.deepEqual(p.attrs(state, path), view.attrs(path));
50 }
51 checks++;
52 };
53 for (let i = 0; i < 36; i++) {
54 const file = i % 2 ? "f.txt" : "g.txt",
55 other = i % 2 ? "g.txt" : "f.txt",
56 view = repo.view();
57 let input: Parameters<Repo["edit"]>[0];
58 if (i % 3 === 0 && view.lines(file).length) input = { move: { file, start: 0, end: 1, to: other, at: 0 } };
59 else if (i % 3 === 1)
60 input = {
61 edits: [{ file, old: view.text(file), new: `new ${i}\n${view.text(file)}` }],
62 attrs: [{ file, name: "exec", value: i % 2 ? "1" : null }],
63 };
64 else input = { edits: [{ file, old: view.text(file), new: `rewrite ${i}\n` }] };
65 const atom = repo.edit(input);
66 graph.add(atom);
67 atoms.push(atom);
68 state = p.apply(state, atom);
69 check();
70 if (i % 9 === 0) roots.push({ state, f: p.lines(state, "f.txt"), g: p.lines(state, "g.txt") });
71 }
72 for (const atom of atoms.toReversed()) {
73 repo.shared.delete(atom.id);
74 state = p.removeAtom(state, atom);
75 check();
76 }
77 for (const root of roots) {
78 assert.deepEqual(p.lines(root.state, "f.txt"), root.f);
79 assert.deepEqual(p.lines(root.state, "g.txt"), root.g);
80 }
81 assert.equal(checks, 72);
82 } finally {
83 db.db.close();
84 }
85});
87test("typed marker namespaces isolate file identities and temporary metadata", () => {
88 const storage = new SqlState();
89 try {
90 const repo = Repo.init({ "f.txt": "f\n", "g.txt": "g\n" });
91 const graph = new Graph({ storage: new CachedStateStorage(storage) });
92 graph.add(repo.graph.atom("a0"));
93 const alpha = new MarkerProjection(storage, graph, "f.txt", "alpha");
94 const beta = new MarkerProjection(storage, graph, "g.txt", "beta");
95 const a = alpha.apply(alpha.empty(), repo.graph.atom("a0"));
96 const b = beta.apply(beta.empty(), repo.graph.atom("a0"));
97 const wrongFile = new MarkerProjection(storage, graph, "g.txt", "alpha");
98 for (const other of [beta, wrongFile]) {
99 const reads = storage.reads,
100 writes = storage.writes;
101 assert.throws(() => other.lines(a, "f.txt"), /constructor context mismatch/);
102 assert.throws(() => other.apply(a, repo.graph.atom("a0")), /constructor context mismatch/);
103 assert.equal(storage.reads, reads);
104 assert.equal(storage.writes, writes);
105 }
107 for (const path of ["f.txt", "g.txt"]) {
108 assert.deepEqual(
109 alpha.lines(a, path),
110 repo
111 .view()
112 .lines(path)
113 .map((x) => x.id),
114 );
115 assert.deepEqual(
116 beta.lines(b, path),
117 repo
118 .view()
119 .lines(path)
120 .map((x) => x.id),
121 );
122 }
123 alpha.storage.write("sequence-meta", "work-scope", 91);
124 assert.equal(beta.storage.read("sequence-meta", "work-scope"), undefined);
125 const rows = () => JSON.stringify(storage.db.prepare("SELECT * FROM state ORDER BY collection,key").all());
126 const before = rows();
127 assert.throws(
128 () => alpha.relocateBatch(a, [{ n: -1, parentKey: 0, key: "0".repeat(57) }]),
129 /invalid relocation plan/,
130 );
131 assert.equal(rows(), before);
132 assert.deepEqual(
133 beta.lines(b, "f.txt"),
134 repo
135 .view()
136 .lines("f.txt")
137 .map((x) => x.id),
138 );
139 } finally {
140 storage.db.close();
141 }
142});
144test("visibility batches roll back earlier batches and retry with exact line parity", (t) => {
145 const db = new SqlState();
146 t.after(() => db.db.close());
147 const text = Array.from({ length: 35 }, (_, n) => `line ${n}\n`).join("");
148 const repo = Repo.init({ "f.txt": text });
149 const graph = new Graph({ storage: new CachedStateStorage(db) });
150 graph.add(repo.graph.atom("a0"));
151 const p = new MarkerProjection(db, graph, "f.txt", "batch-failure");
152 const initial = p.apply(p.empty(), repo.graph.atom("a0"));
153 const originalLines = p.lines(initial, "f.txt");
154 const atom = repo.edit({ edits: [{ file: "f.txt", old: text, new: "" }] });
155 graph.add(atom);
156 const rows = () => db.db.prepare("SELECT * FROM state ORDER BY collection,key").all();
157 const before = rows();
158 const original = p.sequence.updateMany.bind(p.sequence);
159 const sizes: number[] = [];
160 let fail = true;
161 t.mock.method(
162 p.sequence,
163 "updateMany",
164 (state: Parameters<typeof original>[0], operations: Parameters<typeof original>[1]) => {
165 const next = original(state, operations);
166 if (operations.every((op) => op.kind === "visibility")) {
167 sizes.push(operations.length);
168 if (fail && sizes.length === 2) throw Error("second visibility batch failure");
169 }
170 return next;
171 },
172 );
173 assert.throws(() => p.apply(initial, atom), /second visibility batch/);
174 assert.deepEqual(sizes, [16, 16]);
175 assert.deepEqual(rows(), before);
176 assert.deepEqual(p.lines(initial, "f.txt"), originalLines);
177 fail = false;
178 sizes.length = 0;
179 const next = p.apply(initial, atom);
180 assert.deepEqual(sizes, [16, 16, 3]);
181 assert.deepEqual(
182 p.lines(next, "f.txt"),
183 repo
184 .view()
185 .lines("f.txt")
186 .map((line) => line.id),
187 );
188 assert.deepEqual(p.lines(initial, "f.txt"), originalLines);
189});