- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/test/projection-scratch.test.ts?format=json
101 linescodex-capacity-chair 97% · capacity 3%Last change capacity in version 253, 3 versions changed it
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { withProjectionScratch } from "../projection-scratch.ts";
4import { CachedStateStorage, type GraphStateStorage } from "../state-source.ts";
5import { SqlState } from "./storage-helper.ts";
7test("projection scratch is isolated, detached and rolled back inside canonical transactions", () => {
8 const source = new SqlState();
9 const storage = new CachedStateStorage(source);
10 try {
11 storage.transaction(() => {
12 storage.write("canonical", "before", 1);
13 let escaped: GraphStateStorage | undefined;
14 const result = withProjectionScratch(
15 storage,
16 () => {},
17 (scratch) => {
18 escaped = scratch;
19 scratch.write("canonical", "before", 99);
20 const inner = withProjectionScratch(
21 storage,
22 () => {},
23 (nested) => {
24 assert.equal(nested.read("canonical", "before"), undefined);
25 nested.write("canonical", "before", 101);
26 return nested.read("canonical", "before");
27 },
28 );
29 assert.equal(inner, 101);
30 return { value: scratch.read("canonical", "before"), data: new Int32Array([1, 2]) };
31 },
32 );
33 assert.deepEqual(result, { value: 99, data: new Int32Array([1, 2]) });
34 assert.throws(() => escaped!.write("x", "y", true), /ended/);
35 // A scratch rollback drops only what the scratch wrote (abso Capacity): the canonical value written before it, in
36 // the outer transaction, stays cached and is still right.
37 assert.equal(storage.retainedEntries, 1, "only the canonical value written before the scratch remains");
38 assert.equal(storage.read("canonical", "before"), 1);
39 storage.write("canonical", "after", 2);
40 });
41 assert.equal(source.db.prepare("SELECT count(*) AS n FROM state").get()!.n, 2);
42 assert.equal(storage.retainedEntries, 2, "only the canonical read and successful counter write remain");
43 assert.equal(storage.read("canonical", "after"), 2);
44 } finally {
45 source.db.close();
46 }
47});
49test("projection errors, changed identities and escaping capabilities cannot commit scratch", () => {
50 const source = new SqlState();
51 try {
52 let checks = 0;
53 assert.throws(
54 () =>
55 withProjectionScratch(
56 source,
57 () => {
58 if (++checks === 2) throw new Error("identity changed");
59 },
60 (scratch) => {
61 scratch.write("x", "y", 1);
62 return 2;
63 },
64 ),
65 /identity changed/,
66 );
67 assert.throws(
68 () =>
69 withProjectionScratch(
70 source,
71 () => {},
72 (scratch) => {
73 scratch.write("x", "y", 1);
74 throw new Error("failed computation");
75 },
76 ),
77 /failed computation/,
78 );
79 assert.throws(
80 () =>
81 withProjectionScratch(
82 source,
83 () => {},
84 (scratch) => scratch,
85 ),
86 /clone|cloned/i,
87 );
88 assert.throws(
89 () =>
90 withProjectionScratch(
91 source,
92 () => {},
93 () => Promise.resolve(1),
94 ),
95 /synchronously/,
96 );
97 assert.equal(source.db.prepare("SELECT count(*) AS n FROM state").get()!.n, 0);
98 } finally {
99 source.db.close();
100 }
101});