import test from "node:test";
import assert from "node:assert/strict";
import { formatFetchOutput, formatSearchOutput, truncateText } from "./format.ts";

test("formatSearchOutput renders a compact metadata-only list", () => {
  const output = formatSearchOutput({
    providerName: "exa-main",
    results: [
      {
        title: "Exa Docs",
        url: "https://exa.ai/docs",
        publishedDate: "2026-04-09",
        author: "Exa",
        score: 0.98,
      },
    ],
  });

  assert.match(output, /Found 1 web result via exa-main:/);
  assert.match(output, /Exa Docs/);
  assert.match(output, /https:\/\/exa.ai\/docs/);
});

test("truncateText shortens long fetch bodies with an ellipsis", () => {
  assert.equal(truncateText("abcdef", 4), "abc…");
  assert.equal(truncateText("abc", 10), "abc");
});

test("formatFetchOutput includes both successful and failed URLs", () => {
  const output = formatFetchOutput(
    {
      providerName: "exa-main",
      results: [
        {
          url: "https://good.example",
          title: "Good",
          text: "This is a very long body that should be truncated in the final output.",
        },
        {
          url: "https://bad.example",
          title: null,
          error: "429 rate limited",
        },
      ],
    },
    { maxCharactersPerResult: 20 },
  );

  assert.match(output, /Status: ok/);
  assert.match(output, /Status: failed/);
  assert.match(output, /429 rate limited/);
  assert.match(output, /This is a very long…/);
});
