test-onboarding-input.mjs 2.1 KB

12345678910111213141516171819202122232425262728
  1. import { test } from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import fs from 'node:fs';
  4. import vm from 'node:vm';
  5. import { createHash } from 'node:crypto';
  6. import { validatedPromptInputs } from './lib/onboarding-input.mjs';
  7. 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 };
  8. const context = vm.createContext({});
  9. vm.runInContext(fs.readFileSync(new URL('../cloud/onboarding/prompt.js', import.meta.url), 'utf8'), context);
  10. 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);
  11. const env = Object.fromEntries(instruction.split('\n').map(line => line.match(/^([A-Z][A-Z0-9_]*)=(.*)$/)).filter(Boolean).map(([, key, value]) => [key, value.trim()]));
  12. const receipt = { instructionSha256: createHash('sha256').update(instruction).digest('hex'), bundle: { ...bundle } };
  13. test('unchanged APK prompt yields exactly its own environment and installer', () => {
  14. const result = validatedPromptInputs(instruction, receipt, env, bundle);
  15. assert.deepEqual(result.env, env); assert.ok(result.script.includes(bundle.sha256));
  16. });
  17. test('changed prompt is rejected even when the original URL and SHA remain present', () => {
  18. assert.throws(() => validatedPromptInputs(instruction + '\n# changed after capture', receipt, env, bundle), /原文摘要发生变化/);
  19. });
  20. test('changed credentials file is rejected even when identity IDs are unchanged', () => {
  21. assert.throws(() => validatedPromptInputs(instruction, receipt, { ...env, MATRIX_PASSWORD: 'different' }, bundle), /与已校验的APK原文不一致/);
  22. });
  23. test('same archive SHA cannot hide changed URL or runtime image after capture', () => {
  24. assert.throws(() => validatedPromptInputs(instruction, receipt, env, { ...bundle, url: 'https://s3.fmode.cn/other.tar.gz' }), /元数据发生变化/);
  25. assert.throws(() => validatedPromptInputs(instruction, receipt, env, { ...bundle, image: 'different/runtime' }), /元数据发生变化/);
  26. });