|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { CronJob } from "../../cron/types.js"; |
| 3 | +import type { GatewayRpcOpts } from "../gateway-rpc.js"; |
| 4 | + |
| 5 | +const callGatewayFromCli = vi.fn(); |
| 6 | + |
| 7 | +vi.mock("../gateway-rpc.js", async () => { |
| 8 | + const actual = await vi.importActual<typeof import("../gateway-rpc.js")>("../gateway-rpc.js"); |
| 9 | + return { |
| 10 | + ...actual, |
| 11 | + callGatewayFromCli: (...args: Parameters<typeof actual.callGatewayFromCli>) => |
| 12 | + callGatewayFromCli(...args), |
| 13 | + }; |
| 14 | +}); |
| 15 | + |
| 16 | +const { loadCronJobForShow } = await import("./register.cron-simple.js"); |
| 17 | + |
| 18 | +const opts: GatewayRpcOpts = {} as GatewayRpcOpts; |
| 19 | + |
| 20 | +describe("loadCronJobForShow pagination guard (regression for #83856)", () => { |
| 21 | + beforeEach(() => { |
| 22 | + callGatewayFromCli.mockReset(); |
| 23 | + }); |
| 24 | + afterEach(() => { |
| 25 | + vi.restoreAllMocks(); |
| 26 | + }); |
| 27 | + |
| 28 | + it("throws when nextOffset fails to advance", async () => { |
| 29 | + callGatewayFromCli.mockResolvedValue({ |
| 30 | + jobs: [], |
| 31 | + hasMore: true, |
| 32 | + nextOffset: 0, |
| 33 | + }); |
| 34 | + await expect(loadCronJobForShow(opts, "missing")).rejects.toThrow(/pagination did not advance/); |
| 35 | + expect(callGatewayFromCli).toHaveBeenCalledTimes(1); |
| 36 | + }); |
| 37 | + |
| 38 | + it("throws when pagination exceeds the max page count", async () => { |
| 39 | + let nextOffset = 0; |
| 40 | + callGatewayFromCli.mockImplementation(async () => { |
| 41 | + nextOffset += 1; |
| 42 | + return { jobs: [], hasMore: true, nextOffset }; |
| 43 | + }); |
| 44 | + await expect(loadCronJobForShow(opts, "missing")).rejects.toThrow( |
| 45 | + /pagination exceeded maximum pages/, |
| 46 | + ); |
| 47 | + expect(callGatewayFromCli.mock.calls.length).toBeGreaterThan(1); |
| 48 | + expect(callGatewayFromCli.mock.calls.length).toBeLessThanOrEqual(50); |
| 49 | + }); |
| 50 | + |
| 51 | + it("returns the job when found on a later page", async () => { |
| 52 | + const job: CronJob = { id: "abc", name: "wanted" } as unknown as CronJob; |
| 53 | + callGatewayFromCli |
| 54 | + .mockResolvedValueOnce({ jobs: [], hasMore: true, nextOffset: 200 }) |
| 55 | + .mockResolvedValueOnce({ jobs: [job], hasMore: false, nextOffset: null }); |
| 56 | + const result = await loadCronJobForShow(opts, "wanted"); |
| 57 | + expect(result.job?.id).toBe("abc"); |
| 58 | + expect(callGatewayFromCli).toHaveBeenCalledTimes(2); |
| 59 | + }); |
| 60 | + |
| 61 | + it("returns empty result when pagination terminates without a match", async () => { |
| 62 | + callGatewayFromCli.mockResolvedValueOnce({ |
| 63 | + jobs: [], |
| 64 | + hasMore: false, |
| 65 | + nextOffset: null, |
| 66 | + }); |
| 67 | + const result = await loadCronJobForShow(opts, "missing"); |
| 68 | + expect(result.job).toBeUndefined(); |
| 69 | + }); |
| 70 | +}); |
0 commit comments