gongxiangp2/apps/detail-agent-pi/test/client-package-integrity.test.mjs

137 lines
6.6 KiB
JavaScript

import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
import { createHash } from 'node:crypto';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import test from 'node:test';
import {
checkClientPackage, PACKAGE_CHECK_SCRIPT, UI_FONT_FILES, UI_FONT_LICENSE,
verifyDaemonBoot, verifyLockfileNodeModules,
} from '../scripts/client-package-integrity.mjs';
import { packageClient } from '../scripts/package-client.mjs';
const nativeRoot = fileURLToPath(new URL('../../detail-photocraft/', import.meta.url));
async function fixture(t) {
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'xiaoyi-package-fonts-'));
t.after(() => fs.rm(root, { recursive: true, force: true }));
const sourceRoot = path.join(root, 'source');
const destination = path.join(root, 'portable');
const binary = path.join(root, 'photocraft.exe');
const nodeLicense = path.join(root, 'LICENSE-Node.txt');
const sdk = path.join(sourceRoot, 'node_modules/@earendil-works/pi-coding-agent');
for (const dir of [path.join(sourceRoot, 'src'), path.join(sourceRoot, 'vendor'), sdk]) await fs.mkdir(dir, { recursive: true });
for (const file of [binary, nodeLicense, path.join(sourceRoot, 'package.json'), path.join(sourceRoot, 'package-lock.json')]) {
await fs.writeFile(file, '{}');
}
await fs.writeFile(path.join(sdk, 'package.json'), JSON.stringify({ version: '1.5.2' }));
return { binary, destination, sourceRoot, nativeRoot, nodeLicense };
}
test('portable package ships all three unmodified font weights and the original license', async t => {
const options = await fixture(t);
const result = await packageClient(options);
assert.equal(result.packageReady, true);
const { destination } = options;
const manifest = JSON.parse(await fs.readFile(path.join(destination, 'build-manifest.json'), 'utf8'));
const checkResult = execFileSync(process.execPath, [path.join(destination, 'agent', PACKAGE_CHECK_SCRIPT)], {
encoding: 'utf8', windowsHide: true,
});
assert.deepEqual(JSON.parse(checkResult), { packageReady: true, files: result.files, bytes: result.bytes });
for (const file of [...UI_FONT_FILES, UI_FONT_LICENSE]) {
const original = await fs.readFile(path.join(nativeRoot, file));
assert.deepEqual(await fs.readFile(path.join(destination, file)), original);
assert.deepEqual(manifest.files.find(entry => entry.path === file), {
path: file,
bytes: original.length,
sha256: createHash('sha256').update(original).digest('hex'),
});
}
await t.test('missing weight or license fails package validation', async () => {
for (const file of [UI_FONT_FILES[1], UI_FONT_LICENSE]) {
const target = path.join(destination, file);
await fs.unlink(target);
await assert.rejects(checkClientPackage(options), /缺少有效文件/);
await fs.copyFile(path.join(nativeRoot, file), target);
}
});
await t.test('same-length font tampering fails the manifest hash check', async () => {
const file = UI_FONT_FILES[2];
const original = await fs.readFile(path.join(destination, file));
const changed = Buffer.from(original);
changed[0] ^= 1;
await fs.writeFile(path.join(destination, file), changed);
await assert.rejects(checkClientPackage(options), /文件校验失败.*Semibold/);
await fs.writeFile(path.join(destination, file), original);
});
await t.test('the UI font directory rejects any unapproved file', async () => {
await fs.writeFile(path.join(destination, 'assets/ui-fonts/unapproved.ttf'), 'extra');
await assert.rejects(checkClientPackage(options), /未批准文件.*unapproved/);
});
});
test('missing source font fails preflight before creating the output', async t => {
const options = await fixture(t);
options.nativeRoot = path.join(options.sourceRoot, 'native');
await fs.mkdir(path.join(options.nativeRoot, 'assets/ui-fonts'), { recursive: true });
for (const file of ['LICENSE-MIT', 'LICENSE-APACHE', 'NOTICE', 'ATTRIBUTION.md', UI_FONT_LICENSE]) {
await fs.copyFile(path.join(nativeRoot, file), path.join(options.nativeRoot, file));
}
await assert.rejects(packageClient(options), /缺少有效文件.*HarmonyOS_SansSC_Regular/);
await assert.rejects(fs.stat(options.destination), { code: 'ENOENT' });
});
test('verifyLockfileNodeModules rejects missing or mismatched dependencies and passes exact lockfile match', async t => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'xiaoyi-lock-check-'));
t.after(() => fs.rm(root, { recursive: true, force: true }));
await fs.writeFile(path.join(root, 'package.json'), JSON.stringify({
dependencies: { fflate: '0.8.2' },
}));
await fs.writeFile(path.join(root, 'package-lock.json'), JSON.stringify({
packages: {
'node_modules/fflate': { version: '0.8.2' },
'node_modules/eslint': { version: '9.0.0', dev: true },
},
}));
await assert.rejects(verifyLockfileNodeModules(root), /缺失 1 项.*fflate/);
const modDir = path.join(root, 'node_modules', 'fflate');
await fs.mkdir(modDir, { recursive: true });
await fs.writeFile(path.join(modDir, 'package.json'), JSON.stringify({ version: '0.8.0' }));
await assert.rejects(verifyLockfileNodeModules(root), /版本不符 1 项.*fflate/);
await fs.writeFile(path.join(modDir, 'package.json'), JSON.stringify({ version: '0.8.2' }));
await verifyLockfileNodeModules(root);
});
test('verifyDaemonBoot validates ready + heartbeat_ack and surfaces stderr on boot crash', async t => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'xiaoyi-daemon-boot-'));
t.after(() => fs.rm(root, { recursive: true, force: true }));
const srcDir = path.join(root, 'src');
await fs.mkdir(srcDir, { recursive: true });
const script = path.join(srcDir, 'desktop-host.mjs');
await fs.writeFile(script, [
"import { createInterface } from 'node:readline';",
"console.log(JSON.stringify({ type: 'ready' }));",
"const rl = createInterface({ input: process.stdin, crlfDelay: Infinity });",
"rl.on('line', line => {",
" const msg = JSON.parse(line);",
" if (msg.type === 'heartbeat') console.log(JSON.stringify({ type: 'heartbeat_ack', id: msg.id }));",
" if (msg.type === 'prepare_exit') process.exit(0);",
"});",
].join('\n'));
await verifyDaemonBoot(root, { nodeExe: process.execPath });
await fs.writeFile(script, [
"console.error(\"Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'fflate'\");",
"process.exit(1);",
].join('\n'));
await assert.rejects(verifyDaemonBoot(root, { nodeExe: process.execPath }), /ERR_MODULE_NOT_FOUND.*fflate/);
});