server.test.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /**
  2. * Integration tests for the brainstorm server.
  3. *
  4. * Tests the full server behavior: HTTP serving, WebSocket communication,
  5. * file watching, and the brainstorming workflow.
  6. *
  7. * Uses the `ws` npm package as a test client (test-only dependency,
  8. * not shipped to end users).
  9. */
  10. const { spawn } = require('child_process');
  11. const http = require('http');
  12. const WebSocket = require('ws');
  13. const fs = require('fs');
  14. const path = require('path');
  15. const assert = require('assert');
  16. const SERVER_PATH = path.join(__dirname, '../../skills/brainstorming/scripts/server.cjs');
  17. const TEST_PORT = 3334;
  18. const TEST_DIR = '/tmp/brainstorm-test';
  19. const CONTENT_DIR = path.join(TEST_DIR, 'content');
  20. const STATE_DIR = path.join(TEST_DIR, 'state');
  21. // Fixed session key so the test client can authenticate (see auth.test.js for
  22. // the security behavior itself; here we just need authorized requests).
  23. const TOKEN = 'testtoken-server-0123456789abcdef';
  24. function cleanup() {
  25. if (fs.existsSync(TEST_DIR)) {
  26. fs.rmSync(TEST_DIR, { recursive: true });
  27. }
  28. }
  29. async function sleep(ms) {
  30. return new Promise(resolve => setTimeout(resolve, ms));
  31. }
  32. async function fetch(url) {
  33. const authed = url + (url.includes('?') ? '&' : '?') + 'key=' + TOKEN;
  34. return new Promise((resolve, reject) => {
  35. http.get(authed, (res) => {
  36. let data = '';
  37. res.on('data', chunk => data += chunk);
  38. res.on('end', () => resolve({
  39. status: res.statusCode,
  40. headers: res.headers,
  41. body: data
  42. }));
  43. }).on('error', reject);
  44. });
  45. }
  46. function startServer() {
  47. return spawn('node', [SERVER_PATH], {
  48. env: { ...process.env, BRAINSTORM_PORT: TEST_PORT, BRAINSTORM_DIR: TEST_DIR, BRAINSTORM_TOKEN: TOKEN }
  49. });
  50. }
  51. async function waitForServer(server) {
  52. let stdout = '';
  53. let stderr = '';
  54. return new Promise((resolve, reject) => {
  55. server.stdout.on('data', (data) => {
  56. stdout += data.toString();
  57. if (stdout.includes('server-started')) {
  58. resolve({ stdout, stderr, getStdout: () => stdout });
  59. }
  60. });
  61. server.stderr.on('data', (data) => { stderr += data.toString(); });
  62. server.on('error', reject);
  63. setTimeout(() => reject(new Error(`Server didn't start. stderr: ${stderr}`)), 5000);
  64. });
  65. }
  66. async function runTests() {
  67. cleanup();
  68. const server = startServer();
  69. let stdoutAccum = '';
  70. server.stdout.on('data', (data) => { stdoutAccum += data.toString(); });
  71. const { stdout: initialStdout } = await waitForServer(server);
  72. let passed = 0;
  73. let failed = 0;
  74. function test(name, fn) {
  75. return fn().then(() => {
  76. console.log(` PASS: ${name}`);
  77. passed++;
  78. }).catch(e => {
  79. console.log(` FAIL: ${name}`);
  80. console.log(` ${e.message}`);
  81. failed++;
  82. });
  83. }
  84. try {
  85. // ========== Server Startup ==========
  86. console.log('\n--- Server Startup ---');
  87. await test('outputs server-started JSON on startup', () => {
  88. const msg = JSON.parse(initialStdout.trim());
  89. assert.strictEqual(msg.type, 'server-started');
  90. assert.strictEqual(msg.port, TEST_PORT);
  91. assert(msg.url, 'Should include URL');
  92. assert(msg.screen_dir, 'Should include screen_dir');
  93. return Promise.resolve();
  94. });
  95. await test('writes server-info to state/', () => {
  96. const infoPath = path.join(STATE_DIR, 'server-info');
  97. assert(fs.existsSync(infoPath), 'state/server-info should exist');
  98. const info = JSON.parse(fs.readFileSync(infoPath, 'utf-8').trim());
  99. assert.strictEqual(info.type, 'server-started');
  100. assert.strictEqual(info.port, TEST_PORT);
  101. assert.strictEqual(info.screen_dir, CONTENT_DIR, 'screen_dir should point to content/');
  102. assert.strictEqual(info.state_dir, STATE_DIR, 'state_dir should point to state/');
  103. return Promise.resolve();
  104. });
  105. // ========== HTTP Serving ==========
  106. console.log('\n--- HTTP Serving ---');
  107. await test('serves waiting page when no screens exist', async () => {
  108. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  109. assert.strictEqual(res.status, 200);
  110. assert(res.body.includes('Waiting for the agent'), 'Should show waiting message');
  111. });
  112. await test('injects helper.js into waiting page', async () => {
  113. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  114. assert(res.body.includes('WebSocket'), 'Should have helper.js injected');
  115. assert(res.body.includes('toggleSelect'), 'Should have toggleSelect from helper');
  116. assert(res.body.includes('brainstorm'), 'Should have brainstorm API from helper');
  117. });
  118. await test('returns Content-Type text/html', async () => {
  119. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  120. assert(res.headers['content-type'].includes('text/html'), 'Should be text/html');
  121. });
  122. await test('serves full HTML documents as-is (not wrapped)', async () => {
  123. const fullDoc = '<!DOCTYPE html>\n<html><head><title>Custom</title></head><body><h1>Custom Page</h1></body></html>';
  124. fs.writeFileSync(path.join(CONTENT_DIR, 'full-doc.html'), fullDoc);
  125. await sleep(300);
  126. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  127. assert(res.body.includes('<h1>Custom Page</h1>'), 'Should contain original content');
  128. assert(res.body.includes('WebSocket'), 'Should still inject helper.js');
  129. assert(!res.body.includes('indicator-bar'), 'Should NOT wrap in frame template');
  130. });
  131. await test('wraps content fragments in frame template', async () => {
  132. const fragment = '<h2>Pick a layout</h2>\n<div class="options"><div class="option" data-choice="a"><div class="letter">A</div></div></div>';
  133. fs.writeFileSync(path.join(CONTENT_DIR, 'fragment.html'), fragment);
  134. await sleep(300);
  135. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  136. assert(res.body.includes('indicator-bar'), 'Fragment should get indicator bar');
  137. assert(!res.body.includes('<!-- CONTENT -->'), 'Placeholder should be replaced');
  138. assert(res.body.includes('Pick a layout'), 'Fragment content should be present');
  139. assert(res.body.includes('data-choice="a"'), 'Fragment interactive elements intact');
  140. });
  141. await test('serves newest file by mtime', async () => {
  142. fs.writeFileSync(path.join(CONTENT_DIR, 'older.html'), '<h2>Older</h2>');
  143. await sleep(100);
  144. fs.writeFileSync(path.join(CONTENT_DIR, 'newer.html'), '<h2>Newer</h2>');
  145. await sleep(300);
  146. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  147. assert(res.body.includes('Newer'), 'Should serve newest file');
  148. });
  149. await test('ignores non-html files for serving', async () => {
  150. // Write a newer non-HTML file — should still serve newest .html
  151. fs.writeFileSync(path.join(CONTENT_DIR, 'data.json'), '{"not": "html"}');
  152. await sleep(300);
  153. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  154. assert(res.body.includes('Newer'), 'Should still serve newest HTML');
  155. assert(!res.body.includes('"not"'), 'Should not serve JSON');
  156. });
  157. await test('ignores macOS resource-fork dotfiles (._*.html) when serving', async () => {
  158. // On macOS/ExFAT/SMB, the OS writes ._name.html sidecar files holding
  159. // binary metadata. They end with .html but must never be served as a screen.
  160. fs.writeFileSync(path.join(CONTENT_DIR, 'real-screen.html'), '<h2>Real Screen Content</h2>');
  161. await sleep(100);
  162. fs.writeFileSync(path.join(CONTENT_DIR, '._real-screen.html'), 'Mac OS X resource fork garbage');
  163. await sleep(300);
  164. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  165. assert(res.body.includes('Real Screen Content'), 'should serve the real screen, not the newer ._ sidecar');
  166. assert(!res.body.includes('resource fork garbage'), 'must not serve ._*.html dotfile content');
  167. });
  168. await test('does not serve dotfiles via /files/', async () => {
  169. fs.writeFileSync(path.join(CONTENT_DIR, '._secret.html'), 'dotfile body should not be served');
  170. const res = await fetch(`http://localhost:${TEST_PORT}/files/._secret.html`);
  171. assert.strictEqual(res.status, 404, '/files/ must 404 on dotfiles');
  172. });
  173. await test('GET /files/ (empty name) returns 404 and does not crash the server', async () => {
  174. const res = await fetch(`http://localhost:${TEST_PORT}/files/`);
  175. assert.strictEqual(res.status, 404, '/files/ (the content dir) must 404, not EISDIR-crash');
  176. // The server must still be alive afterward.
  177. const alive = await fetch(`http://localhost:${TEST_PORT}/`);
  178. assert.strictEqual(alive.status, 200, 'server must survive a /files/ request');
  179. });
  180. await test('returns 404 for non-root paths', async () => {
  181. const res = await fetch(`http://localhost:${TEST_PORT}/other`);
  182. assert.strictEqual(res.status, 404);
  183. });
  184. // ========== WebSocket Communication ==========
  185. console.log('\n--- WebSocket Communication ---');
  186. await test('accepts WebSocket upgrade on /', async () => {
  187. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  188. await new Promise((resolve, reject) => {
  189. ws.on('open', resolve);
  190. ws.on('error', reject);
  191. });
  192. ws.close();
  193. });
  194. await test('relays user events to stdout with source field', async () => {
  195. stdoutAccum = '';
  196. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  197. await new Promise(resolve => ws.on('open', resolve));
  198. ws.send(JSON.stringify({ type: 'click', text: 'Test Button' }));
  199. await sleep(300);
  200. assert(stdoutAccum.includes('"source":"user-event"'), 'Should tag with source');
  201. assert(stdoutAccum.includes('Test Button'), 'Should include event data');
  202. ws.close();
  203. });
  204. await test('writes choice events to state/events', async () => {
  205. // Clean up events from prior tests
  206. const eventsFile = path.join(STATE_DIR, 'events');
  207. if (fs.existsSync(eventsFile)) fs.unlinkSync(eventsFile);
  208. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  209. await new Promise(resolve => ws.on('open', resolve));
  210. ws.send(JSON.stringify({ type: 'click', choice: 'b', text: 'Option B' }));
  211. await sleep(300);
  212. assert(fs.existsSync(eventsFile), '.events should exist');
  213. const lines = fs.readFileSync(eventsFile, 'utf-8').trim().split('\n');
  214. const event = JSON.parse(lines[lines.length - 1]);
  215. assert.strictEqual(event.choice, 'b');
  216. assert.strictEqual(event.text, 'Option B');
  217. ws.close();
  218. });
  219. await test('does NOT write non-choice events to state/events', async () => {
  220. const eventsFile = path.join(STATE_DIR, 'events');
  221. if (fs.existsSync(eventsFile)) fs.unlinkSync(eventsFile);
  222. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  223. await new Promise(resolve => ws.on('open', resolve));
  224. ws.send(JSON.stringify({ type: 'hover', text: 'Something' }));
  225. await sleep(300);
  226. // Non-choice events should not create .events file
  227. assert(!fs.existsSync(eventsFile), '.events should not exist for non-choice events');
  228. ws.close();
  229. });
  230. await test('handles multiple concurrent WebSocket clients', async () => {
  231. const ws1 = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  232. const ws2 = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  233. await Promise.all([
  234. new Promise(resolve => ws1.on('open', resolve)),
  235. new Promise(resolve => ws2.on('open', resolve))
  236. ]);
  237. let ws1Reload = false;
  238. let ws2Reload = false;
  239. ws1.on('message', (data) => {
  240. if (JSON.parse(data.toString()).type === 'reload') ws1Reload = true;
  241. });
  242. ws2.on('message', (data) => {
  243. if (JSON.parse(data.toString()).type === 'reload') ws2Reload = true;
  244. });
  245. fs.writeFileSync(path.join(CONTENT_DIR, 'multi-client.html'), '<h2>Multi</h2>');
  246. await sleep(500);
  247. assert(ws1Reload, 'Client 1 should receive reload');
  248. assert(ws2Reload, 'Client 2 should receive reload');
  249. ws1.close();
  250. ws2.close();
  251. });
  252. await test('cleans up closed clients from broadcast list', async () => {
  253. const ws1 = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  254. await new Promise(resolve => ws1.on('open', resolve));
  255. ws1.close();
  256. await sleep(100);
  257. // This should not throw even though ws1 is closed
  258. fs.writeFileSync(path.join(CONTENT_DIR, 'after-close.html'), '<h2>After</h2>');
  259. await sleep(300);
  260. // If we got here without error, the test passes
  261. });
  262. await test('handles malformed JSON from client gracefully', async () => {
  263. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  264. await new Promise(resolve => ws.on('open', resolve));
  265. // Send invalid JSON — server should not crash
  266. ws.send('not json at all {{{');
  267. await sleep(300);
  268. // Verify server is still responsive
  269. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  270. assert.strictEqual(res.status, 200, 'Server should still be running');
  271. ws.close();
  272. });
  273. // ========== File Watching ==========
  274. console.log('\n--- File Watching ---');
  275. await test('sends reload on new .html file', async () => {
  276. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  277. await new Promise(resolve => ws.on('open', resolve));
  278. let gotReload = false;
  279. ws.on('message', (data) => {
  280. if (JSON.parse(data.toString()).type === 'reload') gotReload = true;
  281. });
  282. fs.writeFileSync(path.join(CONTENT_DIR, 'watch-new.html'), '<h2>New</h2>');
  283. await sleep(500);
  284. assert(gotReload, 'Should send reload on new file');
  285. ws.close();
  286. });
  287. await test('sends reload on .html file change', async () => {
  288. const filePath = path.join(CONTENT_DIR, 'watch-change.html');
  289. fs.writeFileSync(filePath, '<h2>Original</h2>');
  290. await sleep(500);
  291. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  292. await new Promise(resolve => ws.on('open', resolve));
  293. let gotReload = false;
  294. ws.on('message', (data) => {
  295. if (JSON.parse(data.toString()).type === 'reload') gotReload = true;
  296. });
  297. fs.writeFileSync(filePath, '<h2>Modified</h2>');
  298. await sleep(500);
  299. assert(gotReload, 'Should send reload on file change');
  300. ws.close();
  301. });
  302. await test('does NOT send reload for non-.html files', async () => {
  303. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  304. await new Promise(resolve => ws.on('open', resolve));
  305. let gotReload = false;
  306. ws.on('message', (data) => {
  307. if (JSON.parse(data.toString()).type === 'reload') gotReload = true;
  308. });
  309. fs.writeFileSync(path.join(CONTENT_DIR, 'data.txt'), 'not html');
  310. await sleep(500);
  311. assert(!gotReload, 'Should NOT reload for non-HTML files');
  312. ws.close();
  313. });
  314. await test('does NOT send reload for ._*.html resource-fork dotfiles', async () => {
  315. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  316. await new Promise(resolve => ws.on('open', resolve));
  317. let gotReload = false;
  318. ws.on('message', (data) => {
  319. if (JSON.parse(data.toString()).type === 'reload') gotReload = true;
  320. });
  321. fs.writeFileSync(path.join(CONTENT_DIR, '._sidecar.html'), 'resource fork');
  322. await sleep(500);
  323. assert(!gotReload, 'a ._ dotfile appearing must not trigger a reload');
  324. ws.close();
  325. });
  326. await test('clears state/events on new screen', async () => {
  327. // Create an events file
  328. const eventsFile = path.join(STATE_DIR, 'events');
  329. fs.writeFileSync(eventsFile, '{"choice":"a"}\n');
  330. assert(fs.existsSync(eventsFile));
  331. fs.writeFileSync(path.join(CONTENT_DIR, 'clear-events.html'), '<h2>New screen</h2>');
  332. await sleep(500);
  333. assert(!fs.existsSync(eventsFile), 'state/events should be cleared on new screen');
  334. });
  335. await test('logs screen-added on new file', async () => {
  336. stdoutAccum = '';
  337. fs.writeFileSync(path.join(CONTENT_DIR, 'log-test.html'), '<h2>Log</h2>');
  338. await sleep(500);
  339. assert(stdoutAccum.includes('screen-added'), 'Should log screen-added');
  340. });
  341. await test('logs screen-updated on file change', async () => {
  342. const filePath = path.join(CONTENT_DIR, 'log-update.html');
  343. fs.writeFileSync(filePath, '<h2>V1</h2>');
  344. await sleep(500);
  345. stdoutAccum = '';
  346. fs.writeFileSync(filePath, '<h2>V2</h2>');
  347. await sleep(500);
  348. assert(stdoutAccum.includes('screen-updated'), 'Should log screen-updated');
  349. });
  350. // ========== Helper.js Content ==========
  351. console.log('\n--- Helper.js Verification ---');
  352. await test('helper.js defines required APIs', () => {
  353. const helperContent = fs.readFileSync(
  354. path.join(__dirname, '../../skills/brainstorming/scripts/helper.js'), 'utf-8'
  355. );
  356. assert(helperContent.includes('toggleSelect'), 'Should define toggleSelect');
  357. assert(helperContent.includes('sendEvent'), 'Should define sendEvent');
  358. assert(helperContent.includes('selectedChoice'), 'Should track selectedChoice');
  359. assert(helperContent.includes('brainstorm'), 'Should expose brainstorm API');
  360. return Promise.resolve();
  361. });
  362. // ========== Frame Template ==========
  363. console.log('\n--- Frame Template Verification ---');
  364. await test('frame template has required structure', () => {
  365. const template = fs.readFileSync(
  366. path.join(__dirname, '../../skills/brainstorming/scripts/frame-template.html'), 'utf-8'
  367. );
  368. assert(template.includes('indicator-bar'), 'Should have indicator bar');
  369. assert(template.includes('indicator-text'), 'Should have indicator text');
  370. assert(template.includes('<!-- CONTENT -->'), 'Should have content placeholder');
  371. assert(template.includes('frame-content'), 'Should have content container');
  372. return Promise.resolve();
  373. });
  374. // ========== Summary ==========
  375. console.log(`\n--- Results: ${passed} passed, ${failed} failed ---`);
  376. if (failed > 0) process.exit(1);
  377. } finally {
  378. server.kill();
  379. await sleep(100);
  380. cleanup();
  381. }
  382. }
  383. runTests().catch(err => {
  384. console.error('Test failed:', err);
  385. process.exit(1);
  386. });