import { describe, expect, it } from "vitest"
import { mount } from "@vue/test-utils"
import MarkdownDisplay from "./MarkdownDisplay.vue"

describe("MarkdownDisplay", () => {
  it("renders plain content", () => {
    const wrapper = mount(MarkdownDisplay, { props: { value: "Plain content." } })
    expect(wrapper.text()).toContain("Plain content.")
  })

  it("renders empty when value is null", () => {
    const wrapper = mount(MarkdownDisplay, { props: { value: null } })
    expect(wrapper.text()).toBe("")
  })

  it("transforms bold markdown to <strong>", () => {
    const wrapper = mount(MarkdownDisplay, {
      props: { value: "Hello **world**" },
    })
    expect(wrapper.html()).toContain("<strong>world</strong>")
  })

  it("transforms headings to heading tags", () => {
    const wrapper = mount(MarkdownDisplay, { props: { value: "# Title" } })
    expect(wrapper.find("h1").exists()).toBe(true)
    expect(wrapper.find("h1").text()).toBe("Title")
  })

  it("transforms links to anchor tags", () => {
    const wrapper = mount(MarkdownDisplay, {
      props: { value: "[example](https://example.com)" },
    })
    const anchor = wrapper.find("a")
    expect(anchor.exists()).toBe(true)
    expect(anchor.attributes("href")).toBe("https://example.com")
    expect(anchor.text()).toBe("example")
  })

  it("escapes HTML in raw content", () => {
    const wrapper = mount(MarkdownDisplay, {
      props: { value: "<script>x</script>" },
    })
    expect(wrapper.html()).not.toContain("<script>x</script>")
    expect(wrapper.html()).toContain("&lt;script&gt;")
  })

  it("applies classList (string) to the root", () => {
    const wrapper = mount(MarkdownDisplay, {
      props: { value: "x", classList: "extra-class" },
    })
    expect(wrapper.attributes("class")).toContain("extra-class")
  })

  it("applies classList (array) to the root", () => {
    const wrapper = mount(MarkdownDisplay, {
      props: { value: "x", classList: ["a", "b"] },
    })
    expect(wrapper.attributes("class")).toContain("a")
    expect(wrapper.attributes("class")).toContain("b")
  })
})
