import { existsSync } from "node:fs";
import { join } from "node:path";

// Tests must run on the laptop and the buildboxes alike: resolve node the same way
// packaging/frontdoor-migrate.sh does — env override, the pinned v24, then PATH.
export function resolveNode(): string {
  const fromEnv = process.env.OVERDECK_FRONTDOOR_NODE;
  if (fromEnv && existsSync(fromEnv)) return fromEnv;
  const pinned = join(process.env.HOME ?? "", ".claude", "bin", "node");
  if (existsSync(pinned)) return pinned;
  const fromPath = Bun.which("node");
  if (fromPath) return fromPath;
  throw new Error("no node binary found for frontdoor tests");
}
