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

80 lines
3.9 KiB
JavaScript

import assert from 'node:assert/strict';
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, UI_FONT_FILES, UI_FONT_LICENSE } 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'));
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' });
});