|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +import { |
| 4 | + findBaselineScan, |
| 5 | + selectBaselineScan, |
| 6 | +} from './select-baseline-scan.mts' |
| 7 | +import { fetchOrgFullScanList } from '../scan/fetch-list-scans.mts' |
| 8 | + |
| 9 | +vi.mock('../scan/fetch-list-scans.mts', () => ({ |
| 10 | + fetchOrgFullScanList: vi.fn(), |
| 11 | +})) |
| 12 | + |
| 13 | +const scan = { |
| 14 | + api_url: null, |
| 15 | + created_at: '2026-01-01T00:00:00Z', |
| 16 | + html_report_url: 'https://socket.dev/report', |
| 17 | + id: 'scan-base', |
| 18 | + integration_repo_url: null, |
| 19 | + integration_type: null, |
| 20 | + organization_id: 'org-id', |
| 21 | + organization_slug: 'test-org', |
| 22 | + repo: 'test-repo', |
| 23 | + repository_id: 'repo-id', |
| 24 | + repository_slug: 'test-repo', |
| 25 | + updated_at: '2026-01-01T00:00:00Z', |
| 26 | + commit_hash: 'base-sha', |
| 27 | + branch: 'main', |
| 28 | +} |
| 29 | + |
| 30 | +describe('selectBaselineScan', () => { |
| 31 | + beforeEach(() => { |
| 32 | + vi.mocked(fetchOrgFullScanList).mockReset() |
| 33 | + }) |
| 34 | + |
| 35 | + it('selects the exact commit when the scan list includes nearby scans', () => { |
| 36 | + expect( |
| 37 | + findBaselineScan( |
| 38 | + [{ ...scan, id: 'newer', commit_hash: 'newer-sha' }, scan], |
| 39 | + 'base-sha', |
| 40 | + ), |
| 41 | + ).toEqual(scan) |
| 42 | + }) |
| 43 | + |
| 44 | + it('selects an exact commit scan from the matching repo and branch query', async () => { |
| 45 | + vi.mocked(fetchOrgFullScanList).mockResolvedValue({ |
| 46 | + ok: true, |
| 47 | + data: { results: [scan] }, |
| 48 | + }) |
| 49 | + await expect( |
| 50 | + selectBaselineScan({ |
| 51 | + branch: 'main', |
| 52 | + commitHash: 'base-sha', |
| 53 | + orgSlug: 'test-org', |
| 54 | + repo: 'test-repo', |
| 55 | + }), |
| 56 | + ).resolves.toEqual({ ok: true, data: scan }) |
| 57 | + expect(fetchOrgFullScanList).toHaveBeenCalledWith( |
| 58 | + expect.objectContaining({ |
| 59 | + branch: 'main', |
| 60 | + commitHash: 'base-sha', |
| 61 | + perPage: 1, |
| 62 | + repo: 'test-repo', |
| 63 | + }), |
| 64 | + ) |
| 65 | + }) |
| 66 | + |
| 67 | + it('fails without a commit SHA', async () => { |
| 68 | + await expect( |
| 69 | + selectBaselineScan({ |
| 70 | + branch: 'main', |
| 71 | + commitHash: '', |
| 72 | + orgSlug: 'test-org', |
| 73 | + repo: 'test-repo', |
| 74 | + }), |
| 75 | + ).resolves.toMatchObject({ ok: false }) |
| 76 | + expect(fetchOrgFullScanList).not.toHaveBeenCalled() |
| 77 | + }) |
| 78 | + |
| 79 | + it('fails closed when the exact commit has no scan', async () => { |
| 80 | + vi.mocked(fetchOrgFullScanList).mockResolvedValue({ |
| 81 | + ok: true, |
| 82 | + data: { results: [scan] }, |
| 83 | + }) |
| 84 | + await expect( |
| 85 | + selectBaselineScan({ |
| 86 | + branch: 'main', |
| 87 | + commitHash: 'unknown-sha', |
| 88 | + orgSlug: 'test-org', |
| 89 | + repo: 'test-repo', |
| 90 | + }), |
| 91 | + ).resolves.toMatchObject({ |
| 92 | + ok: false, |
| 93 | + message: 'No matching PR scan baseline found', |
| 94 | + }) |
| 95 | + }) |
| 96 | +}) |
0 commit comments