import assert from "node:assert/strict"
import { describe, it } from "node:test"
import {
  computePageTopInViewport,
  resolveCurrentPageAtViewportTop,
  resolveCurrentPageInViewport,
} from "./docs-ruler-sync-math.ts"

describe("docs ruler vertical sync", () => {
  const stride = 1000
  const viewport = 800

  it("keeps page 0 while viewport center is on page 1", () => {
    assert.equal(resolveCurrentPageInViewport(48, viewport, stride, 3), 0)
    assert.equal(computePageTopInViewport(48, 0, stride), 48)
  })

  it("advances page when viewport center crosses page boundary", () => {
    // centerInStack = 400 - (-700) = 1100 → page 1
    assert.equal(resolveCurrentPageInViewport(-700, viewport, stride, 3), 1)
    assert.equal(computePageTopInViewport(-700, 1, stride), 300)

    // Old top-edge rule still on page 0 here
    assert.equal(resolveCurrentPageAtViewportTop(-700, stride, 3), 0)
  })

  it("switches before page 1 fully leaves viewport (vs top-edge rule)", () => {
    // centerInStack = 400 - (-750) = 1150 → page 1; page 1 bottom still visible
    assert.equal(resolveCurrentPageInViewport(-750, viewport, stride, 2), 1)
    assert.equal(resolveCurrentPageAtViewportTop(-750, stride, 2), 0)
  })

  it("clamps to last page", () => {
    assert.equal(resolveCurrentPageInViewport(-5000, viewport, stride, 3), 2)
    assert.equal(computePageTopInViewport(-5000, 2, stride), -3000)
  })
})
