server.test.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. return new Promise((resolve, reject) => {
  34. const headers = { Cookie: `brainstorm-key-${TEST_PORT}=${TOKEN}` };
  35. http.get(url, { headers }, (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. class SkipTest extends Error {
  67. constructor(message) {
  68. super(message);
  69. this.skip = true;
  70. }
  71. }
  72. function skip(message) {
  73. throw new SkipTest(message);
  74. }
  75. function serverStartedMessage(out) {
  76. const line = out.trim().split('\n').find(l => l.includes('server-started'));
  77. assert(line, 'server-started JSON should be present');
  78. return JSON.parse(line);
  79. }
  80. function assertStartedOnExpectedPort(out) {
  81. const msg = serverStartedMessage(out);
  82. assert.strictEqual(
  83. msg.port,
  84. TEST_PORT,
  85. `server.test.js expected fixed port ${TEST_PORT}, got ${msg.port}; fixed-port tests must not run through fallback`
  86. );
  87. return msg;
  88. }
  89. function ensureSymlinkWorks(target, link) {
  90. try {
  91. fs.symlinkSync(target, link);
  92. fs.unlinkSync(link);
  93. } catch (e) {
  94. try { fs.unlinkSync(link); } catch (ignore) {}
  95. skip(`symlink creation unavailable on this host: ${e.message}`);
  96. }
  97. }
  98. async function runTests() {
  99. cleanup();
  100. const server = startServer();
  101. let stdoutAccum = '';
  102. server.stdout.on('data', (data) => { stdoutAccum += data.toString(); });
  103. const { stdout: initialStdout } = await waitForServer(server);
  104. assertStartedOnExpectedPort(initialStdout);
  105. let passed = 0;
  106. let failed = 0;
  107. let skipped = 0;
  108. function test(name, fn) {
  109. return fn().then(() => {
  110. console.log(` PASS: ${name}`);
  111. passed++;
  112. }).catch(e => {
  113. if (e.skip) {
  114. console.log(` SKIP: ${name}`);
  115. console.log(` ${e.message}`);
  116. skipped++;
  117. return;
  118. }
  119. console.log(` FAIL: ${name}`);
  120. console.log(` ${e.message}`);
  121. failed++;
  122. });
  123. }
  124. try {
  125. // ========== Server Startup ==========
  126. console.log('\n--- Server Startup ---');
  127. await test('outputs server-started JSON on startup', () => {
  128. const msg = serverStartedMessage(initialStdout);
  129. assert.strictEqual(msg.type, 'server-started');
  130. assert.strictEqual(msg.port, TEST_PORT);
  131. assert(msg.url, 'Should include URL');
  132. assert(msg.screen_dir, 'Should include screen_dir');
  133. return Promise.resolve();
  134. });
  135. await test('writes server-info to state/', () => {
  136. const infoPath = path.join(STATE_DIR, 'server-info');
  137. assert(fs.existsSync(infoPath), 'state/server-info should exist');
  138. const info = JSON.parse(fs.readFileSync(infoPath, 'utf-8').trim());
  139. assert.strictEqual(info.type, 'server-started');
  140. assert.strictEqual(info.port, TEST_PORT);
  141. assert.strictEqual(info.screen_dir, CONTENT_DIR, 'screen_dir should point to content/');
  142. assert.strictEqual(info.state_dir, STATE_DIR, 'state_dir should point to state/');
  143. return Promise.resolve();
  144. });
  145. // ========== HTTP Serving ==========
  146. console.log('\n--- HTTP Serving ---');
  147. await test('serves waiting page when no screens exist', async () => {
  148. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  149. assert.strictEqual(res.status, 200);
  150. assert(res.body.includes('Waiting for the agent'), 'Should show waiting message');
  151. });
  152. await test('injects helper.js into waiting page', async () => {
  153. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  154. assert(res.body.includes('WebSocket'), 'Should have helper.js injected');
  155. assert(res.body.includes('toggleSelect'), 'Should have toggleSelect from helper');
  156. assert(res.body.includes('brainstorm'), 'Should have brainstorm API from helper');
  157. });
  158. await test('returns Content-Type text/html', async () => {
  159. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  160. assert(res.headers['content-type'].includes('text/html'), 'Should be text/html');
  161. });
  162. await test('serves full HTML documents as-is (not wrapped)', async () => {
  163. const fullDoc = '<!DOCTYPE html>\n<html><head><title>Custom</title></head><body><h1>Custom Page</h1></body></html>';
  164. fs.writeFileSync(path.join(CONTENT_DIR, 'full-doc.html'), fullDoc);
  165. await sleep(300);
  166. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  167. assert(res.body.includes('<h1>Custom Page</h1>'), 'Should contain original content');
  168. assert(res.body.includes('WebSocket'), 'Should still inject helper.js');
  169. assert(!res.body.includes('indicator-bar'), 'Should NOT wrap in frame template');
  170. });
  171. await test('wraps content fragments in frame template', async () => {
  172. const fragment = '<h2>Pick a layout</h2>\n<div class="options"><div class="option" data-choice="a"><div class="letter">A</div></div></div>';
  173. fs.writeFileSync(path.join(CONTENT_DIR, 'fragment.html'), fragment);
  174. await sleep(300);
  175. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  176. assert(res.body.includes('indicator-bar'), 'Fragment should get indicator bar');
  177. assert(!res.body.includes('<!-- CONTENT -->'), 'Placeholder should be replaced');
  178. assert(res.body.includes('Pick a layout'), 'Fragment content should be present');
  179. assert(res.body.includes('data-choice="a"'), 'Fragment interactive elements intact');
  180. });
  181. await test('serves newest file by mtime', async () => {
  182. fs.writeFileSync(path.join(CONTENT_DIR, 'older.html'), '<h2>Older</h2>');
  183. await sleep(100);
  184. fs.writeFileSync(path.join(CONTENT_DIR, 'newer.html'), '<h2>Newer</h2>');
  185. await sleep(300);
  186. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  187. assert(res.body.includes('Newer'), 'Should serve newest file');
  188. });
  189. await test('ignores non-html files for serving', async () => {
  190. // Write a newer non-HTML file — should still serve newest .html
  191. fs.writeFileSync(path.join(CONTENT_DIR, 'data.json'), '{"not": "html"}');
  192. await sleep(300);
  193. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  194. assert(res.body.includes('Newer'), 'Should still serve newest HTML');
  195. assert(!res.body.includes('"not"'), 'Should not serve JSON');
  196. });
  197. await test('ignores macOS resource-fork dotfiles (._*.html) when serving', async () => {
  198. // On macOS/ExFAT/SMB, the OS writes ._name.html sidecar files holding
  199. // binary metadata. They end with .html but must never be served as a screen.
  200. fs.writeFileSync(path.join(CONTENT_DIR, 'real-screen.html'), '<h2>Real Screen Content</h2>');
  201. await sleep(100);
  202. fs.writeFileSync(path.join(CONTENT_DIR, '._real-screen.html'), 'Mac OS X resource fork garbage');
  203. await sleep(300);
  204. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  205. assert(res.body.includes('Real Screen Content'), 'should serve the real screen, not the newer ._ sidecar');
  206. assert(!res.body.includes('resource fork garbage'), 'must not serve ._*.html dotfile content');
  207. });
  208. await test('does not serve dotfiles via /files/', async () => {
  209. fs.writeFileSync(path.join(CONTENT_DIR, '._secret.html'), 'dotfile body should not be served');
  210. const res = await fetch(`http://localhost:${TEST_PORT}/files/._secret.html`);
  211. assert.strictEqual(res.status, 404, '/files/ must 404 on dotfiles');
  212. });
  213. await test('GET /files/ (empty name) returns 404 and does not crash the server', async () => {
  214. const res = await fetch(`http://localhost:${TEST_PORT}/files/`);
  215. assert.strictEqual(res.status, 404, '/files/ (the content dir) must 404, not EISDIR-crash');
  216. // The server must still be alive afterward.
  217. const alive = await fetch(`http://localhost:${TEST_PORT}/`);
  218. assert.strictEqual(alive.status, 200, 'server must survive a /files/ request');
  219. });
  220. await test('does not serve symlinks that escape content dir via /files/', async () => {
  221. const target = path.join(STATE_DIR, 'server-info');
  222. const link = path.join(CONTENT_DIR, 'linked-server-info.txt');
  223. try { fs.unlinkSync(link); } catch (e) {}
  224. ensureSymlinkWorks(target, link);
  225. fs.symlinkSync(target, link);
  226. const res = await fetch(`http://localhost:${TEST_PORT}/files/linked-server-info.txt`);
  227. assert.strictEqual(res.status, 404, 'symlink to state/server-info must not be served');
  228. assert(!res.body.includes('server-started'), 'response must not include server-info body');
  229. });
  230. await test('does not serve hard links to files outside content dir via /files/', async () => {
  231. const target = path.join(STATE_DIR, 'server-info');
  232. const link = path.join(CONTENT_DIR, 'hard-linked-server-info.txt');
  233. try { fs.unlinkSync(link); } catch (e) {}
  234. fs.linkSync(target, link);
  235. const res = await fetch(`http://localhost:${TEST_PORT}/files/hard-linked-server-info.txt`);
  236. assert.strictEqual(res.status, 404, 'hard link to state/server-info must not be served');
  237. assert(!res.body.includes('server-started'), 'response must not include server-info body');
  238. });
  239. await test('does not serve symlinks that escape content dir via root screen selection', async () => {
  240. const target = path.join(STATE_DIR, 'server-info');
  241. const link = path.join(CONTENT_DIR, 'root-linked-server-info.html');
  242. try { fs.unlinkSync(link); } catch (e) {}
  243. ensureSymlinkWorks(target, link);
  244. fs.symlinkSync(target, link);
  245. const future = new Date(Date.now() + 2000);
  246. fs.utimesSync(target, future, future);
  247. await sleep(300);
  248. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  249. assert.strictEqual(res.status, 200);
  250. assert(!res.body.includes('"type":"server-started"'), 'root screen must not serve state/server-info through a symlink');
  251. assert(!res.body.includes('"state_dir"'), 'root screen must not include server-info body');
  252. });
  253. await test('does not serve hard links that escape content dir via root screen selection', async () => {
  254. const target = path.join(STATE_DIR, 'server-info');
  255. const link = path.join(CONTENT_DIR, 'root-hard-linked-server-info.html');
  256. try { fs.unlinkSync(link); } catch (e) {}
  257. try {
  258. fs.linkSync(target, link);
  259. } catch (e) {
  260. skip(`hardlink creation unavailable on this host: ${e.message}`);
  261. }
  262. const linkStat = fs.lstatSync(link);
  263. if (linkStat.nlink <= 1) {
  264. skip(`hardlink nlink did not expose multiple links: ${linkStat.nlink}`);
  265. }
  266. const future = new Date(Date.now() + 3000);
  267. fs.utimesSync(target, future, future);
  268. await sleep(300);
  269. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  270. assert.strictEqual(res.status, 200);
  271. assert(!res.body.includes('"type":"server-started"'), 'root screen must not serve state/server-info through a hardlink');
  272. assert(!res.body.includes('"state_dir"'), 'root screen must not include server-info body');
  273. });
  274. await test('returns 404 for non-root paths', async () => {
  275. const res = await fetch(`http://localhost:${TEST_PORT}/other`);
  276. assert.strictEqual(res.status, 404);
  277. });
  278. // ========== WebSocket Communication ==========
  279. console.log('\n--- WebSocket Communication ---');
  280. await test('accepts WebSocket upgrade on /', async () => {
  281. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  282. await new Promise((resolve, reject) => {
  283. ws.on('open', resolve);
  284. ws.on('error', reject);
  285. });
  286. ws.close();
  287. });
  288. await test('relays user events to stdout with source field', async () => {
  289. stdoutAccum = '';
  290. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  291. await new Promise(resolve => ws.on('open', resolve));
  292. ws.send(JSON.stringify({ type: 'click', text: 'Test Button' }));
  293. await sleep(300);
  294. assert(stdoutAccum.includes('"source":"user-event"'), 'Should tag with source');
  295. assert(stdoutAccum.includes('Test Button'), 'Should include event data');
  296. ws.close();
  297. });
  298. await test('writes choice events to state/events', async () => {
  299. // Clean up events from prior tests
  300. const eventsFile = path.join(STATE_DIR, 'events');
  301. if (fs.existsSync(eventsFile)) fs.unlinkSync(eventsFile);
  302. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  303. await new Promise(resolve => ws.on('open', resolve));
  304. ws.send(JSON.stringify({ type: 'click', choice: 'b', text: 'Option B' }));
  305. await sleep(300);
  306. assert(fs.existsSync(eventsFile), '.events should exist');
  307. const lines = fs.readFileSync(eventsFile, 'utf-8').trim().split('\n');
  308. const event = JSON.parse(lines[lines.length - 1]);
  309. assert.strictEqual(event.choice, 'b');
  310. assert.strictEqual(event.text, 'Option B');
  311. ws.close();
  312. });
  313. await test('does NOT write non-choice events to state/events', async () => {
  314. const eventsFile = path.join(STATE_DIR, 'events');
  315. if (fs.existsSync(eventsFile)) fs.unlinkSync(eventsFile);
  316. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  317. await new Promise(resolve => ws.on('open', resolve));
  318. ws.send(JSON.stringify({ type: 'hover', text: 'Something' }));
  319. await sleep(300);
  320. // Non-choice events should not create .events file
  321. assert(!fs.existsSync(eventsFile), '.events should not exist for non-choice events');
  322. ws.close();
  323. });
  324. await test('handles multiple concurrent WebSocket clients', async () => {
  325. const ws1 = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  326. const ws2 = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  327. await Promise.all([
  328. new Promise(resolve => ws1.on('open', resolve)),
  329. new Promise(resolve => ws2.on('open', resolve))
  330. ]);
  331. let ws1Reload = false;
  332. let ws2Reload = false;
  333. ws1.on('message', (data) => {
  334. if (JSON.parse(data.toString()).type === 'reload') ws1Reload = true;
  335. });
  336. ws2.on('message', (data) => {
  337. if (JSON.parse(data.toString()).type === 'reload') ws2Reload = true;
  338. });
  339. fs.writeFileSync(path.join(CONTENT_DIR, 'multi-client.html'), '<h2>Multi</h2>');
  340. await sleep(500);
  341. assert(ws1Reload, 'Client 1 should receive reload');
  342. assert(ws2Reload, 'Client 2 should receive reload');
  343. ws1.close();
  344. ws2.close();
  345. });
  346. await test('cleans up closed clients from broadcast list', async () => {
  347. const ws1 = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  348. await new Promise(resolve => ws1.on('open', resolve));
  349. ws1.close();
  350. await sleep(100);
  351. // This should not throw even though ws1 is closed
  352. fs.writeFileSync(path.join(CONTENT_DIR, 'after-close.html'), '<h2>After</h2>');
  353. await sleep(300);
  354. // If we got here without error, the test passes
  355. });
  356. await test('handles malformed JSON from client gracefully', async () => {
  357. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  358. await new Promise(resolve => ws.on('open', resolve));
  359. // Send invalid JSON — server should not crash
  360. ws.send('not json at all {{{');
  361. await sleep(300);
  362. // Verify server is still responsive
  363. const res = await fetch(`http://localhost:${TEST_PORT}/`);
  364. assert.strictEqual(res.status, 200, 'Server should still be running');
  365. ws.close();
  366. });
  367. // ========== File Watching ==========
  368. console.log('\n--- File Watching ---');
  369. await test('sends reload on new .html file', async () => {
  370. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  371. await new Promise(resolve => ws.on('open', resolve));
  372. let gotReload = false;
  373. ws.on('message', (data) => {
  374. if (JSON.parse(data.toString()).type === 'reload') gotReload = true;
  375. });
  376. fs.writeFileSync(path.join(CONTENT_DIR, 'watch-new.html'), '<h2>New</h2>');
  377. await sleep(500);
  378. assert(gotReload, 'Should send reload on new file');
  379. ws.close();
  380. });
  381. await test('sends reload on .html file change', async () => {
  382. const filePath = path.join(CONTENT_DIR, 'watch-change.html');
  383. fs.writeFileSync(filePath, '<h2>Original</h2>');
  384. await sleep(500);
  385. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  386. await new Promise(resolve => ws.on('open', resolve));
  387. let gotReload = false;
  388. ws.on('message', (data) => {
  389. if (JSON.parse(data.toString()).type === 'reload') gotReload = true;
  390. });
  391. fs.writeFileSync(filePath, '<h2>Modified</h2>');
  392. await sleep(500);
  393. assert(gotReload, 'Should send reload on file change');
  394. ws.close();
  395. });
  396. await test('does NOT send reload for non-.html files', async () => {
  397. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  398. await new Promise(resolve => ws.on('open', resolve));
  399. let gotReload = false;
  400. ws.on('message', (data) => {
  401. if (JSON.parse(data.toString()).type === 'reload') gotReload = true;
  402. });
  403. fs.writeFileSync(path.join(CONTENT_DIR, 'data.txt'), 'not html');
  404. await sleep(500);
  405. assert(!gotReload, 'Should NOT reload for non-HTML files');
  406. ws.close();
  407. });
  408. await test('does NOT send reload for ._*.html resource-fork dotfiles', async () => {
  409. const ws = new WebSocket(`ws://localhost:${TEST_PORT}/?key=${TOKEN}`);
  410. await new Promise(resolve => ws.on('open', resolve));
  411. let gotReload = false;
  412. ws.on('message', (data) => {
  413. if (JSON.parse(data.toString()).type === 'reload') gotReload = true;
  414. });
  415. fs.writeFileSync(path.join(CONTENT_DIR, '._sidecar.html'), 'resource fork');
  416. await sleep(500);
  417. assert(!gotReload, 'a ._ dotfile appearing must not trigger a reload');
  418. ws.close();
  419. });
  420. await test('clears state/events on new screen', async () => {
  421. // Create an events file
  422. const eventsFile = path.join(STATE_DIR, 'events');
  423. fs.writeFileSync(eventsFile, '{"choice":"a"}\n');
  424. assert(fs.existsSync(eventsFile));
  425. fs.writeFileSync(path.join(CONTENT_DIR, 'clear-events.html'), '<h2>New screen</h2>');
  426. await sleep(500);
  427. assert(!fs.existsSync(eventsFile), 'state/events should be cleared on new screen');
  428. });
  429. await test('logs screen-added on new file', async () => {
  430. stdoutAccum = '';
  431. fs.writeFileSync(path.join(CONTENT_DIR, 'log-test.html'), '<h2>Log</h2>');
  432. await sleep(500);
  433. assert(stdoutAccum.includes('screen-added'), 'Should log screen-added');
  434. });
  435. await test('logs screen-updated on file change', async () => {
  436. const filePath = path.join(CONTENT_DIR, 'log-update.html');
  437. fs.writeFileSync(filePath, '<h2>V1</h2>');
  438. await sleep(500);
  439. stdoutAccum = '';
  440. fs.writeFileSync(filePath, '<h2>V2</h2>');
  441. await sleep(500);
  442. assert(stdoutAccum.includes('screen-updated'), 'Should log screen-updated');
  443. });
  444. // ========== Helper.js Content ==========
  445. console.log('\n--- Helper.js Verification ---');
  446. await test('helper.js defines required APIs', () => {
  447. const helperContent = fs.readFileSync(
  448. path.join(__dirname, '../../skills/brainstorming/scripts/helper.js'), 'utf-8'
  449. );
  450. assert(helperContent.includes('toggleSelect'), 'Should define toggleSelect');
  451. assert(helperContent.includes('sendEvent'), 'Should define sendEvent');
  452. assert(helperContent.includes('selectedChoice'), 'Should track selectedChoice');
  453. assert(helperContent.includes('brainstorm'), 'Should expose brainstorm API');
  454. return Promise.resolve();
  455. });
  456. // ========== Frame Template ==========
  457. console.log('\n--- Frame Template Verification ---');
  458. await test('frame template has required structure', () => {
  459. const template = fs.readFileSync(
  460. path.join(__dirname, '../../skills/brainstorming/scripts/frame-template.html'), 'utf-8'
  461. );
  462. assert(template.includes('indicator-bar'), 'Should have indicator bar');
  463. assert(template.includes('indicator-text'), 'Should have indicator text');
  464. assert(template.includes('<!-- CONTENT -->'), 'Should have content placeholder');
  465. assert(template.includes('frame-content'), 'Should have content container');
  466. return Promise.resolve();
  467. });
  468. // ========== Summary ==========
  469. console.log(`\n--- Results: ${passed} passed, ${failed} failed, ${skipped} skipped ---`);
  470. if (failed > 0) process.exit(1);
  471. } finally {
  472. server.kill();
  473. await sleep(100);
  474. cleanup();
  475. }
  476. }
  477. runTests().catch(err => {
  478. console.error('Test failed:', err);
  479. process.exit(1);
  480. });