import { describe, expect, it } from "vitest"

import {
  registryChunkSchema,
  registryConfigSchema,
  registrySchema,
} from "./schema"

describe("registryConfigSchema", () => {
  it("should accept valid registry names starting with @", () => {
    const validConfig = {
      "@v0": "https://v0.dev/{name}.json",
      "@acme": {
        url: "https://acme.com/{name}.json",
        headers: {
          Authorization: "Bearer token",
        },
      },
    }

    const result = registryConfigSchema.safeParse(validConfig)
    expect(result.success).toBe(true)
  })

  it("should reject registry names not starting with @", () => {
    const invalidConfig = {
      v0: "https://v0.dev/{name}.json",
      acme: "https://acme.com/{name}.json",
    }

    const result = registryConfigSchema.safeParse(invalidConfig)
    expect(result.success).toBe(false)
    if (!result.success) {
      expect(result.error.errors[0].message).toContain(
        "Registry names must start with @"
      )
    }
  })

  it("should reject URLs without {name} placeholder", () => {
    const invalidConfig = {
      "@v0": "https://v0.dev/component.json",
    }

    const result = registryConfigSchema.safeParse(invalidConfig)
    expect(result.success).toBe(false)
    if (!result.success) {
      expect(result.error.errors[0].message).toContain(
        "Registry URL must include {name} placeholder"
      )
    }
  })
})

describe("registrySchema", () => {
  it("should accept registry chunks with includes", () => {
    const result = registryChunkSchema.safeParse({
      include: ["./registry/ui/registry.json"],
    })

    expect(result.success).toBe(true)
    if (result.success) {
      expect(result.data.items).toEqual([])
    }
  })

  it("should require name and homepage for root registries", () => {
    const result = registrySchema.safeParse({
      include: ["./registry/ui/registry.json"],
    })

    expect(result.success).toBe(false)
  })

  it("should reject registries without items or include", () => {
    const result = registryChunkSchema.safeParse({
      name: "example",
      homepage: "https://example.com",
    })

    expect(result.success).toBe(false)
  })
})
