|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { AgentTelemetryReporter } from './agent-telemetry-reporter.ts'; |
| 4 | + |
| 5 | +vi.mock('storybook/internal/telemetry', () => ({ |
| 6 | + telemetry: vi.fn(), |
| 7 | + isExampleStoryId: vi.fn( |
| 8 | + (id: string) => |
| 9 | + id.startsWith('example-button--') || |
| 10 | + id.startsWith('example-header--') || |
| 11 | + id.startsWith('example-page--') |
| 12 | + ), |
| 13 | +})); |
| 14 | + |
| 15 | +const { telemetry } = await import('storybook/internal/telemetry'); |
| 16 | + |
| 17 | +function createMockTestCase({ |
| 18 | + storyId, |
| 19 | + status, |
| 20 | + reports = [], |
| 21 | + errors = [], |
| 22 | +}: { |
| 23 | + storyId?: string; |
| 24 | + status: 'passed' | 'failed' | 'pending'; |
| 25 | + reports?: Array<{ type: string; result?: Record<string, unknown> }>; |
| 26 | + errors?: Array<{ message: string; stack?: string }>; |
| 27 | +}) { |
| 28 | + return { |
| 29 | + meta: () => ({ storyId, reports }), |
| 30 | + result: () => ({ |
| 31 | + state: status, |
| 32 | + errors: status === 'failed' ? errors : [], |
| 33 | + }), |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +function createMockTestModules(testCounts: { passed: number; failed: number }) { |
| 38 | + const tests: Array<{ result: () => { state: string } }> = []; |
| 39 | + for (let i = 0; i < testCounts.passed; i++) { |
| 40 | + tests.push({ result: () => ({ state: 'passed' }) }); |
| 41 | + } |
| 42 | + for (let i = 0; i < testCounts.failed; i++) { |
| 43 | + tests.push({ result: () => ({ state: 'failed' }) }); |
| 44 | + } |
| 45 | + return [ |
| 46 | + { |
| 47 | + children: { |
| 48 | + allTests: function* (filter?: string) { |
| 49 | + for (const t of tests) { |
| 50 | + if (!filter || t.result().state === filter) { |
| 51 | + yield t; |
| 52 | + } |
| 53 | + } |
| 54 | + }, |
| 55 | + }, |
| 56 | + errors: () => [], |
| 57 | + }, |
| 58 | + ]; |
| 59 | +} |
| 60 | + |
| 61 | +describe('AgentTelemetryReporter', () => { |
| 62 | + let reporter: AgentTelemetryReporter; |
| 63 | + |
| 64 | + beforeEach(() => { |
| 65 | + vi.clearAllMocks(); |
| 66 | + reporter = new AgentTelemetryReporter({ |
| 67 | + configDir: '.storybook', |
| 68 | + agent: { name: 'claude' }, |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('onTestCaseResult', () => { |
| 73 | + it('should collect story test results', () => { |
| 74 | + const testCase = createMockTestCase({ |
| 75 | + storyId: 'my-story--primary', |
| 76 | + status: 'passed', |
| 77 | + }); |
| 78 | + reporter.onTestCaseResult(testCase as any); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should skip tests without storyId', () => { |
| 82 | + const testCase = createMockTestCase({ |
| 83 | + storyId: undefined, |
| 84 | + status: 'passed', |
| 85 | + }); |
| 86 | + reporter.onTestCaseResult(testCase as any); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should skip example story IDs', () => { |
| 90 | + const testCase = createMockTestCase({ |
| 91 | + storyId: 'example-button--primary', |
| 92 | + status: 'passed', |
| 93 | + }); |
| 94 | + reporter.onTestCaseResult(testCase as any); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('onTestRunEnd', () => { |
| 99 | + it('should send telemetry with analysis of collected results', async () => { |
| 100 | + reporter.onInit({ config: { watch: false } } as any); |
| 101 | + |
| 102 | + reporter.onTestCaseResult(createMockTestCase({ storyId: 's1', status: 'passed' }) as any); |
| 103 | + reporter.onTestCaseResult( |
| 104 | + createMockTestCase({ |
| 105 | + storyId: 's2', |
| 106 | + status: 'failed', |
| 107 | + errors: [{ message: 'Error: Module not found: foo' }], |
| 108 | + }) as any |
| 109 | + ); |
| 110 | + reporter.onTestCaseResult( |
| 111 | + createMockTestCase({ |
| 112 | + storyId: 's3', |
| 113 | + status: 'passed', |
| 114 | + reports: [{ type: 'render-analysis', result: { emptyRender: true } }], |
| 115 | + }) as any |
| 116 | + ); |
| 117 | + |
| 118 | + await reporter.onTestRunEnd(createMockTestModules({ passed: 2, failed: 1 }) as any, []); |
| 119 | + |
| 120 | + expect(telemetry).toHaveBeenCalledWith( |
| 121 | + 'ai-setup-self-healing-scoring', |
| 122 | + expect.objectContaining({ |
| 123 | + agent: { name: 'claude' }, |
| 124 | + analysis: expect.objectContaining({ |
| 125 | + total: 3, |
| 126 | + passed: 2, |
| 127 | + passedButEmptyRender: 1, |
| 128 | + successRate: 0.67, |
| 129 | + successRateWithoutEmptyRender: 0.33, |
| 130 | + uniqueErrorCount: 1, |
| 131 | + }), |
| 132 | + unhandledErrorCount: 0, |
| 133 | + watch: false, |
| 134 | + }), |
| 135 | + { configDir: '.storybook', stripMetadata: true } |
| 136 | + ); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should filter out example stories from analysis', async () => { |
| 140 | + reporter.onInit({ config: { watch: false } } as any); |
| 141 | + |
| 142 | + reporter.onTestCaseResult( |
| 143 | + createMockTestCase({ storyId: 'my-story--primary', status: 'passed' }) as any |
| 144 | + ); |
| 145 | + reporter.onTestCaseResult( |
| 146 | + createMockTestCase({ storyId: 'example-button--primary', status: 'passed' }) as any |
| 147 | + ); |
| 148 | + |
| 149 | + await reporter.onTestRunEnd(createMockTestModules({ passed: 2, failed: 0 }) as any, []); |
| 150 | + |
| 151 | + expect(telemetry).toHaveBeenCalledWith( |
| 152 | + 'ai-setup-self-healing-scoring', |
| 153 | + expect.objectContaining({ |
| 154 | + analysis: expect.objectContaining({ |
| 155 | + total: 1, |
| 156 | + passed: 1, |
| 157 | + }), |
| 158 | + }), |
| 159 | + expect.anything() |
| 160 | + ); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should count unhandled errors', async () => { |
| 164 | + reporter.onInit({ config: { watch: false } } as any); |
| 165 | + |
| 166 | + await reporter.onTestRunEnd( |
| 167 | + createMockTestModules({ passed: 0, failed: 0 }) as any, |
| 168 | + [{ message: 'unhandled' }, { message: 'another' }] as any |
| 169 | + ); |
| 170 | + |
| 171 | + expect(telemetry).toHaveBeenCalledWith( |
| 172 | + 'ai-setup-self-healing-scoring', |
| 173 | + expect.objectContaining({ |
| 174 | + unhandledErrorCount: 2, |
| 175 | + }), |
| 176 | + expect.anything() |
| 177 | + ); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should reset collected results after each run', async () => { |
| 181 | + reporter.onInit({ config: { watch: false } } as any); |
| 182 | + |
| 183 | + reporter.onTestCaseResult(createMockTestCase({ storyId: 's1', status: 'passed' }) as any); |
| 184 | + await reporter.onTestRunEnd(createMockTestModules({ passed: 1, failed: 0 }) as any, []); |
| 185 | + |
| 186 | + reporter.onTestCaseResult( |
| 187 | + createMockTestCase({ |
| 188 | + storyId: 's2', |
| 189 | + status: 'failed', |
| 190 | + errors: [{ message: 'err' }], |
| 191 | + }) as any |
| 192 | + ); |
| 193 | + await reporter.onTestRunEnd(createMockTestModules({ passed: 0, failed: 1 }) as any, []); |
| 194 | + |
| 195 | + const secondCall = vi.mocked(telemetry).mock.calls[1]; |
| 196 | + expect(secondCall[1]).toEqual( |
| 197 | + expect.objectContaining({ |
| 198 | + analysis: expect.objectContaining({ |
| 199 | + total: 1, |
| 200 | + passed: 0, |
| 201 | + }), |
| 202 | + }) |
| 203 | + ); |
| 204 | + }); |
| 205 | + }); |
| 206 | +}); |
0 commit comments