Move the tests from scripts/ to tests/ by feature

Unit tests go to tests/unit/<area>/, browser smoke tests to
tests/smoke/<area>/, and tests against a running desktop app to
tests/native/. Files named after dates, work passes, or Kanban cards get
feature names, for example editorOctober12 -> editor/footnotes.

scripts/ keeps the build, release, and package tools. Plugin repositories
import scripts/packagePlugin.mjs, and the Terminal smoke test imports
scripts/pluginMocks.mjs, which now re-exports the harness copy.

The unit tests get their own type check (tests/tsconfig.json) with Node
types. pnpm check runs it after the app check. Add @types/node and fix
the three test mocks that it reported.
This commit is contained in:
Knotty Wyvern
2026-10-27 19:47:39 -07:00
parent 90d1940c73
commit e433bed28e
154 changed files with 269 additions and 227 deletions
+57
View File
@@ -0,0 +1,57 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { initialDocumentPanes, syncDocumentPanes, splitDocumentPane, closeDocumentPane, selectPane, readDocumentPane } from "../../../src/lib/workspace/documentPanes.ts";
import { paneBounds } from "../../../src/lib/workspace/splitLayout.ts";
import { registerSelection, selectedEditorText } from "../../../src/lib/editor/selection.ts";
test("opening a link to the right keeps its source and focuses an existing pane for duplicate targets", () => {
let state = syncDocumentPanes(initialDocumentPanes(), ["a", "b", "c"], "a");
const primary = state.active;
state = readDocumentPane(state, primary, true);
state = splitDocumentPane(state, primary, "b", "right");
assert.deepEqual(state.panes.map(pane => pane.documentId), ["a", "b"]);
assert.equal(state.active, "right");
assert.deepEqual(paneBounds(state.layout).right, { left: 50, top: 0, width: 50, height: 100 });
state = splitDocumentPane(state, "right", "a", "unused");
assert.equal(state.active, primary);
assert.equal(state.panes.length, 2);
assert.equal(state.panes[0].reading, true);
state = splitDocumentPane(state, "right", "c", "third");
assert.equal(paneBounds(state.layout).third.left, 75);
});
test("closing a split preserves the remaining pane and a later tab selection can restore its hidden document", () => {
let state = syncDocumentPanes(initialDocumentPanes(), ["a", "b"], "a");
state = splitDocumentPane(state, state.active, "b", "right");
state = closeDocumentPane(state, "right");
assert.equal(state.panes[0].documentId, "a");
assert.equal(state.layout, state.active);
state = syncDocumentPanes(state, ["a", "b"], "b");
assert.equal(state.panes[0].documentId, "b");
});
test("tab replacement stays in its focused pane, and closing another tab collapses only its empty pane", () => {
let state = syncDocumentPanes(initialDocumentPanes(), ["a", "b"], "a");
state = splitDocumentPane(state, state.active, "b", "right");
state = syncDocumentPanes(state, ["a", "c"], "c");
assert.deepEqual(state.panes.map(pane => pane.documentId), ["a", "c"]);
assert.equal(state.active, "right");
const unchanged = syncDocumentPanes(state, ["a", "c"], "c");
assert.equal(unchanged, state);
state = syncDocumentPanes(state, ["c"], "c");
assert.equal(state.panes.length, 1);
assert.equal(state.layout, "right");
assert.equal(selectPane(state, "missing"), state);
});
test("selection readers remain available in both panes and disposal cannot clear a replacement reader", () => {
const first = registerSelection("a", () => "first selection");
const second = registerSelection("b", () => "second selection");
assert.equal(selectedEditorText("a"), "first selection");
assert.equal(selectedEditorText("b"), "second selection");
const replacement = registerSelection("a", () => "new selection");
first(); second();
assert.equal(selectedEditorText("a"), "new selection");
assert.equal(selectedEditorText("b"), "");
replacement();
});
+32
View File
@@ -0,0 +1,32 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { DEFAULT_SETTINGS, SETTINGS_KEY, parseSettings, saveSettings } from "../../../src/lib/workspace/settings.ts";
test("missing or damaged preferences keep safe defaults", () => {
for (const raw of [null, "{broken", "null", "[]", "42", '"text"']) {
assert.deepEqual(parseSettings(raw), DEFAULT_SETTINGS);
}
for (const autosaveSeconds of [0, -1, 301, 1.5, "10", null]) {
assert.deepEqual(parseSettings(JSON.stringify({ autosaveEnabled: false, autosaveSeconds, showInternalFolder: false })), { autosaveEnabled: false, autosaveSeconds: 5, showInternalFolder: false });
}
assert.deepEqual(parseSettings('{"autosaveEnabled":"false","autosaveSeconds":10}'), { autosaveEnabled: true, autosaveSeconds: 10, showInternalFolder: false });
});
test("preferences persist and load at both interval limits", () => {
const store = new Map<string, string>();
for (const autosaveSeconds of [1, 23, 300]) {
const settings = { autosaveEnabled: false, autosaveSeconds, showInternalFolder: false };
saveSettings({ setItem: (key, value) => { store.set(key, value); } }, settings);
assert.deepEqual(parseSettings(store.get(SETTINGS_KEY)!), settings);
}
});
test("invalid changes never reach storage and storage failures reach the caller", () => {
let writes = 0;
const storage = { setItem: () => { writes++; } };
for (const autosaveSeconds of [NaN, Infinity, 0, 301, 2.5]) {
assert.throws(() => saveSettings(storage, { autosaveEnabled: true, autosaveSeconds, showInternalFolder: false }));
}
assert.equal(writes, 0);
assert.throws(() => saveSettings({ setItem: () => { throw new Error("Storage unavailable"); } }, { ...DEFAULT_SETTINGS }), /Storage unavailable/);
});
+18
View File
@@ -0,0 +1,18 @@
import test from "node:test";
import assert from "node:assert/strict";
import { splitPane, removePane, paneBounds, containsPane } from "../../../src/lib/workspace/splitLayout.ts";
test("nested splits preserve the source and divide the requested pane", () => {
const right = splitPane("a", "a", "b", "right");
const nested = splitPane(right, "b", "c", "down");
assert.deepEqual(paneBounds(nested), {
a: { left: 0, top: 0, width: 50, height: 100 },
b: { left: 50, top: 0, width: 50, height: 50 },
c: { left: 50, top: 50, width: 50, height: 50 },
});
assert.equal(containsPane(nested, "c"), true);
const closed = removePane(nested, "b")!;
assert.deepEqual(paneBounds(closed).c, { left: 50, top: 0, width: 50, height: 100 });
assert.equal(removePane(closed, "a"), "c");
assert.equal(removePane("c", "c"), null);
});
+46
View File
@@ -0,0 +1,46 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { tabsToClose } from "../../../src/lib/workspace/tabActions.ts";
import { Documents } from "../../../src/lib/workspace/documents.ts";
function setup() {
return new Documents({
read: async () => "original", write: async () => {}, image: async () => "data:image/png;base64,",
kind: () => "text", chrome: () => "Writing",
}, () => {});
}
const entry = (path: string) => ({ rel_path: path, name: path, kind: "file" as const });
test("group closes follow tab order and preserve pins without changing the active tab", async () => {
const docs = setup();
for (const name of ["a.md", "b.md", "c.md", "d.md"]) await docs.open(entry(name));
docs.togglePin("b.md");
docs.move("d.md", 1);
const paths = (scope: "one" | "others" | "after" | "all") => tabsToClose(docs.buffers, "d.md", scope).map(tab => tab.relPath);
assert.deepEqual(paths("one"), ["d.md"]);
assert.deepEqual(paths("others"), ["a.md", "c.md"]);
assert.deepEqual(paths("after"), ["c.md"]);
assert.deepEqual(paths("all"), ["a.md", "d.md", "c.md"]);
assert.equal(docs.activePath, "d.md");
assert.deepEqual(tabsToClose(docs.buffers, "missing.md", "all"), []);
assert.equal(tabsToClose(docs.buffers, "b.md", "one")[0].pinned, true);
});
test("pinning protects current and empty tabs from replacement, and survives rename", async () => {
const docs = setup();
await docs.open(entry("a.md"));
docs.togglePin("a.md");
await docs.open(entry("b.md"), "current");
assert.deepEqual(docs.buffers.map(tab => tab.relPath), ["a.md", "b.md"]);
docs.rename("a.md", "renamed.md");
assert.equal(docs.buffers[0].pinned, true);
docs.newTab();
const blank = docs.active!;
docs.togglePin(blank.relPath);
await docs.open(entry("c.md"));
assert.ok(docs.buffers.includes(blank));
docs.select("renamed.md");
docs.togglePin("renamed.md");
await docs.open(entry("d.md"), "current");
assert.ok(!docs.buffers.some(tab => tab.relPath === "renamed.md"));
});
+30
View File
@@ -0,0 +1,30 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { DEFAULT_WORKBENCH_LAYOUT, WORKBENCH_LAYOUT_KEY, parseWorkbenchLayout, saveWorkbenchLayout } from "../../../src/lib/workspace/workbenchLayout.ts";
test("missing and damaged layouts use defaults", () => {
for (const raw of [null, "{broken", "null", "[]", "42", '"text"']) {
assert.deepEqual(parseWorkbenchLayout(raw), DEFAULT_WORKBENCH_LAYOUT);
}
});
test("invalid layout fields do not discard valid collapsed states", () => {
for (const leftWidth of [0, -1, 159, 481, "260", null]) {
assert.deepEqual(parseWorkbenchLayout(JSON.stringify({ leftOpen: false, rightOpen: false, leftWidth, rightWidth: 320, leftView: "unknown" })), {
...DEFAULT_WORKBENCH_LAYOUT, leftOpen: false, rightOpen: false, rightWidth: 320,
});
}
assert.deepEqual(parseWorkbenchLayout('{"leftOpen":"false","rightOpen":0}'), DEFAULT_WORKBENCH_LAYOUT);
});
test("layout survives storage round trips with hidden panels and all navigation views", () => {
const store = new Map<string, string>();
for (const leftView of ["files", "search", "bookmarks"] as const) {
for (const leftWidth of [160, 321.5, 480]) {
const layout = { leftOpen: false, rightOpen: false, leftWidth, rightWidth: 400, leftView };
saveWorkbenchLayout({ setItem: (key, value) => { store.set(key, value); } }, layout);
assert.deepEqual(parseWorkbenchLayout(store.get(WORKBENCH_LAYOUT_KEY)!), layout);
}
}
assert.throws(() => saveWorkbenchLayout({ setItem: () => { throw new Error("Storage unavailable"); } }, { ...DEFAULT_WORKBENCH_LAYOUT }), /Storage unavailable/);
});
+38
View File
@@ -0,0 +1,38 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { orderWorkbenchTabs } from "../../../src/lib/workspace/workbenchTabs.ts";
import { tabsToClose } from "../../../src/lib/workspace/tabActions.ts";
import { Documents } from "../../../src/lib/workspace/documents.ts";
test("mixed tabs retain their positions through title changes, closes, and new tabs", () => {
const tabs = [
{ id: "a", relPath: "a.md", name: "a.md", pinned: true },
{ id: "b", relPath: "renamed.md", name: "renamed.md" },
{ id: "plugin-a", relPath: "plugin:a", name: "New page title" },
{ id: "plugin-b", relPath: "plugin:b", name: "Board" },
];
const ordered = orderWorkbenchTabs(tabs, ["plugin-a", "b", "a", "closed", "plugin-a"]);
assert.deepEqual(ordered.map(tab => tab.id), ["plugin-a", "b", "a", "plugin-b"]);
assert.equal(ordered[0].name, "New page title");
assert.deepEqual(tabsToClose(ordered, "plugin:a", "after").map(tab => tab.id), ["b", "plugin-b"]);
assert.deepEqual(tabsToClose(ordered, "renamed.md", "others").map(tab => tab.id), ["plugin-a", "plugin-b"]);
assert.deepEqual(tabsToClose(ordered, "plugin:b", "all").map(tab => tab.id), ["plugin-a", "b", "plugin-b"]);
});
test("replacing an empty or current document retains its tab position and creates a new buffer", async () => {
const docs = new Documents({
read: async path => path, write: async () => {}, image: async () => "",
kind: () => "text", chrome: () => "Writing",
}, () => {});
docs.newTab();
const emptyId = docs.active!.id;
const order = ["plugin-a", emptyId, "plugin-b"];
for (const rel_path of ["a.md", "b.md"]) {
const previousBuffer = docs.active!.id;
await docs.open({ rel_path, name: rel_path, kind: "file" }, "current");
assert.notEqual(docs.active!.id, previousBuffer);
assert.equal(docs.active!.tabId, emptyId);
const tabs = [{ id: "plugin-a" }, { id: "plugin-b" }, { id: docs.active!.tabId! }];
assert.deepEqual(orderWorkbenchTabs(tabs, order).map(tab => tab.id), order);
}
});