/**
 * Tests for `isDeficitDegenerate` — issue #1958 (flat/degenerate forecast state).
 *
 * The §22 deficit line is "flat and uninformative" when either:
 *   (a) the backend collapsed the 3 scenarios (scenarios_collapsed) — the
 *       base/aggressive/conservative deficit lines are identical; OR
 *   (b) every present deficit point is EQUAL and sits at the clamp floor
 *       (|value| ≈ 1) — no segment differentiation, the line hugs the edge and
 *       is near-invisible.
 *
 * In either case ForecastChart renders a grey flat-state callout over the chart.
 * The real differentiation fix is backend #1959 (separate).
 */
import { describe, expect, it } from "vitest";

import { isDeficitDegenerate } from "../ForecastChart";

describe("isDeficitDegenerate — #1958 flat-state detection", () => {
  it("is degenerate when scenarios_collapsed is true (regardless of values)", () => {
    expect(isDeficitDegenerate([0.1, 0.2, -0.3], true)).toBe(true);
    expect(isDeficitDegenerate([], true)).toBe(true);
  });

  it("is degenerate when all points equal AND clamped at +1", () => {
    expect(isDeficitDegenerate([1, 1, 1], false)).toBe(true);
  });

  it("is degenerate when all points equal AND clamped at −1", () => {
    expect(isDeficitDegenerate([-1, -1, -1], false)).toBe(true);
  });

  it("treats near-1 (clamp floor within eps) as degenerate", () => {
    expect(isDeficitDegenerate([0.99, 0.99, 0.99], false)).toBe(true);
  });

  it("is NOT degenerate when all equal but mid-range (flat but informative)", () => {
    // Flat at e.g. 0.3 is a real balanced signal, not a clamp artefact.
    expect(isDeficitDegenerate([0.3, 0.3, 0.3], false)).toBe(false);
  });

  it("is NOT degenerate when points differ (real trajectory)", () => {
    expect(isDeficitDegenerate([1, 0.5, 0.2], false)).toBe(false);
    expect(isDeficitDegenerate([-1, -0.8, -0.4], false)).toBe(false);
  });

  it("ignores nulls and gates on present values only", () => {
    expect(isDeficitDegenerate([null, 1, null, 1], false)).toBe(true);
    expect(isDeficitDegenerate([null, 1, 0.5], false)).toBe(false);
  });

  it("is NOT degenerate when there are no present values", () => {
    expect(isDeficitDegenerate([null, null], false)).toBe(false);
    expect(isDeficitDegenerate([], false)).toBe(false);
  });

  it("a single clamped point counts as degenerate; single mid-range does not", () => {
    expect(isDeficitDegenerate([1], false)).toBe(true);
    expect(isDeficitDegenerate([0.4], false)).toBe(false);
  });
});
