Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
98 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 132, 1 version changed it

You're reading version 136. The newest is 318, and 2 versions since then changed this file. Go to the newestCompare with 318

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";
6
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.read("canonical", "before"), 1);
36 storage.write("canonical", "after", 2);
37 });
38 assert.equal(source.db.prepare("SELECT count(*) AS n FROM state").get()!.n, 2);
39 assert.equal(storage.retainedEntries, 1);
40 assert.equal(storage.read("canonical", "after"), 2);
41 } finally {
42 source.db.close();
43 }
44});
46test("projection errors, changed identities and escaping capabilities cannot commit scratch", () => {
47 const source = new SqlState();
48 try {
49 let checks = 0;
50 assert.throws(
51 () =>
52 withProjectionScratch(
53 source,
54 () => {
55 if (++checks === 2) throw new Error("identity changed");
56 },
57 (scratch) => {
58 scratch.write("x", "y", 1);
59 return 2;
60 },
61 ),
62 /identity changed/,
63 );
64 assert.throws(
65 () =>
66 withProjectionScratch(
67 source,
68 () => {},
69 (scratch) => {
70 scratch.write("x", "y", 1);
71 throw new Error("failed computation");
72 },
73 ),
74 /failed computation/,
75 );
76 assert.throws(
77 () =>
78 withProjectionScratch(
79 source,
80 () => {},
81 (scratch) => scratch,
82 ),
83 /clone|cloned/i,
84 );
85 assert.throws(
86 () =>
87 withProjectionScratch(
88 source,
89 () => {},
90 () => Promise.resolve(1),
91 ),
92 /synchronously/,
93 );
94 assert.equal(source.db.prepare("SELECT count(*) AS n FROM state").get()!.n, 0);
95 } finally {
96 source.db.close();
97 }
98});