|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | +import { act, type InputHTMLAttributes, type ReactNode } from 'react' |
| 3 | +import { createRoot, type Root } from 'react-dom/client' |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const mocks = vi.hoisted(() => ({ |
| 7 | + chatRequest: vi.fn(), |
| 8 | + chatVerify: vi.fn(), |
| 9 | + fileRequest: vi.fn(), |
| 10 | + fileVerify: vi.fn(), |
| 11 | + refresh: vi.fn(), |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@sim/emcn', () => ({ |
| 15 | + cn: (...values: Array<string | false | null | undefined>) => values.filter(Boolean).join(' '), |
| 16 | + ChipInput: ({ |
| 17 | + error: _error, |
| 18 | + size: _size, |
| 19 | + ...props |
| 20 | + }: Omit<InputHTMLAttributes<HTMLInputElement>, 'size'> & { |
| 21 | + error?: boolean |
| 22 | + size?: string |
| 23 | + }) => <input {...props} />, |
| 24 | + Label: ({ children, htmlFor }: { children: ReactNode; htmlFor?: string }) => ( |
| 25 | + <label htmlFor={htmlFor}>{children}</label> |
| 26 | + ), |
| 27 | + InputOTP: ({ |
| 28 | + children, |
| 29 | + value, |
| 30 | + onChange, |
| 31 | + 'aria-invalid': invalid, |
| 32 | + }: { |
| 33 | + children: ReactNode |
| 34 | + value: string |
| 35 | + onChange: (value: string) => void |
| 36 | + 'aria-invalid'?: boolean |
| 37 | + }) => ( |
| 38 | + <div> |
| 39 | + <input |
| 40 | + data-testid='otp-code' |
| 41 | + aria-invalid={invalid} |
| 42 | + value={value} |
| 43 | + onChange={(event) => onChange(event.target.value)} |
| 44 | + /> |
| 45 | + {children} |
| 46 | + </div> |
| 47 | + ), |
| 48 | + InputOTPGroup: ({ children }: { children: ReactNode }) => <div>{children}</div>, |
| 49 | + InputOTPSlot: ({ invalid, index }: { invalid?: boolean; index: number }) => ( |
| 50 | + <span data-otp-slot={index} data-invalid={invalid} /> |
| 51 | + ), |
| 52 | +})) |
| 53 | +vi.mock('@sim/logger', () => ({ createLogger: () => ({ error: vi.fn() }) })) |
| 54 | +vi.mock('@/lib/messaging/email/validation', () => ({ |
| 55 | + quickValidateEmail: () => ({ isValid: true }), |
| 56 | +})) |
| 57 | +vi.mock('@/app/(auth)/components', () => ({ |
| 58 | + AuthSubmitButton: ({ |
| 59 | + children, |
| 60 | + loading: _loading, |
| 61 | + loadingLabel: _loadingLabel, |
| 62 | + ...props |
| 63 | + }: React.ButtonHTMLAttributes<HTMLButtonElement> & { |
| 64 | + loading?: boolean |
| 65 | + loadingLabel?: string |
| 66 | + }) => ( |
| 67 | + <button type='submit' {...props}> |
| 68 | + {children} |
| 69 | + </button> |
| 70 | + ), |
| 71 | +})) |
| 72 | +vi.mock('@/app/(auth)/components/auth-button-classes', () => ({ AUTH_TEXT_LINK: '' })) |
| 73 | +vi.mock('@/components/auth/public-auth-header', () => ({ |
| 74 | + PublicAuthHeader: ({ title }: { title: string }) => <h1>{title}</h1>, |
| 75 | +})) |
| 76 | +vi.mock('@/app/f/[token]/public-file-auth-shell', () => ({ |
| 77 | + PublicFileAuthShell: ({ children }: { children: ReactNode }) => <div>{children}</div>, |
| 78 | +})) |
| 79 | +vi.mock('next/navigation', () => ({ useRouter: () => ({ refresh: mocks.refresh }) })) |
| 80 | +vi.mock('@/hooks/queries/chats', () => ({ |
| 81 | + useChatEmailOtpRequest: () => ({ mutateAsync: mocks.chatRequest, isPending: false }), |
| 82 | + useChatEmailOtpVerify: () => ({ mutateAsync: mocks.chatVerify, isPending: false }), |
| 83 | +})) |
| 84 | +vi.mock('@/hooks/queries/public-shares', () => ({ |
| 85 | + usePublicFileOtpRequest: () => ({ mutateAsync: mocks.fileRequest, isPending: false }), |
| 86 | + usePublicFileOtpVerify: () => ({ mutateAsync: mocks.fileVerify, isPending: false }), |
| 87 | +})) |
| 88 | + |
| 89 | +import EmailAuth from '@/app/(interfaces)/chat/components/auth/email/email-auth' |
| 90 | +import { PublicFileEmailAuth } from '@/app/f/[token]/public-file-email-auth' |
| 91 | + |
| 92 | +let root: Root |
| 93 | +let container: HTMLDivElement |
| 94 | + |
| 95 | +function changeInput(input: HTMLInputElement, value: string) { |
| 96 | + Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')?.set?.call(input, value) |
| 97 | + input.dispatchEvent(new Event('input', { bubbles: true })) |
| 98 | +} |
| 99 | + |
| 100 | +function button(label: string) { |
| 101 | + const found = Array.from(container.querySelectorAll('button')).find( |
| 102 | + (candidate) => candidate.textContent?.trim() === label |
| 103 | + ) |
| 104 | + if (!found) throw new Error(`Missing button: ${label}`) |
| 105 | + return found |
| 106 | +} |
| 107 | + |
| 108 | +function expectOtpInvalid(invalid: boolean) { |
| 109 | + expect(container.querySelector('[data-testid="otp-code"]')?.getAttribute('aria-invalid')).toBe( |
| 110 | + String(invalid) |
| 111 | + ) |
| 112 | + const slots = container.querySelectorAll('[data-otp-slot]') |
| 113 | + expect(slots).toHaveLength(6) |
| 114 | + for (const slot of slots) expect(slot.getAttribute('data-invalid')).toBe(String(invalid)) |
| 115 | +} |
| 116 | + |
| 117 | +beforeEach(() => { |
| 118 | + vi.clearAllMocks() |
| 119 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 120 | + mocks.chatRequest.mockResolvedValue({}) |
| 121 | + mocks.chatVerify.mockResolvedValue({}) |
| 122 | + mocks.fileRequest.mockResolvedValue({}) |
| 123 | + mocks.fileVerify.mockResolvedValue({}) |
| 124 | + container = document.createElement('div') |
| 125 | + document.body.appendChild(container) |
| 126 | + root = createRoot(container) |
| 127 | +}) |
| 128 | + |
| 129 | +afterEach(() => { |
| 130 | + act(() => root.unmount()) |
| 131 | + container.remove() |
| 132 | + vi.unstubAllGlobals() |
| 133 | +}) |
| 134 | + |
| 135 | +describe('OTP error provenance', () => { |
| 136 | + it('keeps the chat code valid on resend failure and marks only a failed verification invalid', async () => { |
| 137 | + act(() => root.render(<EmailAuth identifier='chat-1' />)) |
| 138 | + act(() => changeInput(container.querySelector('#email')!, 'member@example.com')) |
| 139 | + await act(async () => button('Continue').click()) |
| 140 | + |
| 141 | + mocks.chatRequest.mockRejectedValueOnce(new Error('Delivery failed')) |
| 142 | + await act(async () => button('Resend').click()) |
| 143 | + expect(container.textContent).toContain('Delivery failed') |
| 144 | + expectOtpInvalid(false) |
| 145 | + |
| 146 | + mocks.chatVerify.mockRejectedValueOnce(new Error('Incorrect code')) |
| 147 | + await act(async () => |
| 148 | + changeInput(container.querySelector('[data-testid="otp-code"]')!, '123456') |
| 149 | + ) |
| 150 | + expect(container.textContent).toContain('Incorrect code') |
| 151 | + expectOtpInvalid(true) |
| 152 | + }) |
| 153 | + |
| 154 | + it('keeps the public-file code valid on resend failure and marks only a failed verification invalid', async () => { |
| 155 | + act(() => root.render(<PublicFileEmailAuth token='share-1' />)) |
| 156 | + act(() => changeInput(container.querySelector('#email')!, 'member@example.com')) |
| 157 | + await act(async () => button('Continue').click()) |
| 158 | + |
| 159 | + mocks.fileRequest.mockRejectedValueOnce(new Error('Delivery failed')) |
| 160 | + await act(async () => button('Resend').click()) |
| 161 | + expect(container.textContent).toContain('Delivery failed') |
| 162 | + expectOtpInvalid(false) |
| 163 | + |
| 164 | + mocks.fileVerify.mockRejectedValueOnce(new Error('Incorrect code')) |
| 165 | + await act(async () => |
| 166 | + changeInput(container.querySelector('[data-testid="otp-code"]')!, '123456') |
| 167 | + ) |
| 168 | + expect(container.textContent).toContain('Incorrect code') |
| 169 | + expectOtpInvalid(true) |
| 170 | + }) |
| 171 | +}) |
0 commit comments