|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockDbSelect } = vi.hoisted(() => ({ |
| 7 | + mockDbSelect: vi.fn(), |
| 8 | +})) |
| 9 | + |
| 10 | +vi.mock('@sim/db', () => ({ |
| 11 | + db: { |
| 12 | + select: (...args: unknown[]) => mockDbSelect(...args), |
| 13 | + }, |
| 14 | +})) |
| 15 | + |
| 16 | +vi.mock('@sim/db/schema', () => ({ |
| 17 | + account: { |
| 18 | + id: 'account.id', |
| 19 | + userId: 'account.userId', |
| 20 | + providerId: 'account.providerId', |
| 21 | + }, |
| 22 | + credential: { |
| 23 | + id: 'credential.id', |
| 24 | + accountId: 'credential.accountId', |
| 25 | + displayName: 'credential.displayName', |
| 26 | + providerId: 'credential.providerId', |
| 27 | + workspaceId: 'credential.workspaceId', |
| 28 | + type: 'credential.type', |
| 29 | + }, |
| 30 | + credentialMember: { |
| 31 | + credentialId: 'credentialMember.credentialId', |
| 32 | + userId: 'credentialMember.userId', |
| 33 | + status: 'credentialMember.status', |
| 34 | + }, |
| 35 | + document: { |
| 36 | + id: 'document.id', |
| 37 | + userExcluded: 'document.userExcluded', |
| 38 | + archivedAt: 'document.archivedAt', |
| 39 | + deletedAt: 'document.deletedAt', |
| 40 | + }, |
| 41 | + knowledgeBase: { |
| 42 | + id: 'knowledgeBase.id', |
| 43 | + deletedAt: 'knowledgeBase.deletedAt', |
| 44 | + workspaceId: 'knowledgeBase.workspaceId', |
| 45 | + }, |
| 46 | + mcpServers: { |
| 47 | + id: 'mcpServers.id', |
| 48 | + enabled: 'mcpServers.enabled', |
| 49 | + deletedAt: 'mcpServers.deletedAt', |
| 50 | + workspaceId: 'mcpServers.workspaceId', |
| 51 | + }, |
| 52 | + workflow: { |
| 53 | + id: 'workflow.id', |
| 54 | + archivedAt: 'workflow.archivedAt', |
| 55 | + }, |
| 56 | +})) |
| 57 | + |
| 58 | +vi.mock('@sim/logger', () => ({ |
| 59 | + createLogger: () => ({ |
| 60 | + warn: vi.fn(), |
| 61 | + error: vi.fn(), |
| 62 | + }), |
| 63 | +})) |
| 64 | + |
| 65 | +vi.mock('drizzle-orm', () => ({ |
| 66 | + and: vi.fn((...args: unknown[]) => ({ type: 'and', args })), |
| 67 | + eq: vi.fn((...args: unknown[]) => ({ type: 'eq', args })), |
| 68 | + inArray: vi.fn((...args: unknown[]) => ({ type: 'inArray', args })), |
| 69 | + isNull: vi.fn((field: unknown) => ({ type: 'isNull', field })), |
| 70 | + or: vi.fn((...args: unknown[]) => ({ type: 'or', args })), |
| 71 | +})) |
| 72 | + |
| 73 | +import { validateSelectorIds } from './selector-validator' |
| 74 | + |
| 75 | +function createSelectChain(result: unknown) { |
| 76 | + const chain: Record<string, unknown> = {} |
| 77 | + Object.assign(chain, { |
| 78 | + from: vi.fn().mockReturnValue(chain), |
| 79 | + innerJoin: vi.fn().mockReturnValue(chain), |
| 80 | + leftJoin: vi.fn().mockReturnValue(chain), |
| 81 | + where: vi.fn().mockResolvedValue(result), |
| 82 | + }) |
| 83 | + return chain |
| 84 | +} |
| 85 | + |
| 86 | +describe('validateSelectorIds', () => { |
| 87 | + beforeEach(() => { |
| 88 | + vi.clearAllMocks() |
| 89 | + }) |
| 90 | + |
| 91 | + it('accepts shared workspace credential ids and legacy account ids for oauth-input', async () => { |
| 92 | + mockDbSelect.mockReturnValueOnce( |
| 93 | + createSelectChain([{ credentialId: 'cred-1', accountId: 'acct-1' }]) |
| 94 | + ) |
| 95 | + |
| 96 | + const result = await validateSelectorIds('oauth-input', ['cred-1', 'acct-1'], { |
| 97 | + userId: 'user-1', |
| 98 | + workspaceId: 'workspace-1', |
| 99 | + }) |
| 100 | + |
| 101 | + expect(result).toEqual({ |
| 102 | + valid: ['cred-1', 'acct-1'], |
| 103 | + invalid: [], |
| 104 | + }) |
| 105 | + expect(mockDbSelect).toHaveBeenCalledTimes(1) |
| 106 | + }) |
| 107 | + |
| 108 | + it('reports accessible workspace credentials in warnings for invalid oauth-input ids', async () => { |
| 109 | + mockDbSelect.mockReturnValueOnce(createSelectChain([])).mockReturnValueOnce( |
| 110 | + createSelectChain([ |
| 111 | + { |
| 112 | + id: 'cred-2', |
| 113 | + displayName: 'Shared Gmail', |
| 114 | + accountId: 'acct-2', |
| 115 | + credentialProviderId: null, |
| 116 | + accountProviderId: 'google-email', |
| 117 | + }, |
| 118 | + ]) |
| 119 | + ) |
| 120 | + |
| 121 | + const result = await validateSelectorIds('oauth-input', 'missing-cred', { |
| 122 | + userId: 'user-1', |
| 123 | + workspaceId: 'workspace-1', |
| 124 | + }) |
| 125 | + |
| 126 | + expect(result.valid).toEqual([]) |
| 127 | + expect(result.invalid).toEqual(['missing-cred']) |
| 128 | + expect(result.warning).toContain('Accessible workspace credentials:') |
| 129 | + expect(result.warning).toContain('Shared Gmail [cred-2]') |
| 130 | + }) |
| 131 | +}) |
0 commit comments