import { describe, it, expect } from "bun:test"
import { computeLineHash } from "./hash-computation"
import { parseLineRef, validateLineRef } from "./validation"

describe("parseLineRef", () => {
  it("parses valid LINE#ID reference", () => {
    //#given
    const ref = "42#VK"

    //#when
    const result = parseLineRef(ref)

    //#then
    expect(result).toEqual({ line: 42, hash: "VK" })
  })

  it("throws on invalid format", () => {
    //#given
    const ref = "42:VK"

    //#when / #then
    expect(() => parseLineRef(ref)).toThrow("LINE#ID")
  })

  it("accepts refs copied with markers and trailing content", () => {
    //#given
    const ref = ">>> 42#VK:const value = 1"

    //#when
    const result = parseLineRef(ref)

    //#then
    expect(result).toEqual({ line: 42, hash: "VK" })
  })
})

describe("validateLineRef", () => {
  it("accepts matching reference", () => {
    //#given
    const lines = ["function hello() {", "  return 42", "}"]
    const hash = computeLineHash(1, lines[0])

    //#when / #then
    expect(() => validateLineRef(lines, `1#${hash}`)).not.toThrow()
  })

  it("throws on mismatch and includes current hash", () => {
    //#given
    const lines = ["function hello() {"]

    //#when / #then
    expect(() => validateLineRef(lines, "1#ZZ")).toThrow(/current hash/)
  })
})
