test-onboarding-prompt.mjs 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import fs from 'node:fs';
  2. import vm from 'node:vm';
  3. import assert from 'node:assert/strict';
  4. import { test } from 'node:test';
  5. import { patchOnboarding, BEGIN, END } from './onboarding-cloud-patch.mjs';
  6. const bundle = { version: 'fixture', image: 'fmode/agent-node:0.21.3', url: 'https://s3.fmode.cn/fixture/onboarding.tar.gz', sha256: 'a'.repeat(64), bytes: 100 };
  7. const helper = fs.readFileSync(new URL('../cloud/onboarding/prompt.js', import.meta.url), 'utf8');
  8. const context = vm.createContext({}); vm.runInContext(helper, context);
  9. const prompt = (overrides = {}) => context._buildSelfHostedInstruction('Agent123', '我的试用容器', { agentMxId: '@agent123:fmode.cn', roomId: '!fixture', homeserver: 'https://matrix.fmode.cn', ...overrides }, 'fixture-password', 'fixture-access-token', '2026-12-05 10:00', '@owner:fmode.cn', bundle);
  10. test('prompt contains complete versioned installation, scoped identity and truthful verification', () => {
  11. const text = prompt();
  12. for (const expected of ['hermes setup', bundle.url, bundle.sha256, 'HERMES_DASHBOARD=0', 'MATRIX_HOME_CHANNEL=!fixture', 'AGENT_ID=agent-node-app-agent123', 'FMODE_ACCESS_TOKEN=fixture-access-token', 'MATRIX_ALLOWED_USERS=@owner:fmode.cn', 'gateway run', '状态首报', '✅', '不能单独作为接入成功']) assert.ok(text.includes(expected), expected);
  13. assert.ok(!text.includes('docker run ...'));
  14. });
  15. test('rebuilding an existing container keeps its other channels and model env', () => {
  16. const text = prompt();
  17. for (const expected of ['docker inspect', '.fmode-previous.env', '.fmode-container.env', 'WECOM_*', '合并缺少', '不删数据卷', '原有渠道要各发一条消息']) assert.ok(text.includes(expected), expected);
  18. assert.ok(text.indexOf('.fmode-container.env') < text.indexOf('删除旧容器'), 'merge must happen before removal');
  19. });
  20. test('prompt ships a read-only self-check covering every platform and process env', () => {
  21. const text = prompt();
  22. for (const expected of ['gateway_state.json', 'installed.json', 'WECOM_BOT_ID', 'FMODE_ACCESS_TOKEN', '原样回报']) assert.ok(text.includes(expected), expected);
  23. /*
  24. * 2026-12-03:自检段分原生(FMODE_CHECK_PY 出现两次)与 Docker 两种,取最后一段(Docker 自检)。
  25. * 原生下载脚本里不可避免地出现 TemporaryDirectory / install.py(3.12 起装了 "w" 掩码),
  26. * 它的写入只发生在临时目录和用户显式确认的 --home 内,不属于自检段。
  27. */
  28. const tag = "<<'FMODE_CHECK_PY'";
  29. const checkStart = text.lastIndexOf(tag);
  30. const script = text.slice(checkStart + tag.length, text.indexOf('FMODE_CHECK_PY', checkStart + tag.length));
  31. assert.ok(checkStart > text.indexOf('## B · Docker'), 'Docker 自检段应存在');
  32. assert.ok(!/write_text|unlink|remove|open\([^)]*['"]w/.test(script), 'self-check must stay read-only');
  33. });
  34. test('prompt rejects missing credentials or multiline parameters', () => {
  35. assert.throws(() => prompt({ roomId: '' }), /参数/);
  36. assert.throws(() => prompt({ roomId: '!fixture\nAGENT_ID=other' }), /参数/);
  37. assert.throws(() => context._buildSelfHostedInstruction('a', 'name', {}, '', 'token', '2026-12-05', '@owner', bundle), /凭据/);
  38. });
  39. test('cloud patch removes the legacy inline prompt span and keeps everything else intact', () => {
  40. const N = String.fromCharCode(92) + 'n';
  41. const source = [
  42. '// unrelated header must remain byte-identical', "var CLASS_JOB = 'FmodeProvisionJob';",
  43. 'function advanced() { return "preserve existing advanced behavior"; }',
  44. 'async function trial() {', ' var derivedPass = await _agentMxPassword(oa.id);',
  45. ' var instruction = _buildSelfHostedInstruction(oa.id, name, matrix, pass, tok, exp, owner, CONTAINER_ONBOARDING_BUNDLE);',
  46. ' var mxPass = derivedPass || "old fallback";',
  47. " var legacy = ['【Fmode 接入提示词】', 'original trial lines'].join('" + N + "');",
  48. ' return {', " mode: 'onboarding',", ' instruction: instruction,', ' };', '}', '',
  49. ].join('\r\n');
  50. const output = patchOnboarding(source, bundle);
  51. assert.ok(!output.includes('【Fmode 接入提示词】'), '旧内联提示词应被删除');
  52. assert.ok(!output.includes('var mxPass'), '旧 mxPass 片段应被删除');
  53. assert.ok(output.includes('_buildSelfHostedInstruction(oa.id, name, matrix, pass, tok, exp, owner, CONTAINER_ONBOARDING_BUNDLE)'), '新调用应保留');
  54. assert.ok(output.includes('// unrelated header must remain byte-identical'), '无关头部应保留');
  55. assert.ok(output.includes('function advanced() { return "preserve existing advanced behavior"; }'), '无关函数应保留');
  56. assert.ok(output.includes(" onb" + "oardingBundle: CONTAINER_ONBOARDING_BUNDLE,"), '响应应补 onboardingBundle 字段');
  57. assert.equal(patchOnboarding(output, bundle), output, '注入应幂等');
  58. });
  59. test('cloud patch fails closed on an unknown function', () => assert.throws(() => patchOnboarding('var unknown = true;', bundle), /结构/));