|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
| 2 | +import { setupMockServer } from "../mocks/server"; |
| 3 | +import { finalize } from "./finalize"; |
| 4 | + |
| 5 | +setupMockServer(); |
| 6 | + |
| 7 | +describe("#finalize", () => { |
| 8 | + beforeEach(() => { |
| 9 | + // Neutralize CI detection so the tests behave the same locally and on |
| 10 | + // GitHub Actions, where a nonce would be derived from the run |
| 11 | + // automatically. |
| 12 | + vi.stubEnv("GITHUB_ACTIONS", ""); |
| 13 | + vi.stubEnv("ARGOS_COMMIT", "f16f980bd17cccfa93a1ae7766727e67950773d0"); |
| 14 | + vi.stubEnv("ARGOS_BRANCH", "main"); |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + vi.unstubAllEnvs(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("finalizes builds matching the nonce", async () => { |
| 22 | + const result = await finalize({ |
| 23 | + apiBaseUrl: "https://api.argos-ci.dev", |
| 24 | + token: "92d832e0d22ab113c8979d73a87a11130eaa24a9", |
| 25 | + parallel: { nonce: "nonce-123" }, |
| 26 | + }); |
| 27 | + |
| 28 | + expect(result).toEqual({ |
| 29 | + builds: [{ id: "456", url: "https://app.argos-ci.dev/builds/456" }], |
| 30 | + skippedBuild: null, |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + it("returns no builds when the nonce matches nothing", async () => { |
| 35 | + const result = await finalize({ |
| 36 | + apiBaseUrl: "https://api.argos-ci.dev", |
| 37 | + token: "92d832e0d22ab113c8979d73a87a11130eaa24a9", |
| 38 | + parallel: { nonce: "empty" }, |
| 39 | + }); |
| 40 | + |
| 41 | + expect(result).toEqual({ builds: [], skippedBuild: null }); |
| 42 | + }); |
| 43 | + |
| 44 | + it("throws without a nonce", async () => { |
| 45 | + await expect( |
| 46 | + finalize({ |
| 47 | + apiBaseUrl: "https://api.argos-ci.dev", |
| 48 | + token: "92d832e0d22ab113c8979d73a87a11130eaa24a9", |
| 49 | + }), |
| 50 | + ).rejects.toThrow("parallel.nonce is required to finalize the build"); |
| 51 | + }); |
| 52 | + |
| 53 | + describe("with skipIfEmpty", () => { |
| 54 | + it("creates a skipped build when the nonce matches nothing", async () => { |
| 55 | + const result = await finalize({ |
| 56 | + apiBaseUrl: "https://api.argos-ci.dev", |
| 57 | + token: "92d832e0d22ab113c8979d73a87a11130eaa24a9", |
| 58 | + parallel: { nonce: "empty" }, |
| 59 | + skipIfEmpty: true, |
| 60 | + buildName: "unit", |
| 61 | + }); |
| 62 | + |
| 63 | + expect(result).toEqual({ |
| 64 | + builds: [], |
| 65 | + skippedBuild: { |
| 66 | + id: "123", |
| 67 | + url: "https://app.argos-ci.dev/builds/123", |
| 68 | + }, |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it("does not create a skipped build when builds are finalized", async () => { |
| 73 | + const result = await finalize({ |
| 74 | + apiBaseUrl: "https://api.argos-ci.dev", |
| 75 | + token: "92d832e0d22ab113c8979d73a87a11130eaa24a9", |
| 76 | + parallel: { nonce: "nonce-123" }, |
| 77 | + skipIfEmpty: true, |
| 78 | + buildName: "unit", |
| 79 | + }); |
| 80 | + |
| 81 | + expect(result).toEqual({ |
| 82 | + builds: [{ id: "456", url: "https://app.argos-ci.dev/builds/456" }], |
| 83 | + skippedBuild: null, |
| 84 | + }); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments