import { createRequire } from "node:module";
import { describe, expect, it } from "vitest";

import {
  ModelConfigurationService,
  portableRuntimeModelCandidates,
} from "../../apps/api/src/model-configuration.js";

const requireFromApi = createRequire(new URL("../../apps/api/package.json", import.meta.url));
const Database = requireFromApi("better-sqlite3") as new (path: string) => {
  close(): void;
};

describe("POSTV1-02 portable runtime model seed", () => {
  it("enables only models backed by the real OneAPI contract", () => {
    const database = new Database(":memory:");
    try {
      const models = new ModelConfigurationService({ database, seedCandidates: portableRuntimeModelCandidates }).read();
      const flash = models.models.find((model) => model.model_id === "gemini-3.1-flash-image-preview");
      const pro = models.models.find((model) => model.model_id === "gemini-3-pro-image-preview");
      const gpt = models.models.find((model) => model.model_id === "gpt-image-2");

      expect(models.configured_default_model_id).toBe("gemini-3.1-flash-image-preview");
      expect(flash).toMatchObject({
        contract_validation_status: "verified",
        enabled: true,
        runtime_availability: { available_for_new_jobs: true, reason: "available" },
      });
      expect(flash?.route_profile).toMatchObject({ endpoint: "https://oneapi.intelligrow.cn/v1/chat/completions" });
      expect(pro).toMatchObject({
        contract_validation_status: "unverified",
        enabled: false,
        runtime_availability: { available_for_new_jobs: false, reason: "configured_disabled" },
      });
      expect(gpt).toMatchObject({
        contract_validation_status: "verified",
        enabled: true,
        runtime_availability: { available_for_new_jobs: true, reason: "available" },
      });
    } finally {
      database.close();
    }
  });
});
