Files
cindy/scripts/__tests__/agent-binary-cdn-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

177 lines
6.3 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.

// agent-binary-cdn-fallback 单测:验证 CDN 兜底的 URL 拼接、gunzip、best-effort sha256 校验。
// 用 node 内置 http server mock CDN(manifest + .gz),node 内置 test runner,无 vitest 依赖。
// 直接 `node --test scripts/__tests__/agent-binary-cdn-fallback.test.mjs`。
import { test, before, after, beforeEach } from 'node:test';
import assert from 'node:assert/strict';
import http from 'node:http';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import crypto from 'node:crypto';
import zlib from 'node:zlib';
import { downloadFromCdn } from '../agent-binary-cdn-fallback.mjs';
const VERSION = '2.1.186';
const BINARY = Buffer.alloc(2048, 9); // 裸二进制内容(>1024)
const GZ = zlib.gzipSync(BINARY);
function sha256(buf) {
return crypto.createHash('sha256').update(buf).digest('hex');
}
const GZ_SHA = sha256(GZ);
const BIN_SHA = sha256(BINARY);
// ── 可配置 mock CDN:每个测试设置 routes(url path → handler) ────────────────
let server;
let base;
let routes; // Map<string, (req,res)=>void>
before(async () => {
server = http.createServer((req, res) => {
const urlPath = req.url.split('?')[0];
const handler = routes.get(urlPath);
if (!handler) {
res.writeHead(404);
res.end('not found');
return;
}
handler(req, res);
});
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
base = `http://127.0.0.1:${server.address().port}`;
process.env.XDT_CDN_BASE_URL = base;
});
after(async () => {
delete process.env.XDT_CDN_BASE_URL;
await new Promise((resolve) => server.close(resolve));
});
beforeEach(() => {
routes = new Map();
});
function tmpBinPath(name = 'claude') {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cdn-fb-test-'));
return path.join(dir, name);
}
function serveJson(obj) {
return (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(obj));
};
}
function serveGz() {
return (req, res) => {
res.writeHead(200, { 'Content-Type': 'application/octet-stream' });
res.end(GZ);
};
}
function manifest(field, overrides = {}) {
return {
[field]: {
version: VERSION,
file: `claude-code/${VERSION}/darwin-arm64/claude.gz`,
sha256: GZ_SHA,
size: GZ.length,
binarySha256: BIN_SHA,
...overrides,
},
};
}
test('downloadFromCdn: 成功兜底——gunzip 后 binPath 内容正确,sha 全校验', async () => {
routes.set('/manifest-darwin-arm64-canary.json', serveJson(manifest('claudeCode')));
routes.set(`/claude-code/${VERSION}/darwin-arm64/claude.gz`, serveGz());
const binPath = tmpBinPath();
const r = await downloadFromCdn({ kind: 'claude', version: VERSION, platformKey: 'darwin-arm64', binPath });
assert.equal(r.gzVerified, true);
assert.equal(r.binaryVerified, true);
assert.deepEqual(fs.readFileSync(binPath), BINARY);
// 中转文件清理
assert.equal(fs.existsSync(`${binPath}.gz.tmp`), false);
assert.equal(fs.existsSync(`${binPath}.tmp`), false);
});
test('downloadFromCdn: manifest version 不匹配 pin → 跳过 sha 校验仍成功', async () => {
// canary 拿得到但 version 不符;stable 404 → tryGetManifestAsset 返回 null
routes.set('/manifest-darwin-arm64-canary.json', serveJson(manifest('claudeCode', { version: '9.9.9' })));
routes.set(`/claude-code/${VERSION}/darwin-arm64/claude.gz`, serveGz());
const binPath = tmpBinPath();
const r = await downloadFromCdn({ kind: 'claude', version: VERSION, platformKey: 'darwin-arm64', binPath });
assert.equal(r.gzVerified, false);
assert.equal(r.binaryVerified, false);
assert.deepEqual(fs.readFileSync(binPath), BINARY);
});
test('downloadFromCdn: .gz sha256 mismatch → 抛错且不落地', async () => {
routes.set('/manifest-darwin-arm64-canary.json', serveJson(manifest('claudeCode', { sha256: 'deadbeef'.repeat(8) })));
routes.set(`/claude-code/${VERSION}/darwin-arm64/claude.gz`, serveGz());
const binPath = tmpBinPath();
await assert.rejects(
() => downloadFromCdn({ kind: 'claude', version: VERSION, platformKey: 'darwin-arm64', binPath }),
/\.gz sha256 mismatch/,
);
assert.equal(fs.existsSync(binPath), false);
});
test('downloadFromCdn: 二进制 binarySha256 mismatch → 抛错', async () => {
routes.set('/manifest-darwin-arm64-canary.json', serveJson(manifest('claudeCode', { binarySha256: 'cafe'.repeat(16) })));
routes.set(`/claude-code/${VERSION}/darwin-arm64/claude.gz`, serveGz());
const binPath = tmpBinPath();
await assert.rejects(
() => downloadFromCdn({ kind: 'claude', version: VERSION, platformKey: 'darwin-arm64', binPath }),
/binary sha256 mismatch/,
);
assert.equal(fs.existsSync(binPath), false);
});
test('downloadFromCdn: win32 平台拼出 .exe.gz URL', async () => {
let hitPath = null;
routes.set('/manifest-win32-x64-canary.json', serveJson({
codex: { version: VERSION, file: 'x', sha256: GZ_SHA, size: GZ.length, binarySha256: BIN_SHA },
}));
routes.set(`/codex/${VERSION}/win32-x64/codex.exe.gz`, (req, res) => {
hitPath = req.url.split('?')[0];
res.writeHead(200);
res.end(GZ);
});
const binPath = tmpBinPath('codex.exe');
const r = await downloadFromCdn({ kind: 'codex', version: VERSION, platformKey: 'win32-x64', binPath });
assert.equal(hitPath, `/codex/${VERSION}/win32-x64/codex.exe.gz`);
assert.equal(r.binaryVerified, true);
});
test('downloadFromCdn: ripgrep 没有 manifest 字段时也按约定 URL 兜底成功', async () => {
let hitPath = null;
routes.set(`/ripgrep/${VERSION}/darwin-arm64/rg.gz`, (req, res) => {
hitPath = req.url.split('?')[0];
res.writeHead(200);
res.end(GZ);
});
const binPath = tmpBinPath('rg');
const r = await downloadFromCdn({ kind: 'ripgrep', version: VERSION, platformKey: 'darwin-arm64', binPath });
assert.equal(hitPath, `/ripgrep/${VERSION}/darwin-arm64/rg.gz`);
assert.equal(r.gzVerified, false);
assert.equal(r.binaryVerified, false);
assert.deepEqual(fs.readFileSync(binPath), BINARY);
});
test('downloadFromCdn: 不支持的 kind 直接抛错', async () => {
const binPath = tmpBinPath('rg');
await assert.rejects(
() => downloadFromCdn({ kind: 'gemini', version: VERSION, platformKey: 'darwin-arm64', binPath }),
/not supported for kind: gemini/,
);
});