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
86 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 143, 1 version changed it
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { ExperimentalMarkerProjection as MarkerProjection } from "../experimental/projection-marker.ts";
4import { Graph } from "../graph.ts";
5import { CachedStateStorage } from "../state-source.ts";
6import { Repo, Rng } from "./helpers.ts";
7import { SqlState } from "./storage-helper.ts";
8
9test("typed marker move transaction rolls back each selected phase", () => {
10 const db = new SqlState();
11 try {
12 const repo = Repo.init({ "f.txt": "a\nb\nc\n" }),
13 graph = new Graph({ storage: new CachedStateStorage(db) });
14 graph.add(repo.graph.atom("a0"));
15 const p = new MarkerProjection(db, graph, "f.txt", "typed-" + "move-failures"),
16 before = p.apply(p.empty(), repo.graph.atom("a0"));
17 const atom = repo.edit({ move: { file: "f.txt", start: 0, end: 1, to: "f.txt", at: 3 } });
18 graph.add(atom);
19 const originalWrite = db.write.bind(db),
20 originalRemove = db.remove.bind(db),
21 events: { kind: string; collection: string; key: string }[] = [];
22 db.write = (collection, key, value) => {
23 events.push({ kind: "write", collection, key });
24 return originalWrite(collection, key, value);
25 };
26 db.remove = (collection, key) => {
27 events.push({ kind: "remove", collection, key });
28 return originalRemove(collection, key);
29 };
30 const sentinel = Error("rollback measured success");
31 assert.throws(
32 () =>
33 db.transaction(() => {
34 p.apply(before, atom);
35 throw sentinel;
36 }),
37 (e) => e === sentinel,
38 );
39 db.write = originalWrite;
40 db.remove = originalRemove;
41 const offsets = new Set([0, events.length - 1]);
42 for (const prefix of ["sequence-resolver:", "sequence-relocation-work:"]) {
43 offsets.add(events.findIndex((e) => e.collection.includes(prefix)));
44 offsets.add(events.findIndex((e) => e.kind === "remove" && e.collection.includes(prefix)));
45 }
46 const plan = events.findIndex((e) => e.collection.includes("sequence-relocation-work:"));
47 offsets.add(events.findIndex((e, i) => i > plan && e.collection.includes(":sequence")));
48 const rows = () => JSON.stringify(db.db.prepare("SELECT * FROM state ORDER BY collection,key").all());
49 const baseline = rows(),
50 results = [];
51 for (const offset of [...offsets].filter((n) => n >= 0).sort((a, b) => a - b)) {
52 let cursor = 0;
53 const fail = () => {
54 if (cursor++ === offset) throw Error("injected phase failure");
55 };
56 db.write = (c, k, v) => {
57 fail();
58 return originalWrite(c, k, v);
59 };
60 db.remove = (c, k) => {
61 fail();
62 return originalRemove(c, k);
63 };
64 assert.throws(() => p.apply(before, atom), /injected phase failure/);
65 db.write = originalWrite;
66 db.remove = originalRemove;
67 assert.equal(rows(), baseline);
68 assert.deepEqual(p.lines(before), ["a0.0", "a0.1", "a0.2"]);
69 results.push({ offset, ...events[offset] });
70 }
71 const state = p.apply(before, atom);
72 assert.deepEqual(
73 p.lines(state),
74 repo
75 .view()
76 .lines("f.txt")
77 .map((x) => x.id),
78 );
79 const writes = db.writes;
80 assert.equal(p.apply(state, atom), state);
81 assert.equal(db.writes, writes);
82 assert.equal(results.length, 7);
83 } finally {
84 db.db.close();
85 }
86});