|
| 1 | +import { describe, expect, it } from 'bun:test'; |
| 2 | +import { appAvailabilityCheck } from '../checks/app-availability'; |
| 3 | +import type { CheckContext, CheckVariableValues } from '../../../types'; |
| 4 | +import type { |
| 5 | + VercelDeployment, |
| 6 | + VercelDeploymentsResponse, |
| 7 | + VercelProject, |
| 8 | + VercelProjectsResponse, |
| 9 | +} from '../types'; |
| 10 | + |
| 11 | +const makeProject = (id: string, name: string): VercelProject => ({ |
| 12 | + id, |
| 13 | + name, |
| 14 | + accountId: 'acc_1', |
| 15 | + createdAt: 0, |
| 16 | + updatedAt: 0, |
| 17 | +}); |
| 18 | + |
| 19 | +const makeReadyDeployment = (): VercelDeployment => ({ |
| 20 | + uid: 'dpl_1', |
| 21 | + name: 'd', |
| 22 | + url: 'd.vercel.app', |
| 23 | + state: 'READY', |
| 24 | + type: 'LAMBDAS', |
| 25 | + created: Date.now(), |
| 26 | + createdAt: Date.now(), |
| 27 | + creator: { uid: 'u' }, |
| 28 | +}); |
| 29 | + |
| 30 | +interface RunResult { |
| 31 | + passedResourceIds: string[]; |
| 32 | + failedResourceIds: string[]; |
| 33 | + checkedProjectIds: string[]; |
| 34 | +} |
| 35 | + |
| 36 | +async function runWithVariables( |
| 37 | + projects: VercelProject[], |
| 38 | + variables: CheckVariableValues | undefined, |
| 39 | +): Promise<RunResult> { |
| 40 | + const checkedProjectIds: string[] = []; |
| 41 | + const passed: string[] = []; |
| 42 | + const failed: string[] = []; |
| 43 | + |
| 44 | + const ctx: CheckContext = { |
| 45 | + accessToken: 'tok', |
| 46 | + credentials: {}, |
| 47 | + variables, |
| 48 | + connectionId: 'conn_1', |
| 49 | + organizationId: 'org_1', |
| 50 | + metadata: { oauth: { team: { id: 'team_1', name: 'Team' } } }, |
| 51 | + log: () => {}, |
| 52 | + pass: (result) => { |
| 53 | + passed.push(result.resourceId); |
| 54 | + }, |
| 55 | + fail: (result) => { |
| 56 | + failed.push(result.resourceId); |
| 57 | + }, |
| 58 | + fetch: (async <T,>(path: string): Promise<T> => { |
| 59 | + if (path === '/v9/projects?teamId=team_1' || path === '/v9/projects') { |
| 60 | + return { projects } satisfies VercelProjectsResponse as unknown as T; |
| 61 | + } |
| 62 | + if (path.startsWith('/v6/deployments')) { |
| 63 | + const url = new URL(path, 'https://api.vercel.com'); |
| 64 | + const projectId = url.searchParams.get('projectId') ?? ''; |
| 65 | + checkedProjectIds.push(projectId); |
| 66 | + return { |
| 67 | + deployments: [makeReadyDeployment()], |
| 68 | + } satisfies VercelDeploymentsResponse as unknown as T; |
| 69 | + } |
| 70 | + throw new Error(`Unexpected fetch: ${path}`); |
| 71 | + }) as CheckContext['fetch'], |
| 72 | + fetchAllPages: (async () => []) as CheckContext['fetchAllPages'], |
| 73 | + graphql: (async () => ({})) as CheckContext['graphql'], |
| 74 | + } as CheckContext; |
| 75 | + |
| 76 | + await appAvailabilityCheck.run(ctx); |
| 77 | + return { passedResourceIds: passed, failedResourceIds: failed, checkedProjectIds }; |
| 78 | +} |
| 79 | + |
| 80 | +describe('appAvailabilityCheck filter behaviour', () => { |
| 81 | + const projects = [ |
| 82 | + makeProject('prj_a', 'a'), |
| 83 | + makeProject('prj_b', 'b'), |
| 84 | + makeProject('prj_c', 'c'), |
| 85 | + ]; |
| 86 | + |
| 87 | + it('checks all projects when no filter is configured', async () => { |
| 88 | + const result = await runWithVariables(projects, undefined); |
| 89 | + expect(result.checkedProjectIds.sort()).toEqual(['prj_a', 'prj_b', 'prj_c']); |
| 90 | + }); |
| 91 | + |
| 92 | + it('checks all projects when mode is "all" with a selection', async () => { |
| 93 | + const result = await runWithVariables(projects, { |
| 94 | + project_filter_mode: 'all', |
| 95 | + filtered_projects: ['prj_a'], |
| 96 | + }); |
| 97 | + expect(result.checkedProjectIds.sort()).toEqual(['prj_a', 'prj_b', 'prj_c']); |
| 98 | + }); |
| 99 | + |
| 100 | + it('checks only selected projects in include mode', async () => { |
| 101 | + const result = await runWithVariables(projects, { |
| 102 | + project_filter_mode: 'include', |
| 103 | + filtered_projects: ['prj_a', 'prj_c'], |
| 104 | + }); |
| 105 | + expect(result.checkedProjectIds.sort()).toEqual(['prj_a', 'prj_c']); |
| 106 | + }); |
| 107 | + |
| 108 | + it('skips selected projects in exclude mode', async () => { |
| 109 | + const result = await runWithVariables(projects, { |
| 110 | + project_filter_mode: 'exclude', |
| 111 | + filtered_projects: ['prj_b'], |
| 112 | + }); |
| 113 | + expect(result.checkedProjectIds.sort()).toEqual(['prj_a', 'prj_c']); |
| 114 | + }); |
| 115 | + |
| 116 | + it('falls back to all projects when include mode has no selection', async () => { |
| 117 | + const result = await runWithVariables(projects, { |
| 118 | + project_filter_mode: 'include', |
| 119 | + filtered_projects: [], |
| 120 | + }); |
| 121 | + expect(result.checkedProjectIds.sort()).toEqual(['prj_a', 'prj_b', 'prj_c']); |
| 122 | + }); |
| 123 | + |
| 124 | + it('emits a filter-applied evidence pass recording the active mode', async () => { |
| 125 | + const result = await runWithVariables(projects, { |
| 126 | + project_filter_mode: 'exclude', |
| 127 | + filtered_projects: ['prj_b'], |
| 128 | + }); |
| 129 | + expect(result.passedResourceIds).toContain('project-filter'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('does not emit a filter-applied pass when no projects are returned', async () => { |
| 133 | + const result = await runWithVariables([], { |
| 134 | + project_filter_mode: 'exclude', |
| 135 | + filtered_projects: ['prj_anything'], |
| 136 | + }); |
| 137 | + expect(result.passedResourceIds).not.toContain('project-filter'); |
| 138 | + expect(result.failedResourceIds).toContain('projects'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('fails when filter resolves to zero scoped projects', async () => { |
| 142 | + const result = await runWithVariables(projects, { |
| 143 | + project_filter_mode: 'include', |
| 144 | + filtered_projects: ['prj_does_not_exist'], |
| 145 | + }); |
| 146 | + expect(result.failedResourceIds).toContain('project-filter'); |
| 147 | + expect(result.checkedProjectIds).toEqual([]); |
| 148 | + expect(result.passedResourceIds).not.toContain('project-filter'); |
| 149 | + }); |
| 150 | +}); |
0 commit comments