Files
learnharness/tests/practice.test.ts
T
amosblomqvist 4985b6f9fa chains-primary: stored chains replace typed edges; edges become a derived view
Owner rulings 2026-09-30: reasoning chains are the stored truth; the graph
canvas renders a lossy derived image. No migration, no backcompat — old
graph data was wiped.

- contracts: GraphChain (arg_-minted id, premises, conclusion) replaces
  typed edges; GraphEdge survives only as the untyped derived arrow;
  GraphSnapshot carries stored chains; supports edges retired entirely
- store: chains.jsonl ledger (edges.jsonl purged at load alongside legacy
  events.jsonl); readChains/appendChain seam; merge dedupes by
  (sorted premises, conclusion), re-assertions append under the same arg_ id
- core: deriveView mints implication nodes (name ⟹, statement via
  formatChainLine) plus premise→imp→conclusion arrows; clusters stay
  proposition-only (birthSessionPartition); chainsForSelection now groups
  STORED chains (chain rides iff every member selected) — independent
  arguments for one conclusion never union; chainsToGraph/chainsToEdges die
- extraction: extractor submits chains natively (expandFindings deleted);
  auditor receives propositions only; supports fan-out gone
- deletion: full death of a proposition kills every referencing chain;
  arg_-prefixed node deletes kill that chain; chainsRemoved stats
- UI: every arrow renders the ⟹ double shaft; supports toggle + thickness
  slider and detail-card edge sections deleted (card keeps name +
  proposition); chain table + practice pipeline read stored chains
- tests: 1188 green across the new seams
2026-09-30 03:22:52 +03:00

154 lines
6.2 KiB
TypeScript

