import { describe, expect, it } from "vitest"
import { contentToDoc, docToContent } from "@/lib/content-model"
import { schema } from "@/lib/schema"

describe("schema", () => {
  describe("nodes", () => {
    it("has doc node", () => {
      expect(schema.nodes.doc).toBeDefined()
    })

    it("has paragraph node", () => {
      expect(schema.nodes.paragraph).toBeDefined()
    })

    it("has text node", () => {
      expect(schema.nodes.text).toBeDefined()
    })

    it("has hard_break node (re-added for newline fidelity)", () => {
      expect(schema.nodes.hard_break).toBeDefined()
    })
  })

  describe("doc node", () => {
    it("creates doc with one paragraph", () => {
      const doc = schema.nodes.doc.create(null, [
        schema.nodes.paragraph.create(),
      ])
      expect(doc.type.name).toBe("doc")
      expect(doc.childCount).toBe(1)
    })

    it("creates doc with multiple paragraphs", () => {
      const doc = schema.nodes.doc.create(null, [
        schema.nodes.paragraph.create(null, [schema.text("line1")]),
        schema.nodes.paragraph.create(null, [schema.text("line2")]),
      ])
      expect(doc.childCount).toBe(2)
    })

    it("requires block content", () => {
      const doc = schema.nodes.doc.create(null, [
        schema.nodes.paragraph.create(null, [schema.text("hello")]),
      ])
      expect(doc.child(0).type.name).toBe("paragraph")
    })
  })

  describe("paragraph node", () => {
    it("creates empty paragraph", () => {
      const p = schema.nodes.paragraph.create()
      expect(p.type.name).toBe("paragraph")
      expect(p.content.size).toBe(0)
    })

    it("creates paragraph with text", () => {
      const p = schema.nodes.paragraph.create(null, [schema.text("hello")])
      expect(p.textContent).toBe("hello")
    })
  })

  describe("text node", () => {
    it("creates text node", () => {
      const text = schema.text("hello")
      expect(text.type.name).toBe("text")
      expect(text.text).toBe("hello")
    })
  })

  describe("marks", () => {
    it("has no marks defined", () => {
      expect(Object.keys(schema.marks)).toHaveLength(0)
    })
  })

  describe("roundtrip with content-model", () => {
    it("preserves single newline as hard_break", () => {
      const original = "line1\nline2"
      const doc = contentToDoc(original)
      const result = docToContent(doc)

      expect(result).toBe("line1\nline2")
      expect(doc.childCount).toBe(1)
    })
  })
})
