summaryrefslogtreecommitdiffstats
path: root/test/wp.js
diff options
context:
space:
mode:
authorMurilo Ijanc <[email protected]>2026-07-26 16:41:48 -0300
committerMurilo Ijanc <[email protected]>2026-07-26 16:47:14 -0300
commit535dbda6b48171f8115b455e54790de94d97d42a (patch)
tree5c0410011e61a883bdf063ee612875a637fb1d22 /test/wp.js
downloadwoocommerce-29190494c82855f0f0ce7fb16f831c8f971022c5.tar.gz
Initial importv0.1.0
Diffstat (limited to 'test/wp.js')
-rw-r--r--test/wp.js88
1 files changed, 88 insertions, 0 deletions
diff --git a/test/wp.js b/test/wp.js
new file mode 100644
index 0000000..ab2d2a7
--- /dev/null
+++ b/test/wp.js
@@ -0,0 +1,88 @@
+/**
+ * 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();
+}