package-smoke-test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 serverSource = fs.readFileSync(path.join(ROOT, 'mcp', 'src', 'server.js'), 'utf8');
  25. const serverVersion = serverSource.match(/new McpServer\([\s\S]*?version:\s*['"]([^'"]+)/)?.[1];
  26. const toolNames = [...serverSource.matchAll(/registerTool\(\s*['"]([^'"]+)['"]/g)].map(match => match[1]);
  27. const skillDirs = fs.readdirSync(path.join(ROOT, 'skills'), { withFileTypes: true })
  28. .filter(entry => entry.isDirectory() && fs.existsSync(path.join(ROOT, 'skills', entry.name, 'SKILL.md')))
  29. .map(entry => entry.name)
  30. .sort();
  31. assert.equal(pkg.version, lock.version);
  32. assert.equal(pkg.version, lock.packages?.['']?.version);
  33. assert.equal(pkg.version, manifest.version);
  34. assert.equal(pkg.version, plugin.version);
  35. assert.equal(pkg.version, serverVersion);
  36. assert.equal(pkg.engines?.node, '>=22.5.0');
  37. assert.equal(new Set(toolNames).size, toolNames.length, 'MCP 工具名不能重复');
  38. assert.equal(toolNames.length, manifest.mcpToolCount, 'MCP 工具数量与 manifest 不一致');
  39. assert.deepEqual(skillDirs, [...manifest.skills].sort(), 'Skill 目录与 manifest 不一致');
  40. const pack = runNpmPackDryRun();
  41. const packedPaths = new Set(pack.files.map(item => item.path.replace(/\\/g, '/')));
  42. for (const required of [
  43. '.claude-plugin/plugin.json',
  44. '.env.example',
  45. 'install.js',
  46. 'mcp/src/server.js',
  47. 'mcp/src/tools/qiwei-agent-control-run.js',
  48. 'scripts/agent-console-smoke-test.js',
  49. 'skill-package-manifest.json',
  50. 'THIRD_PARTY_NOTICES.md',
  51. ]) {
  52. assert(packedPaths.has(required), `npm 包缺少 ${required}`);
  53. }
  54. const forbidden = [...packedPaths].filter(item =>
  55. /(^|\/)(?:node_modules|outputs?|\.playwright-cli|coverage|dist)(\/|$)/i.test(item) ||
  56. /(^|\/)\.env\.local$/i.test(item) ||
  57. /\.(?:db|sqlite|sqlite3|log|tgz|zip)$/i.test(item)
  58. );
  59. assert.deepEqual(forbidden, [], `npm 包包含本地运行文件:${forbidden.join(', ')}`);
  60. process.stdout.write(`${JSON.stringify({
  61. status: 'ok',
  62. version: pkg.version,
  63. skillCount: skillDirs.length,
  64. mcpToolCount: toolNames.length,
  65. packageEntries: pack.entryCount,
  66. packageSize: pack.size,
  67. unpackedSize: pack.unpackedSize,
  68. forbiddenFiles: forbidden.length,
  69. }, null, 2)}\n`);
  70. }
  71. try { main(); }
  72. catch (error) {
  73. process.stderr.write(`${error.message}\n`);
  74. process.exit(1);
  75. }