// Build client/dist the way the browser e2e scripts need it, and record what
// was built so a later run can tell whether it can reuse the output.
//
// Why this exists. `npm run build` is a RELEASE build, and config.cljs resolves
//
// RELAY-MULTIADDR = or-default(VITE_RELAY_MULTIADDR, goog.DEBUG ? DEV : PROD)
//
// with goog.DEBUG false outside `npm run dev`. So a plain build points the app
// at the production relay, and every e2e script that spawns its own relay is
// then testing something it does not control: mesh's relay-outage and
// relay-restart scenarios killed a relay the app was never connected to and
// passed having proved nothing, and the mailbox stub never saw a byte because
// production's /.well-known/ap2p descriptor advertises the production mailbox,
// which wins over the compile-time pair (lib/descriptor.cljs).
//
// Pointing the build at 127.0.0.1 fixes both: the app dials the relay the test
// controls, and the descriptor fetch fails — which is exactly when the
// compile-time mailbox defaults apply.
//
// Freshness is a marker file, never a grep of the bundle: BOTH multiaddrs are
// `def`s in config.cljs, so the dev one appears in every build regardless of
// which one was selected, and grepping for it always "passes".
import { execSync } from "node:child_process";
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
/** config.cljs's DEV-RELAY-MULTIADDR — what `deploy/relay --dev` listens on. */
export const DEV_RELAY_MULTIADDR =
"/ip4/127.0.0.1/tcp/9090/ws/p2p/12D3KooWJU33yQvEdNF1AXcm5RWyLLyDKxyKkayRvDpJcJtYqhYt";
const CLIENT_DIR = fileURLToPath(new URL("..", import.meta.url));
const DIST_MAIN = fileURLToPath(new URL("../dist/assets/main.js", import.meta.url));
const MARKER = fileURLToPath(new URL("../dist/.e2e-build.json", import.meta.url));
/**
* Ensure dist is built with the dev relay plus `extra` goog-define env.
* Returns true if it rebuilt, false if the existing dist already matched.
*/
export function buildForE2E(extra = {}) {
const env = { VITE_RELAY_MULTIADDR: DEV_RELAY_MULTIADDR, ...extra };
const wanted = JSON.stringify(env, Object.keys(env).sort());
if (existsSync(DIST_MAIN) && existsSync(MARKER) && readFileSync(MARKER, "utf8") === wanted) {
console.log("dist already built for e2e — reusing it");
return false;
}
console.log("building dist for e2e (dev relay" + (Object.keys(extra).length ? " + " + Object.keys(extra).join(", ") : "") + ")…");
execSync("npm run build", { cwd: CLIENT_DIR, stdio: "inherit", env: { ...process.env, ...env } });
writeFileSync(MARKER, wanted);
return true;
}