import { test, expect } from "bun:test";
import { mkdtempSync, existsSync, readFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { generateDocker } from "../src/docker.ts";

test("generateDocker scaffolds Dockerfile, .dockerignore, compose", () => {
  const dir = mkdtempSync(join(tmpdir(), "wire-docker-"));
  generateDocker(dir);
  expect(existsSync(join(dir, "Dockerfile"))).toBe(true);
  expect(existsSync(join(dir, ".dockerignore"))).toBe(true);
  expect(existsSync(join(dir, "docker-compose.yml"))).toBe(true);

  const dockerfile = readFileSync(join(dir, "Dockerfile"), "utf8");
  expect(dockerfile).toContain("FROM oven/bun:1 AS build");
  expect(dockerfile).toContain("bun run build");
  expect(dockerfile).toContain("/healthz"); // health check uses the app endpoint
  expect(dockerfile).toContain('CMD ["bun", "dist/server.js"]');

  expect(readFileSync(join(dir, "docker-compose.yml"), "utf8")).toContain("postgres:16-alpine");
});

test("generateDocker does not overwrite existing files", () => {
  const dir = mkdtempSync(join(tmpdir(), "wire-docker-"));
  generateDocker(dir);
  const before = readFileSync(join(dir, "Dockerfile"), "utf8");
  // second run must leave the file untouched
  generateDocker(dir);
  expect(readFileSync(join(dir, "Dockerfile"), "utf8")).toBe(before);
});
