Skip to content

Commit 6e53586

Browse files
committed
Accept external provisioning artifacts in local gate
1 parent 2ebd0c0 commit 6e53586

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

scripts/cloudflare-local-gate.mjs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { spawn } from "node:child_process"
22
import { createHash, createHmac } from "node:crypto"
3-
import { mkdtemp, rm, writeFile } from "node:fs/promises"
3+
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"
44
import { tmpdir } from "node:os"
55
import { join } from "node:path"
66
import { stripVTControlCharacters } from "node:util"
@@ -15,6 +15,7 @@ const apiToken = "cloudflare-runtime-test-api-token"
1515
const administratorClaimSecret = "cloudflare-runtime-test-administrator-claim-secret"
1616
const coordinator = process.argv.includes("--coordinator=d1") ? "d1" : "durable-object"
1717
const publicProvisioning = process.argv.includes("--public-provisioning")
18+
const artifactPath = process.argv.find((argument) => argument.startsWith("--artifact="))?.slice("--artifact=".length)
1819
const wranglerConfig = coordinator === "d1" ? "packages/runtime-cloudflare/wrangler.d1.jsonc" : "packages/runtime-cloudflare/wrangler.jsonc"
1920
const stateDirectory = await mkdtemp(join(tmpdir(), "wp-codebox-cloudflare-gate-"))
2021
const cookies = []
@@ -135,7 +136,7 @@ async function run(command, args) {
135136
}
136137

137138
async function provisionStaticArtifact() {
138-
const artifact = {
139+
const fixture = {
139140
schema: "blocks-engine/php-transformer/site-artifact/v1",
140141
artifact_type: "website",
141142
version: 1,
@@ -157,7 +158,9 @@ async function provisionStaticArtifact() {
157158
content: "<!doctype html><html><head><meta charset=\"utf-8\"><title>About the Verified Artifact</title></head><body><main><h1>About the Verified Artifact</h1><p>A second imported page.</p></main></body></html>",
158159
}],
159160
}
160-
const serialized = JSON.stringify(artifact)
161+
const serialized = artifactPath ? await readFile(artifactPath, "utf8") : JSON.stringify(fixture)
162+
const artifact = JSON.parse(serialized)
163+
if (artifact?.schema !== "blocks-engine/php-transformer/site-artifact/v1") throw new Error(`The provisioning artifact at ${artifactPath} does not use the canonical portable schema.`)
161164
const sha256 = createHash("sha256").update(serialized).digest("hex")
162165
const key = `sites/default/import-artifacts/${sha256}.json`
163166
if (!publicProvisioning) {
@@ -176,6 +179,7 @@ async function provisionStaticArtifact() {
176179

177180
async function assertPublicProvisioning(input) {
178181
const authorization = { authorization: `Bearer ${apiToken}` }
182+
const expectedPageCount = JSON.parse(input.serialized).files.filter((file) => file.path.endsWith(".html")).length
179183
const stagedResponse = await fetch(`${origin}/v1/artifacts/${input.artifact.sha256}`, {
180184
method: "PUT",
181185
headers: { ...authorization, "content-type": "application/json" },
@@ -214,7 +218,7 @@ async function assertPublicProvisioning(input) {
214218
}
215219
const imported = operation?.receipt?.ssiResult
216220
if (operation?.state !== "succeeded" || imported?.status !== "imported" || imported.staticSiteImporterVersion !== "1.3.4" || !imported.themeSlug
217-
|| !imported.pages || Object.keys(imported.pages).length !== 2 || Object.values(imported.quality ?? {}).some((count) => count !== 0)
221+
|| !imported.pages || Object.keys(imported.pages).length !== expectedPageCount || Object.values(imported.quality ?? {}).some((count) => count !== 0)
218222
|| operation.receipt?.publication?.status !== "none") {
219223
throw new Error(`Public provisioning did not produce a terminal import receipt: ${JSON.stringify(operation)}.`)
220224
}
@@ -398,7 +402,7 @@ async function importStaticArtifact(input, expectedStatus = 201) {
398402

399403
async function assertImportedArtifactPages(adminHtml, imported) {
400404
const pageIds = Object.values(imported.pages ?? {}).filter(Number.isInteger)
401-
if (pageIds.length !== 2) throw new Error(`Static artifact import did not return two page IDs: ${JSON.stringify(imported)}.`)
405+
if (!pageIds.length) throw new Error(`Static artifact import did not return page IDs: ${JSON.stringify(imported)}.`)
402406
const pages = []
403407
for (const pageId of pageIds) {
404408
const response = await request(`${origin}/wp-json/wp/v2/pages/${pageId}?context=edit`, { headers: { "x-wp-nonce": restNonce(adminHtml) } })
@@ -410,10 +414,10 @@ async function assertImportedArtifactPages(adminHtml, imported) {
410414
const link = new URL(page.link)
411415
pages.push({ id: pageId, route: `${link.pathname}${link.search}`, raw })
412416
}
413-
const primary = pages.find(({ raw }) => raw.includes("Imported from a verified R2 artifact."))
414-
const secondary = pages.find(({ raw }) => raw.includes("A second imported page."))
415-
if (!primary || !secondary || secondary.route === "/") throw new Error(`Imported artifact routes are invalid: ${JSON.stringify(pages)}.`)
416-
return { primary, secondary }
417+
const primary = pages.find(({ route }) => route === "/")
418+
const secondary = pages.find(({ route }) => route !== "/")
419+
if (!primary || (pages.length > 1 && !secondary)) throw new Error(`Imported artifact routes are invalid: ${JSON.stringify(pages)}.`)
420+
return { primary, secondary: secondary ?? primary }
417421
}
418422

419423
async function updatePost(adminHtml, post, previousPublicationRevision) {

0 commit comments

Comments
 (0)