- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/test/projection-snapshots.test.ts?format=json
174 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 143, 1 version changed it
1import assert from "node:assert/strict";
2import { mkdtempSync, rmSync } from "node:fs";
3import { tmpdir } from "node:os";
4import { join } from "node:path";
5import { test } from "node:test";
6import { ExperimentalMarkerProjection } from "../experimental/projection-marker.ts";
7import type { ProjectionIdentity } from "../experimental/projection-root.ts";
8import { ProjectionSnapshots } from "../experimental/projection-snapshots.ts";
9import { Graph } from "../graph.ts";
10import { CachedStateStorage } from "../state-source.ts";
11import { Repo } from "./helpers.ts";
12import { SqlState } from "./storage-helper.ts";
14test("experimental snapshots reopen and refuse wrong identities before projection reads", () => {
15 const filename = join(mkdtempSync(join(tmpdir(), "abso-projection-snapshot-")), "state.sqlite");
16 let storage = new SqlState(filename);
17 const repo = Repo.init({ "f.txt": "hello\n", "g.txt": "other\n" });
18 const identity: ProjectionIdentity = {
19 database: "database-a",
20 namespace: "index-a",
21 registry: "registry-a",
22 defaultFile: "f.txt",
23 sourceGeneration: 1,
24 sourceLength: 1,
25 sourceDigest: "a".repeat(64),
26 membership: "base",
27 };
28 try {
29 let graph = new Graph({ storage: new CachedStateStorage(storage) });
30 graph.add(repo.graph.atom("a0"));
31 let adapter = new ExperimentalMarkerProjection(storage, graph, "f.txt", "index-a");
32 let snapshots = new ProjectionSnapshots(storage, adapter, identity);
33 const state = adapter.apply(adapter.empty(), repo.graph.atom("a0"));
34 const handle = snapshots.save(0, identity, state);
35 storage.db.close();
36 storage = new SqlState(filename);
37 graph = new Graph({ storage: new CachedStateStorage(storage) });
38 adapter = new ExperimentalMarkerProjection(storage, graph, "f.txt", "index-a");
39 snapshots = new ProjectionSnapshots(storage, adapter, identity);
40 const reopened = snapshots.load(handle, identity);
41 assert.deepEqual(adapter.lines(reopened), ["a0.0"]);
42 for (const field of Object.keys(identity) as (keyof ProjectionIdentity)[]) {
43 const changed = {
44 ...identity,
45 [field]:
46 typeof identity[field] === "number"
47 ? Number(identity[field]) + 1
48 : field === "sourceDigest"
49 ? "b".repeat(64)
50 : `${identity[field]}:other`,
51 };
52 const reads = storage.reads,
53 writes = storage.writes;
54 assert.throws(() => snapshots.load(handle, changed), /mismatch/);
55 assert.equal(storage.reads, reads);
56 assert.equal(storage.writes, writes);
57 }
58 assert.throws(
59 () =>
60 new ProjectionSnapshots(
61 storage,
62 new ExperimentalMarkerProjection(storage, graph, "g.txt", "index-a"),
63 identity,
64 ),
65 /constructor mismatch/,
66 );
67 const rows = () => JSON.stringify(storage.db.prepare("SELECT * FROM state ORDER BY collection,key").all());
68 const before = rows();
69 assert.throws(
70 () =>
71 storage.transaction(() => {
72 snapshots.save(1, identity, reopened);
73 throw Error("fail after snapshot");
74 }),
75 /fail after snapshot/,
76 );
77 assert.equal(rows(), before);
78 storage.write('experimental-projection:["database-a","index-a"]:context', "binding", {
79 ...identity,
80 registry: "replaced",
81 });
82 assert.throws(() => snapshots.load(handle, identity), /registry binding/);
83 } finally {
84 storage.db.close();
85 }
86});
88test("experimental snapshots preserve mixed files across two reopens and subsequent writes", () => {
89 const directory = mkdtempSync(join(tmpdir(), "abso-projection-mixed-"));
90 const filename = join(directory, "state.sqlite");
91 let storage = new SqlState(filename);
92 const repo = Repo.init({ "f.txt": "a\nb\n", "g.txt": "x\n" });
93 const identity: ProjectionIdentity = {
94 database: "mixed-database",
95 namespace: "mixed-index",
96 registry: "mixed-registry",
97 defaultFile: "f.txt",
98 sourceGeneration: 1,
99 sourceLength: 1,
100 sourceDigest: "a".repeat(64),
101 membership: "base",
102 };
103 let graph = new Graph({ storage: new CachedStateStorage(storage) });
104 let adapter = new ExperimentalMarkerProjection(storage, graph, "f.txt", identity.namespace);
105 let snapshots = new ProjectionSnapshots(storage, adapter, identity);
106 graph.add(repo.graph.atom("a0"));
107 let state = adapter.apply(adapter.empty(), repo.graph.atom("a0"));
108 const original = snapshots.save(0, identity, state);
109 const reopen = () => {
110 storage.db.close();
111 storage = new SqlState(filename);
112 graph = new Graph({ storage: new CachedStateStorage(storage) });
113 adapter = new ExperimentalMarkerProjection(storage, graph, "f.txt", identity.namespace);
114 snapshots = new ProjectionSnapshots(storage, adapter, identity);
115 };
116 const check = () => {
117 const view = repo.view();
118 for (const path of ["f.txt", "g.txt", "empty.txt"]) {
119 assert.deepEqual(
120 adapter.lines(state, path),
121 view.lines(path).map((line) => line.id),
122 );
123 assert.deepEqual(adapter.attrs(state, path), view.attrs(path));
124 }
125 };
126 try {
127 const move = repo.edit({ move: { file: "f.txt", start: 0, end: 1, to: "g.txt", at: 0 } });
128 graph.add(move);
129 state = adapter.apply(state, move);
130 const attr = repo.edit({
131 attrs: [
132 { file: "empty.txt", name: "exists", value: "1" },
133 { file: "g.txt", name: "exec", value: "1" },
134 ],
135 });
136 graph.add(attr);
137 state = adapter.apply(state, attr);
138 const mixedIdentity = { ...identity, sourceLength: 3, sourceDigest: "b".repeat(64), membership: "mixed" };
139 const mixed = snapshots.save(1, mixedIdentity, state);
140 reopen();
141 state = snapshots.load(mixed, mixedIdentity);
142 check();
143 const writes = storage.writes;
144 assert.equal(adapter.apply(state, attr), state);
145 assert.equal(storage.writes, writes);
146 for (const atom of [attr, move]) {
147 repo.shared.delete(atom.id);
148 state = adapter.removeAtom(state, atom);
149 check();
150 }
151 assert.deepEqual(adapter.lines(snapshots.load(original, identity), "f.txt"), ["a0.0", "a0.1"]);
152 const edit = repo.edit({ edits: [{ file: "g.txt", old: "x\n", new: "after restart\n" }] });
153 graph.add(edit);
154 state = adapter.apply(state, edit);
155 check();
156 const finalIdentity = { ...identity, sourceLength: 4, sourceDigest: "c".repeat(64), membership: "edited" };
157 const final = snapshots.save(2, finalIdentity, state);
158 reopen();
159 state = snapshots.load(final, finalIdentity);
160 check();
161 assert.deepEqual(adapter.lines(snapshots.load(mixed, mixedIdentity), "g.txt"), ["a0.0", "a0.2"]);
162 assert.equal(
163 storage.db
164 .prepare(
165 "SELECT count(*) AS n FROM state WHERE collection LIKE '%sequence-resolver:%' OR collection LIKE '%sequence-relocation-work:%'",
166 )
167 .get()!.n,
168 0,
169 );
170 } finally {
171 storage.db.close();
172 rmSync(directory, { recursive: true, force: true });
173 }
174});