// Card month1-switches R10 (CLI half): `setup` sends the two answers a big // first-month order's quote can ask for only when their flags are given. // Runs the BUILT dist/ against a local stand-in for the API, like // campaign.test.mjs, and asserts the exact body. import { test } from "node:test"; import assert from "node:assert/strict"; import { execFile } from "node:child_process"; import { createServer } from "node:http"; import { fileURLToPath } from "node:url"; import { promisify } from "node:util"; const CLI = fileURLToPath(new URL("../dist/index.js", import.meta.url)); const run = promisify(execFile); const REQUIRED = [ "--brand", "Acme", "--primary-domain", "acme.com", "--physical-address", "1 Real St, Austin, TX 78701", "--sender-identity", "Jo Smith ", ]; // Wave 1022 R12: runs `setup` with a stand-in API and returns every request it made // plus the CLI's exit code and stderr, so a local refusal is observable as "no request". async function setupRun(flags) { const requests = []; const server = createServer((req, res) => { let raw = ""; req.on("data", (chunk) => (raw += chunk)); req.on("end", () => { requests.push({ path: req.url, body: JSON.parse(raw) }); res.writeHead(200, { "content-type": "application/json" }); res.end("{}"); }); }); await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); try { const env = { ...process.env, AGENT_COLD_EMAIL_API: `http://127.0.0.1:${server.address().port}`, AGENT_COLD_EMAIL_TOKEN: "test-token" }; try { await run(process.execPath, [CLI, "setup", ...flags], { env }); return { code: 0, stderr: "", requests }; } catch (err) { return { code: err.code, stderr: String(err.stderr), requests }; } } finally { await new Promise((resolve) => server.close(resolve)); } } async function setupBody(flags) { const bodies = []; const server = createServer((req, res) => { let raw = ""; req.on("data", (chunk) => (raw += chunk)); req.on("end", () => { bodies.push({ method: req.method, path: req.url, body: JSON.parse(raw) }); res.writeHead(200, { "content-type": "application/json" }); res.end("{}"); }); }); await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); try { const env = { ...process.env, AGENT_COLD_EMAIL_API: `http://127.0.0.1:${server.address().port}`, AGENT_COLD_EMAIL_TOKEN: "test-token" }; await run(process.execPath, [CLI, "setup", ...flags], { env }); } finally { await new Promise((resolve) => server.close(resolve)); } assert.equal(bodies.length, 1); assert.equal(bodies[0].path, "/setup-infrastructure"); return bodies[0].body; } test("--dot-com-domains and --prepay-second-month send dotComDomains:true and prepaySecondMonth:true", async () => { const body = await setupBody([...REQUIRED, "--domains", "3", "--inboxes-each", "4", "--dot-com-domains", "--prepay-second-month"]); assert.equal(body.dotComDomains, true); assert.equal(body.prepaySecondMonth, true); assert.equal(body.domains, 3); }); test("without the flags the body carries neither field", async () => { const body = await setupBody([...REQUIRED, "--domains", "3"]); assert.equal("dotComDomains" in body, false); assert.equal("prepaySecondMonth" in body, false); }); test("R12: setup without a postal address or sender is refused locally, with no request", async () => { for (const missing of ["--brand", "--primary-domain", "--physical-address", "--sender-identity"]) { const flags = []; for (let i = 0; i < REQUIRED.length; i += 2) if (REQUIRED[i] !== missing) flags.push(REQUIRED[i], REQUIRED[i + 1]); const r = await setupRun(flags); assert.equal(r.code, 1, `${missing} missing must exit 1`); assert.equal(r.requests.length, 0, `${missing} missing must not reach the API`); assert.ok(r.stderr.includes(missing), `message names ${missing}: ${r.stderr}`); } }); test("R12: no placeholder value is ever sent", async () => { const body = await setupBody([...REQUIRED]); const text = JSON.stringify(body); for (const bad of ["Springfield", "123 Main", "Sample Brand", "sample-brand"]) assert.ok(!text.includes(bad), bad); assert.equal(body.physicalAddress, "1 Real St, Austin, TX 78701"); assert.equal(body.senderIdentity, "Jo Smith "); }); test("F5: setup defaults to 3 inboxes per domain and can carry consent, registrant and quoteOnly", async () => { const registrant = { firstName: "Jo", lastName: "Smith", email: "jo@acme.com", phone: "+15125550100", addressLine1: "1 Real St", city: "Austin", state: "TX", country: "US", postalCode: "78701" }; const body = await setupBody([...REQUIRED, "--register-domains", "--registrant", JSON.stringify(registrant), "--quote-only"]); assert.equal(body.inboxesEach, 3); assert.equal(body.registerDomains, true); assert.deepEqual(body.registrant, registrant); assert.equal(body.quoteOnly, true); const plain = await setupBody([...REQUIRED]); for (const k of ["registerDomains", "registrant", "quoteOnly"]) assert.equal(k in plain, false, k); }); test("F5: a --registrant that is not a JSON object is refused locally", async () => { const r = await setupRun([...REQUIRED, "--registrant", "not json"]); assert.equal(r.code, 1); assert.equal(r.requests.length, 0); assert.ok(r.stderr.includes("--registrant")); }); test("R12: --help names every setup flag an agent needs, so it never learns the old short form", async () => { const { stdout } = await run(process.execPath, [CLI, "--help"]); for (const flag of ["--brand", "--primary-domain", "--physical-address", "--sender-identity", "--quote-only", "--register-domains", "--registrant"]) { assert.match(stdout, new RegExp(`setup [^\\n]*(\\n {8}[^\\n]*)*${flag}\\b`), `--help setup line is missing ${flag}`); } });