/**
* PRACTICE-MODULE CORE: chainsForSelection over the STORED chains
* (chains-primary, owner 2026-09-30: a chain rides along iff every member
* is selected; nodes in no surviving chain are singletons) and
* chainCoverage (the set's completeness law: union of the problems' chains
* must equal the set's chains).
*/
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import type { GraphChain } from "../src/core/contracts.ts";
import { chainCoverage, chainsForSelection, formatChainLine } from "../src/core/practice.ts";
// ─── fixtures ────────────────────────────────────────────────────────────────
const n = (id: string) => ({ id });
const arg = (id: string, premises: string[], conclusion: string): GraphChain => ({
id,
premises,
conclusion,
provenance: [],
createdAt: "2026-09-30T00:00:00.000Z",
});
/** The plan's canonical shape: diamond a→b, a→c, b∧c→d — three stored
* arguments (each its own arg_ id, conclusion ids may repeat). */
const diamondNodes = [n("a"), n("b"), n("c"), n("d")];
const diamondArgs = [arg("arg_b", ["a"], "b"), arg("arg_c", ["a"], "c"), arg("arg_d", ["b", "c"], "d")];
// ─── chainsForSelection ──────────────────────────────────────────────────────
describe("chainsForSelection", () => {
it("keeps every fully-selected stored argument, each under its own id", () => {
assert.deepEqual(chainsForSelection(diamondNodes, diamondArgs), [
{ id: "arg_b", premises: ["a"], conclusion: "b" },
{ id: "arg_c", premises: ["a"], conclusion: "c" },
{ id: "arg_d", premises: ["b", "c"], conclusion: "d" },
]);
});
it("a premise-only node is NOT its own chain — it lives inside the arguments it feeds", () => {
const ids = chainsForSelection(diamondNodes, diamondArgs).map((c) => c.id);
assert.ok(!ids.includes("a")); // a is only ever a premise
});
it("an isolated node becomes a singleton chain", () => {
const chains = chainsForSelection([...diamondNodes, n("z")], diamondArgs);
assert.deepEqual(chains.at(-1), { id: "z", premises: [], conclusion: "z" });
});
it("two independent arguments for one conclusion stay DISTINCT chains", () => {
// The bijection problem that killed edge-grouping (owner, 2026-09-30):
// A∧B⇒E and C∧D⇒E must never union into A∧B∧C∧D⇒E.
const chains = chainsForSelection(
[n("a"), n("b"), n("c"), n("d"), n("e")],
[arg("arg_e1", ["a", "b"], "e"), arg("arg_e2", ["c", "d"], "e")],
);
assert.deepEqual(chains, [
{ id: "arg_e1", premises: ["a", "b"], conclusion: "e" },
{ id: "arg_e2", premises: ["c", "d"], conclusion: "e" },
]);
});
it("an argument with ANY member outside the selection is excluded entirely", () => {
// arg_ab needs b; b is not selected, so the argument is gone and
// a (nothing else left) falls back to a singleton.
const chains = chainsForSelection([n("a")], [arg("arg_ab", ["a"], "b"), arg("arg_ia", ["in"], "a")]);
assert.deepEqual(chains, [{ id: "a", premises: [], conclusion: "a" }]);
});
it("a chain referencing unknown node ids simply never applies", () => {
const chains = chainsForSelection([n("a"), n("b")], [arg("arg_ghost", ["ghost"], "b")]);
assert.deepEqual(chains, [
{ id: "a", premises: [], conclusion: "a" },
{ id: "b", premises: [], conclusion: "b" },
]);
});
it("is deterministic — chains sorted by id, premises sorted", () => {
const shuffled = chainsForSelection(
[n("d"), n("c"), n("b"), n("a")],
[arg("arg_d", ["c", "b"], "d"), arg("arg_c", ["a"], "c"), arg("arg_b", ["a"], "b")],
);
assert.deepEqual(shuffled, [
{ id: "arg_b", premises: ["a"], conclusion: "b" },
{ id: "arg_c", premises: ["a"], conclusion: "c" },
{ id: "arg_d", premises: ["b", "c"], conclusion: "d" },
]);
});
});
// ─── chainCoverage ───────────────────────────────────────────────────────────
const diamondChains = [
{ id: "arg_b", premises: ["a"], conclusion: "b" },
{ id: "arg_c", premises: ["a"], conclusion: "c" },
{ id: "arg_d", premises: ["b", "c"], conclusion: "d" },
];
describe("chainCoverage", () => {
it("complete cover: every chain exercised, no missing", () => {
const set = {
chains: diamondChains,
problems: [{ chains: ["arg_b", "arg_c"] }, { chains: ["arg_d"] }],
};
assert.deepEqual(chainCoverage(set), { missing: [], duplicated: [] });
});
it("missing: a chain no problem exercises", () => {
const set = {
chains: diamondChains,
problems: [{ chains: ["arg_d"] }],
};
assert.deepEqual(chainCoverage(set), { missing: ["arg_b", "arg_c"], duplicated: [] });
});
it("duplicated is informational — a chain exercised twice is allowed", () => {
const set = {
chains: diamondChains,
problems: [{ chains: ["arg_b", "arg_c", "arg_d"] }, { chains: ["arg_b", "arg_d"] }],
};
assert.deepEqual(chainCoverage(set), { missing: [], duplicated: ["arg_b", "arg_d"] });
});
it("problems claiming chain ids outside the set neither cover nor fail anything", () => {
const set = {
chains: [{ id: "arg_b" }] as Array<{ id: string }>,
problems: [{ chains: ["arg_b", "ghost"] }],
};
assert.deepEqual(chainCoverage(set), { missing: [], duplicated: [] });
});
});
describe("formatChainLine (the chain table's one-line shape, 2026-09-29)", () => {
const props = {
p1: { name: "Premise one" },
p2: { name: "Premise two" },
c1: { name: "Conclusion" },
solo: { name: "Standalone" },
};
it("joins sorted premise NAMES by the derivation arrow", () => {
assert.equal(formatChainLine({ premises: ["p2", "p1"], conclusion: "c1" }, props), "Premise one, Premise two → Conclusion");
});
it("a singleton shows its (no premises) marker", () => {
assert.equal(formatChainLine({ premises: [], conclusion: "solo" }, props), "Standalone (no premises)");
});
it("unknown ids fall back to the raw id (honest, never guessed)", () => {
assert.equal(formatChainLine({ premises: ["ghost"], conclusion: "c1" }, props), "ghost → Conclusion");
});
});