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

// Unit tests for expose logic without mounting
describe("Block expose API", () => {
  describe("getContent logic", () => {
    it("returns content from docToContent", () => {
      const doc = contentToDoc("hello world")
      const result = docToContent(doc)
      expect(result).toBe("hello world")
    })

    it("handles multiline content", () => {
      const doc = contentToDoc("line1\nline2\nline3")
      expect(doc.childCount).toBe(1)
      expect(inlineToString(doc.child(0))).toBe("line1\nline2\nline3")
    })

    it("handles empty content", () => {
      const doc = contentToDoc("")
      const result = docToContent(doc)
      expect(result).toBe("")
    })

    it("handles unicode content", () => {
      const doc = contentToDoc("Hello 🌍 你好 🔥")
      const result = docToContent(doc)
      expect(result).toBe("Hello 🌍 你好 🔥")
    })
  })

  describe("setContent logic", () => {
    it("contentToDoc creates correct doc structure", () => {
      const doc = contentToDoc("updated content")
      expect(doc.childCount).toBe(1)
      expect(doc.child(0).textContent).toBe("updated content")
    })

    it("roundtrips correctly", () => {
      const original = "test content"
      const doc = contentToDoc(original)
      const result = docToContent(doc)
      expect(result).toBe(original)
    })

    it("handles empty string", () => {
      const doc = contentToDoc("")
      expect(doc.childCount).toBe(1)
      expect(doc.child(0).textContent).toBe("")
    })

    it("handles multiline strings", () => {
      const doc = contentToDoc("line1\nline2\nline3")
      expect(doc.childCount).toBe(1)
      expect(doc.child(0).type.name).toBe("paragraph")
      expect(inlineToString(doc.child(0))).toBe("line1\nline2\nline3")
    })
  })

  describe("view null safety", () => {
    it("getContent returns empty string for null view", () => {
      // Simulating the getContent logic when view is null
      const view = null
      const result = view ? docToContent(view.state.doc) : ""
      expect(result).toBe("")
    })

    it("setContent is a no-op for null view", () => {
      const view = null

      if (view) {
        expect.fail("Should not reach here — view is null in this test")
      }
    })
  })
})
