Files
openidea/src/features/terminal/terminal.test.ts
T
l0ng-ai 0558c1127c feat(terminal): option to use the application title as the tab name always (#4477)
Settings › Tools › Terminal gets "Use application title as tab name" (When command is running, the default; Always; Never). The title a program sets via OSC 0/2 named the tab only while a command ran, so a CLI agent's own title did not show when shell integration could not tell a command was running.
2026-11-19 17:55:24 +08:00

595 lines
24 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { afterEach, describe, expect, it, vi } from "vitest";
import { FIT_SETTLE_MS, PROCESS_RESIZE_DELAY_MS, fitScheduler, settledResize } from "./fit";
import { isAllowedIdeShortcut, terminalKeyAction } from "./keys";
import { matchCounter } from "./FindBar";
import { buildSearchDecorations } from "./theme";
import { findTerminalLinks, resolvePath, trimUrl } from "./links";
import {
layoutGroups,
nextTabName,
removeFromLayout,
reorderTabs,
splitLayout,
tabFullTitle,
tabTitle,
trimMiddle,
type TerminalTab,
} from "./store";
import type { SessionSnapshot } from "./session";
import { usePlatform } from "@/platform/platformInfo";
import { useProject } from "@/platform/project";
import type { Settings } from "@/bindings";
import { buildTheme, mix, parseColor, terminalFont, toHex } from "./theme";
describe("theme", () => {
it("parses colour formats", () => {
expect(parseColor("#0f1012")).toEqual([15, 16, 18, 1]);
expect(parseColor("#fff")).toEqual([255, 255, 255, 1]);
expect(parseColor("#00000080")?.[3]).toBeCloseTo(0.5, 2);
expect(parseColor("rgb(1, 2, 3)")).toEqual([1, 2, 3, 1]);
expect(parseColor("rgba(1 2 3 / 50%)")).toEqual([1, 2, 3, 0.5]);
expect(parseColor("color(srgb 1 0 0.5)")).toEqual([255, 0, 127.5, 1]);
expect(parseColor("var(--x)")).toBeNull();
});
it("mixes and formats", () => {
expect(toHex(mix([0, 0, 0, 1], [255, 255, 255, 1], 0.5))).toBe("#808080");
});
it("derives a full palette from tokens", () => {
const tokens: Record<string, string> = {
"--bg-app": "#0f1012",
"--fg": "#b9bbc1",
"--fg-strong": "#e2e3e6",
"--accent": "#8fa5d6",
"--danger": "#c97a6f",
"--success": "#8fae7e",
"--warning": "#c9a35f",
"--syn-keyword": "#8fa5d6",
"--syn-annotation": "#a89bc4",
"--syn-string": "#9fb88a",
"--fg-subtle": "#6d7076",
"--control-off": "#3a3b3f",
};
const theme = buildTheme((n) => tokens[n] ?? "");
expect(theme.background).toBe("#0f1012");
expect(theme.foreground).toBe("#b9bbc1");
expect(theme.cursor).toBe("#8fa5d6");
expect(theme.red).toBe("#c97a6f");
expect(theme.brightWhite).toBe("#e2e3e6");
for (const key of ["black", "cyan", "brightRed", "brightCyan", "selectionBackground"] as const) {
expect(theme[key]).toMatch(/^#[0-9a-f]{6}$/);
}
expect(theme.brightRed).not.toBe(theme.red);
});
it("falls back when tokens are missing", () => {
const theme = buildTheme(() => "");
expect(theme.background).toBe("#0f1012");
expect(theme.red).toMatch(/^#/);
});
});
describe("terminal links", () => {
it("finds URLs and file locations", () => {
const links = findTerminalLinks("see https://example.com/x). error at src/main.rs:10:5 and ./a/b.ts:3");
expect(links.map((l) => l.text)).toEqual(["https://example.com/x", "src/main.rs:10:5", "./a/b.ts:3"]);
const file = links[1]!;
expect(file.kind === "file" && [file.path, file.line, file.col]).toEqual(["src/main.rs", 10, 5]);
});
it("supports paren locations and absolute paths", () => {
const [l] = findTerminalLinks("/Users/me/p/App.tsx(14,3): error");
expect(l?.kind === "file" && [l.path, l.line, l.col]).toEqual(["/Users/me/p/App.tsx", 14, 3]);
});
it("keeps the directories and drive of Windows paths", () => {
const loc = (text: string) =>
findTerminalLinks(text).map((l) => (l.kind === "file" ? [l.text, l.path, l.line, l.col] : l.text));
expect(loc(" --> src\\bin\\main.rs:10:5")).toEqual([["src\\bin\\main.rs:10:5", "src\\bin\\main.rs", 10, 5]]);
expect(loc(" at run (C:\\p\\web\\index.js:3:7)")).toEqual([
["C:\\p\\web\\index.js:3:7", "C:\\p\\web\\index.js", 3, 7],
]);
expect(loc("C:\\p\\Program.cs(10,5): error CS1002")).toEqual([
["C:\\p\\Program.cs(10,5)", "C:\\p\\Program.cs", 10, 5],
]);
expect(loc(".\\main.go:5:2: undefined: x")).toEqual([[".\\main.go:5:2", ".\\main.go", 5, 2]]);
expect(loc("D:/p/a/b.ts:4")).toEqual([["D:/p/a/b.ts:4", "D:/p/a/b.ts", 4, undefined]]);
});
it("ignores plain numbers and URL ports", () => {
expect(findTerminalLinks("12:30 http://localhost:3000").map((l) => l.kind)).toEqual(["url"]);
});
it("opens file:// URLs as file locations", () => {
const line = "[ERROR] file:///develop/mc/src/main/kotlin/top/A%20B.kt:15:56 Unresolved reference 'x'.";
expect(findTerminalLinks(line)).toEqual([
expect.objectContaining({ kind: "file", path: "/develop/mc/src/main/kotlin/top/A B.kt", line: 15, col: 56 }),
]);
expect(findTerminalLinks("see file:///C:/work/a.kt:3")).toEqual([
expect.objectContaining({ kind: "file", path: "C:/work/a.kt", line: 3, col: undefined }),
]);
expect(findTerminalLinks("file://host/srv/x.txt")).toEqual([
expect.objectContaining({ kind: "file", path: "/srv/x.txt", line: undefined }),
]);
});
it("trims trailing punctuation from URLs", () => {
expect(trimUrl("https://a.dev/x.")).toBe("https://a.dev/x");
expect(trimUrl("https://a.dev/(x)")).toBe("https://a.dev/(x)");
expect(trimUrl("https://a.dev/x)")).toBe("https://a.dev/x");
});
it("resolves paths against the cwd", () => {
expect(resolvePath("src/a.ts", "/p")).toBe("/p/src/a.ts");
expect(resolvePath("../b.ts", "/p/web")).toBe("/p/b.ts");
expect(resolvePath("/abs/c.ts", "/p")).toBe("/abs/c.ts");
expect(resolvePath("~/d.ts", "/p", "/Users/me")).toBe("/Users/me/d.ts");
});
it("resolves paths against a Windows cwd (#705)", () => {
expect(resolvePath("a/main.php", "C:\\Users\\me\\proj")).toBe("C:/Users/me/proj/a/main.php");
expect(resolvePath("a\\main.php", "C:/Users/me/proj")).toBe("C:/Users/me/proj/a/main.php");
expect(resolvePath("C:\\abs\\d.ts", "C:/p")).toBe("C:/abs/d.ts");
expect(resolvePath("~/e.ts", "C:/p", "C:\\Users\\me")).toBe("C:/Users/me/e.ts");
expect(resolvePath("../f.ts", "C:/p/web")).toBe("C:/p/f.ts");
});
});
describe("terminal keys", () => {
const key = (k: Partial<KeyboardEvent>) =>
({ key: "", code: "", metaKey: false, ctrlKey: false, altKey: false, shiftKey: false, ...k }) as KeyboardEvent;
it("sends Ctrl+letter to the shell instead of IDE shortcuts", () => {
expect(terminalKeyAction(key({ key: "r", code: "KeyR", ctrlKey: true }), false)).toEqual({
kind: "send",
data: "\x12",
});
expect(terminalKeyAction(key({ key: "d", code: "KeyD", ctrlKey: true }), false)).toEqual({
kind: "send",
data: "\x04",
});
expect(terminalKeyAction(key({ key: "c", code: "KeyC", ctrlKey: true }), false)).toEqual({
kind: "send",
data: "\x03",
});
expect(terminalKeyAction(key({ key: "C", code: "KeyC", ctrlKey: true, shiftKey: true }), true)).toEqual({
kind: "copy",
});
expect(terminalKeyAction(key({ key: "V", code: "KeyV", ctrlKey: true, shiftKey: true }), false)).toEqual({
kind: "paste",
});
// Ctrl+Shift+T is New Tab in the IDE keymap; Ctrl+T still goes to the shell.
expect(terminalKeyAction(key({ key: "T", code: "KeyT", ctrlKey: true, shiftKey: true }), false)).toEqual({
kind: "pass",
});
expect(terminalKeyAction(key({ key: "t", code: "KeyT", ctrlKey: true }), false)).toEqual({
kind: "send",
data: "\x14",
});
// AZERTY: the key typing "z" is physically KeyW; Ctrl+Z must send ^Z (SIGTSTP), not ^W.
expect(terminalKeyAction(key({ key: "z", code: "KeyW", ctrlKey: true }), false)).toEqual({
kind: "send",
data: "\x1a",
});
// Non-QWERTY Ctrl+Shift+C / V / T match the typed letter, not the physical key.
expect(terminalKeyAction(key({ key: "C", code: "KeyI", ctrlKey: true, shiftKey: true }), true)).toEqual({
kind: "copy",
});
expect(terminalKeyAction(key({ key: "V", code: "KeyPeriod", ctrlKey: true, shiftKey: true }), false)).toEqual({
kind: "paste",
});
expect(terminalKeyAction(key({ key: "T", code: "KeyK", ctrlKey: true, shiftKey: true }), false)).toEqual({
kind: "pass",
});
expect(terminalKeyAction(key({ key: "J", code: "KeyC", ctrlKey: true, shiftKey: true }), true)).toEqual({
kind: "send",
data: "\x0a",
});
});
it.each([
{ key: "c", code: "KeyC", ctrlKey: true },
{ key: "c", code: "KeyI", ctrlKey: true },
{ key: "C", code: "KeyC", ctrlKey: true, shiftKey: true },
{ key: "c", code: "KeyC", metaKey: true },
])("copies terminal selections with %o", (stroke) => {
usePlatform.setState({ os: "windows" });
try {
expect(terminalKeyAction(key(stroke), true)).toEqual({ kind: "copy" });
} finally {
usePlatform.setState({ os: "macos" });
}
});
it("keeps Ctrl+C as interrupt on macOS even with a selection", () => {
usePlatform.setState({ os: "macos" });
expect(terminalKeyAction(key({ key: "c", code: "KeyC", ctrlKey: true }), true)).not.toEqual({ kind: "copy" });
});
it("implements macOS conveniences", () => {
expect(terminalKeyAction(key({ key: "c", metaKey: true }), true)).toEqual({ kind: "copy" });
expect(terminalKeyAction(key({ key: "c", metaKey: true }), false)).toEqual({ kind: "pass" });
expect(terminalKeyAction(key({ key: "k", metaKey: true }), false)).toEqual({ kind: "clear" });
expect(terminalKeyAction(key({ key: "t", metaKey: true }), false)).toEqual({ kind: "pass" });
expect(terminalKeyAction(key({ key: "v", metaKey: true }), false)).toEqual({ kind: "paste" });
expect(terminalKeyAction(key({ key: "f", metaKey: true }), false)).toEqual({ kind: "find" });
expect(terminalKeyAction(key({ key: "ArrowLeft", altKey: true }), false)).toEqual({ kind: "send", data: "\x1bb" });
expect(terminalKeyAction(key({ key: "Escape" }), false)).toEqual({ kind: "send", data: "\x1b" });
});
it("leaves other IDE shortcuts alone", () => {
expect(terminalKeyAction(key({ key: "F12", altKey: true }), false)).toEqual({ kind: "pass" });
expect(terminalKeyAction(key({ key: "1", metaKey: true }), false)).toEqual({ kind: "pass" });
expect(terminalKeyAction(key({ key: "a" }), false)).toEqual({ kind: "pass" });
});
it("sends Shift+Enter as Esc+Return, a newline for Claude Code instead of submitting", () => {
expect(terminalKeyAction(key({ key: "Enter", code: "Enter", shiftKey: true }), false)).toEqual({
kind: "send",
data: "\x1b\r",
});
expect(terminalKeyAction(key({ key: "Enter", code: "NumpadEnter", shiftKey: true }), true)).toEqual({
kind: "send",
data: "\x1b\r",
});
expect(terminalKeyAction(key({ key: "Enter", code: "Enter" }), false)).toEqual({ kind: "pass" });
for (const mod of [{ ctrlKey: true }, { altKey: true }, { metaKey: true }]) {
expect(terminalKeyAction(key({ key: "Enter", code: "Enter", shiftKey: true, ...mod }), false)).toEqual({
kind: "pass",
});
}
});
it("clears a selection with Escape before sending it", () => {
expect(terminalKeyAction(key({ key: "Escape" }), true)).toEqual({ kind: "clearSelection" });
expect(terminalKeyAction(key({ key: "Escape", shiftKey: true }), true)).toEqual({ kind: "send", data: "\x1b" });
});
it("lets shortcuts of IDEA's allowed actions reach the IDE", () => {
const bindings: Array<[string, string]> = [
["Ctrl+Shift+A", "navigate.findAction"],
["Ctrl+N", "navigate.gotoClass"],
["Ctrl+E", "navigate.recentFiles"],
["Ctrl+K", "vcs.commit"],
["Ctrl+Shift+K", "vcs.push"],
["Shift+Escape", "toolwindow.hideActive"],
["Ctrl+W Ctrl+F", "terminal.find"],
];
expect(isAllowedIdeShortcut("Ctrl+Shift+A", bindings)).toBe(true);
expect(isAllowedIdeShortcut("Ctrl+N", bindings)).toBe(true);
expect(isAllowedIdeShortcut("Ctrl+Shift+K", bindings)).toBe(true);
expect(isAllowedIdeShortcut("Shift+Escape", bindings)).toBe(true);
expect(isAllowedIdeShortcut("Ctrl+W", bindings)).toBe(true);
// Ctrl+E (end of line) and actions outside the list stay with the shell.
expect(isAllowedIdeShortcut("Ctrl+E", bindings)).toBe(false);
expect(isAllowedIdeShortcut("Ctrl+K", bindings)).toBe(false);
expect(isAllowedIdeShortcut("Ctrl+R", bindings)).toBe(false);
});
});
describe("tab names", () => {
it("numbers duplicates like IDEA", () => {
expect(nextTabName([])).toBe("Local");
expect(nextTabName(["Local"])).toBe("Local (2)");
expect(nextTabName(["Local", "Local (2)"])).toBe("Local (3)");
expect(nextTabName(["Local"], "api")).toBe("api");
});
});
describe("tab order", () => {
const ids = (tabs: { id: string }[]) => tabs.map((t) => t.id).join("");
const tabs = [{ id: "a" }, { id: "b" }, { id: "c" }];
it("moves a dragged tab to the insertion index", () => {
expect(ids(reorderTabs(tabs, "a", 3))).toBe("bca");
expect(ids(reorderTabs(tabs, "a", 2))).toBe("bac");
expect(ids(reorderTabs(tabs, "c", 0))).toBe("cab");
expect(ids(reorderTabs(tabs, "b", 1))).toBe("abc");
expect(ids(reorderTabs(tabs, "b", 2))).toBe("abc");
});
it("ignores unknown tabs", () => expect(ids(reorderTabs(tabs, "x", 0))).toBe("abc"));
});
describe("terminal find", () => {
it("formats the match counter", () => {
expect(matchCounter(null, "x")).toBe("");
expect(matchCounter({ index: 2, count: 12 }, "x")).toBe("3/12");
expect(matchCounter({ index: -1, count: 2000 }, "x")).toBe("2000+");
expect(matchCounter({ index: -1, count: 0 }, "x")).toBe("0 results");
expect(matchCounter({ index: 0, count: 3 }, "")).toBe("");
});
it("builds #rrggbb search decorations from tokens", () => {
const d = buildSearchDecorations((n) => (n === "--match" ? "#8fa5d6" : n === "--bg-app" ? "#000000" : ""));
for (const v of Object.values(d)) expect(v).toMatch(/^#[0-9a-f]{6}$/);
expect(d.matchOverviewRuler).toBe("#8fa5d6");
const fromSearchTokens = buildSearchDecorations((n) => {
if (n === "--bg-app") return "#191a1c";
if (n === "--bg-search-match") return "#ba9752";
if (n === "--bg-search-match-current") return "#f2c55c";
return "";
});
expect(fromSearchTokens.matchBackground).toBe("#ba9752");
expect(fromSearchTokens.activeMatchBackground).toBe("#f2c55c");
expect(fromSearchTokens.activeMatchBorder).toBe("#f2c55c");
});
});
describe("split layout", () => {
it("nests a split when the orientation changes", () => {
let l = splitLayout("a", "a", "b", "vertical", "g1");
expect(l).toEqual({ id: "g1", orientation: "vertical", children: ["a", "b"] });
l = splitLayout(l, "b", "c", "horizontal", "g2");
expect(l).toEqual({
id: "g1",
orientation: "vertical",
children: ["a", { id: "g2", orientation: "horizontal", children: ["b", "c"] }],
});
l = splitLayout(l, "a", "d", "vertical", "g3");
expect(layoutGroups(l)).toEqual(["a", "d", "b", "c"]);
});
it("collapses single-child splits on removal", () => {
const l = splitLayout(splitLayout("a", "a", "b", "vertical", "g1"), "b", "c", "horizontal", "g2");
expect(removeFromLayout(l, "a")).toEqual({ id: "g2", orientation: "horizontal", children: ["b", "c"] });
expect(removeFromLayout(l, "c")).toEqual({ id: "g1", orientation: "vertical", children: ["a", "b"] });
expect(removeFromLayout("a", "a")).toBeNull();
});
});
describe("terminalFont", () => {
const BASE = '"JetBrains Mono", "JetBrains Mono Variable", ui-monospace, Menlo, monospace';
const t = {
useEditorFont: true,
fontFamily: "JetBrains Mono",
fontSize: 10,
lineHeight: 1,
fallbackFontFamily: "",
columnWidth: 1,
};
const editor = { fontFamily: "JetBrains Mono", fontSize: 10, lineHeight: 12 };
const head = (family: string) => family.slice(0, family.indexOf('"Symbols Nerd Font"'));
it.each([
// #26: "Use editor font" with the default family must reach the bundled face, not the system monospace.
[
"editor font, default family",
t,
editor,
`"JetBrains Mono", "JetBrains Mono Variable", ${BASE.replace(", monospace", "")}, `,
],
[
"editor font, no editor settings",
t,
undefined,
`"JetBrains Mono", "JetBrains Mono Variable", ${BASE.replace(", monospace", "")}, `,
],
[
"editor font, custom family",
t,
{ ...editor, fontFamily: "Fira Code" },
`"Fira Code", ${BASE.replace(", monospace", "")}, `,
],
[
"editor font, editor fallback",
t,
{ ...editor, fallbackFont: "PingFang SC" },
`"JetBrains Mono", "JetBrains Mono Variable", "PingFang SC", ${BASE.replace(", monospace", "")}, `,
],
[
"own font, default family",
{ ...t, useEditorFont: false },
editor,
`"JetBrains Mono", "JetBrains Mono Variable", ${BASE.replace(", monospace", "")}, `,
],
[
"own font, custom + fallback",
{ ...t, useEditorFont: false, fontFamily: "Hack", fallbackFontFamily: "PingFang SC" },
editor,
`"Hack", "PingFang SC", ${BASE.replace(", monospace", "")}, `,
],
])("%s resolves through the bundled stack", (_, settings, ed, expected) => {
const font = terminalFont(settings, ed, BASE);
expect(head(font.family)).toBe(expected);
expect(font.family.endsWith(", monospace")).toBe(true);
expect(font.family).not.toContain("var(");
});
it("resolves bundled Inter in inherited, independent and fallback selections", () => {
expect(terminalFont(t, { ...editor, fontFamily: "Inter" }, BASE).family).toMatch(/^"Inter", "Inter Variable",/);
expect(terminalFont({ ...t, useEditorFont: false, fontFamily: "Inter" }, editor, BASE).family).toMatch(
/^"Inter", "Inter Variable",/,
);
expect(terminalFont({ ...t, fallbackFontFamily: "Inter" }, editor, BASE).family).toContain(
'"Inter", "Inter Variable"',
);
expect(terminalFont(t, { ...editor, fallbackFont: "Inter" }, BASE).family).toContain('"Inter", "Inter Variable"');
});
it("prefers the terminal's fallback over the editor's", () => {
const font = terminalFont(
{ ...t, fallbackFontFamily: "Sarasa Mono SC" },
{ ...editor, fallbackFont: "PingFang SC" },
BASE,
);
expect(font.family).toContain('"Sarasa Mono SC"');
expect(font.family).not.toContain("PingFang SC");
});
it("follows the editor's Typography Settings weights with Use editor font", () => {
const f = terminalFont(t, { ...editor, fontWeight: 300, boldFontWeight: 600 }, BASE);
expect([f.weight, f.boldWeight]).toEqual([300, 600]);
const d = terminalFont(t, editor, BASE);
expect([d.weight, d.boldWeight]).toEqual([400, 700]);
});
it("keeps regular / bold for the terminal's own font", () => {
const f = terminalFont({ ...t, useEditorFont: false }, { ...editor, fontWeight: 300, boldFontWeight: 600 }, BASE);
expect([f.weight, f.boldWeight]).toEqual([400, 700]);
});
it("keeps the terminal's line height with Use editor font", () => {
expect(terminalFont(t, { ...editor, lineHeight: 21 }, BASE).lineHeight).toBe(1);
expect(terminalFont({ ...t, lineHeight: 1.2 }, editor, BASE).lineHeight).toBe(1.2);
});
it("prefers full-width Nerd Font fallbacks ahead of shrunk Mono variants", () => {
const { family } = terminalFont(t, editor, BASE);
expect(family.indexOf('"Symbols Nerd Font"')).toBeLessThan(family.indexOf('"Symbols Nerd Font Mono"'));
expect(family.indexOf('"JetBrainsMono Nerd Font"')).toBeLessThan(family.indexOf('"JetBrainsMono Nerd Font Mono"'));
});
it("turns the column width into letter spacing", () => {
expect(terminalFont({ ...t, useEditorFont: false }, editor, BASE).letterSpacing).toBe(0);
expect(terminalFont({ ...t, useEditorFont: false, columnWidth: 1.5 }, editor, BASE).letterSpacing).toBe(3);
});
});
describe("terminal tab titles", () => {
const tab: TerminalTab = { id: "t1", name: "Local", customName: null, shell: null, session: "s1", group: "g1" };
const snap = (appTitle: string | null, commandRunning = true): Record<string, SessionSnapshot> => ({
s1: {
status: "running",
exitCode: null,
title: "zsh",
appTitle,
commandRunning,
progress: null,
working: commandRunning,
cwd: "/p",
unseenDone: false,
},
});
it("shows the default name at the prompt, whatever title the shell sets", () => {
expect(tabTitle(tab, snap("thomas@mac: ~/repo", false))).toBe("Local");
expect(tabFullTitle(tab, snap("thomas@mac: ~/repo", false))).toBe("Local");
});
it("follows the title the running program sets, else the default name", () => {
expect(tabTitle(tab, snap(null))).toBe("Local");
expect(tabTitle(tab, snap("vim notes.md"))).toBe("vim notes.md");
const long = "ssh deploy@build-server-01.internal.example.com";
expect(tabTitle(tab, snap(long))).toHaveLength(30);
expect(tabTitle(tab, snap(long))).toBe(trimMiddle(long, 30));
expect(tabFullTitle(tab, snap(long))).toBe(long);
});
it("uses the program's title always or never, per Settings › Terminal", () => {
const set = (appTitleAsTabName: string) =>
useProject.setState({ settings: { terminal: { appTitleAsTabName } } as unknown as Settings });
try {
set("always");
expect(tabTitle(tab, snap("claude", false))).toBe("claude");
set("off");
expect(tabTitle(tab, snap("vim notes.md"))).toBe("Local");
} finally {
useProject.setState({ settings: null });
}
});
it("keeps a name the user gave the tab", () => {
const renamed = { ...tab, customName: "Server" };
expect(tabTitle(renamed, snap("top"))).toBe("Server");
expect(tabFullTitle(renamed, snap("top"))).toBe("Server");
});
it("elides the middle", () => {
expect(trimMiddle("abcdefghij", 5)).toBe("ab…ij");
expect(trimMiddle("abc", 5)).toBe("abc");
});
});
describe("fit scheduling", () => {
afterEach(() => vi.useRealTimers());
it("fits an idle terminal on the next frame, and a resizing one once it settles", () => {
vi.useFakeTimers();
const fit = vi.fn();
const f = fitScheduler(fit);
f.schedule();
vi.advanceTimersToNextFrame();
expect(fit).toHaveBeenCalledTimes(1);
for (let i = 0; i < 30; i++) {
f.schedule();
vi.advanceTimersByTime(16);
}
expect(fit).toHaveBeenCalledTimes(1);
vi.advanceTimersByTime(FIT_SETTLE_MS);
expect(fit).toHaveBeenCalledTimes(2);
vi.advanceTimersByTime(FIT_SETTLE_MS);
f.schedule();
f.cancel();
vi.advanceTimersByTime(FIT_SETTLE_MS * 2);
expect(fit).toHaveBeenCalledTimes(2);
});
it("fits a lone second request after it settles without needing another request", () => {
vi.useFakeTimers();
const fit = vi.fn();
const f = fitScheduler(fit);
f.schedule();
vi.advanceTimersToNextFrame();
vi.advanceTimersByTime(40);
f.schedule();
vi.advanceTimersByTime(FIT_SETTLE_MS - 1);
expect(fit).toHaveBeenCalledTimes(1);
vi.advanceTimersByTime(1);
expect(fit).toHaveBeenCalledTimes(2);
expect(vi.getTimerCount()).toBe(0);
});
it("fits without animation frames in a background window", () => {
vi.useFakeTimers();
const frame = vi.spyOn(window, "requestAnimationFrame").mockReturnValue(0);
const fit = vi.fn();
try {
fitScheduler(fit).schedule();
vi.advanceTimersByTime(100);
expect(fit).toHaveBeenCalledOnce();
expect(vi.getTimerCount()).toBe(0);
} finally {
frame.mockRestore();
}
});
it.each([false, true])("cancels all pending work after repeated requests: %s", (repeated) => {
vi.useFakeTimers();
const fit = vi.fn();
const f = fitScheduler(fit);
f.schedule();
if (repeated) f.schedule();
f.cancel();
expect(vi.getTimerCount()).toBe(0);
vi.advanceTimersByTime(FIT_SETTLE_MS * 2);
expect(fit).not.toHaveBeenCalled();
});
it("flushes a deferred fit immediately and leaves no delayed duplicate", () => {
vi.useFakeTimers();
const fit = vi.fn();
const f = fitScheduler(fit);
f.schedule();
f.schedule();
f.flush();
expect(fit).toHaveBeenCalledOnce();
expect(vi.getTimerCount()).toBe(0);
vi.advanceTimersByTime(FIT_SETTLE_MS * 2);
expect(fit).toHaveBeenCalledOnce();
});
it("reports only the size the terminal settles on", () => {
vi.useFakeTimers();
const report = vi.fn();
const r = settledResize(report);
r.resized(80, 20);
vi.advanceTimersByTime(PROCESS_RESIZE_DELAY_MS / 2);
r.resized(80, 24);
vi.advanceTimersByTime(PROCESS_RESIZE_DELAY_MS);
expect(report).toHaveBeenCalledExactlyOnceWith(80, 24);
});
});