- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/test/packed-files.test.ts?format=json
84 linescapacity 99% · codex-capacity-chair 1%Last change capacity in version 276, 2 versions changed it
1// Each file's line history packed (abso Capacity, Graph.packedLines): a stored graph lays files out from a few pages,
2// and must give exactly what the in-memory graph gives, through edits, deletes, moves between files and back.
4import assert from "node:assert/strict";
5import { test } from "node:test";
6import { Graph, View } from "../index.ts";
7import { StableOverlaySet } from "../state-source.ts";
8import { lines, Repo } from "./helpers.ts";
9import { SqlState } from "./storage-helper.ts";
11test("a packed file lays out as the in-memory graph does, at every step, moves included", () => {
12 const oracle = Repo.init({ "a.txt": lines("one", "two", "three", "four"), "b.txt": lines("bee"), "c.txt": "" });
13 const sets: Set<string>[] = [new Set(oracle.shared)];
14 const step = (change: Parameters<Repo["edit"]>[0]) => {
15 oracle.edit(change);
16 sets.push(new Set(oracle.shared));
17 };
18 step({ edits: [{ file: "a.txt", old: "two", new: "TWO" }] });
19 step({ move: { file: "a.txt", start: 1, end: 3, to: "b.txt", at: 1 } });
20 step({ edits: [{ file: "b.txt", old: "TWO", new: "two again" }] });
21 step({ edits: [{ file: "a.txt", old: "four", new: "" }] });
22 step({ move: { file: "b.txt", start: 0, end: 2, to: "c.txt", at: 0 } });
23 step({ edits: [{ file: "c.txt", old: "bee", new: "bee\nafter bee" }] });
24 const storage = new SqlState();
25 try {
26 const graph = new Graph({ storage });
27 for (const atom of oracle.graph.atoms()) graph.add(atom);
28 const reopened = new Graph({ storage });
29 assert.equal(reopened.packedFiles, true);
30 for (const [i, set] of sets.entries()) {
31 const packed = new View(reopened, new StableOverlaySet(set));
32 const memory = new View(oracle.graph, set);
33 assert.deepEqual(packed.paths(), memory.paths(), `step ${i}: paths`);
34 for (const path of ["a.txt", "b.txt", "c.txt"]) {
35 assert.equal(packed.text(path), memory.text(path), `step ${i}: ${path}`);
36 assert.deepEqual(packed.sequence(path), memory.sequence(path), `step ${i}: ${path}'s sequence`);
37 assert.deepEqual(packed.attrs(path), memory.attrs(path), `step ${i}: ${path}'s attributes`);
38 }
39 }
40 } finally {
41 storage.db.close();
42 }
43});
45test("a graph made with packFiles off keeps no packed history", () => {
46 const oracle = Repo.init({ "a.txt": lines("one") });
47 const storage = new SqlState();
48 try {
49 const graph = new Graph({ storage, packFiles: false });
50 for (const atom of oracle.graph.atoms()) graph.add(atom);
51 assert.equal(new Graph({ storage }).packedFiles, false);
52 assert.equal(new View(new Graph({ storage }), new StableOverlaySet(oracle.shared)).text("a.txt"), "one\n");
53 } finally {
54 storage.db.close();
55 }
56});
58test("a file's decoded history is kept between views and let go when an atom is added", () => {
59 const oracle = Repo.init({ "a.txt": lines("one", "two", "three"), "b.txt": lines("bee") });
60 const storage = new SqlState();
61 try {
62 const graph = new Graph({ storage });
63 for (const atom of oracle.graph.atoms()) graph.add(atom);
64 const first = new View(graph, new StableOverlaySet(oracle.shared));
65 assert.equal(first.text("a.txt"), "one\ntwo\nthree\n");
66 assert.ok(graph.historyBytes > 0, "the first layout keeps a.txt's history");
67 // Another view of the same file reads only what its membership needs, not the history's pages again.
68 const before = storage.reads;
69 assert.equal(new View(graph, new StableOverlaySet(oracle.shared)).sequence("a.txt").length, 3);
70 const pages = storage.reads - before;
71 const cold = new Graph({ storage });
72 const coldBefore = storage.reads;
73 new View(cold, new StableOverlaySet(oracle.shared)).sequence("a.txt");
74 assert.ok(pages < storage.reads - coldBefore, "the kept history saved reads");
75 // A new atom lets every kept history go; the next layout sees its lines.
76 oracle.edit({ edits: [{ file: "a.txt", old: "two", new: "TWO" }] });
77 graph.add(oracle.graph.atom([...oracle.shared].at(-1)!));
78 assert.equal(graph.historyBytes, 0);
79 const after = new View(graph, new StableOverlaySet(oracle.shared));
80 assert.equal(after.text("a.txt"), new View(oracle.graph, oracle.shared).text("a.txt"));
81 } finally {
82 storage.db.close();
83 }
84});