|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act } from 'react' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | +import { |
| 8 | + createOAuthChatAttempt, |
| 9 | + type OAuthChatAttempt, |
| 10 | + readOAuthChatAttempt, |
| 11 | +} from '@/lib/credentials/oauth-chat-attempt' |
| 12 | +import { ChatCompleteHandoff } from '@/app/oauth/chat-complete/chat-complete-handoff' |
| 13 | + |
| 14 | +function renderAt(search: string): { root: Root } { |
| 15 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 16 | + window.history.replaceState({}, '', `/oauth/chat-complete${search}`) |
| 17 | + const root: Root = createRoot(document.createElement('div')) |
| 18 | + act(() => root.render(<ChatCompleteHandoff />)) |
| 19 | + return { root } |
| 20 | +} |
| 21 | + |
| 22 | +describe('ChatCompleteHandoff', () => { |
| 23 | + let attempt: OAuthChatAttempt |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + vi.clearAllMocks() |
| 27 | + window.localStorage.clear() |
| 28 | + vi.spyOn(window, 'close').mockImplementation(() => {}) |
| 29 | + attempt = createOAuthChatAttempt({ |
| 30 | + workspaceId: 'workspace-1', |
| 31 | + providerId: 'google-email', |
| 32 | + baseProviderId: 'google', |
| 33 | + displayName: 'Gmail', |
| 34 | + controlId: 'message-1:0:0', |
| 35 | + baselineCredentialIds: ['existing-gmail'], |
| 36 | + }) |
| 37 | + }) |
| 38 | + |
| 39 | + it('publishes success on arrival, without requiring a new credential to appear', () => { |
| 40 | + // Re-authorizing an already-linked account updates the account row rather |
| 41 | + // than creating one, so no new credential lands — reaching this page is |
| 42 | + // still the server telling us the flow succeeded. |
| 43 | + const { root } = renderAt(`?oauthAttempt=${attempt.id}`) |
| 44 | + |
| 45 | + expect(readOAuthChatAttempt(attempt.id)?.status).toBe('connected') |
| 46 | + expect(window.close).toHaveBeenCalledOnce() |
| 47 | + act(() => root.unmount()) |
| 48 | + }) |
| 49 | + |
| 50 | + it('publishes failure when the provider returned an error', () => { |
| 51 | + const { root } = renderAt(`?oauthAttempt=${attempt.id}&error=access_denied`) |
| 52 | + |
| 53 | + expect(readOAuthChatAttempt(attempt.id)?.status).toBe('failed') |
| 54 | + act(() => root.unmount()) |
| 55 | + }) |
| 56 | + |
| 57 | + it('leaves an unrelated attempt untouched when no attempt is named', () => { |
| 58 | + const { root } = renderAt('') |
| 59 | + |
| 60 | + expect(readOAuthChatAttempt(attempt.id)?.status).toBe('pending') |
| 61 | + expect(window.close).toHaveBeenCalledOnce() |
| 62 | + act(() => root.unmount()) |
| 63 | + }) |
| 64 | + |
| 65 | + it('still publishes the verdict when the attempt store rejects the write', () => { |
| 66 | + const setItem = vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => { |
| 67 | + throw new DOMException('quota', 'QuotaExceededError') |
| 68 | + }) |
| 69 | + |
| 70 | + // The verdict is lost, but the window must still be released — an |
| 71 | + // unguarded throw would strand the popup open on this page. |
| 72 | + expect(() => renderAt(`?oauthAttempt=${attempt.id}`)).not.toThrow() |
| 73 | + expect(window.close).toHaveBeenCalledOnce() |
| 74 | + setItem.mockRestore() |
| 75 | + }) |
| 76 | + |
| 77 | + describe('close-refused fallback', () => { |
| 78 | + const realLocation = window.location |
| 79 | + |
| 80 | + afterEach(() => { |
| 81 | + vi.useRealTimers() |
| 82 | + Object.defineProperty(window, 'location', { configurable: true, value: realLocation }) |
| 83 | + }) |
| 84 | + |
| 85 | + /** |
| 86 | + * jsdom performs no navigation and forbids redefining `location.replace`, |
| 87 | + * so the whole location is swapped for a stub carrying only what the |
| 88 | + * handoff reads: the current href, the origin, and the redirect sink. |
| 89 | + */ |
| 90 | + function renderWithStubbedLocation(search: string): { calls: string[]; root: Root } { |
| 91 | + const calls: string[] = [] |
| 92 | + Object.defineProperty(window, 'location', { |
| 93 | + configurable: true, |
| 94 | + value: { |
| 95 | + href: `https://sim.test/oauth/chat-complete${search}`, |
| 96 | + origin: 'https://sim.test', |
| 97 | + replace: (url: string) => calls.push(url), |
| 98 | + }, |
| 99 | + }) |
| 100 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 101 | + const root: Root = createRoot(document.createElement('div')) |
| 102 | + act(() => root.render(<ChatCompleteHandoff />)) |
| 103 | + return { calls, root } |
| 104 | + } |
| 105 | + |
| 106 | + it('redirects to a same-origin returnTo once the close is refused', () => { |
| 107 | + vi.useFakeTimers() |
| 108 | + const returnTo = 'https://sim.test/workspace/workspace-1/chat/chat-1' |
| 109 | + |
| 110 | + const { calls, root } = renderWithStubbedLocation( |
| 111 | + `?oauthAttempt=${attempt.id}&returnTo=${encodeURIComponent(returnTo)}` |
| 112 | + ) |
| 113 | + act(() => { |
| 114 | + vi.advanceTimersByTime(400) |
| 115 | + }) |
| 116 | + |
| 117 | + expect(calls).toEqual([returnTo]) |
| 118 | + act(() => root.unmount()) |
| 119 | + }) |
| 120 | + |
| 121 | + it('refuses a cross-origin returnTo and falls back to the workspace', () => { |
| 122 | + vi.useFakeTimers() |
| 123 | + |
| 124 | + const { calls, root } = renderWithStubbedLocation( |
| 125 | + `?oauthAttempt=${attempt.id}&returnTo=${encodeURIComponent('https://evil.example/steal')}` |
| 126 | + ) |
| 127 | + act(() => { |
| 128 | + vi.advanceTimersByTime(400) |
| 129 | + }) |
| 130 | + |
| 131 | + expect(calls).toEqual(['/workspace']) |
| 132 | + act(() => root.unmount()) |
| 133 | + }) |
| 134 | + }) |
| 135 | +}) |
0 commit comments