import { describe, expect, it } from "vitest"; import { getNearestPageNumber } from "../src/preview-page"; const pages = [ { top: 0, bottom: 100 }, { top: 124, bottom: 224 }, { top: 248, bottom: 348 } ]; describe("getNearestPageNumber", () => { it("返回视口中心所在的页面", () => { expect(getNearestPageNumber(180, pages)).toBe(2); }); it("在页间空隙中返回距离最近的页面", () => { expect(getNearestPageNumber(110, pages)).toBe(1); expect(getNearestPageNumber(116, pages)).toBe(2); }); it("没有页面或中心坐标无效时返回 0", () => { expect(getNearestPageNumber(20, [])).toBe(0); expect(getNearestPageNumber(Number.NaN, pages)).toBe(0); }); });