/** * Talking to the WordPress under test. * * Everything goes through WP-CLI in the `cli` container of `compose.yml`, so * the tests configure the store exactly the way a store owner would. * * @module */ import { spawnSync } from "node:child_process"; import { dirname } from "node:path"; import { fileURLToPath } from "node:url"; const HERE = dirname(fileURLToPath(import.meta.url)); /** Where the site answers, from the host. */ export const SITE = "http://127.0.0.1:8090"; /** Hostname the fake API is reached by, from both the browser and PHP. */ export const FAKE_HOST = "fakecep"; /** Port the fake API listens on. */ export const FAKE_PORT = 8899; /** Base URL of the fake API, as both sides must spell it. */ export const FAKE_URL = `http://${FAKE_HOST}:${FAKE_PORT}`; /** * Run a WP-CLI command in the container. * * @param {string[]} args Arguments after `wp`. * @returns {string} Trimmed stdout. */ export function wp(args) { const result = spawnSync("docker", ["compose", "exec", "-T", "cli", "wp", ...args], { cwd: HERE, encoding: "utf8", }); if (result.status !== 0) { throw new Error(`wp ${args.join(" ")} failed:\n${result.stdout}\n${result.stderr}`); } return String(result.stdout).trim(); } /** * Run PHP inside WordPress. * * @param {string} code PHP to evaluate. * @returns {string} Trimmed stdout. */ export function wpEval(code) { return wp(["eval", code]); } /** * Set one option. * * @param {string} name Option name. * @param {string} value Option value. */ export function setOption(name, value) { wp(["option", "update", name, value]); } /** * Read the WordPress debug log and clear it. * * @returns {string} Whatever had been logged. */ export function drainDebugLog() { const result = spawnSync( "docker", [ "compose", "exec", "-T", "cli", "sh", "-c", "cat /var/www/html/wp-content/debug.log 2>/dev/null; : > /var/www/html/wp-content/debug.log 2>/dev/null || true", ], { cwd: HERE, encoding: "utf8" }, ); return String(result.stdout || "").trim(); }