import { execFileSync } from "node:child_process";
import { join } from "node:path";

const defaultBinary = process.execPath;
const defaultArgs = [
	join(import.meta.dirname, "..", "..", "dist", "loader.js"),
];
const binary = process.env.SF_SMOKE_BINARY || defaultBinary;
const args = process.env.SF_SMOKE_BINARY
	? ["--version"]
	: [...defaultArgs, "--version"];

const output = execFileSync(binary, args, {
	encoding: "utf8",
	timeout: 30_000,
}).trim();

if (!/^\d+\.\d+\.\d+/.test(output)) {
	console.error(`Version output does not match expected pattern: "${output}"`);
	process.exit(1);
}
