{"draft":null,"from":175,"to":318,"files":[{"path":"src/engine/test/reasons-to.test.ts","added":56,"removed":0,"diff":"--- /dev/null\n+++ b/src/engine/test/reasons-to.test.ts\n@@ -0,0 +1,56 @@\n+// The atoms whose reasons point at an atom (abso Capacity, Graph.reasonsTo): why's \"reasons in\" reads them instead of\n+// every atom. In seq order, once each, whatever the kind and however many reasons one atom gives; kept from a fresh\n+// stored graph on, and undefined on a stored graph made before, where the caller looks at every atom.\n+\n+import assert from \"node:assert/strict\";\n+import { test } from \"node:test\";\n+import { Graph } from \"../index.ts\";\n+import { lines, Repo } from \"./helpers.ts\";\n+import { SqlState } from \"./storage-helper.ts\";\n+\n+function history() {\n+\tconst repo = Repo.init({ \"f.txt\": lines(\"one\", \"two\", \"three\") });\n+\tconst x = repo.edit({ author: \"ann\", message: \"x\", edits: [{ file: \"f.txt\", old: \"one\", new: \"ONE\" }] });\n+\trepo.edit({\n+\t\tauthor: \"bob\",\n+\t\tmessage: \"y\",\n+\t\tedits: [{ file: \"f.txt\", old: \"two\", new: \"TWO\" }],\n+\t\treasons: [\n+\t\t\t{ to: x.id, kind: \"together\", text: \"same idea\" },\n+\t\t\t{ to: x.id, kind: \"because-of\", text: \"and because of it\" },\n+\t\t],\n+\t});\n+\trepo.edit({ author: \"cy\", message: \"unrelated\", edits: [{ file: \"f.txt\", old: \"three\", new: \"3\" }] });\n+\trepo.edit({\n+\t\tauthor: \"dee\",\n+\t\tmessage: \"z\",\n+\t\tedits: [{ file: \"f.txt\", old: \"3\", new: \"THREE\" }],\n+\t\treasons: [{ to: x.id, kind: \"supersedes\", text: \"replaces it\" }],\n+\t});\n+\treturn { repo, x };\n+}\n+\n+/** Every atom whose reasons point at id, by looking at each one: what reasonsTo must give. */\n+const scanned = (graph: Graph, id: string) =>\n+\t[...graph.atoms()].filter((a) => a.reasons.some((r) => r.to === id)).map((a) => a.id);\n+\n+test(\"reasonsTo lists the atoms pointing at an atom, in seq order, once each\", () => {\n+\tconst { repo, x } = history();\n+\tconst want = scanned(repo.graph, x.id);\n+\tassert.equal(want.length, 2, \"bob's two reasons and dee's\");\n+\tassert.deepEqual(repo.graph.reasonsTo(x.id), want);\n+\tconst storage = new SqlState();\n+\tconst stored = new Graph({ storage });\n+\tfor (const atom of repo.graph.atoms()) stored.add(atom);\n+\tassert.deepEqual(new Graph({ storage }).reasonsTo(x.id), want);\n+\tassert.deepEqual(new Graph({ storage }).reasonsTo(\"a0\"), []);\n+});\n+\n+test(\"a stored graph made before the index keeps none, and says so\", () => {\n+\tconst { repo, x } = history();\n+\tconst storage = new SqlState();\n+\tconst stored = new Graph({ storage });\n+\tfor (const atom of repo.graph.atoms()) stored.add(atom);\n+\tstorage.remove(\"reasons-in:meta\", \"kept\");\n+\tassert.equal(new Graph({ storage }).reasonsTo(x.id), undefined);\n+});"}],"unmatched":[]}