- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/test/state-cache.test.ts?format=json
165 linescodex-capacity-chair 65% · capacity 35%Last change capacity in version 260, 5 versions changed it
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { CachedStateStorage } from "../state-source.ts";
4import { SqlState } from "./storage-helper.ts";
6test("numeric cache pages are detached on first and repeated reads, and rollback invalidates them", () => {
7 const source = new SqlState();
8 try {
9 const cache = new CachedStateStorage(source, 4096, 4);
10 cache.write("numbers", "page", [1, 2, 3]);
11 (cache.read("numbers", "page") as number[])[0] = 99;
12 assert.deepEqual(cache.read("numbers", "page"), [1, 2, 3]);
13 (cache.read("numbers", "page") as number[]).push(4);
14 assert.deepEqual(cache.read("numbers", "page"), [1, 2, 3]);
15 assert.throws(
16 () =>
17 cache.transaction(() => {
18 cache.write("numbers", "page", [8]);
19 assert.deepEqual(cache.read("numbers", "page"), [8]);
20 throw new Error("rollback numeric page");
21 }),
22 /rollback numeric page/,
23 );
24 assert.equal(cache.retainedBytes, 0);
25 assert.deepEqual(cache.read("numbers", "page"), [1, 2, 3]);
26 cache.remove("numbers", "page");
27 assert.equal(cache.read("numbers", "page"), undefined);
28 } finally {
29 source.db.close();
30 }
31});
33test("cached scalar metadata and bounded numeric pages need no JSON decode on hits", (t) => {
34 const source = new SqlState();
35 try {
36 const cache = new CachedStateStorage(source, 8192, 8);
37 const values = [undefined, null, true, "metadata", 42, Array.from({ length: 256 }, (_, n) => n)];
38 for (let i = 0; i < values.length; i++) {
39 cache.write("rows", String(i), values[i]);
40 cache.read("rows", String(i));
41 }
42 const reads = source.reads;
43 t.mock.method(JSON, "parse", () => {
44 throw new Error("cache hit unexpectedly decoded JSON");
45 });
46 for (let i = 0; i < values.length; i++) assert.deepEqual(cache.read("rows", String(i)), values[i]);
47 assert.equal(source.reads, reads);
48 assert.ok(cache.retainedBytes <= 8192);
49 } finally {
50 source.db.close();
51 }
52});
54test("bounded writer cache copies values, invalidates writes and clears after rollback", () => {
55 const source = new SqlState();
56 const cache = new CachedStateStorage(source, 1024, 3);
57 source.write("rows", "a", { list: [1] });
58 const first = cache.read("rows", "a") as { list: number[] };
59 first.list.push(2);
60 const reads = source.reads;
61 assert.deepEqual(cache.read("rows", "a"), { list: [1] });
62 assert.equal(source.reads, reads);
63 cache.write("rows", "a", { list: [3] });
64 assert.deepEqual(cache.read("rows", "a"), { list: [3] });
65 assert.throws(
66 () =>
67 cache.transaction(() => {
68 cache.write("rows", "a", { list: [4] });
69 assert.deepEqual(cache.read("rows", "a"), { list: [4] });
70 throw new Error("rollback");
71 }),
72 /rollback/,
73 );
74 assert.equal(cache.retainedEntries, 0);
75 assert.deepEqual(cache.read("rows", "a"), { list: [3] });
76 cache.remove("rows", "a");
77 assert.equal(cache.read("rows", "a"), undefined);
78 cache.write("rows", "a", "restored");
79 assert.equal(cache.read("rows", "a"), "restored");
80 source.db.close();
81});
83test("writer cache evicts by both payload and entry count and skips oversized values", () => {
84 const source = new SqlState();
85 const cache = new CachedStateStorage(source, 1024, 3);
86 for (let i = 0; i < 100; i++) {
87 cache.write("rows", String(i), "small");
88 assert.equal(cache.read("rows", String(i)), "small");
89 assert.ok(cache.retainedBytes <= 1024);
90 assert.ok(cache.retainedEntries <= 3);
91 }
92 const reads = source.reads;
93 cache.read("rows", "99");
94 assert.equal(source.reads, reads);
95 cache.read("rows", "0");
96 assert.equal(source.reads, reads + 1);
97 cache.write("rows", "large", "x".repeat(1024));
98 const before = cache.retainedBytes;
99 cache.read("rows", "large");
100 assert.equal(cache.retainedBytes, before);
101 const afterLarge = source.reads;
102 cache.read("rows", "large");
103 assert.equal(source.reads, afterLarge + 1);
104 cache.clear();
105 assert.equal(cache.retainedBytes, 0);
106 source.db.close();
107});
109test("a rollback drops only what its transaction wrote; a nested commit's writes go with an outer rollback (abso Capacity)", () => {
110 const source = new SqlState();
111 try {
112 const cache = new CachedStateStorage(source, 8192, 16);
113 source.write("rows", "kept", "k");
114 source.write("rows", "inner", "i0");
115 source.write("rows", "outer", "o0");
116 for (const key of ["kept", "inner", "outer"]) cache.read("rows", key);
117 const reads = source.reads;
118 // A projection scratch scope: rolled back on purpose, and the rows read around it stay cached.
119 assert.throws(
120 () =>
121 cache.transaction(() => {
122 cache.write("scratch", "x", "temporary");
123 throw new Error("scratch rollback");
124 }),
125 /scratch rollback/,
126 );
127 assert.equal(cache.read("rows", "kept"), "k");
128 assert.equal(source.reads, reads, "the rows read before were not read again");
129 assert.equal(cache.read("scratch", "x"), undefined);
130 // A nested transaction commits, then the outer one rolls back: both writes are undone, and the cache forgets both.
131 assert.throws(
132 () =>
133 cache.transaction(() => {
134 cache.write("rows", "outer", "o1");
135 cache.transaction(() => cache.write("rows", "inner", "i1"));
136 assert.equal(cache.read("rows", "inner"), "i1");
137 throw new Error("outer rollback");
138 }),
139 /outer rollback/,
140 );
141 assert.equal(cache.read("rows", "inner"), "i0");
142 assert.equal(cache.read("rows", "outer"), "o0");
143 assert.equal(cache.read("rows", "kept"), "k");
144 } finally {
145 source.db.close();
146 }
147});
149test("a row over the per-row limit is read but not kept, and pushes nothing out (abso Capacity)", () => {
150 const source = new SqlState();
151 try {
152 const cache = new CachedStateStorage(source, 8192, 64, 1024);
153 source.write("rows", "small", "s");
154 source.write("rows", "big", "x".repeat(2000));
155 cache.read("rows", "small");
156 const reads = source.reads;
157 assert.equal((cache.read("rows", "big") as string).length, 2000);
158 assert.equal(cache.read("rows", "small"), "s");
159 assert.equal(source.reads, reads + 1, "the big row was read from storage once and the small one stayed cached");
160 assert.equal((cache.read("rows", "big") as string).length, 2000);
161 assert.equal(source.reads, reads + 2, "the big row isn't kept");
162 } finally {
163 source.db.close();
164 }
165});