package-smoke-test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 serverVersion = serverSource.match(/new McpServer\([\s\S]*?version:\s*['"]([^'"]+)/)?.[1];
  28. const toolNames = [...serverSource.matchAll(/registerTool\(\s*['"]([^'"]+)['"]/g)].map(match => match[1]);
  29. const skillDirs = fs.readdirSync(path.join(ROOT, 'skills'), { withFileTypes: true })
  30. .filter(entry => entry.isDirectory() && fs.existsSync(path.join(ROOT, 'skills', entry.name, 'SKILL.md')))
  31. .map(entry => entry.name)
  32. .sort();
  33. assert.equal(pkg.name, 'fmode-qiwei');
  34. assert.equal(pkg.bin?.['fmode-qiwei'], 'install.js');
  35. assert.equal(pkg.version, lock.version);
  36. assert.equal(pkg.version, lock.packages?.['']?.version);
  37. assert.equal(pkg.version, manifest.version);
  38. assert.equal(pkg.version, plugin.version);
  39. assert.equal(pkg.version, serverVersion);
  40. assert.equal(manifest.name, pkg.name);
  41. assert.equal(manifest.npmPackage, pkg.name);
  42. assert.equal(manifest.status, 'published');
  43. assert.match(manifest.installCommand, /fmode-qiwei@latest/);
  44. assert.equal(pkg.engines?.node, '>=22.5.0');
  45. assert.equal(new Set(toolNames).size, toolNames.length, 'MCP 工具名不能重复');
  46. assert.equal(toolNames.length, manifest.mcpToolCount, 'MCP 工具数量与 manifest 不一致');
  47. assert.deepEqual(skillDirs, [...manifest.skills].sort(), 'Skill 目录与 manifest 不一致');
  48. assert.equal(
  49. (catalogSource.match(/q-[a-z-]+=/gi) || []).length,
  50. 0,
  51. '公开接口清单不能包含外部签名参数'
  52. );
  53. const createClient = catalog.endpoints.find(endpoint => endpoint.id === 'client.createClient');
  54. assert(createClient, '接口清单缺少 client.createClient');
  55. const hiddenNetworkParam = ['a', 'i', 'd'].join('');
  56. assert(
  57. !createClient.params.some(param => String(param.name).toLowerCase() === hiddenNetworkParam),
  58. '公开接口清单不能暴露内部网络组件参数'
  59. );
  60. assert.equal(
  61. createClient.params.find(param => param.name === 'clientVersion')?.desc,
  62. '客户端版本由 Fmode 服务自动选择,通常无需填写。'
  63. );
  64. const pack = runNpmPackDryRun();
  65. const packedPaths = new Set(pack.files.map(item => item.path.replace(/\\/g, '/')));
  66. for (const required of [
  67. '.claude-plugin/plugin.json',
  68. '.env.example',
  69. 'install.js',
  70. 'mcp/src/core/runtime-context.js',
  71. 'mcp/src/core/startup-summary.js',
  72. 'mcp/src/server.js',
  73. 'mcp/src/tools/qiwei-agent-control-run.js',
  74. 'scripts/agent-console-smoke-test.js',
  75. 'scripts/preview-dashboard.js',
  76. 'scripts/startup-preview-smoke-test.js',
  77. 'skill-package-manifest.json',
  78. 'THIRD_PARTY_NOTICES.md',
  79. ]) {
  80. assert(packedPaths.has(required), `npm 包缺少 ${required}`);
  81. }
  82. const forbidden = [...packedPaths].filter(item =>
  83. /(^|\/)(?:node_modules|outputs?|\.playwright-cli|coverage|dist)(\/|$)/i.test(item) ||
  84. /(^|\/)\.env\.local$/i.test(item) ||
  85. /\.(?:db|sqlite|sqlite3|log|tgz|zip)$/i.test(item)
  86. );
  87. assert.deepEqual(forbidden, [], `npm 包包含本地运行文件:${forbidden.join(', ')}`);
  88. process.stdout.write(`${JSON.stringify({
  89. status: 'ok',
  90. version: pkg.version,
  91. skillCount: skillDirs.length,
  92. mcpToolCount: toolNames.length,
  93. packageEntries: pack.entryCount,
  94. packageSize: pack.size,
  95. unpackedSize: pack.unpackedSize,
  96. forbiddenFiles: forbidden.length,
  97. }, null, 2)}\n`);
  98. }
  99. try { main(); }
  100. catch (error) {
  101. process.stderr.write(`${error.message}\n`);
  102. process.exit(1);
  103. }