- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/engine/test/reasons-to.test.ts?format=json
56 linescapacity 100%Last change capacity in version 242, 1 version changed it
1// The atoms whose reasons point at an atom (abso Capacity, Graph.reasonsTo): why's "reasons in" reads them instead of
2// every atom. In seq order, once each, whatever the kind and however many reasons one atom gives; kept from a fresh
3// stored graph on, and undefined on a stored graph made before, where the caller looks at every atom.
5import assert from "node:assert/strict";
6import { test } from "node:test";
7import { Graph } from "../index.ts";
8import { lines, Repo } from "./helpers.ts";
9import { SqlState } from "./storage-helper.ts";
11function history() {
12 const repo = Repo.init({ "f.txt": lines("one", "two", "three") });
13 const x = repo.edit({ author: "ann", message: "x", edits: [{ file: "f.txt", old: "one", new: "ONE" }] });
14 repo.edit({
15 author: "bob",
16 message: "y",
17 edits: [{ file: "f.txt", old: "two", new: "TWO" }],
18 reasons: [
19 { to: x.id, kind: "together", text: "same idea" },
20 { to: x.id, kind: "because-of", text: "and because of it" },
21 ],
22 });
23 repo.edit({ author: "cy", message: "unrelated", edits: [{ file: "f.txt", old: "three", new: "3" }] });
24 repo.edit({
25 author: "dee",
26 message: "z",
27 edits: [{ file: "f.txt", old: "3", new: "THREE" }],
28 reasons: [{ to: x.id, kind: "supersedes", text: "replaces it" }],
29 });
30 return { repo, x };
31}
33/** Every atom whose reasons point at id, by looking at each one: what reasonsTo must give. */
34const scanned = (graph: Graph, id: string) =>
35 [...graph.atoms()].filter((a) => a.reasons.some((r) => r.to === id)).map((a) => a.id);
37test("reasonsTo lists the atoms pointing at an atom, in seq order, once each", () => {
38 const { repo, x } = history();
39 const want = scanned(repo.graph, x.id);
40 assert.equal(want.length, 2, "bob's two reasons and dee's");
41 assert.deepEqual(repo.graph.reasonsTo(x.id), want);
42 const storage = new SqlState();
43 const stored = new Graph({ storage });
44 for (const atom of repo.graph.atoms()) stored.add(atom);
45 assert.deepEqual(new Graph({ storage }).reasonsTo(x.id), want);
46 assert.deepEqual(new Graph({ storage }).reasonsTo("a0"), []);
47});
49test("a stored graph made before the index keeps none, and says so", () => {
50 const { repo, x } = history();
51 const storage = new SqlState();
52 const stored = new Graph({ storage });
53 for (const atom of repo.graph.atoms()) stored.add(atom);
54 storage.remove("reasons-in:meta", "kept");
55 assert.equal(new Graph({ storage }).reasonsTo(x.id), undefined);
56});