diff --git a/tests/fixtures/reporting-fetch.mjs b/tests/fixtures/reporting-fetch.mjs new file mode 100644 index 0000000..ad985b2 --- /dev/null +++ b/tests/fixtures/reporting-fetch.mjs @@ -0,0 +1,55 @@ +import { writeFileSync } from "node:fs"; + +// Keep scenario values separate from executable preload source. +const scenario = process.env.COMMERCEKIT_TEST_DEPLOY_STATUS; +if ( + !new Set([ + "DEPLOYED", + "FAIL", + "UNDEPLOYED", + "FUTURE", + "handoff", + "invalid", + ]).has(scenario) +) + throw new Error("Unexpected synthetic deployment scenario."); +const handoff = scenario === "handoff"; + +let requests = 0; +process.once("exit", () => + writeFileSync(new URL("./requests", import.meta.url), String(requests)), +); +globalThis.fetch = async (url, init) => { + requests++; + url = String(url); + if ( + ![ + "https://auth.example.invalid", + "https://portalapi.commerce.ondemand.com", + ].includes(new URL(url).origin) + ) + throw new Error("Unexpected network destination"); + if (init.redirect !== "error") throw new Error("Unsafe redirects"); + if (url.includes("oauth2")) + return Response.json({ + access_token: "SYNTHETIC_BUNDLE_TOKEN", + token_type: "Bearer", + }); + if (init.method === "POST") + return Response.json({ code: "synthetic.1" }, { status: 201 }); + if (url.endsWith("/progress")) + return Response.json({ + deploymentStatus: handoff ? "DEPLOYING" : scenario, + percentage: 90, + }); + if (url.endsWith("/decisions")) + return Response.json({ deploymentDecisions: [] }); + return Response.json({ + code: "synthetic.1", + status: "DEPLOYING", + strategy: handoff ? "GREEN" : "ROLLING_UPDATE", + isCanaryAvailable: handoff, + canaryStatus: "STARTED", + expireAtTimestamp: "2099-01-01T00:00:00Z", + }); +}; diff --git a/tests/reporting-bundle.test.ts b/tests/reporting-bundle.test.ts index b5cdf6d..d7286ca 100644 --- a/tests/reporting-bundle.test.ts +++ b/tests/reporting-bundle.test.ts @@ -8,6 +8,7 @@ import { import { tmpdir } from "node:os"; import { join } from "node:path"; import { spawnSync } from "node:child_process"; +import { pathToFileURL } from "node:url"; import { expect, it } from "vitest"; import { parseNotificationEventJson } from "@commercekit-tools/notification-contract"; import { inputDefinitions } from "../src/reporting.js"; @@ -41,18 +42,15 @@ it.each(scenarios)( const outputPath = join(dir, "output"); const summaryPath = join(dir, "summary"); const requestsPath = join(dir, "requests"); - const preload = join(dir, "fetch.cjs"); + const preload = join(dir, "fetch.mjs"); writeFileSync(outputPath, ""); writeFileSync(summaryPath, ""); - writeFileSync( - preload, - `const fs=require('node:fs');let requests=0;process.once('exit',()=>fs.writeFileSync(${JSON.stringify(requestsPath)},String(requests)));global.fetch=async(url,init)=>{requests++;url=String(url);if(!['https://auth.example.invalid','https://portalapi.commerce.ondemand.com'].includes(new URL(url).origin))throw Error('Unexpected network destination');if(init.redirect!=='error')throw Error('Unsafe redirects');if(url.includes('oauth2'))return Response.json({access_token:'SYNTHETIC_BUNDLE_TOKEN',token_type:'Bearer'});if(init.method==='POST')return Response.json({code:'synthetic.1'},{status:201});if(url.endsWith('/progress'))return Response.json({deploymentStatus:${JSON.stringify(status === "handoff" ? "DEPLOYING" : status)},percentage:90});if(url.endsWith('/decisions'))return Response.json({deploymentDecisions:[]});return Response.json({code:'synthetic.1',status:'DEPLOYING',strategy:${JSON.stringify(status === "handoff" ? "GREEN" : "ROLLING_UPDATE")},isCanaryAvailable:${status === "handoff"},canaryStatus:'STARTED',expireAtTimestamp:'2099-01-01T00:00:00Z'});};`, - ); + copyFileSync("tests/fixtures/reporting-fetch.mjs", preload); copyFileSync("dist/index.js", join(dir, "index.js")); copyFileSync("dist/package.json", join(dir, "package.json")); const child = spawnSync( process.execPath, - ["--require", preload, join(dir, "index.js")], + ["--import", pathToFileURL(preload).href, join(dir, "index.js")], { cwd: dir, encoding: "utf8", @@ -63,6 +61,7 @@ it.each(scenarios)( GITHUB_TOKEN: "", GH_TOKEN: "", NODE_PATH: "", + COMMERCEKIT_TEST_DEPLOY_STATUS: status, "INPUT_TOKEN-ENDPOINT": "https://auth.example.invalid/oauth2/token", "INPUT_CLIENT-ID": "SYNTHETIC_BUNDLE_CLIENT", "INPUT_CLIENT-SECRET": "SYNTHETIC_BUNDLE_SECRET",