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

import {
  selectTipSupportPrompt,
  TIP_SUPPORT_PROMPTS,
} from "../tip_support_prompts";

describe("Tip support prompts", () => {
  it("contains exactly 30 unique, non-empty prompts", () => {
    expect(TIP_SUPPORT_PROMPTS).toHaveLength(30);
    expect(new Set(TIP_SUPPORT_PROMPTS)).toHaveLength(30);
    expect(
      TIP_SUPPORT_PROMPTS.every(
        (prompt) => prompt.length > 0 && prompt === prompt.trim(),
      ),
    ).toBe(true);
  });

  it("selects across the full prompt range", () => {
    expect(selectTipSupportPrompt(null, () => 0).index).toBe(0);
    expect(selectTipSupportPrompt(null, () => 0.999999).index).toBe(29);
  });

  it("excludes the previous prompt without skewing out of range", () => {
    expect(selectTipSupportPrompt(0, () => 0).index).toBe(1);
    expect(selectTipSupportPrompt(15, () => 0.5).index).not.toBe(15);
    expect(selectTipSupportPrompt(29, () => 0.999999).index).toBe(28);
  });

  it("safely normalizes invalid previous indexes and random values", () => {
    expect(selectTipSupportPrompt(100, () => 0).index).toBe(0);
    expect(selectTipSupportPrompt(null, () => Number.NaN).index).toBe(0);
    expect(selectTipSupportPrompt(null, () => 2).index).toBe(29);
  });

  it("names the active character and never frames a purchase as affection", () => {
    const selection = selectTipSupportPrompt(null, () => 0, "Maya Tan");

    expect(selection.prompt).toContain("support Maya Tan");
    expect(selection.prompt).not.toMatch(/tiny hug|spoil|buy.*love|owe me/i);
    expect(
      TIP_SUPPORT_PROMPTS.every(
        (prompt) =>
          /support/i.test(prompt) && /this character/i.test(prompt),
      ),
    ).toBe(true);
  });
});
