|
| 1 | +import type { ProductionCommandResult } from '../../scripts/lib/production-deploy' |
| 2 | +import { describe, expect, it, vi } from 'vitest' |
| 3 | +import { isTransientCloudflareFailure, withTransientRetry } from '../../scripts/lib/cloudflare-retry' |
| 4 | + |
| 5 | +// The Deploy Artifact signer step on 2026-10-07, run 37562888913 attempt 1. |
| 6 | +const UNKNOWN_ERROR = [ |
| 7 | + '✘ [ERROR] A request to the Cloudflare API (/accounts/abc/workers/scripts/skilld-artifact-signer/deployments) failed.', |
| 8 | + ' An unknown error has occurred. If this error persists, please file a report in workers-sdk (https://github.com/cloudflare/workers-sdk/issues) or reach out to your account team. [code: 10013]', |
| 9 | +].join('\n') |
| 10 | +const AUTH_ERROR = '✘ [ERROR] A request to the Cloudflare API (/accounts/abc/workers/scripts/x) failed.\n Authentication error [code: 10000]' |
| 11 | + |
| 12 | +function failed(stderr: string): ProductionCommandResult { |
| 13 | + return { _tag: 'failed', stdout: '', stderr, exitCode: 1 } |
| 14 | +} |
| 15 | + |
| 16 | +const passed: ProductionCommandResult = { _tag: 'passed', stdout: 'Deployed', stderr: '' } |
| 17 | + |
| 18 | +describe('isTransientCloudflareFailure', () => { |
| 19 | + it('treats the Cloudflare API unknown error as transient', () => { |
| 20 | + expect(isTransientCloudflareFailure(UNKNOWN_ERROR)).toBe(true) |
| 21 | + }) |
| 22 | + |
| 23 | + it('treats a gateway error from the Cloudflare API as transient', () => { |
| 24 | + expect(isTransientCloudflareFailure('A request to the Cloudflare API (/accounts/abc/workers/scripts/x) failed.\n 503 Service Unavailable')).toBe(true) |
| 25 | + }) |
| 26 | + |
| 27 | + it('never retries an authentication or configuration error', () => { |
| 28 | + expect(isTransientCloudflareFailure(AUTH_ERROR)).toBe(false) |
| 29 | + expect(isTransientCloudflareFailure('✘ [ERROR] Missing entry-point to Worker script or to assets directory')).toBe(false) |
| 30 | + }) |
| 31 | +}) |
| 32 | + |
| 33 | +describe('withTransientRetry', () => { |
| 34 | + it('retries a transient failure and returns the passing result', async () => { |
| 35 | + const command = vi.fn() |
| 36 | + .mockResolvedValueOnce(failed(UNKNOWN_ERROR)) |
| 37 | + .mockResolvedValueOnce(passed) |
| 38 | + const wait = vi.fn().mockResolvedValue(undefined) |
| 39 | + const retrying = withTransientRetry(command, { wait, log: () => {} }) |
| 40 | + |
| 41 | + await expect(retrying(['deploy'])).resolves.toEqual(passed) |
| 42 | + expect(command).toHaveBeenCalledTimes(2) |
| 43 | + expect(wait).toHaveBeenCalledOnce() |
| 44 | + }) |
| 45 | + |
| 46 | + it('returns a non-transient failure without a retry', async () => { |
| 47 | + const command = vi.fn().mockResolvedValue(failed(AUTH_ERROR)) |
| 48 | + const wait = vi.fn().mockResolvedValue(undefined) |
| 49 | + const retrying = withTransientRetry(command, { wait, log: () => {} }) |
| 50 | + |
| 51 | + await expect(retrying(['deploy'])).resolves.toEqual(failed(AUTH_ERROR)) |
| 52 | + expect(command).toHaveBeenCalledOnce() |
| 53 | + expect(wait).not.toHaveBeenCalled() |
| 54 | + }) |
| 55 | + |
| 56 | + it('stops after the last delay and returns the last failure', async () => { |
| 57 | + const command = vi.fn().mockResolvedValue(failed(UNKNOWN_ERROR)) |
| 58 | + const wait = vi.fn().mockResolvedValue(undefined) |
| 59 | + const retrying = withTransientRetry(command, { wait, log: () => {}, delaysMs: [10, 20] }) |
| 60 | + |
| 61 | + await expect(retrying(['deploy'])).resolves.toEqual(failed(UNKNOWN_ERROR)) |
| 62 | + expect(command).toHaveBeenCalledTimes(3) |
| 63 | + expect(wait.mock.calls).toEqual([[10], [20]]) |
| 64 | + }) |
| 65 | +}) |
0 commit comments