import assert from "node:assert/strict"; import test, { before } from "node:test"; import React from "react"; import { renderToStaticMarkup } from "react-dom/server"; import { I18nProvider } from "../application/i18n/I18nProvider"; import { loadLocaleMessages } from "../application/i18n/messages"; import type { Host, TerminalSession } from "../types"; import { QuickSwitcher } from "./QuickSwitcher"; const host = (overrides: Partial = {}): Host => ({ id: "saved-1", label: "core-switch", hostname: "10.1.1.1", username: "admin", tags: [], os: "linux", protocol: "ssh", ...overrides, }) as Host; const renderSwitcher = ( query: string, results: Host[] = [], locale = "en", ) => renderToStaticMarkup( undefined} onSelect={() => undefined} onSelectTab={() => undefined} onClose={() => undefined} onQuickConnect={() => undefined} showSftpTab={false} /> , ); // Non-English dictionaries load on demand; static rendering runs no effects. before(async () => { await loadLocaleMessages("zh-TW"); await loadLocaleMessages("zh-CN"); }); test("quick switcher keeps a matching host ahead of an address row", () => { const markup = renderSwitcher("10.1.1.1", [host()]); const quickConnectAt = markup.indexOf("Quick connect to 10.1.1.1"); const hostAt = markup.indexOf("core-switch"); const selectedAt = markup.indexOf("bg-primary/15"); assert.ok(selectedAt >= 0); assert.ok(hostAt > selectedAt); assert.ok(quickConnectAt > hostAt); assert.equal(markup.indexOf("bg-primary/15", selectedAt + 1), -1); }); test("quick switcher does not steal Enter from a saved host label", () => { for (const label of ["lab", "web01", "prod", "lab-switch"]) { const markup = renderSwitcher(label, [host({ id: label, label, hostname: "10.0.0.5" })]); const hostAt = markup.indexOf(`>${label}<`); const selectedAt = markup.indexOf("bg-primary/15"); assert.equal(markup.includes("Quick connect to"), false, label); assert.ok(selectedAt >= 0 && hostAt > selectedAt, label); } }); test("quick switcher keeps a matching open session ahead of an address row", () => { const markup = renderToStaticMarkup( undefined} onSelect={() => undefined} onSelectTab={() => undefined} onClose={() => undefined} onQuickConnect={() => undefined} showSftpTab={false} /> , ); const sessionAt = markup.indexOf("open-core"); const quickConnectAt = markup.indexOf("Quick connect to 10.4.4.4"); const selectedAt = markup.indexOf("bg-primary/15"); assert.ok(selectedAt >= 0); assert.ok(sessionAt > selectedAt); assert.ok(quickConnectAt > sessionAt); assert.equal(markup.indexOf("bg-primary/15", selectedAt + 1), -1); }); test("quick switcher offers quick connect when nothing else matches", () => { const markup = renderSwitcher("root@10.2.0.8:2222"); assert.match(markup, /Quick connect to root@10\.2\.0\.8:2222/); assert.match(markup, /bg-primary\/15[\s\S]*Quick connect to root@10\.2\.0\.8:2222/); }); test("quick switcher does not offer quick connect for ordinary search text", () => { const markup = renderSwitcher("core switch"); assert.doesNotMatch(markup, /Quick connect to/); }); test("quick switcher quick connect row uses zh-TW wording", () => { const markup = renderToStaticMarkup( undefined} onSelect={() => undefined} onSelectTab={() => undefined} onClose={() => undefined} onQuickConnect={() => undefined} onCreateWorkspace={() => undefined} showSftpTab={false} /> , ); assert.match(markup, /快速連線到 10\.0\.0\.8/); assert.match(markup, />主機新增工作區 { const markup = renderToStaticMarkup( undefined} onSelect={() => undefined} onSelectTab={() => undefined} onClose={() => undefined} onQuickConnect={() => undefined} onCreateWorkspace={() => undefined} showSftpTab={false} /> , ); assert.match(markup, />主机标签页新建工作区