import { describe, expect, it } from "vitest";
import {
  ASSISTANT_MAX_AGGREGATE_BYTES,
  ASSISTANT_MAX_CONTENT_LENGTH,
  ASSISTANT_MAX_PAGE_CONTEXT,
  assistantChatInputSchema,
} from "../router/assistant-procedure-support.js";

describe("assistantChatInputSchema bounds", () => {
  it("accepts a normal-sized message", () => {
    const result = assistantChatInputSchema.safeParse({
      messages: [{ role: "user", content: "Hello" }],
    });
    expect(result.success).toBe(true);
  });

  it("rejects a single message above the per-message length cap", () => {
    const huge = "x".repeat(ASSISTANT_MAX_CONTENT_LENGTH + 1);
    const result = assistantChatInputSchema.safeParse({
      messages: [{ role: "user", content: huge }],
    });
    expect(result.success).toBe(false);
  });

  it("rejects a pageContext above the page-context cap", () => {
    const huge = "x".repeat(ASSISTANT_MAX_PAGE_CONTEXT + 1);
    const result = assistantChatInputSchema.safeParse({
      messages: [{ role: "user", content: "Hi" }],
      pageContext: huge,
    });
    expect(result.success).toBe(false);
  });

  it("rejects an aggregate payload above the total-bytes cap", () => {
    // Each message is below the per-message cap, but together they exceed
    // the aggregate cap.
    const oneMessageBytes = 5_000;
    const each = "x".repeat(oneMessageBytes);
    const count = Math.ceil(ASSISTANT_MAX_AGGREGATE_BYTES / oneMessageBytes) + 2;
    const messages = Array.from({ length: count }, () => ({
      role: "user" as const,
      content: each,
    }));
    const result = assistantChatInputSchema.safeParse({ messages });
    expect(result.success).toBe(false);
  });

  it("accepts an aggregate payload right under the cap", () => {
    const count = Math.floor(ASSISTANT_MAX_AGGREGATE_BYTES / 1_000) - 1;
    const messages = Array.from({ length: count }, () => ({
      role: "user" as const,
      content: "x".repeat(1_000),
    }));
    const result = assistantChatInputSchema.safeParse({ messages });
    expect(result.success).toBe(true);
  });

  it("rejects an empty messages array", () => {
    const result = assistantChatInputSchema.safeParse({ messages: [] });
    expect(result.success).toBe(false);
  });

  it("rejects more than 200 messages", () => {
    const messages = Array.from({ length: 201 }, () => ({
      role: "user" as const,
      content: "x",
    }));
    const result = assistantChatInputSchema.safeParse({ messages });
    expect(result.success).toBe(false);
  });
});
