import { describe, it, expect } from "vitest";
import {
  createKnowledgePointSchema,
  updateKnowledgePointSchema,
  addPrerequisiteSchema,
} from "./knowledge-points.dto.js";

describe("knowledge-points.dto", () => {
  describe("createKnowledgePointSchema", () => {
    it("should parse valid input with all fields", () => {
      const result = createKnowledgePointSchema.parse({
        chapterId: "ch-1",
        title: "Algebra Basics",
        description: "Intro to algebra",
        difficulty: 4,
        metadata: { tags: ["math"] },
      });
      expect(result.chapterId).toBe("ch-1");
      expect(result.title).toBe("Algebra Basics");
      expect(result.description).toBe("Intro to algebra");
      expect(result.difficulty).toBe(4);
      expect(result.metadata).toEqual({ tags: ["math"] });
    });

    it("should default difficulty to 3 when not provided", () => {
      const result = createKnowledgePointSchema.parse({
        chapterId: "ch-1",
        title: "Algebra Basics",
      });
      expect(result.difficulty).toBe(3);
    });

    it("should accept null metadata", () => {
      const result = createKnowledgePointSchema.parse({
        chapterId: "ch-1",
        title: "Algebra Basics",
        metadata: null,
      });
      expect(result.metadata).toBeNull();
    });

    it("should reject empty chapterId", () => {
      expect(() =>
        createKnowledgePointSchema.parse({ chapterId: "", title: "Title" }),
      ).toThrow();
    });

    it("should reject difficulty greater than 5", () => {
      expect(() =>
        createKnowledgePointSchema.parse({
          chapterId: "ch-1",
          title: "Title",
          difficulty: 6,
        }),
      ).toThrow();
    });
  });

  describe("updateKnowledgePointSchema", () => {
    it("should parse partial update with title only", () => {
      const result = updateKnowledgePointSchema.parse({ title: "New Title" });
      expect(result.title).toBe("New Title");
    });

    it("should accept null metadata in update", () => {
      const result = updateKnowledgePointSchema.parse({ metadata: null });
      expect(result.metadata).toBeNull();
    });

    it("should accept difficulty update within valid range", () => {
      const result = updateKnowledgePointSchema.parse({ difficulty: 5 });
      expect(result.difficulty).toBe(5);
    });
  });

  describe("addPrerequisiteSchema", () => {
    it("should parse valid prerequisiteId", () => {
      const result = addPrerequisiteSchema.parse({
        prerequisiteId: "kp-1",
      });
      expect(result.prerequisiteId).toBe("kp-1");
    });

    it("should reject empty prerequisiteId", () => {
      expect(() =>
        addPrerequisiteSchema.parse({ prerequisiteId: "" }),
      ).toThrow();
    });
  });
});
