|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
| 2 | +// @ts-expect-error - plain CJS module, no type declarations |
| 3 | +import changelog from "./changelog.cjs"; |
| 4 | + |
| 5 | +const { withFallback } = changelog; |
| 6 | + |
| 7 | +// The wrapper delegates getReleaseLine / getDependencyReleaseLine to |
| 8 | +// `withFallback`, which holds the retry-then-fall-back logic. We test that logic |
| 9 | +// directly with injected fakes rather than mocking @changesets/changelog-github |
| 10 | +// (vi.mock cannot intercept the require() inside the .cjs module). |
| 11 | +describe("changelog withFallback", () => { |
| 12 | + beforeEach(() => { |
| 13 | + // Remove retry delays so the failing-path tests run instantly. vi.stubEnv + |
| 14 | + // vi.unstubAllEnvs keeps these mutations from leaking out of the file. |
| 15 | + vi.stubEnv("CHANGELOG_GITHUB_RETRY_MS", "0"); |
| 16 | + vi.stubEnv("CHANGELOG_GITHUB_ATTEMPTS", undefined); |
| 17 | + vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + vi.unstubAllEnvs(); |
| 22 | + vi.restoreAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + it("uses the GitHub generator when it succeeds (no fallback)", async () => { |
| 26 | + const githubFn = vi.fn().mockResolvedValue("- GH line"); |
| 27 | + const gitFn = vi.fn().mockResolvedValue("- git line"); |
| 28 | + |
| 29 | + const result = await withFallback("release line", githubFn, gitFn); |
| 30 | + |
| 31 | + expect(result).toBe("- GH line"); |
| 32 | + expect(githubFn).toHaveBeenCalledTimes(1); |
| 33 | + expect(gitFn).not.toHaveBeenCalled(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("retries the GitHub generator and keeps its result on recovery", async () => { |
| 37 | + const githubFn = vi |
| 38 | + .fn() |
| 39 | + .mockRejectedValueOnce(new Error("Premature close")) |
| 40 | + .mockResolvedValue("- GH line"); |
| 41 | + const gitFn = vi.fn().mockResolvedValue("- git line"); |
| 42 | + |
| 43 | + const result = await withFallback("release line", githubFn, gitFn); |
| 44 | + |
| 45 | + expect(result).toBe("- GH line"); |
| 46 | + expect(githubFn).toHaveBeenCalledTimes(2); |
| 47 | + expect(gitFn).not.toHaveBeenCalled(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("falls back to the git generator after GitHub keeps failing", async () => { |
| 51 | + const githubFn = vi.fn().mockRejectedValue(new Error("Premature close")); |
| 52 | + const gitFn = vi.fn().mockResolvedValue("- abc1234: git line"); |
| 53 | + |
| 54 | + const result = await withFallback("release line", githubFn, gitFn); |
| 55 | + |
| 56 | + expect(result).toBe("- abc1234: git line"); |
| 57 | + expect(githubFn).toHaveBeenCalledTimes(3); // default maxAttempts |
| 58 | + expect(gitFn).toHaveBeenCalledTimes(1); |
| 59 | + }); |
| 60 | + |
| 61 | + it("falls back without throwing when GitHub rejects with a non-Error value", async () => { |
| 62 | + // A non-Error rejection must not make `.message` throw inside the catch. |
| 63 | + const githubFn = vi.fn().mockRejectedValue(undefined); |
| 64 | + const gitFn = vi.fn().mockResolvedValue("- git line"); |
| 65 | + |
| 66 | + const result = await withFallback("release line", githubFn, gitFn); |
| 67 | + |
| 68 | + expect(result).toBe("- git line"); |
| 69 | + expect(githubFn).toHaveBeenCalledTimes(3); |
| 70 | + expect(gitFn).toHaveBeenCalledTimes(1); |
| 71 | + }); |
| 72 | + |
| 73 | + it("honours a custom attempt count via CHANGELOG_GITHUB_ATTEMPTS", async () => { |
| 74 | + vi.stubEnv("CHANGELOG_GITHUB_ATTEMPTS", "5"); |
| 75 | + const githubFn = vi.fn().mockRejectedValue(new Error("Premature close")); |
| 76 | + const gitFn = vi.fn().mockResolvedValue("- git line"); |
| 77 | + |
| 78 | + await withFallback("release line", githubFn, gitFn); |
| 79 | + |
| 80 | + expect(githubFn).toHaveBeenCalledTimes(5); |
| 81 | + expect(gitFn).toHaveBeenCalledTimes(1); |
| 82 | + }); |
| 83 | + |
| 84 | + it("ignores an invalid attempt-count override and uses the default", async () => { |
| 85 | + vi.stubEnv("CHANGELOG_GITHUB_ATTEMPTS", "-1"); |
| 86 | + const githubFn = vi.fn().mockRejectedValue(new Error("Premature close")); |
| 87 | + const gitFn = vi.fn().mockResolvedValue("- git line"); |
| 88 | + |
| 89 | + await withFallback("release line", githubFn, gitFn); |
| 90 | + |
| 91 | + expect(githubFn).toHaveBeenCalledTimes(3); // -1 ignored -> default 3 |
| 92 | + expect(gitFn).toHaveBeenCalledTimes(1); |
| 93 | + }); |
| 94 | + |
| 95 | + it("tolerates a non-numeric retry-delay override without producing NaN waits", async () => { |
| 96 | + vi.stubEnv("CHANGELOG_GITHUB_RETRY_MS", "not-a-number"); |
| 97 | + vi.stubEnv("CHANGELOG_GITHUB_ATTEMPTS", "1"); // 1 attempt -> no sleep path |
| 98 | + const githubFn = vi.fn().mockRejectedValue(new Error("Premature close")); |
| 99 | + const gitFn = vi.fn().mockResolvedValue("- git line"); |
| 100 | + |
| 101 | + const result = await withFallback("release line", githubFn, gitFn); |
| 102 | + |
| 103 | + expect(result).toBe("- git line"); |
| 104 | + expect(githubFn).toHaveBeenCalledTimes(1); |
| 105 | + }); |
| 106 | + |
| 107 | + it("exposes getReleaseLine and getDependencyReleaseLine for changesets", () => { |
| 108 | + expect(typeof changelog.getReleaseLine).toBe("function"); |
| 109 | + expect(typeof changelog.getDependencyReleaseLine).toBe("function"); |
| 110 | + }); |
| 111 | +}); |
0 commit comments