startup-preview-smoke-test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 'use strict';
  2. const assert = require('assert/strict');
  3. const fs = require('fs');
  4. const os = require('os');
  5. const path = require('path');
  6. const { spawnSync } = require('child_process');
  7. const { resolveWorkspaceRoot, workspaceIdentity } = require('../mcp/src/core/runtime-context');
  8. const { buildStartupSummary, startupAssistantMessage } = require('../mcp/src/core/startup-summary');
  9. const ROOT = path.resolve(__dirname, '..');
  10. function testRuntimeContext(tempRoot) {
  11. const installedPackage = path.join(tempRoot, '.claude', 'plugins', 'qiwei-assistant');
  12. fs.mkdirSync(installedPackage, { recursive: true });
  13. assert.equal(resolveWorkspaceRoot({ packageRoot: installedPackage, cwd: installedPackage }), tempRoot);
  14. assert.equal(resolveWorkspaceRoot({ packageRoot: ROOT, workspaceRoot: tempRoot }), tempRoot);
  15. assert.match(workspaceIdentity(tempRoot), /^[0-9a-f]{12}$/);
  16. }
  17. function testStartupSummary() {
  18. const offline = buildStartupSummary(
  19. { summary: { authConfigured: true, subscribed: true } },
  20. { data: { account: { configured: true, online: false }, listener: { running: false }, config: { allowedSenderCount: 1 } } },
  21. );
  22. assert.equal(offline.ready, false);
  23. assert.equal(offline.nextAction, '在工作台点击“恢复登录”');
  24. assert.match(startupAssistantMessage(offline), /企微账号待处理/);
  25. const ready = buildStartupSummary(
  26. { summary: { authConfigured: true, subscribed: true } },
  27. { data: { account: { configured: true, online: true }, listener: { running: true }, config: { allowedSenderCount: 1 } } },
  28. );
  29. assert.equal(ready.ready, true);
  30. assert.match(ready.nextAction, /可以进入智能会话/);
  31. }
  32. function testWorkspaceInstall(tempRoot) {
  33. const target = path.join(tempRoot, 'customer-project');
  34. fs.mkdirSync(target, { recursive: true });
  35. fs.writeFileSync(path.join(target, '.gitignore'), '# Customer rules\ncustomer-cache/\n', 'utf8');
  36. const result = spawnSync(process.execPath, [path.join(ROOT, 'install.js'), 'workspace', target, '--skip-install'], {
  37. cwd: ROOT,
  38. encoding: 'utf8',
  39. windowsHide: true,
  40. });
  41. assert.equal(result.status, 0, result.stderr || result.stdout);
  42. const mcp = JSON.parse(fs.readFileSync(path.join(target, '.mcp.json'), 'utf8'));
  43. const config = mcp.mcpServers?.['qiwei-assistant'];
  44. assert.equal(config.cwd, target);
  45. assert.equal(config.env?.QIWEI_WORKSPACE_ROOT, target);
  46. assert.equal(config.env?.QIWEI_OUTPUTS_DIR, path.join(target, 'outputs'));
  47. assert.equal(config.env?.CLAUDE_CODE_WORKDIR, target);
  48. assert(fs.existsSync(path.join(target, '.claude', 'plugins', 'qiwei-assistant', 'scripts', 'preview-dashboard.js')));
  49. assert(!fs.existsSync(path.join(target, '.claude', 'plugins', 'qiwei-assistant', '.env.local')));
  50. const gitignorePath = path.join(target, '.gitignore');
  51. const firstGitignore = fs.readFileSync(gitignorePath, 'utf8');
  52. assert.match(firstGitignore, /# Customer rules\ncustomer-cache\//);
  53. assert.match(firstGitignore, /# >>> qiwei-assistant managed ignores >>>/);
  54. assert.match(firstGitignore, /\*\*\/\.env\.local/);
  55. assert.match(firstGitignore, /outputs\/messages\/\*\.db/);
  56. const repeated = spawnSync(process.execPath, [path.join(ROOT, 'install.js'), 'workspace', target, '--skip-install'], {
  57. cwd: ROOT,
  58. encoding: 'utf8',
  59. windowsHide: true,
  60. });
  61. assert.equal(repeated.status, 0, repeated.stderr || repeated.stdout);
  62. const secondGitignore = fs.readFileSync(gitignorePath, 'utf8');
  63. assert.equal((secondGitignore.match(/# >>> qiwei-assistant managed ignores >>>/g) || []).length, 1);
  64. assert.match(secondGitignore, /# Customer rules\ncustomer-cache\//);
  65. }
  66. function main() {
  67. const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'qiwei-preview-smoke-'));
  68. try {
  69. testRuntimeContext(tempRoot);
  70. testStartupSummary();
  71. testWorkspaceInstall(tempRoot);
  72. } finally {
  73. fs.rmSync(tempRoot, { recursive: true, force: true });
  74. }
  75. process.stdout.write(`${JSON.stringify({ status: 'ok', checks: 3 }, null, 2)}\n`);
  76. }
  77. try { main(); }
  78. catch (error) {
  79. process.stderr.write(`${error.message}\n`);
  80. process.exit(1);
  81. }