|
| 1 | +import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest' |
| 2 | + |
| 3 | +import type {PagesDeployment} from '@/common/cloudflare/types.js' |
| 4 | +import type {MockApi} from '@/tests/helpers/api.js' |
| 5 | + |
| 6 | +import {statusCloudflareDeployment} from '@/common/cloudflare/deployment/status.js' |
| 7 | +import {sleep} from '@/common/utils.js' |
| 8 | +import RESPONSE_DEPLOYMENTS_IDLE from '@/responses/api.cloudflare.com/pages/deployments/deployments.idle.response.json' with {type: 'json'} |
| 9 | +import RESPONSE_DEPLOYMENTS from '@/responses/api.cloudflare.com/pages/deployments/deployments.response.json' with {type: 'json'} |
| 10 | +import { |
| 11 | + MOCK_ACCOUNT_ID, |
| 12 | + MOCK_API_PATH_DEPLOYMENTS, |
| 13 | + MOCK_PROJECT_NAME, |
| 14 | + setMockApi |
| 15 | +} from '@/tests/helpers/api.js' |
| 16 | + |
| 17 | +vi.mock(import('@/common/utils.js')) |
| 18 | +vi.mock(import('@actions/core')) |
| 19 | + |
| 20 | +const API_ENDPOINT = { |
| 21 | + accountId: MOCK_ACCOUNT_ID, |
| 22 | + projectName: MOCK_PROJECT_NAME |
| 23 | +} |
| 24 | + |
| 25 | +type LatestStage = PagesDeployment['latest_stage'] |
| 26 | + |
| 27 | +const withLatestStage = ( |
| 28 | + name: LatestStage['name'], |
| 29 | + status: LatestStage['status'] |
| 30 | +): typeof RESPONSE_DEPLOYMENTS => ({ |
| 31 | + ...RESPONSE_DEPLOYMENTS, |
| 32 | + result: RESPONSE_DEPLOYMENTS.result.map((deployment, index) => |
| 33 | + index === 0 |
| 34 | + ? { |
| 35 | + ...deployment, |
| 36 | + latest_stage: { |
| 37 | + name, |
| 38 | + status, |
| 39 | + started_on: null, |
| 40 | + ended_on: null |
| 41 | + } as LatestStage |
| 42 | + } |
| 43 | + : deployment |
| 44 | + ) as (typeof RESPONSE_DEPLOYMENTS)['result'] |
| 45 | +}) |
| 46 | + |
| 47 | +describe(statusCloudflareDeployment, () => { |
| 48 | + let mockApi: MockApi |
| 49 | + |
| 50 | + beforeEach(() => { |
| 51 | + mockApi = setMockApi() |
| 52 | + }) |
| 53 | + |
| 54 | + afterEach(async () => { |
| 55 | + mockApi.mockAgent.assertNoPendingInterceptors() |
| 56 | + await mockApi.mockAgent.close() |
| 57 | + }) |
| 58 | + |
| 59 | + test('returns success when deploy stage succeeds', async () => { |
| 60 | + expect.assertions(3) |
| 61 | + |
| 62 | + mockApi.interceptCloudflare(MOCK_API_PATH_DEPLOYMENTS, RESPONSE_DEPLOYMENTS) |
| 63 | + |
| 64 | + const {deployment, status} = await statusCloudflareDeployment(API_ENDPOINT) |
| 65 | + |
| 66 | + expect(status).toBe('success') |
| 67 | + expect(deployment.id).toMatchInlineSnapshot( |
| 68 | + `"206e215c-33b3-4ce4-adf4-7fc6c9b65483"` |
| 69 | + ) |
| 70 | + expect(vi.mocked(sleep)).not.toHaveBeenCalled() |
| 71 | + }) |
| 72 | + |
| 73 | + test('polls until deploy stage succeeds', async () => { |
| 74 | + expect.assertions(2) |
| 75 | + |
| 76 | + mockApi |
| 77 | + .interceptCloudflare(MOCK_API_PATH_DEPLOYMENTS, RESPONSE_DEPLOYMENTS_IDLE) |
| 78 | + .times(2) |
| 79 | + mockApi.interceptCloudflare(MOCK_API_PATH_DEPLOYMENTS, RESPONSE_DEPLOYMENTS) |
| 80 | + |
| 81 | + const {status} = await statusCloudflareDeployment(API_ENDPOINT) |
| 82 | + |
| 83 | + expect(status).toBe('success') |
| 84 | + expect(vi.mocked(sleep)).toHaveBeenCalledTimes(2) |
| 85 | + }) |
| 86 | + |
| 87 | + test.for([ |
| 88 | + {stage: 'build', status: 'failure'}, |
| 89 | + {stage: 'build', status: 'canceled'}, |
| 90 | + {stage: 'deploy', status: 'active'} |
| 91 | + ] satisfies {stage: LatestStage['name']; status: LatestStage['status']}[])( |
| 92 | + 'returns $status immediately without polling ($stage stage)', |
| 93 | + async ({stage, status}) => { |
| 94 | + expect.assertions(2) |
| 95 | + |
| 96 | + mockApi.interceptCloudflare( |
| 97 | + MOCK_API_PATH_DEPLOYMENTS, |
| 98 | + withLatestStage(stage, status) |
| 99 | + ) |
| 100 | + |
| 101 | + const result = await statusCloudflareDeployment(API_ENDPOINT) |
| 102 | + |
| 103 | + expect(result.status).toBe(status) |
| 104 | + expect(vi.mocked(sleep)).not.toHaveBeenCalled() |
| 105 | + } |
| 106 | + ) |
| 107 | + |
| 108 | + test('polls while a non-deploy stage is active', async () => { |
| 109 | + expect.assertions(2) |
| 110 | + |
| 111 | + mockApi.interceptCloudflare( |
| 112 | + MOCK_API_PATH_DEPLOYMENTS, |
| 113 | + withLatestStage('build', 'active') |
| 114 | + ) |
| 115 | + mockApi.interceptCloudflare(MOCK_API_PATH_DEPLOYMENTS, RESPONSE_DEPLOYMENTS) |
| 116 | + |
| 117 | + const {status} = await statusCloudflareDeployment(API_ENDPOINT) |
| 118 | + |
| 119 | + expect(status).toBe('success') |
| 120 | + expect(vi.mocked(sleep)).toHaveBeenCalledTimes(1) |
| 121 | + }) |
| 122 | + |
| 123 | + test('throws when the api returns an error', async () => { |
| 124 | + expect.assertions(1) |
| 125 | + |
| 126 | + mockApi.interceptCloudflare( |
| 127 | + MOCK_API_PATH_DEPLOYMENTS, |
| 128 | + {result: null, success: false, errors: [], messages: []}, |
| 129 | + 404 |
| 130 | + ) |
| 131 | + |
| 132 | + await expect( |
| 133 | + statusCloudflareDeployment(API_ENDPOINT) |
| 134 | + ).rejects.toThrowErrorMatchingInlineSnapshot( |
| 135 | + `[ParseError: A request to the Cloudflare API (https://api.cloudflare.com/client/v4/accounts/mock-cloudflare-account-id/pages/projects/mock-cloudflare-project-name/deployments) failed.]` |
| 136 | + ) |
| 137 | + }) |
| 138 | +}) |
0 commit comments