Files
RuneStone/tests/unit/workspace/documentSession.spec.ts
2026-11-16 04:21:21 -07:00

30 lines
1.7 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { documentSessionKey, parseDocumentSession, restoreDocumentSession } from "../../../src/lib/workspace/documentSession.ts";
test("document sessions isolate workspaces and discard unsafe or duplicate paths", () => {
assert.notEqual(documentSessionKey("C:\\one"), documentSessionKey("C:\\two"));
assert.equal(parseDocumentSession("invalid"), null);
assert.equal(parseDocumentSession('{"version":2,"documents":[]}'), null);
assert.deepEqual(parseDocumentSession(JSON.stringify({ version: 1, activePath: "b.md", documents: [
{ path: "b.md", pinned: true, contents: "unsaved" }, { path: "a.md" }, { path: "b.md" },
{ path: "../outside.md" }, { path: "C:\\outside.md" }, { path: "empty:test" }, null,
] })), { version: 1, activePath: "b.md", documents: [{ path: "b.md", pinned: true }, { path: "a.md", pinned: false }] });
});
test("restoration keeps order and active tab when a missing file fails", async () => {
const opened: string[] = [];
const session = parseDocumentSession(JSON.stringify({ version: 1, activePath: "c.md",
documents: ["b.md", "missing.md", "c.md", "a.md"].map(path => ({ path })) }))!;
const active = await restoreDocumentSession(session, async item => {
if (item.path === "missing.md") throw new Error("Not found");
opened.push(item.path);
});
assert.deepEqual(opened, ["b.md", "c.md", "a.md"]);
assert.equal(active, "c.md");
assert.equal(await restoreDocumentSession({ ...session, activePath: "missing.md" }, async item => {
if (item.path === "missing.md") throw new Error("Not found");
}), "b.md");
assert.equal(await restoreDocumentSession(session, async () => { throw new Error("Unavailable"); }), null);
});