/**
 * @copyright nhcarrigan
 * @license Naomi's Public License
 * @author Naomi Carrigan
 */

import { describe, it, expect } from "vitest";
import { html } from "../src/config/html.ts";
import { mommy } from "../src/config/mommy.ts";
import { phrases } from "../src/config/phrases.ts";
import { main } from "../src/index.ts";

/* eslint-disable max-nested-callbacks, max-lines-per-function -- Not sure how to better optimise this. */

describe("fastify Server", () => {
  it("should return HTML content on /", async() => {
    expect.assertions(3);
    const server = await main();
    const response = await server.inject({
      method: "GET",
      url:    "/",
    });

    expect(response.statusCode, "wrong status").toBe(200);
    expect(response.headers["content-type"], "wrong content type").
      toContain("text/html");
    expect(response.body, "wrong response").toBe(html);
    await server.close();
  });

  it("should return a formatted phrase on /api", async() => {
    expect.assertions(3);
    const server = await main();
    const response = await server.inject({
      method: "GET",
      url:    "/api",
    });

    expect(response.statusCode, "wrong status").toBe(200);
    expect(response.headers["content-type"], "wrong content type").
      toContain("text/plain");

    const possibleResponses = phrases.flatMap((phrase) => {
      return mommy.map((mom) => {
        return phrase.
          replaceAll("{{ name }}", "dear").
          replaceAll("{{ mommy }}", mom).
          toLowerCase();
      });
    });

    expect(possibleResponses, "where did it come from").
      toContain(response.body);
    await server.close();
  });

  it("should return a formatted phrase with a name query on /api", async() => {
    expect.assertions(3);
    const server = await main();
    const testName = "Naomi";
    const response = await server.inject({
      method: "GET",
      url:    `/api?name=${testName}`,
    });

    expect(response.statusCode, "wrong status").toBe(200);
    expect(response.headers["content-type"], "wrong content type").
      toContain("text/plain");

    const possibleResponses = phrases.flatMap((phrase) => {
      return mommy.map((mom) => {
        return phrase.
          replaceAll("{{ name }}", testName).
          replaceAll("{{ mommy }}", mom).
          toLowerCase();
      });
    });

    expect(possibleResponses, "where did it come from").
      toContain(response.body);
    await server.close();
  });
});