|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +import type { NotificationReport } from './report.mts' |
| 4 | + |
| 5 | +const { |
| 6 | + sendJiraTaskNotification, |
| 7 | + sendSlackWebhookNotifications, |
| 8 | + sendSlackBotNotifications, |
| 9 | +} = vi.hoisted(() => ({ |
| 10 | + sendJiraTaskNotification: vi.fn(), |
| 11 | + sendSlackWebhookNotifications: vi.fn(), |
| 12 | + sendSlackBotNotifications: vi.fn(), |
| 13 | +})) |
| 14 | + |
| 15 | +vi.mock('./jira.mts', () => ({ |
| 16 | + isJiraEnabled: () => true, |
| 17 | + sendJiraTaskNotification, |
| 18 | +})) |
| 19 | +vi.mock('./slack-webhook.mts', () => ({ |
| 20 | + getSlackNotificationConfig: () => ({}), |
| 21 | + isSlackWebhookConfigured: () => true, |
| 22 | + sendSlackWebhookNotifications, |
| 23 | +})) |
| 24 | +vi.mock('./slack-bot.mts', () => ({ sendSlackBotNotifications })) |
| 25 | +vi.mock('../../../utils/logger.mts', () => ({ |
| 26 | + logger: { warn: vi.fn() }, |
| 27 | +})) |
| 28 | + |
| 29 | +import { dispatchScanNotifications } from './dispatch.mts' |
| 30 | + |
| 31 | +const report: NotificationReport = { |
| 32 | + kind: 'diff', |
| 33 | + diffScanId: 'diff-42', |
| 34 | + scanId: 'scan-42', |
| 35 | + repository: 'example/service', |
| 36 | + branch: 'feature/fix', |
| 37 | + pullRequest: 42, |
| 38 | + commitSha: '1234567890abcdef', |
| 39 | + scanUrl: 'https://socket.dev/dashboard/org/example/diff/base/scan-42', |
| 40 | + decision: 'fail', |
| 41 | + summary: '1 finding (1 blocking, 0 warning)', |
| 42 | + reachabilityEnabled: true, |
| 43 | + reachabilitySummary: '1 reachable', |
| 44 | + findings: [], |
| 45 | + reachabilityFindings: [ |
| 46 | + { |
| 47 | + packageName: 'left-pad', |
| 48 | + ecosystem: 'npm', |
| 49 | + version: '1.0.0', |
| 50 | + alertType: 'cve', |
| 51 | + severity: 'high', |
| 52 | + action: 'error', |
| 53 | + advisoryId: 'GHSA-1234', |
| 54 | + purl: 'pkg:npm/left-pad@1.0.0', |
| 55 | + url: 'https://socket.dev/npm/package/left-pad/overview/1.0.0', |
| 56 | + reachability: 'reachable', |
| 57 | + }, |
| 58 | + ], |
| 59 | +} |
| 60 | + |
| 61 | +afterEach(() => { |
| 62 | + vi.unstubAllEnvs() |
| 63 | + vi.clearAllMocks() |
| 64 | +}) |
| 65 | + |
| 66 | +describe('scan notification dispatch', () => { |
| 67 | + it('sends the same report to Jira and both Slack transports despite a provider failure', async () => { |
| 68 | + vi.stubEnv('SOCKET_SLACK_BOT_TOKEN', 'xoxb-test-token') |
| 69 | + sendJiraTaskNotification.mockRejectedValueOnce( |
| 70 | + new Error('Jira unavailable'), |
| 71 | + ) |
| 72 | + sendSlackWebhookNotifications.mockRejectedValueOnce( |
| 73 | + new Error('Slack webhook unavailable'), |
| 74 | + ) |
| 75 | + |
| 76 | + await expect(dispatchScanNotifications(report)).resolves.toBeUndefined() |
| 77 | + |
| 78 | + expect(sendJiraTaskNotification).toHaveBeenCalledWith(report) |
| 79 | + expect(sendSlackWebhookNotifications).toHaveBeenCalledWith(report, {}) |
| 80 | + expect(sendSlackBotNotifications).toHaveBeenCalledWith(report, {}) |
| 81 | + }) |
| 82 | +}) |
0 commit comments