// src/components/keywords/sparkline.test.ts
import { expect, test } from "bun:test";
import { slopePct, sparklinePath } from "./sparkline";

test("maps a flat series to a horizontal line", () => {
  const d = sparklinePath([5, 5, 5], 100, 20);
  // 3 points → x at 0, 50, 100; flat → all y identical
  expect(d).toBe("M0,10 L50,10 L100,10");
});

test("min maps to bottom, max maps to top (y inverted)", () => {
  const d = sparklinePath([0, 10], 100, 20);
  // pad of 2 keeps points inside [2, 18]; min→18 (bottom), max→2 (top)
  expect(d).toBe("M0,18 L100,2");
});

test("single point renders a dot-line at mid height", () => {
  expect(sparklinePath([7], 100, 20)).toBe("M0,10 L0,10");
});

test("empty series returns empty path", () => {
  expect(sparklinePath([], 100, 20)).toBe("");
});

// slopePct is the colour fallback the Sparkline uses when growth is
// undefined (growth ?? slopePct(values)) — a declining series must read red.
test("slopePct: declining series yields a negative slope", () => {
  expect(slopePct([200, 150, 100])).toBe(-50);
});

test("slopePct: rising series yields a positive slope", () => {
  expect(slopePct([100, 150, 200])).toBe(100);
});

test("slopePct: fewer than 2 points → 0 (flat)", () => {
  expect(slopePct([])).toBe(0);
  expect(slopePct([7])).toBe(0);
});

test("slopePct: zero base guards to 1 instead of dividing by 0", () => {
  expect(slopePct([0, 5])).toBe(500);
});
