← 教程列表
news

Task 2: Update `index.js` — content injection and `.events` file

📅 2026/6/12 16:30:02⏱ 约 5 分钟阅读👁 7 阅读

**Files:** - Modify: `lib/brainstorm-server/index.js` - [ ] **Step 1: Write failing test for `.events` file writing** Add to `tests/brainstorm-server/server.test.js` after Test 4 area — a new test that sends a WebSocket event with a `choice` field and verifies `.events` file is written: ```javascript // Test: Choice events written to .events file console.log('Test: Choice events written to .events file'); const ws3 = new WebSocket(`ws://localhost:${TEST_PORT}`); await new Promise(resolve => ws3.on('open', resolve)); ws3.send(JSON.stringify({ type: 'click', choice: 'a', text: 'Option A' })); await sleep(300); const eventsFile = path.join(TEST_DIR, '.events'); assert(fs.existsSync(eventsFile), '.events file should exist after choice click'); const lines = fs.readFileSync(eventsFile, 'utf-8').trim().split('\n'); const event = JSON.parse(lines[lines.length - 1]); assert.strictEqual(event.choice, 'a', 'Event should contain choice'); assert.strictEqual(event.text, 'Option A', 'Event should contain text'); ws3.close(); console.log(' PASS'); ``` - [ ] **Step 2: Run test to verify it fails** ```bash cd /Users/drewritter/prime-rad/superpowers && node tests/brainstorm-server/server.test.js ``` Expected: New test FAILS — `.events` file doesn't exist yet. - [ ] **Step 3: Write failing test for `.events` file clearing on new screen** Add another test: ```javascript // Test: .events cleared on new screen console.log('Test: .events cleared on new screen'); // .events file should still exist from previous test assert(fs.existsSync(path.join(TEST_DIR, '.events')), '.events should exist before new screen'); fs.writeFileSync(path.join(TEST_DIR, 'new-screen.html'), '

New screen

'); await sleep(500); assert(!fs.existsSync(path.join(TEST_DIR, '.events')), '.events should be cleared after new screen'); console.log(' PASS'); ``` - [ ] **Step 4: Run test to verify it fails** ```bash cd /Users/drewritter/prime-rad/superpowers && node tests/brainstorm-server/server.test.js ``` Expected: New test FAILS — `.events` not cleared on screen push. - [ ] **Step 5: Implement `.events` file writing in `index.js`** In the WebSocket `message` handler (line 74-77 of `index.js`), after the `console.log`, add: ```javascript // Write user events to .events file for Claude to read if (event.choice) { const eventsFile = path.join(SCREEN_DIR, '.events'); fs.appendFileSync(eventsFile, JSON.stringify(event) + '\n'); } ``` In the chokidar `add` handler (line 104-111), add `.events` clearing: ```javascript if (filePath.endsWith('.html')) { // Clear events from previous screen const eventsFile = path.join(SCREEN_DIR, '.events'); if (fs.existsSync(eventsFile)) fs.unlinkSync(eventsFile); console.log(JSON.stringify({ type: 'screen-added', file: filePath })); // ... existing reload broadcast } ``` - [ ] **Step 6: Replace `wrapInFrame` with comment placeholder injection** Replace the `wrapInFrame` function (lines 27-32 of `index.js`): ```javascript function wrapInFrame(content) { return frameTemplate.replace('', content); } ``` - [ ] **Step 7: Run all tests** ```bash cd /Users/drewritter/prime-rad/superpowers && node tests/brainstorm-server/server.test.js ``` Expected: New `.events` tests PASS. Existing tests may still have failures from old assertions (fixed in Task 4). - [ ] **Step 8: Commit** ```bash git add lib/brainstorm-server/index.js tests/brainstorm-server/server.test.js git commit -m "Add .events file writing and comment-based content injection to brainstorm server" ``` ---

← 返回教程列表 AI8899 · 内容仅供学习参考