server.test.js 23 KB

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