|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const mocks = vi.hoisted(() => ({ |
| 7 | + requestJson: vi.fn(), |
| 8 | + requireWorkspaceCredentialListResponse: vi.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +vi.mock('@sim/emcn', () => ({ |
| 12 | + toast: { error: vi.fn(), success: vi.fn() }, |
| 13 | +})) |
| 14 | +vi.mock('next/navigation', () => ({ |
| 15 | + useParams: vi.fn(), |
| 16 | + useRouter: vi.fn(), |
| 17 | +})) |
| 18 | +vi.mock('@/lib/api/client/request', () => ({ requestJson: mocks.requestJson })) |
| 19 | +vi.mock('@/hooks/queries/utils/fetch-workspace-credentials', () => ({ |
| 20 | + requireWorkspaceCredentialListResponse: mocks.requireWorkspaceCredentialListResponse, |
| 21 | +})) |
| 22 | + |
| 23 | +import type { OAuthReturnContext } from '@/lib/credentials/client-state' |
| 24 | +import { resolveOAuthMessage } from '@/hooks/use-oauth-return' |
| 25 | + |
| 26 | +const context: OAuthReturnContext = { |
| 27 | + origin: 'integrations', |
| 28 | + displayName: 'New Gmail', |
| 29 | + providerId: 'google-email', |
| 30 | + preCount: 1, |
| 31 | + baselineCredentials: [ |
| 32 | + { |
| 33 | + id: 'credential-existing', |
| 34 | + accountId: 'account-1', |
| 35 | + updatedAt: '2026-08-14T17:00:00.000Z', |
| 36 | + }, |
| 37 | + ], |
| 38 | + workspaceId: 'workspace-1', |
| 39 | + requestedAt: Date.now(), |
| 40 | +} |
| 41 | + |
| 42 | +const existingCredential = { |
| 43 | + id: 'credential-existing', |
| 44 | + workspaceId: 'workspace-1', |
| 45 | + type: 'oauth' as const, |
| 46 | + displayName: 'Existing Gmail', |
| 47 | + description: null, |
| 48 | + providerId: 'google-email', |
| 49 | + accountId: 'account-1', |
| 50 | + envKey: null, |
| 51 | + envOwnerUserId: null, |
| 52 | + createdBy: 'user-1', |
| 53 | + createdAt: '2026-08-01T00:00:00.000Z', |
| 54 | + updatedAt: '2026-08-14T18:00:00.000Z', |
| 55 | +} |
| 56 | + |
| 57 | +describe('resolveOAuthMessage', () => { |
| 58 | + beforeEach(() => { |
| 59 | + vi.clearAllMocks() |
| 60 | + mocks.requestJson.mockResolvedValue({}) |
| 61 | + }) |
| 62 | + |
| 63 | + it('recognizes an idempotent already-connected account from its reconnect timestamp', async () => { |
| 64 | + mocks.requireWorkspaceCredentialListResponse.mockReturnValue([existingCredential]) |
| 65 | + |
| 66 | + await expect(resolveOAuthMessage(context)).resolves.toEqual({ |
| 67 | + kind: 'success', |
| 68 | + text: 'This account is already connected as "Existing Gmail".', |
| 69 | + }) |
| 70 | + }) |
| 71 | + |
| 72 | + it('does not report success when the credential list is unchanged', async () => { |
| 73 | + mocks.requireWorkspaceCredentialListResponse.mockReturnValue([ |
| 74 | + { |
| 75 | + ...existingCredential, |
| 76 | + updatedAt: context.baselineCredentials?.[0].updatedAt, |
| 77 | + }, |
| 78 | + ]) |
| 79 | + |
| 80 | + await expect(resolveOAuthMessage(context)).resolves.toEqual({ |
| 81 | + kind: 'error', |
| 82 | + text: 'We couldn’t verify the "New Gmail" connection. Try again.', |
| 83 | + }) |
| 84 | + }) |
| 85 | +}) |
0 commit comments