跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

prepare-release-assets.test.mjs

154 行 · 8.5 KB历史下载
1import assert from 'node:assert/strict';
2import { createHash } from 'node:crypto';
3import { lstat, mkdir, mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises';
4import { tmpdir } from 'node:os';
5import { join } from 'node:path';
6import test from 'node:test';
7import { prepareReleaseAssets } from './prepare-release-assets.mjs';
8import { checksumFor } from '../www/scripts/sync-release.mjs';
9
10const version = (await readFile(new URL('../VERSION', import.meta.url), 'utf8')).trim();
11const dependencies = JSON.parse(await readFile(new URL('../packaging/windows/dependencies.json', import.meta.url), 'utf8'));
12const macCore = (await readFile(new URL('./fetch-proxy-core.sh', import.meta.url), 'utf8')).match(/^version=(v[\d.]+)$/m)[1];
13const names = {
14 mac: `Cage-${version}-macos-arm64.dmg`,
15 windows: `Cage-${version}-windows-amd64-setup.exe`,
16 macSource: `mihomo-${macCore}-source.tar.gz`,
17 macGuestSource: `cage-macos-guest-${version}-source.tar.gz`,
18 windowsSource: `mihomo-${dependencies.mihomo.version}-source.tar.gz`,
19 qemu: `qemu-${dependencies.qemu.version}-windows-full-source.zip`,
20};
21const digest = data => createHash('sha256').update(data).digest('hex');
22
23async function fixture(t) {
24 const root = await mkdtemp(join(tmpdir(), 'cage-release-test-'));
25 t.after(() => rm(root, { recursive: true, force: true }));
26 return { root, mac: join(root, 'mac'), windows: join(root, 'windows'), output: join(root, 'publish') };
27}
28
29async function platform(directory, assets) {
30 await mkdir(directory, { recursive: true });
31 for (const [name, data] of Object.entries(assets)) await writeFile(join(directory, name), data);
32 await writeFile(join(directory, 'SHA256SUMS'), Object.entries(assets).map(([name, data]) => `${digest(data)} ${name}\n`).join(''));
33}
34
35const macAssets = () => ({ [names.mac]: 'mac package fixture', [names.macSource]: 'Mihomo source fixture', [names.macGuestSource]: 'macOS guest source fixture' });
36const windowsAssets = () => ({ [names.windows]: 'Windows package fixture', [names.windowsSource]: 'Mihomo source fixture', [names.qemu]: 'complete QEMU source fixture' });
37
38test('includes Linux deb only with its verified corresponding source and merges common sources', async t => {
39 const f = await fixture(t);
40 const linux = join(f.root, 'linux');
41 const dependencies = JSON.parse(await readFile(new URL('../packaging/linux/dependencies.json', import.meta.url), 'utf8'));
42 const deb = `Cage-${version}-linux-amd64.deb`;
43 await platform(linux, { [deb]: 'Linux package fixture' });
44 await assert.rejects(prepareReleaseAssets({ inputs: [linux], output: f.output }), /Missing Linux Mihomo source/);
45 await platform(linux, { [deb]: 'Linux package fixture', [dependencies.mihomoSource.file]: 'Mihomo source fixture' });
46 await platform(f.mac, macAssets());
47 const prepared = await prepareReleaseAssets({ inputs: [f.mac, linux], output: f.output });
48 assert.ok(prepared.assets.includes(deb));
49 assert.equal(await readFile(join(f.output, deb), 'utf8'), 'Linux package fixture');
50 assert.equal(prepared.assets.filter(name => name === dependencies.mihomoSource.file).length, 1);
51});
52
53test('combines both platforms, deduplicates identical sources and keeps website checksum lookup valid', async t => {
54 const f = await fixture(t);
55 const extraSource = `${dependencies.qemuSource.file}.gz`;
56 const packagedSources = {
57 [extraSource]: 'QEMU repository source fixture',
58 [`qemu-keycodemapdb-${dependencies.qemuKeycodemap.version}-source.tar.gz`]: 'keymap source fixture',
59 [`qemu-${dependencies.qemu.version}-cage-changes-source.tar.gz`]: 'Cage QEMU patch fixture',
60 };
61 await platform(f.mac, macAssets());
62 await platform(f.windows, { ...windowsAssets(), ...packagedSources });
63 const result = await prepareReleaseAssets({ inputs: [f.mac, f.windows], output: f.output });
64 const expected = new Map(Object.entries({ ...macAssets(), ...windowsAssets(), ...packagedSources }));
65 assert.deepEqual(new Set(await readdir(f.output)), new Set([...expected.keys(), 'SHA256SUMS']));
66 assert.equal(result.version, version);
67 const sums = await readFile(join(f.output, 'SHA256SUMS'), 'utf8');
68 for (const [name, data] of expected) {
69 assert.equal(await readFile(join(f.output, name), 'utf8'), data);
70 assert.equal(checksumFor(sums, { name }), digest(data));
71 }
72 assert.equal(sums.trimEnd().split('\n').length, expected.size);
73});
74
75test('supports a single Windows or macOS platform with its corresponding sources', async t => {
76 const f = await fixture(t);
77 await platform(f.mac, macAssets());
78 await platform(f.windows, windowsAssets());
79 assert.equal((await prepareReleaseAssets({ inputs: [f.mac], output: f.output })).assets.length, 3);
80 assert.equal((await prepareReleaseAssets({ inputs: [f.windows], output: join(f.root, 'windows-only') })).assets.length, 3);
81});
82
83test('rejects corrupted bytes before creating the output', async t => {
84 const f = await fixture(t);
85 await platform(f.mac, macAssets());
86 await writeFile(join(f.mac, names.mac), 'changed after packaging');
87 await assert.rejects(prepareReleaseAssets({ inputs: [f.mac], output: f.output }), /SHA-256 mismatch/);
88 await assert.rejects(lstat(f.output), { code: 'ENOENT' });
89});
90
91test('rejects conflicting contents under the same asset name', async t => {
92 const f = await fixture(t);
93 const second = join(f.root, 'second-mac');
94 await platform(f.mac, macAssets());
95 await platform(second, { ...macAssets(), [names.macSource]: 'different source archive' });
96 await assert.rejects(prepareReleaseAssets({ inputs: [f.mac, second], output: f.output }), /Conflicting same-name/);
97});
98
99test('checks duplicate input bytes even when their manifests claim identical digests', async t => {
100 const f = await fixture(t);
101 const second = join(f.root, 'second-mac');
102 await platform(f.mac, macAssets());
103 await platform(second, macAssets());
104 await writeFile(join(second, names.macSource), 'corrupted duplicate');
105 await assert.rejects(prepareReleaseAssets({ inputs: [f.mac, second], output: f.output }), /SHA-256 mismatch/);
106});
107
108test('rejects unsigned development installers including unlisted leftovers', async t => {
109 const f = await fixture(t);
110 const unsigned = names.windows.replace(/\.exe$/, '-unsigned.exe');
111 const assets = windowsAssets();
112 assets[unsigned] = assets[names.windows];
113 delete assets[names.windows];
114 await platform(f.windows, assets);
115 await assert.rejects(prepareReleaseAssets({ inputs: [f.windows], output: f.output }), /Unsigned Windows development/);
116 await platform(f.windows, windowsAssets());
117 await assert.rejects(prepareReleaseAssets({ inputs: [f.windows], output: f.output }), /Unsigned Windows development/);
118});
119
120test('requires Windows complete QEMU source in the same verified manifest', async t => {
121 const f = await fixture(t);
122 const assets = windowsAssets();
123 delete assets[names.qemu];
124 await platform(f.windows, assets);
125 await writeFile(join(f.windows, names.qemu), 'unlisted source is not verified');
126 await assert.rejects(prepareReleaseAssets({ inputs: [f.windows], output: f.output }), /complete QEMU source/);
127});
128
129test('requires the shipped macOS guest corresponding source in the verified manifest', async t => {
130 const f = await fixture(t);
131 const assets = macAssets();
132 delete assets[names.macGuestSource];
133 await platform(f.mac, assets);
134 await writeFile(join(f.mac, names.macGuestSource), 'unlisted source is not verified');
135 await assert.rejects(prepareReleaseAssets({ inputs: [f.mac], output: f.output }), /macOS guest corresponding source/);
136});
137
138test('rejects wrong versions and manifest paths outside the release directory', async t => {
139 const f = await fixture(t);
140 await platform(f.mac, { ...macAssets(), 'Cage-99.0.0-macos-arm64.dmg': 'wrong version' });
141 await assert.rejects(prepareReleaseAssets({ inputs: [f.mac], output: f.output }), /Unsupported asset or version/);
142 await writeFile(join(f.mac, 'SHA256SUMS'), `${digest('outside')} ../escape.dmg\n`);
143 await assert.rejects(prepareReleaseAssets({ inputs: [f.mac], output: f.output }), /Invalid or duplicate/);
144});
145
146test('never overwrites an existing output, including an empty one', async t => {
147 const f = await fixture(t);
148 await platform(f.mac, macAssets());
149 await mkdir(f.output);
150 await assert.rejects(prepareReleaseAssets({ inputs: [f.mac], output: f.output }), /Output must be a new directory/);
151 await writeFile(join(f.output, 'published-asset'), 'keep');
152 await assert.rejects(prepareReleaseAssets({ inputs: [f.mac], output: f.output }), /Output must be a new directory/);
153 assert.equal(await readFile(join(f.output, 'published-asset'), 'utf8'), 'keep');
154});