package-smoke-test.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. 'use strict';
  2. const assert = require('assert/strict');
  3. const fs = require('fs');
  4. const path = require('path');
  5. const { spawnSync } = require('child_process');
  6. const ROOT = path.resolve(__dirname, '..');
  7. function readJson(relativePath) {
  8. return JSON.parse(fs.readFileSync(path.join(ROOT, relativePath), 'utf8').replace(/^\uFEFF/, ''));
  9. }
  10. function runNpmPackDryRun() {
  11. const command = process.platform === 'win32' ? 'cmd.exe' : 'npm';
  12. const args = process.platform === 'win32'
  13. ? ['/d', '/s', '/c', 'npm', 'pack', '--dry-run', '--json']
  14. : ['pack', '--dry-run', '--json'];
  15. const result = spawnSync(command, args, { cwd: ROOT, encoding: 'utf8' });
  16. if (result.status !== 0) throw new Error('npm pack --dry-run 执行失败');
  17. return JSON.parse(result.stdout)[0];
  18. }
  19. function main() {
  20. const pkg = readJson('package.json');
  21. const lock = readJson('package-lock.json');
  22. const manifest = readJson('skill-package-manifest.json');
  23. const plugin = readJson('.claude-plugin/plugin.json');
  24. const catalog = readJson('mcp/catalog/qiwei-endpoints.json');
  25. const catalogSource = fs.readFileSync(path.join(ROOT, 'mcp', 'catalog', 'qiwei-endpoints.json'), 'utf8');
  26. const serverSource = fs.readFileSync(path.join(ROOT, 'mcp', 'src', 'server.js'), 'utf8');
  27. const dashboardSource = fs.readFileSync(path.join(ROOT, 'mcp', 'src', 'dashboard', 'app.js'), 'utf8');
  28. const serverVersion = serverSource.match(/new McpServer\([\s\S]*?version:\s*['"]([^'"]+)/)?.[1];
  29. const toolNames = [...serverSource.matchAll(/registerTool\(\s*['"]([^'"]+)['"]/g)].map(match => match[1]);
  30. const skillDirs = fs.readdirSync(path.join(ROOT, 'skills'), { withFileTypes: true })
  31. .filter(entry => entry.isDirectory() && fs.existsSync(path.join(ROOT, 'skills', entry.name, 'SKILL.md')))
  32. .map(entry => entry.name)
  33. .sort();
  34. assert.equal(pkg.name, 'fmode-qiwei');
  35. assert.equal(pkg.bin?.['fmode-qiwei'], 'install.js');
  36. assert.equal(pkg.version, lock.version);
  37. assert.equal(pkg.version, lock.packages?.['']?.version);
  38. assert.equal(pkg.version, manifest.version);
  39. assert.equal(pkg.version, plugin.version);
  40. assert.equal(pkg.version, serverVersion);
  41. assert.equal(manifest.name, pkg.name);
  42. assert.equal(manifest.npmPackage, pkg.name);
  43. assert.equal(manifest.status, 'published');
  44. assert.match(manifest.installCommand, /fmode-qiwei@latest/);
  45. assert.equal(pkg.engines?.node, '>=22.5.0');
  46. assert.equal(new Set(toolNames).size, toolNames.length, 'MCP 工具名不能重复');
  47. assert.equal(toolNames.length, manifest.mcpToolCount, 'MCP 工具数量与 manifest 不一致');
  48. assert.deepEqual(skillDirs, [...manifest.skills].sort(), 'Skill 目录与 manifest 不一致');
  49. assert.equal(
  50. (catalogSource.match(/q-[a-z-]+=/gi) || []).length,
  51. 0,
  52. '公开接口清单不能包含外部签名参数'
  53. );
  54. const createClient = catalog.endpoints.find(endpoint => endpoint.id === 'client.createClient');
  55. assert(createClient, '接口清单缺少 client.createClient');
  56. const hiddenNetworkParam = ['a', 'i', 'd'].join('');
  57. assert(
  58. !createClient.params.some(param => String(param.name).toLowerCase() === hiddenNetworkParam),
  59. '公开接口清单不能暴露内部网络组件参数'
  60. );
  61. assert.equal(
  62. createClient.params.find(param => param.name === 'clientVersion')?.desc,
  63. '客户端版本由 Fmode 服务自动选择,通常无需填写。'
  64. );
  65. assert.doesNotMatch(
  66. dashboardSource,
  67. /(?:window\s*\.\s*)?(?:alert|confirm|prompt)\s*\(/,
  68. 'Dashboard 不能调用浏览器原生 alert、confirm 或 prompt'
  69. );
  70. const pack = runNpmPackDryRun();
  71. const packedPaths = new Set(pack.files.map(item => item.path.replace(/\\/g, '/')));
  72. for (const required of [
  73. '.claude-plugin/plugin.json',
  74. '.env.example',
  75. 'install.js',
  76. 'mcp/src/core/runtime-context.js',
  77. 'mcp/src/core/startup-summary.js',
  78. 'mcp/src/core/relay-daemon.js',
  79. 'mcp/src/core/agent-memory.js',
  80. 'mcp/src/core/agent-context-builder.js',
  81. 'mcp/src/core/agent-memory-worker.js',
  82. 'mcp/src/core/voice-clone-service.js',
  83. 'mcp/src/server.js',
  84. 'mcp/src/tools/qiwei-agent-control-run.js',
  85. 'qiwei.runtime.config.example.mjs',
  86. 'runtime/callback-service/package.json',
  87. 'runtime/callback-service/src/index.mjs',
  88. 'runtime/callback-service/src/personal-polling.mjs',
  89. 'runtime/callback-service/src/enterprise-relay-client.mjs',
  90. 'scripts/start-callback-runtime.mjs',
  91. 'scripts/callback-runtime-smoke-test.mjs',
  92. 'scripts/callback-relay-smoke-test.js',
  93. 'scripts/voice-clone-smoke-test.js',
  94. 'scripts/agent-console-smoke-test.js',
  95. 'scripts/preview-dashboard.js',
  96. 'scripts/startup-preview-smoke-test.js',
  97. 'skill-package-manifest.json',
  98. 'THIRD_PARTY_NOTICES.md',
  99. ]) {
  100. assert(packedPaths.has(required), `npm 包缺少 ${required}`);
  101. }
  102. const forbidden = [...packedPaths].filter(item =>
  103. /(^|\/)(?:node_modules|outputs?|\.playwright-cli|coverage|dist)(\/|$)/i.test(item) ||
  104. /(^|\/)\.env\.local$/i.test(item) ||
  105. /\.(?:db|sqlite|sqlite3|log|tgz|zip)$/i.test(item)
  106. );
  107. assert.deepEqual(forbidden, [], `npm 包包含本地运行文件:${forbidden.join(', ')}`);
  108. process.stdout.write(`${JSON.stringify({
  109. status: 'ok',
  110. version: pkg.version,
  111. skillCount: skillDirs.length,
  112. mcpToolCount: toolNames.length,
  113. packageEntries: pack.entryCount,
  114. packageSize: pack.size,
  115. unpackedSize: pack.unpackedSize,
  116. forbiddenFiles: forbidden.length,
  117. }, null, 2)}\n`);
  118. }
  119. try { main(); }
  120. catch (error) {
  121. process.stderr.write(`${error.message}\n`);
  122. process.exit(1);
  123. }