|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import axios from "axios"; |
| 3 | +import * as reporter from "./reporter"; |
| 4 | +import { UserInputError } from "./user-input-error"; |
| 5 | + |
| 6 | +vi.mock("@actions/core", () => ({ |
| 7 | + debug: vi.fn(), |
| 8 | + warning: vi.fn(), |
| 9 | + info: vi.fn(), |
| 10 | + error: vi.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock("axios", () => ({ |
| 14 | + default: { |
| 15 | + create: vi.fn(), |
| 16 | + }, |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock("axios-retry", () => { |
| 20 | + const axiosRetry = vi.fn() as unknown as { |
| 21 | + (): void; |
| 22 | + exponentialDelay: unknown; |
| 23 | + isNetworkOrIdempotentRequestError: unknown; |
| 24 | + }; |
| 25 | + axiosRetry.exponentialDelay = vi.fn(); |
| 26 | + axiosRetry.isNetworkOrIdempotentRequestError = vi.fn(); |
| 27 | + return { default: axiosRetry }; |
| 28 | +}); |
| 29 | + |
| 30 | +describe("reportBuildPushActionFailure", () => { |
| 31 | + beforeEach(() => { |
| 32 | + vi.clearAllMocks(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("does not report user input errors to the backend", async () => { |
| 36 | + await reporter.reportBuildPushActionFailure( |
| 37 | + "STICKYDISK_SETUP", |
| 38 | + new UserInputError("The 'cache-key' input is required"), |
| 39 | + "sticky disk setup", |
| 40 | + ); |
| 41 | + |
| 42 | + expect(axios.create).not.toHaveBeenCalled(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("reports other errors to the backend", async () => { |
| 46 | + const post = vi.fn().mockResolvedValue({ data: { status: "recorded" } }); |
| 47 | + vi.mocked(axios.create).mockReturnValue({ post } as never); |
| 48 | + |
| 49 | + await reporter.reportBuildPushActionFailure( |
| 50 | + "STICKYDISK_SETUP", |
| 51 | + new Error("qemu-nbd timed out"), |
| 52 | + "sticky disk setup", |
| 53 | + ); |
| 54 | + |
| 55 | + expect(post).toHaveBeenCalledWith( |
| 56 | + "/stickydisks/report-failed", |
| 57 | + expect.objectContaining({ |
| 58 | + type: "STICKYDISK_SETUP", |
| 59 | + message: "sticky disk setup: qemu-nbd timed out", |
| 60 | + }), |
| 61 | + ); |
| 62 | + }); |
| 63 | +}); |
0 commit comments