import { afterAll, beforeAll, expect, it } from "vitest";
import type { FastifyInstance } from "fastify";

let app: FastifyInstance;
beforeAll(async () => {
  process.env["MIRROR_MODE"] = "on";
  process.env["DATABASE_URL"] ??= "mysql://x:y@127.0.0.1:1/none";
  process.env["APP_ENCRYPTION_KEY"] ??= "0".repeat(64);
  app = await (await import("./buildApp.js")).buildApp({ logger: false, rateLimit: false });
});
afterAll(() => app.close());

it("refuses changes in mirror mode", async () => {
  const r = await app.inject({ method: "POST", url: "/api/v2/projects/1/trainees", payload: {} });
  expect(r.statusCode).toBe(423);
  expect(r.json().code ?? r.body).toContain("READ_ONLY_MIRROR");
  expect((await app.inject({ method: "DELETE", url: "/api/v2/projects/1/trainees/5?x=1" })).statusCode).toBe(423);
});

it("still allows signing in and reading", async () => {
  expect((await app.inject({ method: "POST", url: "/api/v2/auth/login", payload: {} })).statusCode).not.toBe(423);
  expect((await app.inject({ method: "GET", url: "/health/live" })).statusCode).toBe(200);
});
