Understand the exact conditions that trigger the bug:
Read the bug report carefully
Try to reproduce it locally
Document the exact steps to reproduce
Note the expected vs. actual behavior
Example:
Steps to Reproduce:1. Create new episode2. Generate PRF document3. Click "Save" without making changes4. Application crashes with error: "Cannot read property 'content' of undefined"Expected: Should save without errorActual: Application crashes
2
Write a test that reproduces the bug
Before fixing anything, write a test that captures the bug:
// functions/api/episodes/save.test.tsimport { describe, it, expect, vi } from 'vitest'import { onRequestPost } from './save'describe('POST /api/episodes/save', () => { it('should handle empty PRF content without crashing', async () => { const request = new Request('http://localhost/api/episodes/save', { method: 'POST', body: JSON.stringify({ episodeId: '123', prf: { content: null } // Bug: null content causes crash }) }) // This test should capture the bug const response = await onRequestPost({ request, env: mockEnv }) // Should not crash, should return 400 or handle gracefully expect(response.status).toBe(400) })})
3
Verify the test fails
Run the test to confirm it fails:
npm run test:run -- save.test.ts
Expected output:
❌ FAIL functions/api/episodes/save.test.ts POST /api/episodes/save ✕ should handle empty PRF content without crashing (15ms)TypeError: Cannot read property 'content' of undefined at onRequestPost (save.ts:23:15)
If the test passes immediately, you haven’t captured the bug correctly. Revise the test.
4
Fix the bug
Now implement the fix:
// functions/api/episodes/save.tsexport const onRequestPost: PagesFunction<Env> = async ({ request, env }) => { const body = await request.json() // ✅ Fix: Validate PRF content before accessing if (!body.prf || body.prf.content === null) { return new Response( JSON.stringify({ error: 'PRF content is required' }), { status: 400 } ) } // Safe to access body.prf.content now const content = body.prf.content // ... rest of save logic}
5
Verify the test passes
Run the test again:
npm run test:run -- save.test.ts
Expected output:
✅ PASS functions/api/episodes/save.test.ts POST /api/episodes/save ✓ should handle empty PRF content without crashing (8ms)
Title: Editor crashes when selecting text in empty documentSteps to reproduce:1. Create new episode2. Open PRF editor (empty)3. Click and drag to select text4. Editor crashes with: "Cannot read property 'text' of null"Expected: Should handle empty selection gracefullyActual: Editor crashes
// src/components/editor/TipTapEditor.tsximport { useEditor, EditorContent } from '@tiptap/react'export function TipTapEditor({ initialContent }: Props) { const editor = useEditor({ extensions: [StarterKit], content: initialContent, onSelectionUpdate: ({ editor }) => { const { from, to } = editor.state.selection // ✅ Fix: Check if selection exists and has content if (from === to) return // Empty selection const text = editor.state.doc.textBetween(from, to) if (!text) return // No text in selection // Safe to use text now handleSelection(text) } }) return <EditorContent editor={editor} />}