import assert from "node:assert/strict"; import test from "node:test"; import en from "../application/i18n/locales/en.ts"; import zhCN from "../application/i18n/locales/zh-CN.ts"; import zhTW from "../application/i18n/locales/zh-TW.ts"; import { filterSettingsSearchCatalog } from "./settingsSearch.ts"; import { resolveSettingsPlatform } from "./settingsSearchCatalog.ts"; import { SETTINGS_SEARCH_CATALOG } from "./settingsSearchCatalog.ts"; const EN: Record = { "settings.tab.application": "Application", "settings.tab.appearance": "Appearance", "settings.tab.terminal": "Terminal", "settings.tab.shortcuts": "Shortcuts", "settings.tab.sftpFileAssociations": "SFTP", "settings.tab.ai": "AI", "settings.tab.syncCloud": "Sync & Cloud", "settings.tab.system": "System", "settings.tab.plugins": "Plugins", "settings.appearance.theme": "Theme", "settings.appearance.uiTheme": "UI theme", "settings.terminal.behavior.copyOnSelect": "Copy on select", "settings.terminal.behavior.copyOnSelect.desc": "Copy selected text automatically", "settings.terminal.cursor.barWidth": "Bar cursor width", "settings.terminal.cursor.barWidth.desc": "Width of bar cursors used by applications such as vi (1-4 px)", "settings.terminal.section.cursor": "Cursor", "settings.terminal.behavior.disconnectedNotice": "When a connection disconnects", "settings.terminal.behavior.disconnectedNotice.desc": "Show a terminal notice or dialog", "settings.terminal.section.behavior": "Behavior", "settings.system.networkProxy.mode": "Proxy mode", "settings.system.networkProxy.description": "HTTP(S) proxy for cloud sync and AI", "settings.system.networkProxy.title": "Network Proxy", "settings.appLock.title": "App Lock", "settings.appLock.description": "Require a local lock password when Netcatty opens", "ai.safety.permissionMode": "Permission mode", "ai.safety.permissionMode.description": "Control tool approvals", "ai.safety.title": "Safety", "cloudSync.localBackups.title": "Local backups", "cloudSync.localBackups.desc": "Keep offline snapshots", }; const ZH: Record = { ...EN, "settings.appearance.theme": "主题", "settings.terminal.behavior.copyOnSelect": "选中即复制", "settings.terminal.cursor.barWidth": "竖线光标粗细", "settings.terminal.cursor.barWidth.desc": "vi 等应用使用竖线光标时的宽度(1-4 像素)", "settings.terminal.section.cursor": "光标", "settings.terminal.behavior.disconnectedNotice": "连接断开时", "settings.terminal.behavior.disconnectedNotice.desc": "在终端内显示简短提示,或使用完整弹窗", "settings.system.networkProxy.mode": "代理模式", "settings.system.networkProxy.title": "网络代理", "settings.appLock.title": "应用锁定", "settings.appLock.description": "在 Netcatty 打开、长时间无操作或手动锁定后,要求输入本地锁定密码。", "ai.safety.permissionMode": "权限模式", }; const tEn = (key: string) => EN[key] ?? key; const tZh = (key: string) => ZH[key] ?? key; test("settings search catalog has unique ids", () => { const ids = SETTINGS_SEARCH_CATALOG.map((entry) => entry.id); assert.equal(new Set(ids).size, ids.length); }); test("newly indexed gated settings retain visible parent destinations in search", () => { const fallbacks = { "terminal-hibernate-hidden-tabs-delay": "terminal-hibernate-hidden-tabs", "terminal-hibernate-skip-alt-screen": "terminal-hibernate-hidden-tabs", "terminal-hibernate-keep-renderer-count": "terminal-hibernate-hidden-tabs", "terminal-hibernate-replay-chunk-bytes": "terminal-hibernate-hidden-tabs", "terminal-hibernate-prefer-wasm-serialize": "terminal-hibernate-hidden-tabs", "terminal-inline-images-kitty": "terminal-inline-images-enabled", "terminal-inline-images-sixel": "terminal-inline-images-enabled", "terminal-inline-images-iip": "terminal-inline-images-enabled", "terminal-inline-images-storage-limit": "terminal-inline-images-enabled", "terminal-inline-images-max-megapixels": "terminal-inline-images-enabled", "terminal-inline-images-sequence-limit": "terminal-inline-images-enabled", "terminal-host-info-bar-title-mode": "terminal-host-info-bar-show", "terminal-server-stats-refresh-interval": "terminal-server-stats-show", "ai-web-search-api-key": "ai-web-search-provider", "system-network-proxy-url": "system-network-proxy-mode", "system-network-proxy-bypass": "system-network-proxy-mode", "system-app-lock-timeout": "system-app-lock", }; const hits = filterSettingsSearchCatalog("", (key) => en[key] ?? key, { limit: 500 }); for (const [id, fallbackId] of Object.entries(fallbacks)) { const entry = hits.find((hit) => hit.entry.id === id)?.entry; assert.ok(entry, id); assert.equal(entry.fallbackAnchorId, fallbackId, id); const parent = SETTINGS_SEARCH_CATALOG.find((candidate) => candidate.id === fallbackId); assert.ok(parent, fallbackId); assert.equal(parent.tab, entry.tab, id); assert.equal(parent.aiSubTab, entry.aiSubTab, id); assert.equal(parent.fallbackAnchorId, undefined, fallbackId); } // The local start-directory row is always mounted in this UI. assert.equal(hits.find((hit) => hit.entry.id === "terminal-local-shell-start-dir")?.entry.fallbackAnchorId, undefined); }); test("filterSettingsSearchCatalog matches English labels", () => { const hits = filterSettingsSearchCatalog("copy on select", tEn); assert.ok(hits.some((hit) => hit.entry.id === "terminal-copy-on-select")); }); test("settings search finds the disconnected notice preference", () => { const byEnglish = filterSettingsSearchCatalog("connection disconnects", tEn); assert.ok(byEnglish.some((hit) => hit.entry.id === "terminal-disconnected-notice")); const byChinese = filterSettingsSearchCatalog("断开连接", tZh); assert.ok(byChinese.some((hit) => hit.entry.id === "terminal-disconnected-notice")); const byPopupAlias = filterSettingsSearchCatalog("弹窗提醒", tZh); assert.ok(byPopupAlias.some((hit) => hit.entry.id === "terminal-disconnected-notice")); }); test("settings search finds bar cursor width in English and Chinese", () => { const byEnglish = filterSettingsSearchCatalog("bar cursor width", tEn); assert.ok(byEnglish.some((hit) => hit.entry.id === "terminal-cursor-bar-width")); const byChinese = filterSettingsSearchCatalog("竖线光标粗细", tZh); assert.ok(byChinese.some((hit) => hit.entry.id === "terminal-cursor-bar-width")); }); test("filterSettingsSearchCatalog matches Chinese labels and pinyin", () => { const byLabel = filterSettingsSearchCatalog("主题", tZh); assert.ok(byLabel.some((hit) => hit.entry.id === "appearance-theme")); const byPinyin = filterSettingsSearchCatalog("zhuti", tZh); assert.ok(byPinyin.some((hit) => hit.entry.id === "appearance-theme")); }); test("filterSettingsSearchCatalog matches keywords and tab labels", () => { const proxyHits = filterSettingsSearchCatalog("proxy", tEn); assert.ok(proxyHits.some((hit) => hit.entry.id === "system-network-proxy-mode")); const syncHits = filterSettingsSearchCatalog("backup", tEn); assert.ok(syncHits.some((hit) => hit.entry.id === "sync-local-backups")); }); test("filterSettingsSearchCatalog matches App Lock by title and aliases", () => { const byEnglish = filterSettingsSearchCatalog("app lock", tEn); assert.ok(byEnglish.some((hit) => hit.entry.id === "system-app-lock")); const byChineseTitle = filterSettingsSearchCatalog("应用锁定", tZh); assert.ok(byChineseTitle.some((hit) => hit.entry.id === "system-app-lock")); // Users commonly search the shorter "应用锁" alias rather than "应用锁定". const byChineseAlias = filterSettingsSearchCatalog("应用锁", tZh); assert.ok(byChineseAlias.some((hit) => hit.entry.id === "system-app-lock")); const byTimeout = filterSettingsSearchCatalog("无操作", tZh); assert.ok(byTimeout.some((hit) => hit.entry.id === "system-app-lock")); }); test("settings search finds App Lock from the zh-CN locale", () => { const t = (key: string) => zhCN[key] ?? key; const hits = filterSettingsSearchCatalog("应用锁", t); assert.ok(hits.some((hit) => hit.entry.id === "system-app-lock")); }); test("settings search finds folder compression from the real locales", () => { const tZhReal = (key: string) => zhCN[key] ?? key; const tEnReal = (key: string) => en[key] ?? key; for (const [t, queries] of [ [tZhReal, ["压缩", "普通传输"]], [tEnReal, ["compress", "tar"]], ] as const) { for (const query of queries) { const hits = filterSettingsSearchCatalog(query, t); assert.ok(hits.some((hit) => hit.entry.id === "sftp-compressed-upload"), query); } } }); test("settings search finds skip unchanged files from the real locales", () => { const tZhReal = (key: string) => zhCN[key] ?? key; const tEnReal = (key: string) => en[key] ?? key; for (const [t, queries] of [ [tZhReal, ["跳过未改文件", "修改时间"]], [tEnReal, ["skip unchanged", "rsync"]], ] as const) { for (const query of queries) { const hits = filterSettingsSearchCatalog(query, t); assert.ok(hits.some((hit) => hit.entry.id === "sftp-skip-unchanged"), query); } } }); test("settings search finds SSH connection keep-alive from the real locales", () => { const tZhReal = (key: string) => zhCN[key] ?? key; const tEnReal = (key: string) => en[key] ?? key; for (const [t, queries] of [ [tZhReal, ["保活时间", "重新认证"]], [tEnReal, ["keep-alive", "re-authentication"]], ] as const) { for (const query of queries) { const hits = filterSettingsSearchCatalog(query, t); assert.ok(hits.some((hit) => hit.entry.id === "sftp-transport-idle-ttl"), query); } } }); test("settings search finds note typography from the real locales", () => { const tZhReal = (key: string) => zhCN[key] ?? key; const tEnReal = (key: string) => en[key] ?? key; for (const [id, zhQuery, enQuery] of [ ["appearance-vault-notes-font", "代码字体", "code font"], ["appearance-vault-notes-font-size", "笔记字号", "note font size"], ["appearance-vault-notes-code-font-size", "代码字号", "code font size"], ] as const) { for (const [t, query] of [[tZhReal, zhQuery], [tEnReal, enQuery]] as const) { const hit = filterSettingsSearchCatalog(query, t).find((hit) => hit.entry.id === id); assert.ok(hit, `${query} should find ${id}`); assert.equal(hit.entry.tab, "appearance"); } } }); test("settings search finds session log options from the real locales", () => { const tZhReal = (key: string) => zhCN[key] ?? key; const tEnReal = (key: string) => en[key] ?? key; for (const [id, t, queries] of [ ["system-session-logs-directory", tZhReal, ["保存目录"]], ["system-session-logs-directory", tEnReal, ["save directory"]], ["system-session-logs-format", tZhReal, ["日志格式"]], ["system-session-logs-format", tEnReal, ["log format"]], ["system-session-logs-timestamps", tZhReal, ["时间戳"]], ["system-session-logs-timestamps", tEnReal, ["timestamp", "Add timestamps"]], ] as const) { for (const query of queries) { const hit = filterSettingsSearchCatalog(query, t).find((hit) => hit.entry.id === id); assert.ok(hit, `${query} should find ${id}`); assert.equal(hit.entry.tab, "system"); assert.equal(hit.entry.sectionKey, "settings.sessionLogs.title"); } } }); test("later-added settings retain their tab and sub-tab destinations", () => { const tZhReal = (key: string) => zhCN[key] ?? key; const tEnReal = (key: string) => en[key] ?? key; for (const [id, tab, subTab, labelKey] of [ ["terminal-font-ligatures", "terminal", undefined, "settings.terminal.font.ligatures"], ["terminal-local-shell-side-panel-auto-open", "terminal", undefined, "settings.terminal.localShellSidePanel.autoOpen"], ["terminal-system-manager-process-refresh-interval", "terminal", undefined, "settings.terminal.systemManager.processRefreshInterval"], ["ai-chat-history-message-cap", "ai", "tools", "ai.chatHistory.messageCap"], ["ai-web-search-max-results", "ai", "search", "ai.webSearch.maxResults"], ["ai-safety-max-iterations", "ai", "safety", "ai.safety.maxIterations"], ["sync-ai-sessions", "sync", "status", "cloudSync.aiSessions.title"], ["system-network-proxy-url", "system", undefined, "settings.system.networkProxy.url"], ] as const) { for (const t of [tZhReal, tEnReal]) { assert.notEqual(t(labelKey), labelKey); const hit = filterSettingsSearchCatalog(t(labelKey), t).find((hit) => hit.entry.id === id); assert.ok(hit, `${t(labelKey)} should find ${id}`); assert.equal(hit.entry.tab, tab); assert.equal(hit.entry.aiSubTab ?? hit.entry.syncSubTab, subTab); } } }); test("settings search finds temporary network device mode", () => { const tEnReal = (key: string) => en[key] ?? key; const tZhTW = (key: string) => zhTW[key] ?? key; const english = filterSettingsSearchCatalog("network device", tEnReal); assert.ok(english.some((hit) => hit.entry.id === "terminal-temporary-network-device-mode")); const traditional = filterSettingsSearchCatalog("網路裝置", tZhTW); assert.ok(traditional.some((hit) => hit.entry.id === "terminal-temporary-network-device-mode")); }); test("filterSettingsSearchCatalog can exclude plugins tab", () => { const hits = filterSettingsSearchCatalog("plugin", tEn, { includePlugins: false }); assert.equal(hits.some((hit) => hit.entry.tab === "plugins"), false); }); test("AI safety entries retain aiSubTab for navigation", () => { const hits = filterSettingsSearchCatalog("permission", tEn); const safety = hits.find((hit) => hit.entry.id === "ai-safety-permission-mode"); assert.ok(safety); assert.equal(safety?.entry.aiSubTab, "safety"); }); test("Sync status entries retain syncSubTab for navigation", () => { const hits = filterSettingsSearchCatalog("backup", tEn); const backups = hits.find((hit) => hit.entry.id === "sync-local-backups"); assert.ok(backups); assert.equal(backups?.entry.syncSubTab, "status"); }); test("macOS-only settings stay out of search on other platforms", () => { const blurId = "appearance-window-background-blur"; // Both a targeted query and the default (empty query) list must skip it, // otherwise selecting the hit jumps to a tab with no anchor to focus. const macHits = filterSettingsSearchCatalog("", tEn, { platform: "mac", limit: 500 }); assert.ok(macHits.some((hit) => hit.entry.id === blurId)); for (const platform of ["win", "linux"] as const) { const searched = filterSettingsSearchCatalog("frosted glass", tEn, { platform }); assert.equal(searched.some((hit) => hit.entry.id === blurId), false); const defaults = filterSettingsSearchCatalog("", tEn, { platform, limit: 500 }); assert.equal(defaults.some((hit) => hit.entry.id === blurId), false); } }); test("platform-gated entries never hide cross-platform settings", () => { for (const platform of ["mac", "win", "linux"] as const) { const hits = filterSettingsSearchCatalog("opacity", tEn, { platform }); assert.ok( hits.some((hit) => hit.entry.id === "appearance-window-opacity"), `window opacity must stay searchable on ${platform}`, ); } }); test("resolveSettingsPlatform maps Electron-style platform strings", () => { assert.equal(resolveSettingsPlatform("MacIntel"), "mac"); assert.equal(resolveSettingsPlatform("Win32"), "win"); assert.equal(resolveSettingsPlatform("Linux x86_64"), "linux"); assert.equal(resolveSettingsPlatform(""), "linux"); });