import { describe, expect, it } from "vitest";
import { pickIndexMix } from "../route.js";

describe("pickIndexMix", () => {
  it("entity → vector-heavy + Person|Company|Concept", () => {
    const plan = pickIndexMix("entity");
    expect(plan.skip).toBe(false);
    expect(plan.labelFilter).toEqual(["Person", "Company", "Concept"]);
    expect(plan.vectorWeight).toBe(0.85);
    expect(plan.bm25Weight).toBe(0.15);
  });

  it("temporal → bm25-heavy + Event", () => {
    const plan = pickIndexMix("temporal");
    expect(plan.labelFilter).toEqual(["Event"]);
    expect(plan.vectorWeight).toBe(0.2);
    expect(plan.bm25Weight).toBe(0.8);
  });

  it("event → bm25-only + Event", () => {
    const plan = pickIndexMix("event");
    expect(plan.labelFilter).toEqual(["Event"]);
    expect(plan.vectorWeight).toBe(0);
    expect(plan.bm25Weight).toBe(1);
  });

  it("general → balanced (matches the baseline weighted-sum defaults)", () => {
    const plan = pickIndexMix("general");
    expect(plan.skip).toBe(false);
    expect(plan.labelFilter).toBeUndefined();
    expect(plan.vectorWeight).toBe(0.7);
    expect(plan.bm25Weight).toBe(0.3);
  });

  it("none → skip", () => {
    const plan = pickIndexMix("none");
    expect(plan.skip).toBe(true);
  });

  it("unknown value → general (with stderr warning)", () => {
    const errs: string[] = [];
    const orig = process.stderr.write.bind(process.stderr);
    (process.stderr.write as unknown as (s: string) => boolean) = (s: string) => {
      errs.push(s);
      return true;
    };
    try {
      const plan = pickIndexMix("nonsense");
      expect(plan.skip).toBe(false);
      expect(plan.vectorWeight).toBe(0.7);
    } finally {
      (process.stderr.write as unknown as typeof orig) = orig;
    }
    expect(errs.some((e) => e.includes("[graph-search:unknown-class]"))).toBe(true);
  });

  it("undefined → general silently", () => {
    const plan = pickIndexMix(undefined);
    expect(plan.skip).toBe(false);
    expect(plan.vectorWeight).toBe(0.7);
  });
});
