import { expect } from "chai";
import { prisma } from "../../src/db/prisma.js";
import {
  MIN_OVERALL_F1,
  runTechStepEvalSuite,
} from "../../src/lib/recipe-matching/tech-step-eval-runner.js";
import { resetDatabase } from "../../test-support/reset-db.js";

/**
 * Regression gate for `TECH_STEP_TRAINING_DATA` — every change to that
 * corpus (including a maintainer applying suggestions from
 * `TechStepTrainingSuggestion`, see `scripts/retrain-tech-steps.ts`) must
 * keep this suite green. Runs {@link runTechStepEvalSuite} (the real
 * trained classifier against `tech-step-eval-dataset.ts`) and asserts the
 * aggregate F1 doesn't fall below {@link MIN_OVERALL_F1} — see that
 * constant's own doc comment (`tech-step-eval-runner.ts`) for the real run
 * it was calibrated against.
 */

describe("tech-step-eval", () => {
  beforeEach(async () => {
    await resetDatabase();
  });

  after(async () => {
    await prisma.$disconnect();
  });

  it(`scores at least ${MIN_OVERALL_F1} aggregate F1 against the labeled evaluation set`, async () => {
    const { overall, byKey } = await runTechStepEvalSuite();

    expect(
      overall.f1,
      `aggregate F1 ${overall.f1.toFixed(3)} (precision ${overall.precision.toFixed(3)}, recall ${overall.recall.toFixed(3)}) fell below the ${MIN_OVERALL_F1} floor — per-technique breakdown: ${JSON.stringify(byKey)}`,
    ).to.be.at.least(MIN_OVERALL_F1);
  });
});
