/**
 * B3 (#2445) — CONFIDENCE_COLOR[velocity.confidence] / CONFIDENCE_LABEL[...]
 * had no fallback: an unrecognized `confidence` (schema drift / partial
 * deploy) made the lookup `undefined`, and the next `.bg`/`.fg` access threw,
 * crashing the block render.
 *
 * These tests pin the defensive fallback (mirrors ParcelDrawer's
 * `STATUS_COLORS[...] ?? '#73767E'` pattern): an unknown confidence must
 * render a neutral color + a neutral "—" label, not throw.
 */
import { render, screen } from "@testing-library/react";
import { describe, expect, it } from "vitest";

import { VelocityBlock } from "../VelocityBlock";
import type { Velocity } from "@/types/site-finder";

function makeVelocity(overrides: Partial<Velocity> = {}): Velocity {
  return {
    competitors_count: 3,
    monthly_velocity_sqm: 500,
    ekb_median_sqm: 400,
    velocity_score: 0.5,
    confidence: "high",
    months_observed: 6,
    period: { start: "2026-01", end: "2026-06" },
    sample_competitors: [],
    ...overrides,
  };
}

describe("VelocityBlock", () => {
  it("renders known confidence without throwing", () => {
    render(<VelocityBlock velocity={makeVelocity({ confidence: "high" })} />);
    expect(screen.getByText("Высокая")).toBeInTheDocument();
  });

  it("unrecognized confidence renders neutral fallback color + label, does not throw", () => {
    const drifted = makeVelocity({
      // Simulates schema drift: backend sends a value outside the closed
      // "high"|"medium"|"low" union (cast through unknown, not `any`).
      confidence: "unknown_value" as unknown as Velocity["confidence"],
    });
    expect(() => render(<VelocityBlock velocity={drifted} />)).not.toThrow();
    // Neutral "—" fallback label instead of a crash or an invented semantic default.
    expect(screen.getByText("—")).toBeInTheDocument();
  });
});
