| 12345678910111213141516171819202122232425262728 |
- import { test } from 'node:test';
- import assert from 'node:assert/strict';
- import fs from 'node:fs';
- import vm from 'node:vm';
- import { createHash } from 'node:crypto';
- import { validatedPromptInputs } from './lib/onboarding-input.mjs';
- const bundle = { version: '1.0.1', image: 'fmode/agent-node:0.21.3', url: 'https://s3.fmode.cn/fixture.tar.gz', sha256: 'a'.repeat(64), bytes: 100 };
- const context = vm.createContext({});
- vm.runInContext(fs.readFileSync(new URL('../cloud/onboarding/prompt.js', import.meta.url), 'utf8'), context);
- const instruction = context._buildSelfHostedInstruction('agent', 'fixture', { agentMxId: '@agent:fmode.cn', roomId: '!room' }, 'fixture-password', 'fixture-access-token', '2026-12-05 10:00', '@owner:fmode.cn', bundle);
- const env = Object.fromEntries(instruction.split('\n').map(line => line.match(/^([A-Z][A-Z0-9_]*)=(.*)$/)).filter(Boolean).map(([, key, value]) => [key, value.trim()]));
- const receipt = { instructionSha256: createHash('sha256').update(instruction).digest('hex'), bundle: { ...bundle } };
- test('unchanged APK prompt yields exactly its own environment and installer', () => {
- const result = validatedPromptInputs(instruction, receipt, env, bundle);
- assert.deepEqual(result.env, env); assert.ok(result.script.includes(bundle.sha256));
- });
- test('changed prompt is rejected even when the original URL and SHA remain present', () => {
- assert.throws(() => validatedPromptInputs(instruction + '\n# changed after capture', receipt, env, bundle), /原文摘要发生变化/);
- });
- test('changed credentials file is rejected even when identity IDs are unchanged', () => {
- assert.throws(() => validatedPromptInputs(instruction, receipt, { ...env, MATRIX_PASSWORD: 'different' }, bundle), /与已校验的APK原文不一致/);
- });
- test('same archive SHA cannot hide changed URL or runtime image after capture', () => {
- assert.throws(() => validatedPromptInputs(instruction, receipt, env, { ...bundle, url: 'https://s3.fmode.cn/other.tar.gz' }), /元数据发生变化/);
- assert.throws(() => validatedPromptInputs(instruction, receipt, env, { ...bundle, image: 'different/runtime' }), /元数据发生变化/);
- });
|