import { test } from "node:test"; import assert from "node:assert/strict"; import { initialDocumentPanes, syncDocumentPanes, splitDocumentPane, closeDocumentPane, selectPane, readDocumentPane, splitDocumentView, splitWithContent, showInActivePane, paneOf, releaseSolePlugin, dropInPane, moveToNewPane, resizeDocumentPanes } from "../../../src/lib/workspace/documentPanes.ts"; import { paneBounds, splitDividers, splitSibling, stackLayout, setSplitRatio } 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); }); test("a divider changes the ratio of its split, within limits, and the narrow layout keeps the ratio", () => { let state = syncDocumentPanes(initialDocumentPanes(), ["a", "b", "c"], "a"); state = splitDocumentPane(state, state.active, "b", "right"); state = splitWithContent(state, "right", { documentId: "c" }, "low", "down"); const dividers = splitDividers(state.layout); assert.deepEqual(dividers.map(divider => [divider.path.join("/"), divider.direction, divider.ratio]), [["", "right", 0.5], ["second", "down", 0.5]]); state = resizeDocumentPanes(state, [], 0.3); state = resizeDocumentPanes(state, ["second"], 0.75); const bounds = paneBounds(state.layout); assert.equal(bounds["document-main"].width, 30); assert.deepEqual(bounds.right, { left: 30, top: 0, width: 70, height: 75 }); assert.equal(bounds.low.top, 75); assert.equal(paneBounds(stackLayout(state.layout))["document-main"].height, 30); assert.equal(paneBounds(resizeDocumentPanes(state, [], 0.02).layout)["document-main"].width, 10, "A pane keeps a tenth of the split at least"); assert.equal(setSplitRatio("only", [], 0.4), "only"); assert.equal(resizeDocumentPanes(state, ["second", "first"], 0.4), state, "A path to a pane changes nothing"); }); test("a tab drop replaces the content of a pane or opens a pane beside it, and the content leaves its old pane", () => { let state = syncDocumentPanes(initialDocumentPanes(), ["a", "b", "c"], "a"); const main = state.active; state = splitWithContent(state, main, { pluginKey: "web" }, "right"); // A document dropped in the browser pane replaces the browser there. The browser stays open as a tab. let next = dropInPane(state, { documentId: "b" }, "right", "center", "new"); assert.deepEqual(next.panes.map(pane => [pane.documentId, pane.pluginKey]), [["a", null], ["b", null]]); assert.equal(next.active, "right"); // A drop on the bottom side of a pane opens a new pane there. next = dropInPane(state, { documentId: "c" }, main, "down", "new"); assert.deepEqual(paneBounds(next.layout).new, { left: 0, top: 50, width: 50, height: 50 }); assert.equal(next.active, "new"); // A drop on the left side of a pane opens a new pane before it. next = dropInPane(state, { documentId: "c" }, "right", "left", "new"); assert.deepEqual(paneBounds(next.layout), { [main]: { left: 0, top: 0, width: 50, height: 100 }, new: { left: 50, top: 0, width: 25, height: 100 }, right: { left: 75, top: 0, width: 25, height: 100 } }); assert.equal(next.active, "new"); // A visible tab moves: its old pane closes. next = dropInPane(state, { pluginKey: "web" }, main, "down", "new"); assert.deepEqual(next.panes.map(pane => [pane.id, pane.pluginKey]), [[main, null], ["new", "web"]]); assert.deepEqual(paneBounds(next.layout).new, { left: 0, top: 50, width: 100, height: 50 }); next = dropInPane(state, { pluginKey: "web" }, main, "center", "new"); assert.deepEqual(next.panes.map(pane => pane.pluginKey), ["web"]); // A drop on the pane that shows the content only makes it active. assert.equal(dropInPane(state, { pluginKey: "web" }, "right", "down", "new").panes.length, 2); assert.equal(dropInPane(state, { documentId: "a" }, "missing", "center", "new"), state); }); test("the content of a pane moves to a new pane beside it, and the pane shows other content", () => { let state = syncDocumentPanes(initialDocumentPanes(), ["a", "b", "c"], "a"); const main = state.active; state = readDocumentPane(state, main, true); // The visible document moves to the right. Its pane shows the next document, in the default view. let next = moveToNewPane(state, main, { documentId: "b" }, { documentId: "a" }, "right", "new"); assert.deepEqual(next.panes.map(pane => [pane.id, pane.documentId, pane.reading]), [[main, "b", false], ["new", "a", false]]); assert.deepEqual(paneBounds(next.layout).new, { left: 50, top: 0, width: 50, height: 100 }); assert.equal(next.active, "new"); // A plugin view that covers the area moves below. The pane keeps its document and its view. next = moveToNewPane(state, main, { documentId: "a" }, { pluginKey: "web" }, "down", "new"); assert.deepEqual(next.panes.map(pane => [pane.documentId, pane.pluginKey, pane.reading]), [["a", null, true], [null, "web", false]]); assert.deepEqual(paneBounds(next.layout).new, { left: 0, top: 50, width: 100, height: 50 }); // The visible document moves to a new pane on the left. next = moveToNewPane(state, main, { documentId: "b" }, { documentId: "a" }, "left", "new"); assert.deepEqual(next.panes.map(pane => [pane.id, pane.documentId]), [[main, "b"], ["new", "a"]]); assert.deepEqual(paneBounds(next.layout), { new: { left: 0, top: 0, width: 50, height: 100 }, [main]: { left: 50, top: 0, width: 50, height: 100 } }); next = moveToNewPane(state, main, { documentId: "a" }, { pluginKey: "web" }, "down", "new"); // Content that a different pane shows cannot stay, and cannot move from this pane. assert.equal(moveToNewPane(next, main, { pluginKey: "web" }, { documentId: "a" }, "right", "third"), next); assert.equal(moveToNewPane(next, main, { documentId: "b" }, { pluginKey: "web" }, "right", "third"), next); assert.equal(moveToNewPane(state, "missing", { documentId: "b" }, { documentId: "a" }, "right", "new"), state); });