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
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/src/engine/test/record-query-contract.test.ts?format=json
129 linescodex-capacity-policy 96% · codex-capacity-chair 4%Last change codex-capacity-chair in version 124, 2 versions changed it
1// A small live graph and a graph rebuilt from its serialized history must
2// answer the same one-file record query.
3
4import assert from "node:assert/strict";
5import { describe, it } from "node:test";
6import { buildAtom, Graph, View } from "../index.ts";
7import { lines, Repo } from "./helpers.ts";
8
9describe("record-query replay contract", () => {
10 it("matches a one-file record query after repeated writers, readers, and delete/recreate history", () => {
11 const repo = Repo.init({ "src.txt": lines("top", "bottom"), "recreated.txt": lines("old") });
12 const base = ["a0"];
13 const insertAtTopGap = (text: string) =>
14 repo.edit({ edits: [{ file: "src.txt", start: 1, end: 1, lines: [text], mode: "after" }] }, base);
15 const writerOne = insertAtTopGap("writer one");
16 const writerTwo = insertAtTopGap("writer two");
17 const readerOne = insertAtTopGap("reader one");
18 const readerTwo = insertAtTopGap("reader two");
19 const attrOne = repo.edit({ attrs: [{ file: "src.txt", name: "exec", value: "1" }] }, base);
20 const attrTwo = repo.edit({ attrs: [{ file: "src.txt", name: "exec", value: null }] }, base);
21 const deletion = repo.edit({ edits: [{ file: "recreated.txt", old: lines("old"), new: "" }] });
22 const recreation = repo.edit({ edits: [{ file: "recreated.txt", old: "", new: lines("fresh") }] });
24 // Nine existing atoms make a finite, repeatable history. The last query
25 // both replaces a line and inserts at the repeatedly written gap.
26 assert.equal(repo.graph.size, 9);
27 assert.deepEqual(recreation.deps, [deletion.id]);
28 assert.deepEqual(repo.graph.closureOf(recreation.deps), repo.graph.closureOf(["a0", deletion.id]));
29 assert.deepEqual(repo.view().attrs("src.txt"), {
30 exists: false,
31 exec: false,
32 link: null,
33 blob: null,
34 });
36 const input = {
37 author: "record-query-test",
38 message: "record one file",
39 time: 100,
40 edits: [
41 { file: "src.txt", start: 0, end: 1, lines: ["TOP"] },
42 { file: "src.txt", start: 1, end: 1, lines: ["recorded"] },
43 ],
44 attrs: [{ file: "src.txt", name: "exec" as const, value: "1" }],
45 };
46 const liveAtom = buildAtom(repo.view(), input);
47 const expectedDeps = [writerOne.id, writerTwo.id, readerOne.id, readerTwo.id, attrOne.id, attrTwo.id];
48 assert.deepEqual(liveAtom.deps, expectedDeps);
49 assert.deepEqual(repo.graph.closureOf(liveAtom.deps), repo.graph.closureOf(["a0", ...expectedDeps]));
50 assert.equal(liveAtom.deps.length, 6);
51 assert.equal(liveAtom.writes.length, 3);
52 assert.equal(liveAtom.reads.length, 2);
53 assert.equal(liveAtom.holds.length, 0);
54 assert.equal(liveAtom.seq, 9);
55 assert.deepEqual(liveAtom.deletes, ["a0.1"]);
56 assert.deepEqual(liveAtom.inserts, [
57 { file: "src.txt", parent: "a0.1", lines: ["TOP"] },
58 { file: "src.txt", parent: "a0.1", lines: ["recorded"] },
59 ]);
60 assert.deepEqual(liveAtom.attrs, [{ file: "src.txt", name: "exec", value: "1" }]);
62 const coldGraph = Graph.fromJSON(JSON.parse(JSON.stringify(repo.graph.toJSON())));
63 const coldSet = new Set(repo.shared);
64 const coldAtom = buildAtom(new View(coldGraph, coldSet), input);
65 assert.deepEqual(coldAtom, liveAtom);
66 assert.equal(JSON.stringify(coldAtom), JSON.stringify(liveAtom));
67 const perturbedInput = {
68 ...input,
69 edits: input.edits.map((edit, index) => (index === 1 ? { ...edit, lines: ["different"] } : edit)),
70 };
71 const perturbedAtom = buildAtom(new View(coldGraph, coldSet), perturbedInput);
72 assert.notDeepEqual(perturbedAtom, liveAtom);
73 assert.notEqual(JSON.stringify(perturbedAtom), JSON.stringify(liveAtom));
75 repo.graph.add(liveAtom);
76 repo.shared.add(liveAtom.id);
77 coldGraph.add(coldAtom);
78 coldSet.add(coldAtom.id);
79 const liveView = new View(repo.graph, repo.shared);
80 const coldView = new View(coldGraph, coldSet);
81 const visible = (view: View, path: string) => view.lines(path).map((line) => [line.id, line.text, line.atom]);
82 assert.deepEqual(visible(liveView, "src.txt"), visible(coldView, "src.txt"));
83 assert.deepEqual(
84 visible(liveView, "src.txt").map((line) => line[1]),
85 ["TOP", "recorded", "reader two", "reader one", "writer two", "writer one", "bottom"],
86 );
87 assert.deepEqual(liveView.attrs("src.txt"), coldView.attrs("src.txt"));
88 assert.equal(liveView.attrs("src.txt").exec, true);
89 assert.deepEqual(visible(liveView, "recreated.txt"), visible(coldView, "recreated.txt"));
90 assert.deepEqual(
91 visible(liveView, "recreated.txt").map((line) => line[1]),
92 ["fresh"],
93 );
94 assert.deepEqual(
95 liveView.sequence("recreated.txt").map((line) => [line.id, line.text, line.visible, line.deletedBy]),
96 [
97 [`${recreation.id}.0`, "fresh", true, []],
98 ["a0.0", "old", false, [deletion.id]],
99 ],
100 );
102 const recreatedFileInput = {
103 author: "record-query-test",
104 message: "record the recreated file",
105 time: 101,
106 edits: [{ file: "recreated.txt", old: lines("fresh"), new: lines("FRESH") }],
107 };
108 const liveRecreatedFileAtom = buildAtom(liveView, recreatedFileInput);
109 const coldRecreatedFileAtom = buildAtom(coldView, recreatedFileInput);
110 assert.deepEqual(liveRecreatedFileAtom.deps, [recreation.id]);
111 assert.deepEqual(coldRecreatedFileAtom, liveRecreatedFileAtom);
112 assert.equal(JSON.stringify(coldRecreatedFileAtom), JSON.stringify(liveRecreatedFileAtom));
113 repo.graph.add(liveRecreatedFileAtom);
114 repo.shared.add(liveRecreatedFileAtom.id);
115 coldGraph.add(coldRecreatedFileAtom);
116 coldSet.add(coldRecreatedFileAtom.id);
117 assert.deepEqual(
118 visible(new View(repo.graph, repo.shared), "recreated.txt"),
119 visible(new View(coldGraph, coldSet), "recreated.txt"),
120 );
121 assert.deepEqual(
122 visible(new View(repo.graph, repo.shared), "recreated.txt").map((line) => line[1]),
123 ["FRESH"],
124 );
125 assert.equal(repo.graph.size, 11);
126 assert.equal(coldGraph.size, 11);
127 assert.equal(JSON.stringify(coldGraph.toJSON()), JSON.stringify(repo.graph.toJSON()));
128 });
129});