import assert from "node:assert/strict"
import { describe, it } from "node:test"
import { buildHorizontalRulerTicks } from "@/lib/drive/docs-ruler-math"
import { getRulerUnitConfig, resolveRulerUnit, formatMarginDistanceLabel } from "@/lib/drive/docs-ruler-units"
import { MM_TO_PX } from "@/lib/drive/page-formats"

describe("docs ruler units", () => {
  it("uses cm for A4 and inches for Letter", () => {
    assert.equal(resolveRulerUnit("a4"), "cm")
    assert.equal(resolveRulerUnit("a5"), "cm")
    assert.equal(resolveRulerUnit("letter"), "inch")
    assert.equal(resolveRulerUnit("legal"), "inch")
    assert.equal(resolveRulerUnit("tabloid"), "inch")
  })

  it("builds cm ticks for A4 width", () => {
    const widthPx = Math.round(210 * MM_TO_PX)
    const ticks = buildHorizontalRulerTicks(widthPx, "a4")
    const majors = ticks.filter((t) => t.major && t.label)
    assert.equal(majors[0]?.label, "1")
    assert.equal(majors.at(-1)?.label, "21")
    assert.equal(getRulerUnitConfig("a4").majorStepPx, MM_TO_PX * 10)
  })

  it("builds inch ticks for Letter width", () => {
    const widthPx = Math.round(216 * MM_TO_PX)
    const ticks = buildHorizontalRulerTicks(widthPx, "letter")
    const majors = ticks.filter((t) => t.major && t.label)
    assert.equal(majors[0]?.label, "1")
    assert.equal(majors.length, 8)
  })

  it("formats margin tooltip in cm for A4", () => {
    const cmStep = MM_TO_PX * 10
    assert.equal(formatMarginDistanceLabel(cmStep * 2.5, "a4"), "2,5 cm")
    assert.equal(formatMarginDistanceLabel(cmStep * 2, "a4"), "2 cm")
  })

  it("formats margin tooltip in inches for Letter", () => {
    const inchPx = MM_TO_PX * 25.4
    assert.equal(formatMarginDistanceLabel(inchPx * 1.5, "letter"), '1.5"')
  })
})
