import { techStepClassifier } from "../src/lib/recipe-matching/tech-step-matcher.js";
import { resetDatabase } from "./reset-db.js";

/**
 * Mocha root hook plugin (see `.mocharc.json`'s `require`) — runs once
 * before every test file's own suites, regardless of load order.
 *
 * Warms up `techStepClassifier` here — resolving the `TechStep.key -> id`
 * lookup from the DB (see `TechStepClassifierService._loadTechStepIds`) —
 * instead of leaving it to happen lazily on whichever test file Mocha
 * happens to load first, same as `server.ts` does before the real server
 * ever accepts traffic. Fast by itself (one DB query, one HTTP call to
 * `services/tech-step-intent-service`): that service now trains itself
 * entirely at its own process startup (see its own README), so unlike
 * before this migration, nothing here waits on a slow training pass — CI's
 * own "wait for `/health`" step (`.github/workflows/ci.yml`) is what
 * ensures that service is already fully trained before `pnpm --filter api
 * test` even starts.
 *
 * `resetDatabase()` runs first, deliberately: id resolution needs
 * `TechStep` rows, and a freshly-migrated (never-seeded) test database has
 * none yet. Every per-test `beforeEach` in this suite already calls
 * `resetDatabase()` again before its own test, which is a no-op
 * duplication of effort but not a correctness problem: `TRUNCATE ...
 * RESTART IDENTITY` plus deterministic re-seeding (`seedReferenceData`)
 * assigns the exact same ids every time, so the `uid -> id` map memoized
 * here from this first reset stays valid for every reset after it.
 */
export const mochaHooks = {
  // biome-ignore lint/suspicious/noExplicitAny: Mocha's root hook `this` (a Context with `.timeout()`) isn't typed without @types/mocha (not a dependency here) — same untyped-`this` shape already used in tech-step-worker.routes.test.ts.
  async beforeAll(this: any): Promise<void> {
    // A little more generous than Mocha's normal 10s per-test default
    // (`.mocharc.json`) purely for a slower/contended CI runner's first
    // network round-trip to `services/tech-step-intent-service` — not
    // because anything here waits on training anymore.
    this.timeout(30000);
    await resetDatabase();
    await techStepClassifier.warmUp();
  },
};
