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

import {
  createProjectSchema,
  updateProjectSchema,
} from "../../src/modules/diagram/project-router";

describe("createProjectSchema", () => {
  it("should accept a valid name", () => {
    const result = createProjectSchema.safeParse({ name: "My Project" });
    expect(result.success).toBe(true);
  });

  it("should reject empty name", () => {
    const result = createProjectSchema.safeParse({ name: "" });
    expect(result.success).toBe(false);
  });

  it("should reject name over 100 characters", () => {
    const result = createProjectSchema.safeParse({ name: "a".repeat(101) });
    expect(result.success).toBe(false);
  });

  it("should accept name at max length (100)", () => {
    const result = createProjectSchema.safeParse({ name: "a".repeat(100) });
    expect(result.success).toBe(true);
  });

  it("should reject missing name", () => {
    const result = createProjectSchema.safeParse({});
    expect(result.success).toBe(false);
  });

  it("should strip unknown fields", () => {
    const result = createProjectSchema.safeParse({
      name: "Test",
      unknownField: "should be stripped",
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data).not.toHaveProperty("unknownField");
    }
  });
});

describe("updateProjectSchema", () => {
  it("should accept valid name update", () => {
    const result = updateProjectSchema.safeParse({ name: "Updated Name" });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.name).toBe("Updated Name");
    }
  });

  it("should accept valid sortOrder update", () => {
    const result = updateProjectSchema.safeParse({ sortOrder: 5 });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.sortOrder).toBe(5);
    }
  });

  it("should accept both name and sortOrder", () => {
    const result = updateProjectSchema.safeParse({
      name: "New Name",
      sortOrder: 3,
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data).toEqual({ name: "New Name", sortOrder: 3 });
    }
  });

  it("should reject empty object (at least one field required)", () => {
    const result = updateProjectSchema.safeParse({});
    expect(result.success).toBe(false);
  });

  it("should reject name over 100 characters", () => {
    const result = updateProjectSchema.safeParse({ name: "a".repeat(101) });
    expect(result.success).toBe(false);
  });

  it("should reject empty name", () => {
    const result = updateProjectSchema.safeParse({ name: "" });
    expect(result.success).toBe(false);
  });

  it("should reject non-integer sortOrder", () => {
    const result = updateProjectSchema.safeParse({ sortOrder: 1.5 });
    expect(result.success).toBe(false);
  });

  it("should reject non-number sortOrder", () => {
    const result = updateProjectSchema.safeParse({ sortOrder: "abc" });
    expect(result.success).toBe(false);
  });

  it("should strip unknown fields", () => {
    const result = updateProjectSchema.safeParse({
      name: "Test",
      unknownField: "should be stripped",
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data).not.toHaveProperty("unknownField");
    }
  });
});
