From a885d1478e93c8e7d90c443ee7264f55c872f28f Mon Sep 17 00:00:00 2001 From: yunyingk Date: Thu, 29 Oct 2026 15:48:34 +0800 Subject: [PATCH] =?UTF-8?q?test(update):=20=E4=B8=BA=20cleanStaleBinaries?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0=E4=B8=B4=E6=97=B6=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E6=B2=99=E7=AE=B1=E9=9A=94=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在 `src/cli/commands/update.ts` 中为 `cleanStaleBinaries` 增加可选的 `candidateOverrides` 参数,允许在单元测试中注入沙箱路径。 在 `src/cli/commands/update.test.ts` 中重构单测,使用 `mkdtempSync` 创建临时隔离文件测试活跃路径保留与废弃文件清理逻辑,杜绝在宿主机测试期间误伤或删除真实全局二进制。 --- src/cli/commands/update.test.ts | 21 ++++++++++++++++++--- src/cli/commands/update.ts | 14 ++++++++------ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/cli/commands/update.test.ts b/src/cli/commands/update.test.ts index 7c6d6ab..337f919 100644 --- a/src/cli/commands/update.test.ts +++ b/src/cli/commands/update.test.ts @@ -349,8 +349,23 @@ describe("update installation modes", () => { expect(SYSTEM_BINARY_PATH).toBe("/usr/local/bin/seal-use"); }); - test("cleanStaleBinaries ignores activePath and returns list of cleaned files", () => { - const cleaned = cleanStaleBinaries(SYSTEM_BINARY_PATH); - expect(Array.isArray(cleaned)).toBe(true); + test("cleanStaleBinaries ignores activePath and returns list of cleaned files in isolated sandbox", () => { + const tempDir = mkdtempSync(join(tmpdir(), "seal-use-clean-test-")); + try { + const fakeActive = join(tempDir, "active-bin"); + const fakeStale = join(tempDir, "stale-bin"); + const fakeUntouched = join(tempDir, "untouched-bin"); + writeFileSync(fakeActive, "active"); + writeFileSync(fakeStale, "stale"); + writeFileSync(fakeUntouched, "untouched"); + + const cleaned = cleanStaleBinaries(fakeActive, [fakeActive, fakeStale]); + expect(cleaned).toEqual([fakeStale]); + expect(existsSync(fakeActive)).toBe(true); + expect(existsSync(fakeStale)).toBe(false); + expect(existsSync(fakeUntouched)).toBe(true); + } finally { + rmSync(tempDir, { recursive: true, force: true }); + } }); }); diff --git a/src/cli/commands/update.ts b/src/cli/commands/update.ts index 97889c2..47d1dfb 100644 --- a/src/cli/commands/update.ts +++ b/src/cli/commands/update.ts @@ -39,7 +39,7 @@ export function isPathWritable(targetPath: string): boolean { } } -export function cleanStaleBinaries(activePath: string): string[] { +export function cleanStaleBinaries(activePath: string, candidateOverrides?: string[]): string[] { const cleaned: string[] = []; const home = homedir(); const userBinary = getUserBinaryPath(); @@ -49,11 +49,13 @@ export function cleanStaleBinaries(activePath: string): string[] { const homebrewBinary = "/opt/homebrew/bin/seal-use"; const homebrewAlias = "/opt/homebrew/bin/seal-home"; - const candidates = [homebrewBinary, homebrewAlias]; - if (activePath === systemBinary) { - candidates.push(userBinary, userAlias); - } else if (activePath === userBinary) { - candidates.push(systemBinary, systemAlias); + const candidates = candidateOverrides ? [...candidateOverrides] : [homebrewBinary, homebrewAlias]; + if (!candidateOverrides) { + if (activePath === systemBinary) { + candidates.push(userBinary, userAlias); + } else if (activePath === userBinary) { + candidates.push(systemBinary, systemAlias); + } } for (const candidate of candidates) {