Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): 修复跨平台路径大小写判定测试

DavidShend11f6d5f9093
diff --git a/apps/desktop/src/main/hook-control/__tests__/paths.test.ts b/apps/desktop/src/main/hook-control/__tests__/paths.test.ts
index e47381f3a5..662f3f0e1e 100644
--- a/apps/desktop/src/main/hook-control/__tests__/paths.test.ts
+++ b/apps/desktop/src/main/hook-control/__tests__/paths.test.ts
@@ -43,21 +43,17 @@ describe('isPathWithin', () => {
});
it('大小写: Windows 不敏感, 其它平台敏感(规则 15)', () => {
- // 两个分支都要锁住 —— 只在当前平台上跑, 另一半会在重构时悄悄回归
- const original = process.platform;
- const setPlatform = (value: NodeJS.Platform): void => {
- Object.defineProperty(process, 'platform', { value, configurable: true });
- };
- try {
- setPlatform('win32');
- expect(isPathWithin(BASE, path.join(BASE.toUpperCase(), 'SUB'))).toBe(true);
- expect(isPathWithin(BASE.toUpperCase(), BASE)).toBe(true);
-
- setPlatform('linux');
- expect(isPathWithin(BASE, path.join(BASE.toUpperCase(), 'SUB'))).toBe(false);
- expect(isPathWithin(BASE.toUpperCase(), BASE)).toBe(false);
- } finally {
- setPlatform(original);
- }
+ // 路径格式和 path 实现都按目标平台构造,避免宿主系统语义污染模拟分支。
+ const windowsBase = path.win32.resolve('C:\\repos\\demo');
+ expect(
+ isPathWithin(windowsBase, path.win32.join(windowsBase.toUpperCase(), 'SUB'), 'win32'),
+ ).toBe(true);
+ expect(isPathWithin(windowsBase.toUpperCase(), windowsBase, 'win32')).toBe(true);
+
+ const posixBase = path.posix.resolve('/repos/demo');
+ expect(isPathWithin(posixBase, path.posix.join(posixBase.toUpperCase(), 'SUB'), 'linux')).toBe(
+ false,
+ );
+ expect(isPathWithin(posixBase.toUpperCase(), posixBase, 'linux')).toBe(false);
});
});
diff --git a/apps/desktop/src/main/hook-control/paths.ts b/apps/desktop/src/main/hook-control/paths.ts
index 3df58256f8..79574af47d 100644
--- a/apps/desktop/src/main/hook-control/paths.ts
+++ b/apps/desktop/src/main/hook-control/paths.ts
@@ -11,12 +11,6 @@
import path from 'node:path';
-/** 路径比较前的规范化。Windows 大小写不敏感(规则 15)。 */
-function normalizePathForCompare(p: string): string {
- const resolved = path.resolve(p);
- return process.platform === 'win32' ? resolved.toLowerCase() : resolved;
-}
-
/**
* target 是否落在 base 目录内(含相等)。Windows 大小写不敏感(规则 15)。
*
@@ -25,9 +19,21 @@ function normalizePathForCompare(p: string): string {
* 输入(SessionMeta.workDir 在 DB workingDir 为 null 时落成空串, 见
* maker-host/session-storage.ts)。安全判定在这里 fail closed
* (PR #733 review 指出)。
+ *
+ * platform 参数是跨平台测试缝:Node 的默认 path 实现在进程启动时就按宿主系统固定,
+ * 只 mock process.platform 并不能切换 path.relative 的大小写语义。
*/
-export function isPathWithin(base: string, target: string): boolean {
+export function isPathWithin(
+ base: string,
+ target: string,
+ platform: NodeJS.Platform = process.platform,
+): boolean {
if (base.trim() === '' || target.trim() === '') return false;
- const rel = path.relative(normalizePathForCompare(base), normalizePathForCompare(target));
- return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
+ const pathApi = platform === 'win32' ? path.win32 : path.posix;
+ const normalize = (value: string): string => {
+ const resolved = pathApi.resolve(value);
+ return platform === 'win32' ? resolved.toLowerCase() : resolved;
+ };
+ const rel = pathApi.relative(normalize(base), normalize(target));
+ return rel === '' || (!rel.startsWith('..') && !pathApi.isAbsolute(rel));
}