Official
tabs
The tabbed detail pane beside the chat: files to edit and diffs (JavaScript UI plugin).
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/tabs@0.2.5
Permissions in 0.2.5
Files
tabs.test.ts7.1 KB
// Tab identity and content regressions, using the public pure functions.
import assert from "node:assert/strict";
import { test } from "node:test";
import {
tabFromKey,
workspaceOfKey,
activeTab,
closeTab,
extension,
fileContent,
fileResponse,
initialState,
isDirty,
LANGUAGES,
moveTab,
nativeFileContent,
nextRevision,
openRequest,
openTab,
previewKind,
runFinished,
readContent,
tabId,
tabName,
} from "./tabs.ts";
import type { TabInput } from "./tabs.ts";
const file: TabInput = { kind: "file", workspaceId: "w1", path: "src/README.md" };
test("tab ids distinguish kind, workspace and path", () => {
assert.equal(tabId(file), "file:w1:src/README.md");
assert.equal(
new Set(
[file, { ...file, kind: "diff" }, { ...file, workspaceId: "w2" }, { ...file, path: "README.md" }].map((input) =>
tabId(openRequest.omit({ nonce: true }).parse(input)),
),
).size,
4,
);
});
test("opening an existing tab keeps its position and preview", () => {
const state = initialState();
const first = openTab(state, file);
first.preview = true;
openTab(state, { kind: "terminal", workspaceId: "w1", path: "t1" });
assert.equal(openTab(state, file), first);
assert.equal(first.preview, true);
assert.equal(state.tabs.length, 2);
});
test("a tab the app brings back after a restart is made again from its key", () => {
const state = initialState();
const tab = tabFromKey(state, "file:w1:src/a:b.ts");
assert.deepEqual(tab && { kind: tab.kind, workspaceId: tab.workspaceId, path: tab.path }, { kind: "file", workspaceId: "w1", path: "src/a:b.ts" });
assert.equal(tabFromKey(state, "diagram:w1:d1")?.kind, "diagram", "a diagram's source comes back from storage");
assert.equal(tabFromKey(state, "diff:w1:a.ts")?.staged, false, "a diff brought back follows the working tree");
assert.equal(tabFromKey(state, "other:w1:a.ts"), undefined);
});
test("diff contents replace in place and default to the empty diff", () => {
const state = initialState();
const request: TabInput = { ...file, kind: "diff", diff: "first" };
const tab = openTab(state, request);
assert.deepEqual(state.contents[tab.id], { diff: "first" });
openTab(state, { ...request, diff: "second" });
assert.deepEqual(state.contents[tab.id], { diff: "second" });
openTab(state, { ...request, diff: undefined });
assert.deepEqual(state.contents[tab.id], { diff: "" });
});
test("closing selects the next tab, then the previous last tab, and clears content", () => {
const state = initialState();
const tabs = ["a", "b", "c"].map((path) => openTab(state, { ...file, path }));
state.active = tabs[1].id;
state.contents[tabs[1].id] = { text: "old" };
assert.equal(closeTab(state, tabs[1].id), tabs[2]);
assert.equal(state.contents[tabs[1].id], undefined);
assert.equal(closeTab(state, tabs[2].id), tabs[0]);
assert.equal(closeTab(state, tabs[0].id), undefined);
assert.equal(state.active, null);
});
test("closing an inactive or missing tab preserves selection; fallback uses the first tab", () => {
const state = initialState();
const first = openTab(state, file);
const second = openTab(state, { ...file, path: "next" });
closeTab(state, first.id);
closeTab(state, "missing");
assert.equal(state.active, second.id);
state.active = "missing";
assert.equal(activeTab(state), second);
});
test("file truncation starts only above two million UTF-16 code units", () => {
assert.deepEqual(fileContent(""), { text: "" });
const boundary = "a".repeat(2_000_000);
assert.equal(fileContent(boundary).text, boundary);
assert.equal(fileContent(boundary + "extra").text, boundary + "\n… file truncated");
});
test("native file metadata tracks revisions and dirtiness without document text", () => {
const first = nativeFileContent();
assert.deepEqual(first, { revision: 1, fileState: { state: "loading", dirty: false } });
assert.equal(isDirty(first), false);
first.fileState = { state: "ready", dirty: true };
assert.equal(isDirty(first), true);
assert.equal(nextRevision(first), 2);
assert.ok(!("text" in first) && !("draft" in first));
});
test("missing files preserve the runtime error while file responses stay typed", () => {
assert.deepEqual(readContent(fileResponse.parse(null)), { error: "This file no longer exists." });
assert.deepEqual(readContent(fileResponse.parse({ text: "" })), { text: "" });
assert.deepEqual(readContent(fileResponse.parse({ text: "contents" })), { text: "contents" });
assert.equal(fileResponse.safeParse({ text: 1 }).success, false);
});
test("tab labels, markdown extension and language aliases preserve defaults", () => {
const state = initialState();
assert.equal(tabName(openTab(state, file)), "README.md");
assert.equal(tabName(openTab(state, { ...file, kind: "terminal" })), "Terminal");
assert.equal(tabName(openTab(state, { ...file, kind: "terminal", path: "empty-title", title: "" })), "");
assert.equal(extension("README.MD"), "md");
assert.equal(extension("file"), "file");
assert.equal(LANGUAGES.mts, "typescript");
assert.equal(LANGUAGES.tsx, "tsx");
assert.equal(LANGUAGES.markdown, "markdown");
});
test("legacy requests validate and only finished runs match the event selector", () => {
assert.equal(openRequest.safeParse({ ...file, nonce: 1 }).success, true);
assert.equal(openRequest.safeParse({ ...file, nonce: "1" }).success, false);
assert.equal(runFinished({ event: "run_finished", outcome: {} }), true);
for (const kind of [undefined, null, {}, { event: "text_delta" }]) assert.equal(runFinished(kind), false);
});
test("a dragged tab lands on the side of the tab it was dropped on", () => {
const state = initialState();
for (const path of ["a", "b", "c", "d"]) openTab(state, { ...file, path });
const order = () => state.tabs.map((tab) => tab.path);
const id = (path: string) => tabId({ ...file, path });
moveTab(state, id("a"), id("c"), "after");
assert.deepEqual(order(), ["b", "c", "a", "d"]);
moveTab(state, id("d"), id("b"), "before");
assert.deepEqual(order(), ["d", "b", "c", "a"]);
// A kernel without `reorder` reports `into`: the tab takes the other's place.
moveTab(state, id("d"), id("c"), "into");
assert.deepEqual(order(), ["b", "c", "d", "a"]);
moveTab(state, id("a"), id("b"), "into");
assert.deepEqual(order(), ["a", "b", "c", "d"]);
// Onto itself, or a tab that has gone, nothing moves.
moveTab(state, id("b"), id("b"), "after");
moveTab(state, id("b"), id("gone"), "after");
assert.deepEqual(order(), ["a", "b", "c", "d"]);
});
test("Markdown and HTML files preview; other files only edit", () => {
assert.equal(previewKind("README.md"), "markdown");
assert.equal(previewKind("notes/CHANGES.markdown"), "markdown");
assert.equal(previewKind("site/index.html"), "html");
assert.equal(previewKind("OLD.HTM"), "html");
assert.equal(previewKind("src/a.ts"), null);
});
test("a file's, a diff's and a terminal's tab belong to the workspace in their key", () => {
assert.equal(workspaceOfKey("file:w1:src/a.ts"), "w1");
assert.equal(workspaceOfKey("diff:w2:a.ts"), "w2");
assert.equal(workspaceOfKey("terminal:w3:t1"), "w3");
assert.equal(workspaceOfKey("diagram:x"), null);
});
No comments yet.