import test from "node:test";
import assert from "node:assert/strict";

import { canAutoPruneMissingUnit } from "./app-reconcile-utils.mjs";

function makeItem(overrides = {}) {
  return {
    db: { present: true, subdomain: "example-app" },
    issues: [{ code: "missing_unit" }],
    workspace: { exists: false },
    containers: [],
    ...overrides,
  };
}

test("canAutoPruneMissingUnit only matches safe tombstones", () => {
  assert.equal(canAutoPruneMissingUnit(makeItem()), true);
  assert.equal(
    canAutoPruneMissingUnit(makeItem({ workspace: { exists: true } })),
    false,
  );
  assert.equal(
    canAutoPruneMissingUnit(makeItem({ containers: [{ name: "still-running" }] })),
    false,
  );
  assert.equal(
    canAutoPruneMissingUnit(makeItem({ issues: [{ code: "missing_caddy_route" }] })),
    false,
  );
  assert.equal(
    canAutoPruneMissingUnit(makeItem({ db: { present: false } })),
    false,
  );
});
