import { describe, expect, it, vi } from "vitest"; import { fetchAllCursorPages } from "./parse.ts"; describe("fetchAllCursorPages", () => { it("keeps configuration rows beyond the first cursor page", async () => { const fetchPage = vi.fn(async (cursor?: string) => cursor ? { items: [201, 202], nextCursor: null, total: 202 } : { items: Array.from({ length: 200 }, (_, i) => i + 1), nextCursor: "page-2", total: 202 }, ); const result = await fetchAllCursorPages(fetchPage); expect(result.items).toHaveLength(202); expect(result.items.slice(-2)).toEqual([201, 202]); expect(result.total).toBe(202); expect(result.nextCursor).toBeNull(); expect(fetchPage.mock.calls).toEqual([[], ["page-2"]]); }); it("rejects a repeated cursor instead of looping indefinitely", async () => { const fetchPage = vi.fn(async () => ({ items: [1], nextCursor: "same" })); await expect(fetchAllCursorPages(fetchPage)).rejects.toThrow("分页游标重复"); expect(fetchPage).toHaveBeenCalledTimes(2); }); it("propagates a later page failure instead of returning a partial configuration", async () => { const failure = new Error("aborted"); await expect( fetchAllCursorPages(async (cursor) => { if (cursor) throw failure; return { items: [1], nextCursor: "next" }; }), ).rejects.toBe(failure); }); });