Files
Netcatty/components/terminal/autocompleteDescriptionI18n.batch.test.ts
T
rnarany 51d9f715da feat(autocomplete): localize common commands and polish terminal hints (#4172)
* feat(autocomplete): add zh-CN descriptions for zip, unzip, make, nano

Cover 32 missing Fig descriptions, generate zh-TW, add batch coverage tests, and record source and semantic checks.

* fix(autocomplete): correct zip, unzip, and make semantics

Limit zip -9 to the compression level, warn that unzip -0 is a suspect upstream option, and describe make -N as the Apple/NeXT compatibility extension.

* fix(autocomplete): simplify unzip -0 warning and correct NeXT note

Keep the -O gbk version caveat in the research record and describe Next/NeXT as a capitalization difference.

* feat(autocomplete): complete remaining common command descriptions

Add 177 zh-CN descriptions for python, python3, node, vim, sort, awk, and sed; generate zh-TW, cover the batch with real Fig specs, and record one deferred shared node key.

* fix(autocomplete): correct sed n and sort parallel semantics

Add the automatic-print condition to sed n, remove the universal CPU default from sort --parallel, and remove duplicated adopted-entry sections from the source record.

* fix: repair macOS terminal setup and align autocomplete details

- Read OSC 7 setup payloads through base64 stdin so BSD/macOS works
- Keep the FIDO askpass socket path within the Unix sun_path limit
- Align autocomplete descriptions in a shared fixed column
- Fix an invalid optional-parameter declaration and lint warnings
- Harden macOS-sensitive OSC 7, SFTP, temp-dir and privacy tests

* feat(autocomplete): add zh-CN descriptions for file and text utilities

Cover ln, stat, wc, cut, uniq, tee and xargs with 70 localized
descriptions. Add coverage and localization tests, preload the new common
specs, and record the upstream wc/xargs wording corrections.

* fix(autocomplete): keep popup scrollbars draggable

Panel mousedown handlers no longer preventDefault across the whole box, so the right-side detail scrollbar and the suggestion list scrollbar can be dragged while padding clicks still preserve terminal focus.

* fix(autocomplete): keep detail panel open while selecting text

Delay row hover clearing so the pointer can move into the right-side detail panel, and allow native text selection there without stealing terminal focus on ordinary padding clicks.
2026-11-07 20:00:43 +08:00

156 lines
5.9 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import type { FigSpec } from "./autocomplete/figSpecLoader.ts";
const HERE = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.resolve(HERE, "../..");
const SPEC_DIR = path.join(ROOT, "node_modules/@withfig/autocomplete/build");
const DICT_DIR = path.join(HERE, "autocomplete");
const COMMANDS = ["zip", "unzip", "make", "nano"] as const;
const specs = Object.fromEntries(
await Promise.all(
COMMANDS.map(async (name) => {
const mod = await import(pathToFileURL(path.join(SPEC_DIR, `${name}.js`)).href);
return [name, mod.default?.default ?? mod.default] as const;
}),
),
) as Record<(typeof COMMANDS)[number], FigSpec>;
const simplified = JSON.parse(
fs.readFileSync(path.join(DICT_DIR, "descriptions.zh-CN.json"), "utf8"),
) as Record<string, string>;
const traditional = JSON.parse(
fs.readFileSync(path.join(DICT_DIR, "descriptions.zh-TW.json"), "utf8"),
) as Record<string, string>;
Object.defineProperty(globalThis, "localStorage", {
value: {
getItem: () => null,
setItem: () => {},
removeItem: () => {},
clear: () => {},
},
configurable: true,
});
Object.defineProperty(globalThis, "window", {
value: {
netcatty: {
listFigSpecs: async () => [...COMMANDS],
loadFigSpec: async (commandName: string) =>
specs[commandName as (typeof COMMANDS)[number]] ?? null,
},
},
configurable: true,
});
const { getCompletions } = await import("./autocomplete/completionEngine.ts");
const { localizeSpecSuggestionDescriptions } = await import(
"./autocomplete/autocompleteDescriptionI18n.ts"
);
function collectDescriptions(node: unknown, out = new Set<string>(), seen = new WeakSet<object>()): Set<string> {
if (!node || typeof node !== "object" || seen.has(node)) return out;
seen.add(node);
const record = node as { description?: unknown };
if (typeof record.description === "string" && record.description.trim()) {
out.add(record.description);
}
for (const value of Object.values(node)) {
if (Array.isArray(value)) {
for (const item of value) collectDescriptions(item, out, seen);
} else if (value && typeof value === "object") {
collectDescriptions(value, out, seen);
}
}
return out;
}
const batchDescriptions = [...new Set(COMMANDS.flatMap((name) => [...collectDescriptions(specs[name])]))];
function suggestionNamed(
suggestions: Awaited<ReturnType<typeof getCompletions>>,
displayText: string,
) {
return suggestions.find((suggestion) => suggestion.displayText === displayText);
}
test("batch specs have complete zh-CN and zh-TW description coverage", async () => {
assert.ok(batchDescriptions.length >= 32);
for (const description of batchDescriptions) {
assert.equal(typeof simplified[description], "string", `missing zh-CN: ${description}`);
assert.ok(simplified[description].length > 0, `empty zh-CN: ${description}`);
assert.equal(typeof traditional[description], "string", `missing zh-TW: ${description}`);
assert.ok(traditional[description].length > 0, `empty zh-TW: ${description}`);
}
const localized = await localizeSpecSuggestionDescriptions(
batchDescriptions.map((description) => ({ text: description, description })),
"zh-CN",
);
for (const [index, suggestion] of localized.entries()) {
assert.equal(suggestion.text, batchDescriptions[index]);
assert.notEqual(suggestion.description, batchDescriptions[index]);
}
});
test("batch zh-TW spot checks cover corrected semantics", () => {
assert.equal(
traditional["Archive a directory and its contents with the highest level [9] of compression"],
"使用最高壓縮級別 9",
);
assert.equal(
traditional["Extract a zip file created in windows, containing files with non-ascii (chinese) filenames"],
"上游選項疑似錯誤,請核對當前 unzip 版本支援的引數",
);
assert.equal(
traditional["Turn on value of next option"],
"啟用指定的 NeXT 相容選項(Apple 擴充套件)",
);
});
test("zip, unzip, and make options localize without changing insertion text", async () => {
const cases = [
{ input: "zip -", option: "-r", zhCn: "递归打包并压缩目录及其内容" },
{ input: "zip -", option: "-9", zhCn: "使用最高压缩级别 9" },
{ input: "unzip -", option: "-l", zhCn: "列出 zip 文件内容而不解压" },
{
input: "unzip -",
option: "-0",
zhCn: "上游选项疑似错误,请核对当前 unzip 版本支持的参数",
},
{ input: "make -", option: "-N", zhCn: "启用指定的 NeXT 兼容选项(Apple 扩展)" },
{ input: "make --", option: "--warn-undefined-variables", zhCn: "引用未定义变量时发出警告" },
];
for (const { input, option, zhCn } of cases) {
const suggestions = await getCompletions(input, {
locale: "zh-CN",
skipPathCompletion: true,
maxResults: 50,
});
const suggestion = suggestionNamed(suggestions, option);
assert.equal(suggestion?.text, `${input.split(" ")[0]} ${option}`);
assert.equal(suggestion?.description, zhCn);
}
});
test("batch command descriptions localize and English remains the fallback", async () => {
const nano = specs.nano.description;
assert.ok(nano);
const [zhCn] = await localizeSpecSuggestionDescriptions([{ description: nano }], "zh-CN");
const [zhTw] = await localizeSpecSuggestionDescriptions([{ description: nano }], "zh-TW");
const [en] = await localizeSpecSuggestionDescriptions([{ description: nano }], "en");
assert.equal(zhCn.description, "Nano 的 ANOther 编辑器,一个增强的免费 Pico 克隆");
assert.equal(zhTw.description, "Nano 的 ANOther 編輯器,一個增強的免費 Pico 克隆");
assert.equal(en.description, nano);
assert.equal(specs.nano.description, nano);
});