import { test } from "node:test"; import assert from "node:assert/strict"; import { initialDocumentPanes, syncDocumentPanes, splitDocumentPane, closeDocumentPane, selectPane, readDocumentPane, splitDocumentView, splitWithContent, showInActivePane, paneOf, releaseSolePlugin } from "../../../src/lib/workspace/documentPanes.ts"; import { paneBounds, splitSibling, stackLayout } 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("Read and Split exclude each other, and a closed document resets both", () => { let state = syncDocumentPanes(initialDocumentPanes(), ["a", "b"], "a"); const pane = state.active; state = splitDocumentView(state, pane, true); assert.deepEqual([state.panes[0].reading, state.panes[0].split], [false, true]); state = readDocumentPane(state, pane, true); assert.deepEqual([state.panes[0].reading, state.panes[0].split], [true, false]); state = splitDocumentView(state, pane, true); assert.deepEqual([state.panes[0].reading, state.panes[0].split], [false, true]); // Write and Source keep the Split view. state = readDocumentPane(state, pane, false); assert.equal(state.panes[0].split, true); state = syncDocumentPanes(state, ["b"], "b"); assert.deepEqual([state.panes[0].documentId, state.panes[0].reading, state.panes[0].split], ["b", false, false]); }); 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(); }); test("a pane shows a document or a plugin view, and each tab has at most one pane", () => { let state = syncDocumentPanes(initialDocumentPanes(), ["a", "b"], "a"); const primary = state.active; state = splitWithContent(state, primary, { pluginKey: "browser" }, "web"); assert.deepEqual(state.panes.map(pane => [pane.documentId, pane.pluginKey]), [["a", null], [null, "browser"]]); assert.equal(state.active, "web"); assert.equal(splitWithContent(state, primary, { pluginKey: "browser" }, "again"), selectPane(state, "web")); // A document change does not remove the plugin pane. assert.equal(syncDocumentPanes(state, ["a", "b"], null), state); // A selected document replaces the view of the active pane. The view stays open as a tab. const replaced = syncDocumentPanes(state, ["a", "b"], "b"); assert.deepEqual(replaced.panes.map(pane => [pane.documentId, pane.pluginKey]), [["a", null], ["b", null]]); // A plugin view replaces the document of the active pane, and a shown document selects its pane. state = showInActivePane(selectPane(replaced, primary), { pluginKey: "board" }); assert.deepEqual(state.panes.map(pane => [pane.documentId, pane.pluginKey]), [[null, "board"], ["b", null]]); assert.equal(showInActivePane(state, { documentId: "b" }).active, "web"); assert.equal(paneOf(state, { pluginKey: "board" })?.id, primary); }); test("a closed plugin view leaves its pane, and the last pane gives its plugin view back to the whole area", () => { let state = syncDocumentPanes(initialDocumentPanes(), ["a"], "a"); state = splitWithContent(state, state.active, { pluginKey: "browser" }, "web", "down"); assert.deepEqual(paneBounds(state.layout).web, { left: 0, top: 50, width: 100, height: 50 }); assert.deepEqual(paneBounds(stackLayout({ direction: "right", first: "x", second: "y" })).y, { left: 0, top: 50, width: 100, height: 50 }); const closed = syncDocumentPanes(state, ["a"], null, []); assert.deepEqual(closed.panes.map(pane => pane.id), ["document-main"]); const remaining = closeDocumentPane(state, "document-main"); assert.equal(remaining.active, "web"); const released = releaseSolePlugin(remaining); assert.equal(released.pluginKey, "browser"); assert.deepEqual([released.state.panes[0].documentId, released.state.panes[0].pluginKey], [null, null]); assert.equal(releaseSolePlugin(closed).state, closed); }); test("the split sibling names the pane that a plugin view opened beside", () => { const tree = { direction: "right" as const, first: "a", second: { direction: "down" as const, first: "b", second: "c" } }; assert.deepEqual(splitSibling(tree, "c"), { direction: "down", sibling: "b" }); assert.deepEqual(splitSibling(tree, "a"), { direction: "right", sibling: "b" }); assert.equal(splitSibling("a", "a"), null); });