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(test): 修复 Windows 下符号链接权限与模块解析导致的测试失败 (#2382)

DavidShenXDc3018b8abbfb
diff --git a/apps/desktop/scripts/ensure-android-platform-tools.mjs b/apps/desktop/scripts/ensure-android-platform-tools.mjs
index 1c8f82fd07..239f88519e 100644
--- a/apps/desktop/scripts/ensure-android-platform-tools.mjs
+++ b/apps/desktop/scripts/ensure-android-platform-tools.mjs
@@ -45,8 +45,6 @@ import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
-import JSZip from 'jszip';
-
import {
downloadToFileWithTimeout,
createDownloadProgressLogger,
@@ -196,6 +194,7 @@ export function verifyInstalled(platformKey, binRoot = BIN_ROOT) {
*/
async function extractVerifiedFiles(zipBuffer, platformKey, destDir) {
const spec = PINNED[platformKey];
+ const { default: JSZip } = await import('jszip');
const zip = await JSZip.loadAsync(zipBuffer);
const staged = new Map();
diff --git a/apps/desktop/src/main/mcp-integrations/__tests__/ios-simulator-artifact.test.ts b/apps/desktop/src/main/mcp-integrations/__tests__/ios-simulator-artifact.test.ts
index 732aaa5c75..08b634982d 100644
--- a/apps/desktop/src/main/mcp-integrations/__tests__/ios-simulator-artifact.test.ts
+++ b/apps/desktop/src/main/mcp-integrations/__tests__/ios-simulator-artifact.test.ts
@@ -1,5 +1,6 @@
import { createHash } from 'node:crypto';
import { mkdir, mkdtemp, readFile, realpath, rm, symlink, writeFile } from 'node:fs/promises';
+import { mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
@@ -37,6 +38,22 @@ afterEach(async () => {
);
});
+/** 探针创建真实符号链接来检测 OS 能力,不靠平台名猜测(开发者模式 Windows 可以 symlink)。 */
+function canCreateSymlink(): boolean {
+ const probe = mkdtempSync(path.join(os.tmpdir(), 'cindy-ios-artifact-symlink-probe-'));
+ try {
+ writeFileSync(path.join(probe, 'target'), 'probe');
+ symlinkSync(path.join(probe, 'target'), path.join(probe, 'link'));
+ return true;
+ } catch {
+ return false;
+ } finally {
+ rmSync(probe, { recursive: true, force: true });
+ }
+}
+
+const canLink = canCreateSymlink();
+
async function createFixture(
patch: {
executable?: string;
@@ -285,7 +302,7 @@ describe('packaged iOS Simulator sidecar artifact verification', () => {
});
});
- it('rejects a symlinked executable before invoking codesign', async () => {
+ it.skipIf(!canLink)('rejects a symlinked executable before invoking codesign', async () => {
const fixture = await createFixture();
const targetPath = path.join(fixture.root, 'replacement');
await writeFile(targetPath, 'native-sidecar');
diff --git a/packages/ios-simulator-runtime/src/project-adapter.test.ts b/packages/ios-simulator-runtime/src/project-adapter.test.ts
index 1cd727bb27..07dd4c0e9a 100644
--- a/packages/ios-simulator-runtime/src/project-adapter.test.ts
+++ b/packages/ios-simulator-runtime/src/project-adapter.test.ts
@@ -259,7 +259,13 @@ describe("IOSSimulatorProjectBuilder", () => {
roots.push(root, outside);
const outsideWorkspace = path.join(outside, "Outside.xcworkspace");
await mkdir(outsideWorkspace);
- await symlink(outsideWorkspace, path.join(root, "Escape.xcworkspace"));
+ // Windows 上目录符号链接走 junction(无需管理员/开发者模式),
+ // 其他平台走 'dir'。junction escape 同样会被 inspect() 的 realpath 检测到。
+ await symlink(
+ outsideWorkspace,
+ path.join(root, "Escape.xcworkspace"),
+ process.platform === "win32" ? "junction" : "dir",
+ );
const builder = new IOSSimulatorProjectBuilder();
await expect(builder.inspect(root, "README.md")).rejects.toMatchObject({
diff --git a/packages/maker-core/src/agents/shared/review-read-scope.test.ts b/packages/maker-core/src/agents/shared/review-read-scope.test.ts
index 9a31dfe211..d282849fe3 100644
--- a/packages/maker-core/src/agents/shared/review-read-scope.test.ts
+++ b/packages/maker-core/src/agents/shared/review-read-scope.test.ts
@@ -1,4 +1,4 @@
-import { promises as fs } from "node:fs";
+import { mkdirSync, mkdtempSync, promises as fs, rmSync, symlinkSync, writeFileSync } from "node:fs";
import os from "node:os";
import path from "node:path";
@@ -26,6 +26,25 @@ afterEach(async () => {
);
});
+/** 探针创建真实符号链接来检测 OS 能力,不靠平台名猜测(开发者模式 Windows 可以 symlink)。 */
+function canCreateSymlink(): boolean {
+ const probe = mkdtempSync(path.join(os.tmpdir(), "cindy-review-scope-symlink-probe-"));
+ try {
+ writeFileSync(path.join(probe, "target"), "probe");
+ symlinkSync(
+ path.join(probe, "target"),
+ path.join(probe, "link"),
+ );
+ return true;
+ } catch {
+ return false;
+ } finally {
+ rmSync(probe, { recursive: true, force: true });
+ }
+}
+
+const canLink = canCreateSymlink();
+
describe("review read scope", () => {
it.runIf(Boolean(process.env.CINDY_REVIEW_REAL_WORKSPACE))(
"resolves a pnpm-linked source file in an explicitly requested real workspace",
@@ -96,7 +115,7 @@ describe("review read scope", () => {
).toBeNull();
});
- it("resolves symlinks before checking both scope and credential policy", async () => {
+ it.skipIf(!canLink)("resolves symlinks before checking both scope and credential policy", async () => {
const root = await makeTempDir();
const workspace = path.join(root, "workspace");
const outside = path.join(root, "outside.txt");
@@ -118,6 +137,42 @@ describe("review read scope", () => {
expect(await resolveReviewReadPath(keyLink, workspace, grants)).toBeNull();
});
+ it("resolves directory junctions before checking scope and credential policy", async () => {
+ const root = await makeTempDir();
+ const workspace = path.join(root, "workspace");
+ const outsideDir = path.join(root, "outside-dir");
+ await fs.mkdir(workspace);
+ await fs.mkdir(outsideDir);
+ // 在 worktree 外部目录放一个凭证文件和一个普通文件
+ const outsideSecret = path.join(outsideDir, "token.env");
+ const outsideDoc = path.join(outsideDir, "notes.txt");
+ await fs.writeFile(outsideSecret, "SECRET=value");
+ await fs.writeFile(outsideDoc, "public");
+ // 目录链接:Windows junction 无需管理员权限
+ const linkDir = path.join(workspace, "linked-dir");
+ await fs.symlink(
+ outsideDir,
+ linkDir,
+ process.platform === "win32" ? "junction" : "dir",
+ );
+
+ const grants = await buildReviewReadGrants(workspace, []);
+ expect(
+ await resolveReviewReadPath(
+ path.join(linkDir, "token.env"),
+ workspace,
+ grants,
+ ),
+ ).toBeNull();
+ expect(
+ await resolveReviewReadPath(
+ path.join(linkDir, "notes.txt"),
+ workspace,
+ grants,
+ ),
+ ).toBeNull();
+ });
+
it("rejects pre-existing hard links in workspace and explicit file grants", async () => {
if (process.platform === "win32") return;
const root = await makeTempDir();
diff --git a/scripts/__tests__/test-workspaces.test.mjs b/scripts/__tests__/test-workspaces.test.mjs
index f357e13177..71e772a1d8 100644
--- a/scripts/__tests__/test-workspaces.test.mjs
+++ b/scripts/__tests__/test-workspaces.test.mjs
@@ -1879,15 +1879,24 @@ test("runPlannedTests passes selected include files to packageBin commands", asy
allFiles: ["packages/orca-workflow/src/__tests__/orca-bridge-mcp.test.ts"],
manifest,
tier: "unit",
- runCommandImpl: async (command, args) => {
- calls.push({ command, args });
+ runCommandImpl: async (command, args, options) => {
+ calls.push({ command, args, options });
return { exitCode: 0, output: "PASS" };
},
});
- assert.deepEqual(calls[0].args.slice(-1), [
+ // Windows 上 resolvePnpmInvocation 会通过 cmd.exe 包装,实际 pnpm 参数
+ // 放在 env 的 CINDY_PNPM_CMD_ARG_N 里。其他平台参数直接在 args 里。
+ const call = calls[0];
+ const pnpmArgs = call.options?.env
+ ? Object.keys(call.options.env)
+ .filter((k) => k.startsWith("CINDY_PNPM_CMD_ARG_"))
+ .sort()
+ .map((k) => call.options.env[k])
+ : call.args;
+ assert.deepEqual(pnpmArgs.slice(-1), [
"src/__tests__/orca-bridge-mcp.test.ts",
]);
- assert.equal(calls[0].args.includes("src/__tests__/**/*.test.ts"), false);
+ assert.equal(pnpmArgs.includes("src/__tests__/**/*.test.ts"), false);
});
test("buildPnpmArgs rejects selected files outside the workspace", () => {
diff --git a/scripts/shared/oss.mjs b/scripts/shared/oss.mjs
index 9eb54e7898..290e99cc05 100644
--- a/scripts/shared/oss.mjs
+++ b/scripts/shared/oss.mjs
@@ -6,8 +6,8 @@
// 不含任何 manifest 业务逻辑(desktop 的 CDN manifest 拼装仍留在 ci/lib.mjs,
// mobile 的 Expo 协议 manifest 由其自身脚本负责)。
//
-// ali-oss 已 hoist 到仓库根 node_modules,故本文件(位于仓库根 scripts/shared/)
-// 可经 createRequire 直接解析;createOSSClient() 保持无参签名,与原 ci/lib.mjs 一致。
+// ali-oss 是 heavy dependency,仅在 createOSSClient() 内惰性 require,避免 import
+// 本模块就触发 node_modules 解析(script tests 等场景不需要 OSS client)。
// =============================================================================
import fs from 'node:fs';
@@ -16,9 +16,6 @@ import { createGzip } from 'node:zlib';
import { pipeline } from 'node:stream/promises';
import { createRequire } from 'node:module';
-const require = createRequire(import.meta.url);
-const OSS = require('ali-oss');
-
// CDN / OSS 发布目标只接受显式环境变量:XDT_CDN_BASE_URL / XDT_OSS_BUCKET /
// XDT_OSS_PREFIX / XDT_OSS_REGION。客户端运行期端点与发布落点是两类配置,不能再
// 通过 production-endpoints.json 混装或互相兜底。
@@ -161,6 +158,8 @@ export function resolveOssCredentials(releaseRegion = 'cn') {
export function createOSSClient(releaseRegion = ACTIVE_RELEASE_REGION) {
const { accessKeyId, accessKeySecret } = resolveOssCredentials(releaseRegion);
const { region, bucket } = resolveOssConfig(releaseRegion);
+ const require = createRequire(import.meta.url);
+ const OSS = require('ali-oss');
return new OSS({
region,
accessKeyId,