server.test.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. const { spawn } = require('child_process');
  2. const http = require('http');
  3. const WebSocket = require('ws');
  4. const fs = require('fs');
  5. const path = require('path');
  6. const assert = require('assert');
  7. const SERVER_PATH = path.join(__dirname, '../../lib/brainstorm-server/index.js');
  8. const TEST_PORT = 3334;
  9. const TEST_DIR = '/tmp/brainstorm-test';
  10. function cleanup() {
  11. if (fs.existsSync(TEST_DIR)) {
  12. fs.rmSync(TEST_DIR, { recursive: true });
  13. }
  14. }
  15. async function sleep(ms) {
  16. return new Promise(resolve => setTimeout(resolve, ms));
  17. }
  18. async function fetch(url) {
  19. return new Promise((resolve, reject) => {
  20. http.get(url, (res) => {
  21. let data = '';
  22. res.on('data', chunk => data += chunk);
  23. res.on('end', () => resolve({ status: res.statusCode, body: data }));
  24. }).on('error', reject);
  25. });
  26. }
  27. function startServer() {
  28. return spawn('node', [SERVER_PATH], {
  29. env: { ...process.env, BRAINSTORM_PORT: TEST_PORT, BRAINSTORM_DIR: TEST_DIR }
  30. });
  31. }
  32. async function runTests() {
  33. cleanup();
  34. fs.mkdirSync(TEST_DIR, { recursive: true });
  35. const server = startServer();
  36. let stdout = '';
  37. server.stdout.on('data', (data) => { stdout += data.toString(); });
  38. server.stderr.on('data', (data) => { console.error('Server stderr:', data.toString()); });
  39. await sleep(1000);
  40. try {
  41. // Test 1: Server starts and outputs JSON
  42. console.log('Test 1: Server startup message');
  43. assert(stdout.includes('server-started'), 'Should output server-started');
  44. assert(stdout.includes(TEST_PORT.toString()), 'Should include port');
  45. console.log(' PASS');
  46. // Test 2: GET / returns waiting page with helper injected when no screens exist
  47. console.log('Test 2: Serves waiting page with helper injected');
  48. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  49. assert.strictEqual(res.status, 200);
  50. assert(res.body.includes('Waiting for Claude'), 'Should show waiting message');
  51. assert(res.body.includes('WebSocket'), 'Should have helper.js injected');
  52. console.log(' PASS');
  53. // Test 3: WebSocket connection and event relay
  54. console.log('Test 3: WebSocket relays events to stdout');
  55. stdout = '';
  56. const ws = new WebSocket(`ws://localhost:${TEST_PORT}`);
  57. await new Promise(resolve => ws.on('open', resolve));
  58. ws.send(JSON.stringify({ type: 'click', text: 'Test Button' }));
  59. await sleep(300);
  60. assert(stdout.includes('"source":"user-event"'), 'Should relay user events with source field');
  61. assert(stdout.includes('Test Button'), 'Should include event data');
  62. ws.close();
  63. console.log(' PASS');
  64. // Test 4: File change triggers reload notification
  65. console.log('Test 4: File change notifies browsers');
  66. const ws2 = new WebSocket(`ws://localhost:${TEST_PORT}`);
  67. await new Promise(resolve => ws2.on('open', resolve));
  68. let gotReload = false;
  69. ws2.on('message', (data) => {
  70. const msg = JSON.parse(data.toString());
  71. if (msg.type === 'reload') gotReload = true;
  72. });
  73. fs.writeFileSync(path.join(TEST_DIR, 'test-screen.html'), '<html><body>Full doc</body></html>');
  74. await sleep(500);
  75. assert(gotReload, 'Should send reload message on file change');
  76. ws2.close();
  77. console.log(' PASS');
  78. // Test 5: Full HTML document served as-is (not wrapped)
  79. console.log('Test 5: Full HTML document served without frame wrapping');
  80. const fullDoc = '<!DOCTYPE html>\n<html><head><title>Custom</title></head><body><h1>Custom Page</h1></body></html>';
  81. fs.writeFileSync(path.join(TEST_DIR, 'full-doc.html'), fullDoc);
  82. await sleep(300);
  83. const fullRes = await fetch(`http://localhost:${TEST_PORT}/`);
  84. assert(fullRes.body.includes('<h1>Custom Page</h1>'), 'Should contain original content');
  85. assert(fullRes.body.includes('WebSocket'), 'Should still inject helper.js');
  86. // Should NOT have the frame template's feedback footer
  87. assert(!fullRes.body.includes('feedback-footer') || fullDoc.includes('feedback-footer'),
  88. 'Should not wrap full documents in frame template');
  89. console.log(' PASS');
  90. // Test 6: Bare HTML fragment gets wrapped in frame template
  91. console.log('Test 6: Content fragment wrapped in frame template');
  92. const fragment = '<h2>Pick a layout</h2>\n<p class="subtitle">Choose one</p>\n<div class="options"><div class="option" data-choice="a"><div class="letter">A</div><div class="content"><h3>Simple</h3></div></div></div>';
  93. fs.writeFileSync(path.join(TEST_DIR, 'fragment.html'), fragment);
  94. await sleep(300);
  95. const fragRes = await fetch(`http://localhost:${TEST_PORT}/`);
  96. // Should have the frame template structure
  97. assert(fragRes.body.includes('feedback-footer'), 'Fragment should get feedback footer from frame');
  98. assert(fragRes.body.includes('Brainstorm Companion'), 'Fragment should get header from frame');
  99. assert(fragRes.body.includes('--bg-primary'), 'Fragment should get theme CSS from frame');
  100. // Should have the original content inside
  101. assert(fragRes.body.includes('Pick a layout'), 'Fragment content should be present');
  102. assert(fragRes.body.includes('data-choice="a"'), 'Fragment content should be intact');
  103. // Should have helper.js injected
  104. assert(fragRes.body.includes('WebSocket'), 'Fragment should have helper.js injected');
  105. console.log(' PASS');
  106. // Test 7: Helper.js includes toggleSelect and send functions
  107. console.log('Test 7: Helper.js provides toggleSelect and send');
  108. const helperContent = fs.readFileSync(
  109. path.join(__dirname, '../../lib/brainstorm-server/helper.js'), 'utf-8'
  110. );
  111. assert(helperContent.includes('toggleSelect'), 'helper.js should define toggleSelect');
  112. assert(helperContent.includes('send'), 'helper.js should define send function');
  113. assert(helperContent.includes('selectedChoice'), 'helper.js should track selectedChoice');
  114. console.log(' PASS');
  115. // Test 8: sendToClaude confirmation uses CSS variables (dark mode support)
  116. console.log('Test 8: sendToClaude confirmation respects theming');
  117. assert(!helperContent.includes('color: #666'), 'Should not use hardcoded light-mode colors');
  118. assert(!helperContent.includes('color: #333'), 'Should not use hardcoded light-mode colors');
  119. console.log(' PASS');
  120. console.log('\nAll tests passed!');
  121. } finally {
  122. server.kill();
  123. cleanup();
  124. }
  125. }
  126. runTests().catch(err => {
  127. console.error('Test failed:', err);
  128. process.exit(1);
  129. });