import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { test } from "bun:test";

import { rootPath } from "./config";

const runner = readFileSync(rootPath("scripts/src/gc-remote-runner.py"), "utf8");
const hostDocker = readFileSync(rootPath("scripts/src/gc-remote-host-docker.py"), "utf8");
const policyRunner = readFileSync(rootPath("scripts/src/gc-remote-policy-runner.py"), "utf8");
const yaml = readFileSync(rootPath("config/unidesk-cli.yaml"), "utf8");

test("host Docker image GC protects every container state and uses exact image removal", () => {
  assert.match(hostDocker, /docker", "ps", "-qa", "--no-trunc/u);
  assert.match(hostDocker, /unreferencedByAnyContainer/u);
  assert.match(runner, /docker", "image", "rm", image_id/u);
  assert.match(hostDocker, /scope": "host-docker-only/u);
  assert.doesNotMatch(runner, /docker", "system", "prune/u);
  assert.doesNotMatch(runner, /docker", "image", "prune/u);
});

test("policy stages use conservative YAML-owned host Docker limits", () => {
  assert.match(yaml, /imagePrune:[\s\S]*minAgeHours: 168[\s\S]*automatic:[\s\S]*minAgeHours: 336[\s\S]*maxDeletePerRun: 5/u);
  assert.match(yaml, /buildCachePrune:[\s\S]*automatic:[\s\S]*until: 168h/u);
  assert.match(policyRunner, /run_host_docker_build_cache/u);
  assert.match(policyRunner, /run_host_docker_images/u);
  assert.match(policyRunner, /became-referenced/u);
  assert.match(policyRunner, /command_lines\(\["docker", "ps", "-qa", "--no-trunc"\]/u);
  assert.match(policyRunner, /actualDiskReclaimBytes/u);
  assert.match(runner, /"hostDockerGc": rendered\["policyConfig"\]\["hostDockerGc"\]/u);
});
