startup-preview-smoke-test.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 testRuntimeProcessSeparation() {
  67. const previewSource = fs.readFileSync(path.join(ROOT, 'scripts', 'preview-dashboard.js'), 'utf8');
  68. const dashboardSource = fs.readFileSync(path.join(ROOT, 'scripts', 'start-dashboard.js'), 'utf8');
  69. const serverSource = fs.readFileSync(path.join(ROOT, 'mcp', 'src', 'server.js'), 'utf8');
  70. assert.doesNotMatch(previewSource, /\.startRuntime\s*\(/);
  71. assert.match(previewSource, /ensureRelayDaemon\s*\(/);
  72. assert.match(dashboardSource, /ensureRelayDaemon\s*\(/);
  73. assert.match(serverSource, /ensureRelayDaemon\s*\(/);
  74. }
  75. function testAgentWorkbenchOfflineStartup() {
  76. const appSource = fs.readFileSync(path.join(ROOT, 'mcp', 'src', 'dashboard', 'app.js'), 'utf8');
  77. const renderAgentSource = appSource.slice(
  78. appSource.indexOf('function renderAgentPage()'),
  79. appSource.indexOf('// ==================== Login ===================='),
  80. );
  81. assert.match(renderAgentSource, /startAgentPageRuntime\(page\);/);
  82. assert.doesNotMatch(renderAgentSource, /if \(!state\.bootstrapping\)\s+startAgentPageRuntime\(page\)/);
  83. assert.match(appSource, /api\('GET', '\/api\/status\?fast=true', undefined, 2500\)/);
  84. assert.match(appSource, /api\('GET', '\/api\/agent\/status', undefined, 2500\)/);
  85. }
  86. function main() {
  87. const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'qiwei-preview-smoke-'));
  88. try {
  89. testRuntimeContext(tempRoot);
  90. testStartupSummary();
  91. testWorkspaceInstall(tempRoot);
  92. testRuntimeProcessSeparation();
  93. testAgentWorkbenchOfflineStartup();
  94. } finally {
  95. fs.rmSync(tempRoot, { recursive: true, force: true });
  96. }
  97. process.stdout.write(`${JSON.stringify({ status: 'ok', checks: 5 }, null, 2)}\n`);
  98. }
  99. try { main(); }
  100. catch (error) {
  101. process.stderr.write(`${error.message}\n`);
  102. process.exit(1);
  103. }