import { describe, expect, it } from "vitest"
import { parseMarkdown } from "@/lib/markdown-parser"

describe("paragraph-based parsing", () => {
  it("parses single-line blockquote", () => {
    const result = parseMarkdown("> Line one")
    expect(result.blocks).toHaveLength(1)
    expect(result.blocks[0].type).toBe("blockquote")
    expect(result.blocks[0].decorationRanges).toHaveLength(1)
    expect(result.blocks[0].decorationRanges[0]).toEqual({
      type: "hidden",
      from: 0,
      to: 1,
      className: "md-marker",
    })
  })

  it("parses inline bold inside blockquote", () => {
    const result = parseMarkdown("> **bold** text")
    expect(result.blocks).toHaveLength(1)
    expect(result.blocks[0].type).toBe("blockquote")
    // Bold inside the blockquote is still parsed as inline
    expect(result.content).toHaveLength(1)
    expect(result.content[0].type).toBe("strong")
  })

  it("does not match task inside blockquote", () => {
    const result = parseMarkdown("> TODO buy milk")
    expect(result.blocks).toHaveLength(1)
    expect(result.blocks[0].type).toBe("blockquote")
    // Should not also create a task block
    expect(result.blocks.find((b) => b.type === "task")).toBeUndefined()
  })

  it("matches task at top-level paragraph", () => {
    const result = parseMarkdown("TODO buy milk")
    expect(result.blocks).toHaveLength(1)
    expect(result.blocks[0].type).toBe("task")
  })

  it("parses heading as block decoration", () => {
    const result = parseMarkdown("# Hello")
    expect(result.blocks).toHaveLength(1)
    expect(result.blocks[0].type).toBe("heading")
    expect(result.blocks[0].metadata?.level).toBe(1)
  })

  it("inline bold works with full-text parse", () => {
    const result = parseMarkdown("**bold**")
    expect(result.content).toHaveLength(1)
    expect(result.content[0].type).toBe("strong")
    const ranges = result.content[0].decorationRanges
    expect(ranges[0]).toEqual({ type: "hidden", from: 0, to: 2 })
    expect(ranges[1]).toEqual({
      type: "content",
      from: 2,
      to: 6,
      className: "md-strong",
    })
    expect(ranges[2]).toEqual({ type: "hidden", from: 6, to: 8 })
  })
})
