- 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?version=192&format=json
99 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 145, 2 versions changed it
You're reading version 192. The newest is 318, and 1 version since then changed this file.
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 assert.equal(storage.retainedEntries, 0, "scratch rollback invalidates every cached value");
36 assert.equal(storage.read("canonical", "before"), 1);
37 storage.write("canonical", "after", 2);
38 });
39 assert.equal(source.db.prepare("SELECT count(*) AS n FROM state").get()!.n, 2);
40 assert.equal(storage.retainedEntries, 2, "only the canonical read and successful counter write remain");
41 assert.equal(storage.read("canonical", "after"), 2);
42 } finally {
43 source.db.close();
44 }
45});
47test("projection errors, changed identities and escaping capabilities cannot commit scratch", () => {
48 const source = new SqlState();
49 try {
50 let checks = 0;
51 assert.throws(
52 () =>
53 withProjectionScratch(
54 source,
55 () => {
56 if (++checks === 2) throw new Error("identity changed");
57 },
58 (scratch) => {
59 scratch.write("x", "y", 1);
60 return 2;
61 },
62 ),
63 /identity changed/,
64 );
65 assert.throws(
66 () =>
67 withProjectionScratch(
68 source,
69 () => {},
70 (scratch) => {
71 scratch.write("x", "y", 1);
72 throw new Error("failed computation");
73 },
74 ),
75 /failed computation/,
76 );
77 assert.throws(
78 () =>
79 withProjectionScratch(
80 source,
81 () => {},
82 (scratch) => scratch,
83 ),
84 /clone|cloned/i,
85 );
86 assert.throws(
87 () =>
88 withProjectionScratch(
89 source,
90 () => {},
91 () => Promise.resolve(1),
92 ),
93 /synchronously/,
94 );
95 assert.equal(source.db.prepare("SELECT count(*) AS n FROM state").get()!.n, 0);
96 } finally {
97 source.db.close();
98 }
99});