import { describe, it, expect } from "vitest";
import { handleValidate } from "../../src/tools/validate.js";
import { ComponentRegistry } from "../../src/registry.js";

describe("pettyui.validate", () => {
  const registry = new ComponentRegistry();

  it("validates correct props", () => {
    const result = handleValidate(registry, { component: "Dialog", schema: "dialogRoot", props: { open: true, modal: false } });
    expect(result.valid).toBe(true);
    expect(result.errors).toHaveLength(0);
  });

  it("rejects invalid props", () => {
    const result = handleValidate(registry, { component: "Dialog", schema: "dialogRoot", props: { open: "yes" } });
    expect(result.valid).toBe(false);
    expect(result.errors.length).toBeGreaterThan(0);
  });

  it("validates empty props", () => {
    const result = handleValidate(registry, { component: "Dialog", schema: "dialogRoot", props: {} });
    expect(result.valid).toBe(true);
    expect(result.errors).toHaveLength(0);
  });

  it("checks required parts present — missing Title", () => {
    const result = handleValidate(registry, { component: "Dialog", parts: ["Root", "Content"] });
    expect(result.valid).toBe(false);
    expect(result.errors).toContain("Missing required part: Title");
  });

  it("passes when all required parts provided", () => {
    const result = handleValidate(registry, { component: "Dialog", parts: ["Root", "Content", "Title"] });
    expect(result.valid).toBe(true);
    expect(result.errors).toHaveLength(0);
  });

  it("returns error for unknown component", () => {
    const result = handleValidate(registry, { component: "FakeComponent" });
    expect(result.valid).toBe(false);
    expect(result.errors).toContain('Component "FakeComponent" not found');
  });
});
