|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +import { getAlertsMapFromPurls } from './alerts-map.mts' |
| 4 | + |
| 5 | +// Mock all dependencies with vi.hoisted for better type safety. |
| 6 | +const mockSetupSdk = vi.hoisted(() => vi.fn()) |
| 7 | +const mockFindSocketYmlSync = vi.hoisted(() => vi.fn()) |
| 8 | +const mockAddArtifactToAlertsMap = vi.hoisted(() => vi.fn()) |
| 9 | +const mockBatchPackageStream = vi.hoisted(() => vi.fn()) |
| 10 | + |
| 11 | +vi.mock('./sdk.mts', () => ({ |
| 12 | + setupSdk: mockSetupSdk, |
| 13 | +})) |
| 14 | + |
| 15 | +vi.mock('./config.mts', () => ({ |
| 16 | + findSocketYmlSync: mockFindSocketYmlSync, |
| 17 | +})) |
| 18 | + |
| 19 | +vi.mock('./socket-package-alert.mts', () => ({ |
| 20 | + addArtifactToAlertsMap: mockAddArtifactToAlertsMap, |
| 21 | +})) |
| 22 | + |
| 23 | +vi.mock('./filter-config.mts', () => ({ |
| 24 | + toFilterConfig: vi.fn(filter => filter || {}), |
| 25 | +})) |
| 26 | + |
| 27 | +describe('Alerts Map', () => { |
| 28 | + beforeEach(() => { |
| 29 | + vi.clearAllMocks() |
| 30 | + |
| 31 | + // Setup default mock implementations. |
| 32 | + mockFindSocketYmlSync.mockReturnValue({ ok: false, data: undefined }) |
| 33 | + mockAddArtifactToAlertsMap.mockResolvedValue(undefined) |
| 34 | + |
| 35 | + mockBatchPackageStream.mockImplementation(async function* () { |
| 36 | + yield { |
| 37 | + success: true, |
| 38 | + data: { |
| 39 | + alerts: [], |
| 40 | + name: 'lodash', |
| 41 | + purl: 'pkg:npm/lodash@4.17.21', |
| 42 | + version: '4.17.21', |
| 43 | + }, |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + mockSetupSdk.mockResolvedValue({ |
| 48 | + ok: true, |
| 49 | + data: { |
| 50 | + batchPackageStream: mockBatchPackageStream, |
| 51 | + }, |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + describe('getAlertsMapFromPurls', () => { |
| 56 | + it('should pass undefined apiToken to setupSdk when not provided', async () => { |
| 57 | + const purls = ['pkg:npm/lodash@4.17.21'] |
| 58 | + |
| 59 | + await getAlertsMapFromPurls(purls, { |
| 60 | + nothrow: true, |
| 61 | + }) |
| 62 | + |
| 63 | + // setupSdk should be called with undefined apiToken to let it handle token resolution. |
| 64 | + expect(mockSetupSdk).toHaveBeenCalledWith({ apiToken: undefined }) |
| 65 | + }) |
| 66 | + |
| 67 | + it('should pass provided apiToken to setupSdk when explicitly set', async () => { |
| 68 | + const purls = ['pkg:npm/lodash@4.17.21'] |
| 69 | + const customToken = 'sktsec_test_custom_token' |
| 70 | + |
| 71 | + await getAlertsMapFromPurls(purls, { |
| 72 | + apiToken: customToken, |
| 73 | + nothrow: true, |
| 74 | + }) |
| 75 | + |
| 76 | + // setupSdk should be called with the custom token. |
| 77 | + expect(mockSetupSdk).toHaveBeenCalledWith({ apiToken: customToken }) |
| 78 | + }) |
| 79 | + |
| 80 | + it('should return empty map when no purls provided', async () => { |
| 81 | + const alertsMap = await getAlertsMapFromPurls([], { |
| 82 | + nothrow: true, |
| 83 | + }) |
| 84 | + |
| 85 | + expect(alertsMap).toBeInstanceOf(Map) |
| 86 | + expect(alertsMap.size).toBe(0) |
| 87 | + // setupSdk should not be called if there are no purls. |
| 88 | + expect(mockSetupSdk).not.toHaveBeenCalled() |
| 89 | + }) |
| 90 | + |
| 91 | + it('should process purls and return alerts map', async () => { |
| 92 | + const purls = ['pkg:npm/lodash@4.17.21', 'pkg:npm/express@4.18.2'] |
| 93 | + |
| 94 | + const alertsMap = await getAlertsMapFromPurls(purls, { |
| 95 | + nothrow: true, |
| 96 | + }) |
| 97 | + |
| 98 | + expect(alertsMap).toBeInstanceOf(Map) |
| 99 | + expect(mockSetupSdk).toHaveBeenCalledWith({ apiToken: undefined }) |
| 100 | + expect(mockBatchPackageStream).toHaveBeenCalled() |
| 101 | + }) |
| 102 | + |
| 103 | + it('should handle filter options correctly', async () => { |
| 104 | + const purls = ['pkg:npm/lodash@4.17.21'] |
| 105 | + |
| 106 | + await getAlertsMapFromPurls(purls, { |
| 107 | + filter: { actions: ['error', 'warn'] }, |
| 108 | + nothrow: true, |
| 109 | + }) |
| 110 | + |
| 111 | + expect(mockSetupSdk).toHaveBeenCalled() |
| 112 | + expect(mockBatchPackageStream).toHaveBeenCalled() |
| 113 | + }) |
| 114 | + }) |
| 115 | +}) |
0 commit comments