|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | + |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { mockGetEffectiveDecryptedEnv } = vi.hoisted(() => ({ |
| 8 | + mockGetEffectiveDecryptedEnv: vi.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +vi.mock('@/lib/environment/utils', () => ({ |
| 12 | + getEffectiveDecryptedEnv: mockGetEffectiveDecryptedEnv, |
| 13 | +})) |
| 14 | + |
| 15 | +import { |
| 16 | + resolveWebhookProviderConfig, |
| 17 | + resolveWebhookRecordProviderConfig, |
| 18 | +} from '@/lib/webhooks/env-resolver' |
| 19 | + |
| 20 | +describe('webhook env resolver', () => { |
| 21 | + beforeEach(() => { |
| 22 | + vi.clearAllMocks() |
| 23 | + mockGetEffectiveDecryptedEnv.mockResolvedValue({ |
| 24 | + SLACK_BOT_TOKEN: 'xoxb-resolved', |
| 25 | + SLACK_HOST: 'files.slack.com', |
| 26 | + }) |
| 27 | + }) |
| 28 | + |
| 29 | + it('resolves environment variables inside webhook provider config', async () => { |
| 30 | + const result = await resolveWebhookProviderConfig( |
| 31 | + { |
| 32 | + botToken: '{{SLACK_BOT_TOKEN}}', |
| 33 | + includeFiles: true, |
| 34 | + nested: { |
| 35 | + url: 'https://{{SLACK_HOST}}/api/files.info', |
| 36 | + }, |
| 37 | + }, |
| 38 | + 'user-1', |
| 39 | + 'workspace-1' |
| 40 | + ) |
| 41 | + |
| 42 | + expect(result).toEqual({ |
| 43 | + botToken: 'xoxb-resolved', |
| 44 | + includeFiles: true, |
| 45 | + nested: { |
| 46 | + url: 'https://files.slack.com/api/files.info', |
| 47 | + }, |
| 48 | + }) |
| 49 | + expect(mockGetEffectiveDecryptedEnv).toHaveBeenCalledWith('user-1', 'workspace-1') |
| 50 | + }) |
| 51 | + |
| 52 | + it('returns a cloned webhook record with resolved provider config', async () => { |
| 53 | + const webhookRecord = { |
| 54 | + id: 'webhook-1', |
| 55 | + provider: 'slack', |
| 56 | + providerConfig: { |
| 57 | + botToken: '{{SLACK_BOT_TOKEN}}', |
| 58 | + includeFiles: true, |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + const result = await resolveWebhookRecordProviderConfig(webhookRecord, 'user-1', 'workspace-1') |
| 63 | + |
| 64 | + expect(result).toEqual({ |
| 65 | + ...webhookRecord, |
| 66 | + providerConfig: { |
| 67 | + botToken: 'xoxb-resolved', |
| 68 | + includeFiles: true, |
| 69 | + }, |
| 70 | + }) |
| 71 | + expect(result).not.toBe(webhookRecord) |
| 72 | + expect(result.providerConfig).not.toBe(webhookRecord.providerConfig) |
| 73 | + }) |
| 74 | +}) |
0 commit comments