Files
explicata/tests/store.test.ts
T
thalladaandClaude Opus 5.5 bed47c0c7a UI without documents: every page a node, the library root the first; moving, copying, exporting and importing any node
The page has one head for whatever node is the page (crumbs from the library's title, a byline of
sections and nodes, its flags and ⋯); in panes the first pane is the page and every other pane has
a section's head, whatever node it shows. A prompt (a node whose sections cite its sentences) is
folded wherever it shows. The library root's sections open in place like any others; an address
outside the hoisted node un-hoists to the library, and Esc at a hoisted page's top steps out to its
parent's.

What acts on a node and everything under it acts on the node the reader is on (the focused
section, or the one a focused note or selection is in): M moves it under any node (the jump box as
a picker, the root offered too), /duplicate copies it just after itself, /export saves it, /import
reads a file in as its last child (place() prefixes the titles down to the parent), /linkhere scans
it, S lists its links (on the root, the whole library's), history's f filters to it and r reverts
it. Every node's ⋯ offers them.

The documents menu, its mode and keys, /new, /open, /rename and /linkdoc are gone; m is free and
the top bar's left is a plain label with the library's name. Addresses are /n/<node id>, and / the
root; an old /doc/ address shows the library.

Server: /api/docs gives way to /api/nodes/<id>/{tree,export,import}; link jobs take scope "node" or
"library" and pending lists ?node=<id>&subtree=1. Step-1 shims removed: docOf/documents, all of
core/library.ts (IMPORT_MAX and importTree move to seed.ts, address and resolve to address.ts),
docArg, and the old single-document replica check. revertDocTo is revertSubtreeTo.

The page scrolls the focus into view only when the focus, the page or the mode changed, not on a
render for something that arrived in the background; row flags give way on a narrow screen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
2026-11-17 01:49:12 -05:00

140 lines
8.0 KiB
TypeScript

/** The library on disk (seeding and starter files), and a node with everything under it: imported, exported, copied, moved and addressed wherever it is. */
import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { LibraryStore } from "../server/library";
import { FormatError, serializeDoc } from "../src/core/format";
import { address, resolve } from "../src/core/address";
import { resolveNode } from "../src/core/links";
import { Doc, descendants, isStale, sections, staleness, tops } from "../src/core/model";
import { SEED_TITLE, importTree, writeSeed } from "../src/core/seed";
import { seed } from "../src/seed/tree";
const prompt = readFileSync(new URL("../src/seed/prompt.txt", import.meta.url), "utf8");
const temp = () => mkdtempSync(join(tmpdir(), "ie-lib-"));
const quiet = () => {};
const MD = (title: string) => `# ${title}\n\nA root body with a phrase.\n\n## One\n\nSection text.\n\n^ "Section" gloss: a note\n`;
describe("the library on disk", () => {
it("starts an empty data directory as one library with the first tree, written once", () => {
const data = temp();
const store = new LibraryStore(data, quiet).open();
store.seedFirst((doc) => writeSeed(doc, prompt, seed));
expect(store.titles()).toEqual([SEED_TITLE]);
expect(store.main.snap().library).toBe(true);
expect(JSON.parse(readFileSync(join(data, "library.json"), "utf8")).imported).toEqual(["infinite-expand"]);
store.flush();
// A second start loads the same library (same id) and writes nothing new.
const again = new LibraryStore(data, quiet).open();
again.seedFirst(() => {
throw new Error("should not seed twice");
});
expect(again.id).toBe(store.id);
expect(again.main.stateHash()).toBe(store.main.stateHash());
});
it("imports each starter file once, as a child of the root; a deleted one stays deleted and a broken one is skipped", () => {
const data = temp();
const lib = temp();
writeFileSync(join(lib, "good-one.md"), MD("Good one"));
writeFileSync(join(lib, "Second File.md"), MD("Second"));
writeFileSync(join(lib, "broken.md"), MD("Broken").replace('"Section"', '"Missing"'));
const logs: string[] = [];
const store = new LibraryStore(data, (m) => logs.push(m)).open();
store.importLibrary(lib);
expect(store.titles()).toEqual(["Second", "Good one"]); // file-name order: "S" sorts before "g"
expect(logs.join("\n")).toMatch(/skipped broken\.md:\n {2}library\/broken\.md:9: in “One”: quote not found: "Missing"/);
const good = tops(store.main.snap()).find((id) => store.main.snap().nodes[id].title === "Good one")!;
store.main.apply([{ op: "delete", id: good }], "Delete “Good one”");
store.save("main");
const again = new LibraryStore(data, quiet).open();
again.importLibrary(lib);
expect(again.titles()).toEqual(["Second"]);
// Once fixed, the broken file is picked up.
writeFileSync(join(lib, "broken.md"), MD("Broken"));
again.importLibrary(lib);
expect(again.titles()).toEqual(["Second", "Broken"]);
});
});
describe("a node and everything under it, wherever it is", () => {
const LINKED = `# Imported\n\nA root body with a phrase.\n\n## One\n\nSection text.\n\n^ "Section" gloss: a note\n^ "text" link -> ./One\n`;
it("is imported under any node, its paths below the file's top stored from the library root, and exported back the same", () => {
const lib = Doc.library("alice");
const tides = importTree(lib, MD("Tides"), "tides.md");
const one = sections(lib.snap(), tides)[0];
// Under a section two levels down, at the end of its children.
const id = importTree(lib, LINKED, "linked.md", { parent: one });
let snap = lib.snap();
expect(snap.nodes[id].parent).toBe(one);
expect(snap.nodes[one].children.at(-1)).toBe(id);
expect([id, ...descendants(snap, id)].every((n) => snap.nodes[n].by === "llm")).toBe(true);
const inner = sections(snap, id)[0];
const link = snap.nodes[inner].children.map((c) => snap.nodes[c]).find((c) => c.ref)!;
expect(link.ref).toMatchObject({ kind: "node", id: inner, path: ["Tides", "One", "Imported", "One"] });
expect(resolveNode(snap, { kind: "node", path: ["Tides", "One", "Imported", "One"] })).toBe(inner);
// Nothing starts out stale under it; only the node it went under has a new section to summarise.
for (const n of [id, ...descendants(snap, id)]) expect(isStale(snap, n)).toBe(false);
expect(staleness(snap, one).up).toBe(true);
// Exported from where it is, its own paths are written relative to it again.
expect(serializeDoc(snap, id).text).toBe(LINKED);
// At a place among the children, when asked.
const first = importTree(lib, MD("First"), "first.md", { parent: tides, index: 0 });
snap = lib.snap();
expect(snap.nodes[tides].children[0]).toBe(first);
// A file with a bad quote writes nothing, wherever it was to go.
const before = lib.stateHash();
expect(() => importTree(lib, MD("Bad").replace('"Section"', '"Nowhere"'), "bad.md", { parent: inner })).toThrow(FormatError);
expect(lib.stateHash()).toBe(before);
});
it("is copied just after itself with everything under it, and the copy's links inside it still point into the original", () => {
const lib = Doc.library("alice");
const tides = importTree(lib, MD("Tides"), "tides.md");
const id = importTree(lib, LINKED, "linked.md", { parent: sections(lib.snap(), tides)[0] });
const parent = lib.snap().nodes[id].parent!;
const [copy] = lib.apply([{ op: "copy", id, parent, index: lib.snap().nodes[parent].children.indexOf(id) + 1, title: "Imported (copy)" }], "Duplicate").created;
const snap = lib.snap();
expect(snap.nodes[parent].children.indexOf(copy)).toBe(snap.nodes[parent].children.indexOf(id) + 1);
expect(sections(snap, copy).map((c) => snap.nodes[c].title)).toEqual(["One"]);
expect(sections(snap, copy)[0]).not.toBe(sections(snap, id)[0]);
for (const n of [copy, ...descendants(snap, copy)]) expect(isStale(snap, n)).toBe(false);
});
it("is moved under any other node, never under itself", () => {
const lib = Doc.library("alice");
const a = importTree(lib, MD("Alpha"), "a.md");
const b = importTree(lib, MD("Beta"), "b.md");
const deep = sections(lib.snap(), b)[0];
lib.apply([{ op: "move", id: a, parent: deep }], "Move under");
expect(lib.snap().nodes[a].parent).toBe(deep);
expect(() => lib.apply([{ op: "move", id: b, parent: sections(lib.snap(), a)[0] }], "Move under itself")).toThrow(/inside itself/);
// And back to the top of the library.
lib.apply([{ op: "move", id: a, parent: lib.snap().root }], "Move to the top");
expect(tops(lib.snap())).toEqual([b, a]);
});
it("has an address made of its id alone: /n/<id>, and / for the library root", () => {
const lib = Doc.library("alice");
const a = importTree(lib, MD("Tides"), "tides.md");
const b = importTree(lib, MD("Codes"), "codes.md");
const snap = lib.snap();
const one = sections(snap, a)[0];
expect(address(snap, snap.root)).toBe("/");
expect(address(snap, a)).toBe(`/n/${encodeURIComponent(a)}`);
expect(address(snap, one)).toBe(`/n/${encodeURIComponent(one)}`);
for (const id of [snap.root, a, one]) expect(resolve(snap, address(snap, id))).toEqual({ node: id });
// A moved node keeps its address; a renamed one too.
lib.apply([{ op: "move", id: one, parent: b }, { op: "update", id: one, title: "Renamed" }], "move and rename");
expect(address(lib.snap(), one)).toBe(`/n/${encodeURIComponent(one)}`);
expect(resolve(lib.snap(), `/n/${encodeURIComponent(one)}`)).toEqual({ node: one });
// What names nothing says so (the page then shows the library): a gone node, an old document address, anything else.
expect(resolve(lib.snap(), "/n/999%40999")).toEqual({ missing: "no node 999@999" });
expect(resolve(lib.snap(), "/doc/tides")).toEqual({ missing: "no page /doc/tides" });
expect(resolve(lib.snap(), "/n/%E0%A4%A")).toMatchObject({ missing: expect.any(String) });
});
});