Add a workspace session for marks, renames, the index, and events
Phase 4d. session.svelte.ts holds the workspace state of each window: the marks, the note index, renames, and typed change events. It replaces refreshToken. - One rename path: session.rename serves the tree, the note title, the rename command, the link menu, plugins, and the template manager. Open documents, plugin views, the index, and the marks follow it. A title rename, the rename command, and a plugin rename now also move pins and bookmarks. - marksStore.svelte.ts is the only writer of .runestone/marks.json. Changes run in order from the latest marks. A change from another window reloads the marks. - The note index updates only the paths that change, does not rescan on window focus, reads in batches of 64, and keeps links, not text. - Plugins also hear about .runestone/ changes from other windows. - The outline uses the heading parser of heading links and Read, so lines in code blocks are no longer headings. - New note is above New file in the tree menu (card a6032af5). - New workspace/session smoke test and new unit tests for the index and the outline. pnpm check, pnpm test (347 of 347), and the full smoke suite pass, except workspace/manageTemplates, which failed one time under load and passed on retry and 4 of 4 times alone.
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
// Workspace session: a title rename moves pins and bookmarks, marks from another window reach the panes, watcher paths update the tree and backlinks, and focus does not rescan the workspace.
|
||||
import assert from "node:assert/strict";
|
||||
import { mockTauri, runSmoke, smokeUrl } from "../_harness/index.mjs";
|
||||
|
||||
await runSmoke("workspace/session", async ({ page }) => {
|
||||
await mockTauri(page, {
|
||||
commands: test => {
|
||||
const ws = test.workspace({
|
||||
root: "C:\\smoke-workspace",
|
||||
files: {
|
||||
"notes/welcome.md": "# Welcome\n",
|
||||
"notes/other.md": "# Other\n",
|
||||
".runestone/marks.json": JSON.stringify({ pins: ["notes/welcome.md"], bookmarks: ["notes/welcome.md"], order: {} }, null, 2) + "\n",
|
||||
},
|
||||
});
|
||||
const files = ws.files;
|
||||
Object.assign(test, { files, root: ws.root });
|
||||
return {
|
||||
...ws,
|
||||
workspace_rename: ({ from, to }) => {
|
||||
if (!files.has(from)) throw new Error("File is missing");
|
||||
files.set(to, files.get(from));
|
||||
files.delete(from);
|
||||
},
|
||||
spelling_menu_install: () => null,
|
||||
};
|
||||
},
|
||||
});
|
||||
const marks = () => page.evaluate(() => JSON.parse(window.__TEST__.files.get(".runestone/marks.json")));
|
||||
const emit = (kind, paths) => page.evaluate(([kind, paths]) => window.__TEST__.emit("workspace-event", { root: window.__TEST__.root, kind, paths }), [kind, paths]);
|
||||
const tree = page.locator(".tree");
|
||||
const backlinks = page.getByRole("region", { name: "Backlinks" });
|
||||
|
||||
await page.goto(smokeUrl);
|
||||
await page.getByText("smoke-workspace", { exact: true }).waitFor();
|
||||
await tree.getByText("notes", { exact: true }).click();
|
||||
await tree.getByText("welcome", { exact: true }).dblclick();
|
||||
const title = page.getByRole("button", { name: "Rename note", exact: true });
|
||||
await title.click();
|
||||
await page.getByRole("textbox", { name: "Note filename", exact: true }).fill("renamed");
|
||||
await page.keyboard.press("Enter");
|
||||
await page.waitForFunction(() => document.querySelector(".breadcrumb")?.textContent === "notes/renamed.md");
|
||||
await page.waitForFunction(() => window.__TEST__.files.get(".runestone/marks.json").includes("notes/renamed.md"));
|
||||
assert.deepEqual(await marks(), { pins: ["notes/renamed.md"], bookmarks: ["notes/renamed.md"], order: {} }, "A title rename moves the pin and the bookmark");
|
||||
await tree.getByText("renamed", { exact: true }).waitFor();
|
||||
|
||||
await page.getByRole("button", { name: "Bookmarks", exact: true }).click();
|
||||
const bookmarks = page.locator(".pane").filter({ hasText: "Bookmarks" });
|
||||
await bookmarks.getByText("notes/renamed.md").waitFor();
|
||||
// Another window bookmarks a second note. The watcher reports the marks file.
|
||||
await page.evaluate(() => window.__TEST__.files.set(".runestone/marks.json", JSON.stringify({ pins: ["notes/renamed.md"], bookmarks: ["notes/renamed.md", "notes/other.md"], order: {} })));
|
||||
await emit("modify", [".runestone/marks.json"]);
|
||||
await bookmarks.getByText("notes/other.md").waitFor();
|
||||
await page.getByRole("button", { name: "Files", exact: true }).click();
|
||||
|
||||
// A note that another program creates appears in the tree and in the backlinks, without a rescan.
|
||||
const listCalls = () => page.evaluate(() => window.__TEST__.calls.filter(call => call.command === "workspace_list").length);
|
||||
const before = await listCalls();
|
||||
await page.evaluate(() => window.__TEST__.files.set("notes/linker.md", "See [[renamed]] for details.\n"));
|
||||
await emit("create", ["notes/linker.md"]);
|
||||
await tree.getByText("linker", { exact: true }).waitFor();
|
||||
await backlinks.getByText("notes/linker.md").waitFor();
|
||||
assert.ok(await listCalls() - before <= 3, "One change lists only the folders that it touches");
|
||||
|
||||
const afterChange = await listCalls();
|
||||
await page.evaluate(() => window.dispatchEvent(new Event("focus")));
|
||||
await page.waitForTimeout(400);
|
||||
assert.equal(await listCalls(), afterChange, "Window focus does not rescan the workspace");
|
||||
|
||||
// A deleted note leaves the tree and the backlinks.
|
||||
await page.evaluate(() => window.__TEST__.files.delete("notes/linker.md"));
|
||||
await emit("remove", ["notes/linker.md"]);
|
||||
await backlinks.getByText("No other notes link to this file.").waitFor();
|
||||
assert.equal(await tree.getByText("linker", { exact: true }).count(), 0);
|
||||
console.log("PASS: title rename moves marks, marks from another window, watcher paths update the tree and backlinks, no rescan on focus.");
|
||||
});
|
||||
@@ -21,8 +21,8 @@ test("headings use rendered text, Unicode, and unique slugs", () => {
|
||||
test("section lines exclude properties and code, and include Setext headings", () => {
|
||||
const source = "---\r\ntitle: Test\r\n---\r\n\r\n```md\r\n# Fake\r\n```\r\n\r\nInstall\r\n=======\r\n\r\n## Real ###\r\n";
|
||||
assert.deepEqual(documentHeadings(source), [
|
||||
{ text: "Install", slug: "install", line: 9 },
|
||||
{ text: "Real", slug: "real", line: 12 },
|
||||
{ level: 1, text: "Install", slug: "install", line: 9 },
|
||||
{ level: 2, text: "Real", slug: "real", line: 12 },
|
||||
]);
|
||||
assert.equal(headingLinkLine(source, "fake"), null);
|
||||
});
|
||||
|
||||
@@ -162,3 +162,48 @@ test("read and list failures report incomplete coverage and remain rebuildable",
|
||||
await index.rebuild(io);
|
||||
assert.equal(index.backlinks("Target.md").length, 1);
|
||||
});
|
||||
|
||||
test("watcher paths update, add, and remove notes without a full rebuild", async () => {
|
||||
const files = new Map([["Target.md", ""], ["source.md", "[[Target]]"], ["old/note.md", "[[Target]]"]]);
|
||||
const index = new WorkspaceNoteIndex();
|
||||
await index.rebuild(workspace(files));
|
||||
assert.equal(index.backlinks("Target.md").length, 2);
|
||||
let lists = 0;
|
||||
const io = workspace(files), counted: NoteIndexIO = { ...io, list: folder => { lists++; return io.list(folder); } };
|
||||
files.set("source.md", "No link now");
|
||||
files.delete("old/note.md");
|
||||
files.set("new/deeper/linker.md", "[[Target]]");
|
||||
await index.refresh(counted, ["source.md", "old/note.md", "old", "new"]);
|
||||
assert.deepEqual(index.backlinks("Target.md").map(link => link.sourcePath), ["new/deeper/linker.md"]);
|
||||
assert.equal(lists, 4, "The root, the deleted folder, the new folder, and its subfolder");
|
||||
});
|
||||
|
||||
test("refresh keeps the unsaved links of an open draft", async () => {
|
||||
const files = new Map([["Target.md", ""], ["draft.md", ""]]);
|
||||
const index = new WorkspaceNoteIndex();
|
||||
await index.rebuild(workspace(files));
|
||||
index.update("draft.md", "[[Target]]");
|
||||
await index.refresh(workspace(files), ["draft.md"], new Map([["draft.md", "[[Target]]"]]));
|
||||
assert.equal(index.backlinks("Target.md").length, 1);
|
||||
});
|
||||
|
||||
test("a rename keeps the links of the moved note", async () => {
|
||||
const index = new WorkspaceNoteIndex();
|
||||
await index.rebuild(workspace(new Map([["Target.md", ""], ["notes/source.md", "[Up](../Target.md)"]])));
|
||||
index.rename("notes", "archive");
|
||||
assert.deepEqual(index.backlinks("Target.md").map(link => link.sourcePath), ["archive/source.md"]);
|
||||
});
|
||||
|
||||
test("bulk reads use batches of 64 and read a file that is too large for a batch alone", async () => {
|
||||
const files = new Map([["Target.md", ""], ...Array.from({ length: 70 }, (_, i) => [`n${i}.md`, "[[Target]]"] as [string, string])]);
|
||||
const io = workspace(files), batches: number[] = [], singles: string[] = [];
|
||||
const index = new WorkspaceNoteIndex();
|
||||
await index.rebuild({
|
||||
...io,
|
||||
read: async path => { singles.push(path); return io.read(path); },
|
||||
readMany: async paths => { batches.push(paths.length); return paths.map(path => path === "n5.md" ? { path, error: "The batch exceeds the 16 MiB limit." } : { path, source: files.get(path)! }); },
|
||||
});
|
||||
assert.deepEqual(batches, [64, 7]);
|
||||
assert.deepEqual(singles, ["n5.md"]);
|
||||
assert.equal(index.backlinks("Target.md").length, 70);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { buildOutline } from "../../../src/lib/workspace/outline.ts";
|
||||
|
||||
test("the Markdown outline skips code blocks and properties and shows rendered text", () => {
|
||||
const source = "---\ntitle: Note\n---\n# **Bold** title\n\n```sh\n# not a heading\n```\n\nSetext\n------\n";
|
||||
assert.deepEqual(buildOutline("note.md", source), [{ level: 1, text: "Bold title", line: 4 }, { level: 2, text: "Setext", line: 10 }]);
|
||||
});
|
||||
|
||||
test("code files list their definitions", () => {
|
||||
assert.deepEqual(buildOutline("tool.py", "import os\ndef run():\n pass\n"), [{ level: 1, text: "run", line: 2 }]);
|
||||
});
|
||||
Reference in New Issue
Block a user