|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { summarizeChanges } from '../../packages/core/src/analyzer/change-summarizer.js'; |
| 3 | +import type { ParsedDiff } from '../../packages/core/src/analyzer/diff-parser.js'; |
| 4 | +import type { RouteMapping } from '../../packages/core/src/analyzer/route-detector.js'; |
| 5 | + |
| 6 | +function makeMockClient(responseText: string) { |
| 7 | + return { |
| 8 | + messages: { |
| 9 | + create: vi.fn().mockResolvedValue({ |
| 10 | + content: [{ type: 'text', text: responseText }], |
| 11 | + }), |
| 12 | + }, |
| 13 | + } as any; |
| 14 | +} |
| 15 | + |
| 16 | +const SAMPLE_DIFF: ParsedDiff = { |
| 17 | + files: [ |
| 18 | + { path: 'app/routes/home.tsx', changeType: 'modified', hunks: [], additions: 5, deletions: 2 }, |
| 19 | + { path: 'src/components/Button.tsx', changeType: 'added', hunks: [], additions: 20, deletions: 0 }, |
| 20 | + ], |
| 21 | + rawDiff: 'diff --git a/app/routes/home.tsx ...', |
| 22 | +}; |
| 23 | + |
| 24 | +const SAMPLE_ROUTES: RouteMapping[] = [ |
| 25 | + { file: 'app/routes/home.tsx', route: '/', changeType: 'modified' }, |
| 26 | +]; |
| 27 | + |
| 28 | +describe('summarizeChanges', () => { |
| 29 | + it('returns parsed description and demo flow from valid JSON response', async () => { |
| 30 | + const client = makeMockClient(JSON.stringify({ |
| 31 | + description: 'Added a new button component', |
| 32 | + demoFlow: '1. Go to home\n2. Click the button', |
| 33 | + })); |
| 34 | + |
| 35 | + const result = await summarizeChanges(client, SAMPLE_DIFF, SAMPLE_ROUTES, 'claude-sonnet-4-6'); |
| 36 | + |
| 37 | + expect(result.changedFiles).toEqual(['app/routes/home.tsx', 'src/components/Button.tsx']); |
| 38 | + expect(result.affectedRoutes).toEqual(SAMPLE_ROUTES); |
| 39 | + expect(result.changeDescription).toBe('Added a new button component'); |
| 40 | + expect(result.suggestedDemoFlow).toBe('1. Go to home\n2. Click the button'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns defaults when LLM response is not valid JSON', async () => { |
| 44 | + const client = makeMockClient('This is not JSON at all'); |
| 45 | + |
| 46 | + const result = await summarizeChanges(client, SAMPLE_DIFF, SAMPLE_ROUTES, 'claude-sonnet-4-6'); |
| 47 | + |
| 48 | + expect(result.changeDescription).toBe('UI changes detected.'); |
| 49 | + expect(result.suggestedDemoFlow).toBe('Navigate to the affected page and interact with the changes.'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('returns defaults when JSON is missing fields', async () => { |
| 53 | + const client = makeMockClient(JSON.stringify({ unrelated: true })); |
| 54 | + |
| 55 | + const result = await summarizeChanges(client, SAMPLE_DIFF, SAMPLE_ROUTES, 'claude-sonnet-4-6'); |
| 56 | + |
| 57 | + expect(result.changeDescription).toBe('UI changes detected.'); |
| 58 | + expect(result.suggestedDemoFlow).toBe('Navigate to the affected page and interact with the changes.'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('extracts JSON embedded in surrounding text', async () => { |
| 62 | + const client = makeMockClient( |
| 63 | + 'Here is the analysis:\n```json\n{"description": "Modal added", "demoFlow": "Open modal"}\n```' |
| 64 | + ); |
| 65 | + |
| 66 | + const result = await summarizeChanges(client, SAMPLE_DIFF, SAMPLE_ROUTES, 'claude-sonnet-4-6'); |
| 67 | + |
| 68 | + expect(result.changeDescription).toBe('Modal added'); |
| 69 | + expect(result.suggestedDemoFlow).toBe('Open modal'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('truncates large diffs before sending to LLM', async () => { |
| 73 | + const client = makeMockClient(JSON.stringify({ |
| 74 | + description: 'Changes', |
| 75 | + demoFlow: 'Demo', |
| 76 | + })); |
| 77 | + const largeDiff: ParsedDiff = { |
| 78 | + files: SAMPLE_DIFF.files, |
| 79 | + rawDiff: 'x'.repeat(10000), |
| 80 | + }; |
| 81 | + |
| 82 | + await summarizeChanges(client, largeDiff, SAMPLE_ROUTES, 'claude-sonnet-4-6'); |
| 83 | + |
| 84 | + const prompt = client.messages.create.mock.calls[0][0].messages[0].content; |
| 85 | + expect(prompt).toContain('(diff truncated)'); |
| 86 | + // The prompt should contain a truncated diff, not the full 10k chars |
| 87 | + expect(prompt.length).toBeLessThan(10000); |
| 88 | + }); |
| 89 | + |
| 90 | + it('does not truncate short diffs', async () => { |
| 91 | + const client = makeMockClient(JSON.stringify({ |
| 92 | + description: 'Changes', |
| 93 | + demoFlow: 'Demo', |
| 94 | + })); |
| 95 | + |
| 96 | + await summarizeChanges(client, SAMPLE_DIFF, SAMPLE_ROUTES, 'claude-sonnet-4-6'); |
| 97 | + |
| 98 | + const prompt = client.messages.create.mock.calls[0][0].messages[0].content; |
| 99 | + expect(prompt).not.toContain('(diff truncated)'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('includes route list in prompt when routes are provided', async () => { |
| 103 | + const client = makeMockClient(JSON.stringify({ description: 'd', demoFlow: 'f' })); |
| 104 | + |
| 105 | + await summarizeChanges(client, SAMPLE_DIFF, SAMPLE_ROUTES, 'claude-sonnet-4-6'); |
| 106 | + |
| 107 | + const prompt = client.messages.create.mock.calls[0][0].messages[0].content; |
| 108 | + expect(prompt).toContain('app/routes/home.tsx'); |
| 109 | + expect(prompt).toContain('/'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('shows fallback when no routes are detected', async () => { |
| 113 | + const client = makeMockClient(JSON.stringify({ description: 'd', demoFlow: 'f' })); |
| 114 | + |
| 115 | + await summarizeChanges(client, SAMPLE_DIFF, [], 'claude-sonnet-4-6'); |
| 116 | + |
| 117 | + const prompt = client.messages.create.mock.calls[0][0].messages[0].content; |
| 118 | + expect(prompt).toContain('no routes detected automatically'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('passes the specified model to the client', async () => { |
| 122 | + const client = makeMockClient(JSON.stringify({ description: 'd', demoFlow: 'f' })); |
| 123 | + |
| 124 | + await summarizeChanges(client, SAMPLE_DIFF, SAMPLE_ROUTES, 'claude-opus-4-6'); |
| 125 | + |
| 126 | + expect(client.messages.create).toHaveBeenCalledWith( |
| 127 | + expect.objectContaining({ model: 'claude-opus-4-6' }) |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it('handles empty content response from LLM', async () => { |
| 132 | + const client = { |
| 133 | + messages: { |
| 134 | + create: vi.fn().mockResolvedValue({ content: [] }), |
| 135 | + }, |
| 136 | + } as any; |
| 137 | + |
| 138 | + const result = await summarizeChanges(client, SAMPLE_DIFF, SAMPLE_ROUTES, 'claude-sonnet-4-6'); |
| 139 | + |
| 140 | + expect(result.changeDescription).toBe('UI changes detected.'); |
| 141 | + expect(result.suggestedDemoFlow).toBe('Navigate to the affected page and interact with the changes.'); |
| 142 | + }); |
| 143 | +}); |
0 commit comments