|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { parseAiResponse } from './ai-analysis.js'; |
| 3 | +import { buildDoctorPrompt, type AnalysisContext } from '../agent-prompt.js'; |
| 4 | + |
| 5 | +describe('ai-analysis', () => { |
| 6 | + describe('parseAiResponse', () => { |
| 7 | + it('parses valid JSON with findings', () => { |
| 8 | + const json = JSON.stringify({ |
| 9 | + findings: [ |
| 10 | + { |
| 11 | + severity: 'warning', |
| 12 | + title: 'Missing middleware', |
| 13 | + detail: 'No auth middleware found', |
| 14 | + remediation: 'Add middleware.ts', |
| 15 | + filePath: 'src/middleware.ts', |
| 16 | + }, |
| 17 | + ], |
| 18 | + summary: 'Integration needs work', |
| 19 | + }); |
| 20 | + const result = parseAiResponse(json); |
| 21 | + expect(result.findings).toHaveLength(1); |
| 22 | + expect(result.findings[0].severity).toBe('warning'); |
| 23 | + expect(result.findings[0].title).toBe('Missing middleware'); |
| 24 | + expect(result.findings[0].filePath).toBe('src/middleware.ts'); |
| 25 | + expect(result.summary).toBe('Integration needs work'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('parses JSON inside markdown code block', () => { |
| 29 | + const text = `Here is my analysis: |
| 30 | +
|
| 31 | +\`\`\`json |
| 32 | +{ |
| 33 | + "findings": [ |
| 34 | + { |
| 35 | + "severity": "info", |
| 36 | + "title": "Good setup", |
| 37 | + "detail": "Everything looks correct", |
| 38 | + "remediation": "No action needed" |
| 39 | + } |
| 40 | + ], |
| 41 | + "summary": "Looks good" |
| 42 | +} |
| 43 | +\`\`\` |
| 44 | +
|
| 45 | +That's my analysis.`; |
| 46 | + const result = parseAiResponse(text); |
| 47 | + expect(result.findings).toHaveLength(1); |
| 48 | + expect(result.findings[0].severity).toBe('info'); |
| 49 | + expect(result.summary).toBe('Looks good'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('handles empty findings array', () => { |
| 53 | + const json = JSON.stringify({ findings: [], summary: 'All clear' }); |
| 54 | + const result = parseAiResponse(json); |
| 55 | + expect(result.findings).toHaveLength(0); |
| 56 | + expect(result.summary).toBe('All clear'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('defaults invalid severity to info', () => { |
| 60 | + const json = JSON.stringify({ |
| 61 | + findings: [{ severity: 'critical', title: 'Test', detail: 'Test', remediation: 'Test' }], |
| 62 | + summary: '', |
| 63 | + }); |
| 64 | + const result = parseAiResponse(json); |
| 65 | + expect(result.findings[0].severity).toBe('info'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('handles malformed JSON gracefully', () => { |
| 69 | + const result = parseAiResponse('This is not JSON at all'); |
| 70 | + expect(result.findings).toHaveLength(0); |
| 71 | + expect(result.summary).toBe('This is not JSON at all'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('extracts JSON from mixed text', () => { |
| 75 | + const text = `Let me analyze this. |
| 76 | +{"findings": [{"severity": "error", "title": "Bad", "detail": "Very bad", "remediation": "Fix it"}], "summary": "Issues found"} |
| 77 | +Hope this helps.`; |
| 78 | + const result = parseAiResponse(text); |
| 79 | + expect(result.findings).toHaveLength(1); |
| 80 | + expect(result.findings[0].title).toBe('Bad'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('handles missing optional filePath', () => { |
| 84 | + const json = JSON.stringify({ |
| 85 | + findings: [{ severity: 'warning', title: 'T', detail: 'D', remediation: 'R' }], |
| 86 | + summary: '', |
| 87 | + }); |
| 88 | + const result = parseAiResponse(json); |
| 89 | + expect(result.findings[0].filePath).toBeUndefined(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('truncates very long fallback summary', () => { |
| 93 | + const longText = 'x'.repeat(1000); |
| 94 | + const result = parseAiResponse(longText); |
| 95 | + expect(result.summary.length).toBeLessThanOrEqual(500); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('buildDoctorPrompt', () => { |
| 100 | + const baseContext: AnalysisContext = { |
| 101 | + language: { name: 'JavaScript/TypeScript', manifestFile: 'package.json' }, |
| 102 | + framework: { name: 'Next.js', version: '14.0.0', variant: 'app-router' }, |
| 103 | + sdk: { |
| 104 | + name: '@workos/authkit-nextjs', |
| 105 | + version: '1.0.0', |
| 106 | + latest: '1.0.0', |
| 107 | + outdated: false, |
| 108 | + isAuthKit: true, |
| 109 | + language: 'javascript', |
| 110 | + }, |
| 111 | + environment: { |
| 112 | + apiKeyConfigured: true, |
| 113 | + apiKeyType: 'staging', |
| 114 | + clientId: 'client_01J...', |
| 115 | + redirectUri: null, |
| 116 | + cookieDomain: null, |
| 117 | + baseUrl: 'https://api.workos.com', |
| 118 | + }, |
| 119 | + existingIssues: [], |
| 120 | + }; |
| 121 | + |
| 122 | + it('includes project context in prompt', () => { |
| 123 | + const prompt = buildDoctorPrompt(baseContext); |
| 124 | + expect(prompt).toContain('JavaScript/TypeScript'); |
| 125 | + expect(prompt).toContain('Next.js'); |
| 126 | + expect(prompt).toContain('@workos/authkit-nextjs'); |
| 127 | + expect(prompt).toContain('staging'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('includes existing issues for deduplication', () => { |
| 131 | + const context: AnalysisContext = { |
| 132 | + ...baseContext, |
| 133 | + existingIssues: [{ code: 'MISSING_API_KEY', severity: 'error', message: 'API key not set' }], |
| 134 | + }; |
| 135 | + const prompt = buildDoctorPrompt(context); |
| 136 | + expect(prompt).toContain('MISSING_API_KEY'); |
| 137 | + expect(prompt).toContain('API key not set'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('shows "None detected" when no existing issues', () => { |
| 141 | + const prompt = buildDoctorPrompt(baseContext); |
| 142 | + expect(prompt).toContain('None detected'); |
| 143 | + }); |
| 144 | + |
| 145 | + it('handles null framework', () => { |
| 146 | + const context: AnalysisContext = { |
| 147 | + ...baseContext, |
| 148 | + framework: { name: null, version: null }, |
| 149 | + }; |
| 150 | + const prompt = buildDoctorPrompt(context); |
| 151 | + expect(prompt).not.toContain('Framework:'); |
| 152 | + }); |
| 153 | + |
| 154 | + it('handles null SDK', () => { |
| 155 | + const context: AnalysisContext = { |
| 156 | + ...baseContext, |
| 157 | + sdk: { ...baseContext.sdk, name: null, version: null }, |
| 158 | + }; |
| 159 | + const prompt = buildDoctorPrompt(context); |
| 160 | + expect(prompt).toContain('SDK: Not installed'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('includes output format instructions', () => { |
| 164 | + const prompt = buildDoctorPrompt(baseContext); |
| 165 | + expect(prompt).toContain('"findings"'); |
| 166 | + expect(prompt).toContain('"summary"'); |
| 167 | + expect(prompt).toContain('JSON'); |
| 168 | + }); |
| 169 | + |
| 170 | + it('includes SDK knowledge to prevent false positives', () => { |
| 171 | + const prompt = buildDoctorPrompt(baseContext); |
| 172 | + expect(prompt).toContain('SDK Knowledge'); |
| 173 | + expect(prompt).toContain('PKCE'); |
| 174 | + expect(prompt).toContain('@workos-inc/node'); |
| 175 | + expect(prompt).toContain('ANY JavaScript runtime'); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments