Files
cindy/scripts/__tests__/ensure-binary-fallback.test.mjs
Lizi bb3f71d084 chore: Cindy 首次开源提交 / initial open-source release
Cindy 是心动网络 (X.D. Network Inc.) 的跨端 AI Agent 客户端。
本次提交包含 Desktop(Electron)、Mobile(Expo)及全部共享 packages
的完整源码,以 Apache-2.0 协议开源。

This is the first public release of Cindy, X.D. Network's cross-platform
AI agent client: the Electron desktop app, the Expo mobile app, and all
shared workspace packages, licensed under Apache-2.0.

- 许可与合规:根 LICENSE(Apache-2.0)+ NOTICE;第三方组件归属声明与
  SPDX SBOM 收口在 docs/legal/notices/,由 pnpm licenses:generate 生成。
- 社区文件:README / CONTRIBUTING / CODE_OF_CONDUCT / SECURITY / SUPPORT
  中英双语;issue / PR 模板与 CI workflow 在 .github/。
- 协议层以公开 submodule 引入:github.com/makecindy/cindy-protocol。
- 提交前已通过仓库门禁:全量 pnpm test:unit 与涉及 package 的 typecheck。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-24 08:30:44 +08:00

89 lines
4.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// ensureBinary 兜底编排的集成测试:上游失败 → claude/codex/ripgrep 回退 CDN。
//
// 关键技巧:用假 platformKey 'test-fallback-platform'。各 update.mjs 的 ensurePlatform 会先
// `PLATFORMS.find(...)` 找不到而立即抛 "Unknown platform key"——**不打真实网络、不碰任何真实
// 平台的二进制目录**,确定性地模拟"上游失败",再观察 ensureBinary 的兜底分支。mock CDN 提供
// 该假平台路径下的 manifest + .gz。node 内置 test runner,无 vitest 依赖。
import { test, before, after } from 'node:test';
import assert from 'node:assert/strict';
import http from 'node:http';
import zlib from 'node:zlib';
import crypto from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';
import { ensureBinary } from '../ensure-agent-binaries.mjs';
const PLATFORM = 'test-fallback-platform'; // 假平台:上游立即抛 unknown,不打网络、不碰真实二进制
const CLAUDE_PIN = JSON.parse(fs.readFileSync('tools/claude/latest.json', 'utf8')).version;
const CODEX_PIN = JSON.parse(fs.readFileSync('tools/codex/latest.json', 'utf8')).version;
const RIPGREP_PIN = JSON.parse(fs.readFileSync('tools/ripgrep/latest.json', 'utf8')).version;
const BIN = Buffer.alloc(4096, 5);
const GZ = zlib.gzipSync(BIN);
const sha = (b) => crypto.createHash('sha256').update(b).digest('hex');
const dirsToClean = [
path.join('apps', 'claude-code-bin', PLATFORM),
path.join('apps', 'codex-bin', PLATFORM),
path.join('apps', 'ripgrep-bin', PLATFORM),
];
let server;
let savedCdnBase;
before(async () => {
server = http.createServer((req, res) => {
const u = req.url.split('?')[0];
if (u === `/manifest-${PLATFORM}-canary.json`) {
res.end(JSON.stringify({
claudeCode: { version: CLAUDE_PIN, file: 'x', sha256: sha(GZ), size: GZ.length, binarySha256: sha(BIN) },
codex: { version: CODEX_PIN, file: 'x', sha256: sha(GZ), size: GZ.length, binarySha256: sha(BIN) },
ripgrep: { version: RIPGREP_PIN, file: 'x', sha256: sha(GZ), size: GZ.length, binarySha256: sha(BIN) },
}));
} else if (
u === `/claude-code/${CLAUDE_PIN}/${PLATFORM}/claude.gz` ||
u === `/codex/${CODEX_PIN}/${PLATFORM}/codex.gz` ||
u === `/ripgrep/${RIPGREP_PIN}/${PLATFORM}/rg.gz`
) {
res.end(GZ);
} else {
res.writeHead(404);
res.end('nf');
}
});
await new Promise((r) => server.listen(0, '127.0.0.1', r));
savedCdnBase = process.env.XDT_CDN_BASE_URL;
process.env.XDT_CDN_BASE_URL = `http://127.0.0.1:${server.address().port}`;
});
after(async () => {
if (savedCdnBase === undefined) delete process.env.XDT_CDN_BASE_URL;
else process.env.XDT_CDN_BASE_URL = savedCdnBase;
for (const d of dirsToClean) {
try { fs.rmSync(d, { recursive: true, force: true }); } catch { /* ignore */ }
}
await new Promise((r) => server.close(r));
});
test('ensureBinary(claude): 上游失败 → 回退 CDN,落地正确二进制 + .version==pin', async () => {
const binPath = await ensureBinary('claude', PLATFORM, { force: true });
assert.ok(fs.readFileSync(binPath).equals(BIN), 'binary content == mock CDN binary');
const ver = fs.readFileSync(path.join(path.dirname(binPath), '.version'), 'utf8').trim();
assert.equal(ver, CLAUDE_PIN);
});
test('ensureBinary(codex): 上游失败 → 回退 CDN,落地正确二进制 + .version==pin', async () => {
const binPath = await ensureBinary('codex', PLATFORM, { force: true });
assert.ok(fs.readFileSync(binPath).equals(BIN), 'binary content == mock CDN binary');
const ver = fs.readFileSync(path.join(path.dirname(binPath), '.version'), 'utf8').trim();
assert.equal(ver, CODEX_PIN);
});
test('ensureBinary(ripgrep): 上游失败 → 回退 CDN,落地正确二进制 + .version==pin', async () => {
const binPath = await ensureBinary('ripgrep', PLATFORM, { force: true });
assert.ok(fs.readFileSync(binPath).equals(BIN), 'binary content == mock CDN binary');
const ver = fs.readFileSync(path.join(path.dirname(binPath), '.version'), 'utf8').trim();
assert.equal(ver, RIPGREP_PIN);
});