import { readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, test } from "vitest";
import { createDevServerConfig } from "../../vite.config";
import { apiServerArgs } from "./dev";

describe("local development runtime", () => {
  test("starts the Bun API server together with the Vite dev server", () => {
    const packageJson = JSON.parse(
      readFileSync(join(process.cwd(), "package.json"), "utf8"),
    ) as { scripts?: Record<string, string> };

    expect(packageJson.scripts?.dev).toBe("bun src/server/dev.ts");
  });

  test("proxies dashboard API requests from Vite to the Bun API server", () => {
    const server = createDevServerConfig({
      DASHBOARD_DEV_API_TARGET: "http://127.0.0.1:5174",
    });

    expect(server?.proxy?.["/api"]).toMatchObject({
      target: "http://127.0.0.1:5174",
      changeOrigin: true,
    });
  });

  test("uses development package export conditions for the Bun API server", () => {
    expect(apiServerArgs).toEqual([
      "--conditions=development",
      "src/server/index.ts",
    ]);
  });
});
