|
| 1 | +import { afterEach, describe, expect, mock, test } from 'bun:test'; |
| 2 | +import * as React from 'react'; |
| 3 | +import { renderToString } from '../../../utils/staticRender.js'; |
| 4 | +import type { Message } from '../../../types/message.js'; |
| 5 | + |
| 6 | +let transcriptShareDismissed = false; |
| 7 | +let productFeedbackAllowed = true; |
| 8 | +const mockSubmitTranscriptShare = mock(async () => ({ success: true })); |
| 9 | + |
| 10 | +mock.module('../../../utils/config.js', () => ({ |
| 11 | + getGlobalConfig: () => ({ transcriptShareDismissed }), |
| 12 | + saveGlobalConfig: ( |
| 13 | + updater: (current: { transcriptShareDismissed?: boolean }) => { |
| 14 | + transcriptShareDismissed?: boolean; |
| 15 | + }, |
| 16 | + ) => { |
| 17 | + const next = updater({ transcriptShareDismissed }); |
| 18 | + transcriptShareDismissed = next.transcriptShareDismissed ?? false; |
| 19 | + }, |
| 20 | +})); |
| 21 | +mock.module('../../../services/policyLimits/index.js', () => ({ |
| 22 | + isPolicyAllowed: () => productFeedbackAllowed, |
| 23 | +})); |
| 24 | +mock.module('../submitTranscriptShare.js', () => ({ |
| 25 | + submitTranscriptShare: mockSubmitTranscriptShare, |
| 26 | +})); |
| 27 | + |
| 28 | +const { useFrustrationDetection } = await import('../useFrustrationDetection.js'); |
| 29 | + |
| 30 | +type DetectionResult = ReturnType<typeof useFrustrationDetection>; |
| 31 | + |
| 32 | +function apiError(uuid: string): Message { |
| 33 | + return { |
| 34 | + type: 'assistant', |
| 35 | + uuid: uuid as any, |
| 36 | + isApiErrorMessage: true, |
| 37 | + message: { role: 'assistant', content: [] }, |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +async function renderDetection(props: { |
| 42 | + messages: Message[]; |
| 43 | + isLoading?: boolean; |
| 44 | + hasActivePrompt?: boolean; |
| 45 | + otherSurveyOpen?: boolean; |
| 46 | +}): Promise<DetectionResult> { |
| 47 | + let result: DetectionResult | null = null; |
| 48 | + function Probe(): React.ReactNode { |
| 49 | + result = useFrustrationDetection( |
| 50 | + props.messages, |
| 51 | + props.isLoading ?? false, |
| 52 | + props.hasActivePrompt ?? false, |
| 53 | + props.otherSurveyOpen ?? false, |
| 54 | + ); |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + await renderToString(<Probe />); |
| 59 | + if (!result) { |
| 60 | + throw new Error('useFrustrationDetection did not render'); |
| 61 | + } |
| 62 | + return result; |
| 63 | +} |
| 64 | + |
| 65 | +afterEach(() => { |
| 66 | + transcriptShareDismissed = false; |
| 67 | + productFeedbackAllowed = true; |
| 68 | + mockSubmitTranscriptShare.mockClear(); |
| 69 | +}); |
| 70 | + |
| 71 | +describe('useFrustrationDetection', () => { |
| 72 | + test('stays closed without frustration signals', async () => { |
| 73 | + const result = await renderDetection({ messages: [] }); |
| 74 | + |
| 75 | + expect(result.state).toBe('closed'); |
| 76 | + expect(typeof result.handleTranscriptSelect).toBe('function'); |
| 77 | + }); |
| 78 | + |
| 79 | + test('opens a transcript prompt for repeated API errors', async () => { |
| 80 | + const result = await renderDetection({ |
| 81 | + messages: [apiError('a'), apiError('b')], |
| 82 | + }); |
| 83 | + |
| 84 | + expect(result.state).toBe('transcript_prompt'); |
| 85 | + }); |
| 86 | + |
| 87 | + test('does not prompt while loading, prompting, blocked by another survey, dismissed, or policy-denied', async () => { |
| 88 | + const messages = [apiError('a'), apiError('b')]; |
| 89 | + |
| 90 | + expect((await renderDetection({ messages, isLoading: true })).state).toBe('closed'); |
| 91 | + expect((await renderDetection({ messages, hasActivePrompt: true })).state).toBe('closed'); |
| 92 | + expect((await renderDetection({ messages, otherSurveyOpen: true })).state).toBe('closed'); |
| 93 | + |
| 94 | + transcriptShareDismissed = true; |
| 95 | + expect((await renderDetection({ messages })).state).toBe('closed'); |
| 96 | + |
| 97 | + transcriptShareDismissed = false; |
| 98 | + productFeedbackAllowed = false; |
| 99 | + expect((await renderDetection({ messages })).state).toBe('closed'); |
| 100 | + }); |
| 101 | + |
| 102 | + test('submits transcript share when the user accepts', async () => { |
| 103 | + const result = await renderDetection({ |
| 104 | + messages: [apiError('a'), apiError('b')], |
| 105 | + }); |
| 106 | + |
| 107 | + result.handleTranscriptSelect('yes'); |
| 108 | + await new Promise(resolve => setTimeout(resolve, 0)); |
| 109 | + |
| 110 | + expect(mockSubmitTranscriptShare).toHaveBeenCalledWith( |
| 111 | + [apiError('a'), apiError('b')], |
| 112 | + 'frustration', |
| 113 | + expect.any(String), |
| 114 | + ); |
| 115 | + }); |
| 116 | +}); |
0 commit comments