import { describe, it, expect, vi } from "vitest";
import { runLoremIpsum } from "./run";
import type { ToolContext } from "../../core/plugins/plugin-types";
import type { LoremIpsumOptions } from "./index";

describe("Lorem Ipsum Generator", () => {
  const mockContext: ToolContext = {
    signal: new AbortController().signal,
    reportProgress: vi.fn(),
    logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() },
  };

  it("should generate the requested number of words", async () => {
    const result = await runLoremIpsum(
      {},
      { type: "words", count: 5 },
      mockContext
    );
    expect(result.type).toBe("text");
    const words = (result as { type: "text"; value: string }).value.split(" ");
    expect(words).toHaveLength(5);
    words.forEach((w) => expect(w.length).toBeGreaterThan(0));
  });

  it("should generate the requested number of sentences", async () => {
    const result = await runLoremIpsum(
      {},
      { type: "sentences", count: 3 },
      mockContext
    );
    expect(result.type).toBe("text");
    const value = (result as { type: "text"; value: string }).value;
    const sentences = value.split(". ").filter((s) => s.trim().length > 0);
    expect(sentences).toHaveLength(3);
  });

  it("should generate the requested number of paragraphs", async () => {
    const result = await runLoremIpsum(
      {},
      { type: "paragraphs", count: 4 },
      mockContext
    );
    expect(result.type).toBe("text");
    const value = (result as { type: "text"; value: string }).value;
    const paragraphs = value.split("\n\n").filter((p) => p.trim().length > 0);
    expect(paragraphs).toHaveLength(4);
  });

  it("should start sentences with a capital letter", async () => {
    const result = await runLoremIpsum(
      {},
      { type: "sentences", count: 5 },
      mockContext
    );
    const value = (result as { type: "text"; value: string }).value;
    const sentences = value.split(". ").filter((s) => s.trim().length > 0);
    sentences.forEach((s) => {
      const trimmed = s.trim();
      expect(trimmed[0]).toBe(trimmed[0].toUpperCase());
    });
  });

  it("should end sentences with a period", async () => {
    const result = await runLoremIpsum(
      {},
      { type: "sentences", count: 3 },
      mockContext
    );
    const value = (result as { type: "text"; value: string }).value;
    expect(value.endsWith(".")).toBe(true);
  });

  it("should default to paragraphs when type is unrecognized", async () => {
    const result = await runLoremIpsum(
      {},
      { type: "unknown", count: 2 } as unknown as LoremIpsumOptions,
      mockContext
    );
    const value = (result as { type: "text"; value: string }).value;
    expect(value.length).toBeGreaterThan(0);
  });
});
