refactor: streamline codebase, unify duplicated modules, and consolidate test suite (#4307)

* refactor: streamline codebase, unify duplicated modules, and consolidate tests

* fix(test): batch discovered test files into Windows-safe command lines
This commit is contained in:
陈大猫 authored and GitHub committed 2026-11-23 11:00:14 +08:00
1 parent f2c71874cf
commit ad3d0cfeda
406 files changed
+10232 -24299

No files matched your search

-4
View File
@@ -52,10 +52,6 @@ test("build.files includes the single-channel SSH banner matcher", () => {
config.files.includes("domain/singleChannelSshBanner.shared.cjs"),
"packaged SSH and SFTP bridges require the shared banner matcher",
);
assert.ok(
config.files.includes("domain/singleChannelSshBanner.shared.mjs"),
"the shared banner matcher requires its source module",
);
const banner = require("../domain/singleChannelSshBanner.shared.cjs");
assert.equal(banner.remoteSoftwareRequiresSingleChannel("BHostSSH_7.0"), true);
assert.equal(banner.remoteSoftwareRequiresSingleChannel("JumpServer"), false);
+184
View File
@@ -0,0 +1,184 @@
#!/usr/bin/env node
import fs from "node:fs";
import path from "node:path";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
const TEST_FILE_RE = /\.test\.(?:ts|tsx|cjs|mjs|js)$/;
const EXCLUDED_FILE_RE = /\.(?:live|docker)\.test\.(?:ts|tsx|cjs|mjs|js)$/;
const IGNORED_DIRS = new Set(["node_modules", "dist", "release", ".git"]);
const UNIT_ROOT_DIRS = [
"application",
"components",
"domain",
"electron",
"infrastructure",
"lib",
"packages",
"scripts",
];
const UNIT_ROOT_FILES = ["vite.config.test.ts"];
const PLUGIN_RUNTIME_DOMAIN_TESTS = [
"domain/pluginRuntimePolicy.test.cjs",
"domain/pluginPermissionDescriptions.test.cjs",
"domain/pluginThemePack.test.cjs",
"domain/pluginGitHubRepository.test.cjs",
];
function walkTests(dir, out, excludeDirs = new Set()) {
if (!fs.existsSync(dir)) return;
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
if (IGNORED_DIRS.has(entry.name) || excludeDirs.has(full)) continue;
walkTests(full, out, excludeDirs);
} else if (
entry.isFile() &&
TEST_FILE_RE.test(entry.name) &&
!EXCLUDED_FILE_RE.test(entry.name)
) {
out.push(full);
}
}
}
function parseArgs(argv) {
let suite = "unit";
let listOnly = false;
const filters = [];
const extraNodeTestArgs = [];
for (let i = 0; i < argv.length; i += 1) {
const arg = argv[i];
if (arg === "--suite" && argv[i + 1]) {
suite = argv[i + 1];
i += 1;
} else if (arg.startsWith("--suite=")) {
suite = arg.slice("--suite=".length);
} else if (arg === "--filter" && argv[i + 1]) {
filters.push(argv[i + 1]);
i += 1;
} else if (arg.startsWith("--filter=")) {
filters.push(arg.slice("--filter=".length));
} else if (arg === "--list") {
listOnly = true;
} else {
extraNodeTestArgs.push(arg);
}
}
return { suite, listOnly, filters, extraNodeTestArgs };
}
function discoverFiles(suite) {
const files = [];
if (suite === "unit") {
const excludeDirs = new Set([path.join("electron", "plugins")]);
for (const root of UNIT_ROOT_DIRS) {
walkTests(root, files, excludeDirs);
}
for (const file of UNIT_ROOT_FILES) {
if (fs.existsSync(file)) files.push(file);
}
} else if (suite === "plugin-runtime") {
walkTests(path.join("electron", "plugins"), files);
for (const file of PLUGIN_RUNTIME_DOMAIN_TESTS) {
if (fs.existsSync(file)) files.push(file);
}
} else {
console.error(`[run-tests] Unknown suite: ${suite}`);
process.exit(2);
}
return Array.from(new Set(files)).sort();
}
const WINDOWS_MAX_CMD_CHARS = 20_000;
const POSIX_MAX_CMD_CHARS = 120_000;
// Conservative per-argument quoting + separator overhead for CreateProcessW.
const ARG_OVERHEAD_CHARS = 3;
export function batchTestFiles(
files,
baseArgsLength = 0,
maxCmdChars = process.platform === "win32" ? WINDOWS_MAX_CMD_CHARS : POSIX_MAX_CMD_CHARS,
) {
const batches = [];
let current = [];
let currentLen = baseArgsLength;
for (const file of files) {
const addition = file.length + ARG_OVERHEAD_CHARS;
if (current.length > 0 && currentLen + addition > maxCmdChars) {
batches.push(current);
current = [file];
currentLen = baseArgsLength + addition;
} else {
current.push(file);
currentLen += addition;
}
}
if (current.length > 0) {
batches.push(current);
}
return batches;
}
function main() {
const { suite, listOnly, filters, extraNodeTestArgs } = parseArgs(process.argv.slice(2));
let files = discoverFiles(suite);
if (filters.length > 0) {
files = files.filter((file) => filters.some((f) => file.includes(f)));
}
if (listOnly) {
for (const file of files) console.log(file);
process.exit(0);
}
if (files.length === 0) {
console.error(`[run-tests] No test files matched suite="${suite}"`);
process.exit(1);
}
const baseArgs = ["--test"];
if (suite === "unit") {
baseArgs.push("--import", "tsx");
}
baseArgs.push(...extraNodeTestArgs);
const baseLength =
process.execPath.length +
ARG_OVERHEAD_CHARS +
baseArgs.reduce((sum, arg) => sum + arg.length + ARG_OVERHEAD_CHARS, 0);
const batches = batchTestFiles(files, baseLength);
let exitCode = 0;
for (const batch of batches) {
const result = spawnSync(process.execPath, [...baseArgs, ...batch], { stdio: "inherit" });
if (result.error) {
console.error(`[run-tests] Failed to spawn test process: ${result.error.message}`);
process.exit(1);
}
if (result.signal) {
process.kill(process.pid, result.signal);
}
if ((result.status ?? 1) !== 0 && exitCode === 0) {
exitCode = result.status ?? 1;
}
}
process.exit(exitCode);
}
const isMain =
process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url);
if (isMain) {
main();
}
+36
View File
@@ -0,0 +1,36 @@
import assert from "node:assert/strict";
import test from "node:test";
import { batchTestFiles } from "./run-tests.mjs";
test("batchTestFiles splits large test suites safely under the Windows 32,767-char CreateProcessW limit", () => {
const files = Array.from(
{ length: 1400 },
(_, i) => `components/settings/tabs/ai/verySpecificFeatureModule${String(i).padStart(4, "0")}.test.tsx`,
);
const baseArgsLength = 200;
const batches = batchTestFiles(files, baseArgsLength, 20_000);
assert.ok(batches.length > 1, "expected multiple batches for 1,400 paths");
assert.deepEqual(batches.flat(), files, "all files must be preserved in order");
for (const batch of batches) {
const estimatedCmdLen =
baseArgsLength + batch.reduce((sum, f) => sum + f.length + 3, 0);
assert.ok(
estimatedCmdLen <= 20_000,
`batch exceeded budget: ${estimatedCmdLen}`,
);
}
});
test("batchTestFiles keeps a single batch when total command length fits within budget", () => {
const files = ["a.test.ts", "b.test.ts", "c.test.ts"];
const batches = batchTestFiles(files, 100, 20_000);
assert.deepEqual(batches, [files]);
});
test("batchTestFiles never drops or loops on a single path longer than the remaining budget", () => {
const longFile = `${"a".repeat(200)}.test.ts`;
const batches = batchTestFiles([longFile, "short.test.ts"], 150, 200);
assert.deepEqual(batches, [[longFile], ["short.test.ts"]]);
});
@@ -28,7 +28,7 @@ if (!process.versions.electron) {
stdin: {
contents: [
'export { getCompletions } from "./components/terminal/autocomplete/completionEngine";',
'export { resetPinyinCacheForTests, getPinyinCacheStatsForTests } from "./lib/searchMatcher";',
'export { resetPinyinCacheForTests, getPinyinCacheStatsForTests } from "./domain/searchMatcher";',
].join("\n"),
resolveDir: root,
loader: "ts",