Skip to content

Commit 9342ec4

Browse files
committed
Exercise queued D1 artifact imports
1 parent e5980f3 commit 9342ec4

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

scripts/cloudflare-local-gate.mjs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ try {
113113
const importedAdmin = await login()
114114
await assertImportedArtifactPages(importedAdmin, imported)
115115
const duplicateStateBefore = await (await fetch(`${origin}/?phase=r2-state`)).json()
116-
const duplicate = await importStaticArtifact(staticArtifactImport, 200)
116+
const duplicate = await importStaticArtifact(staticArtifactImport, coordinator === "d1" ? 202 : 200)
117117
const duplicateStateAfter = await (await fetch(`${origin}/?phase=r2-state`)).json()
118-
if (duplicate.status !== "duplicate" || duplicateStateAfter.version !== duplicateStateBefore.version || duplicateStateAfter.pointer?.revision !== duplicateStateBefore.pointer?.revision) throw new Error("Idempotent static artifact import changed canonical state.")
118+
if (!(["duplicate", ...(coordinator === "d1" ? ["imported"] : [])].includes(duplicate.status)) || duplicateStateAfter.version !== duplicateStateBefore.version || duplicateStateAfter.pointer?.revision !== duplicateStateBefore.pointer?.revision) throw new Error("Idempotent static artifact import changed canonical state.")
119119
const conflicting = await importStaticArtifact({ ...staticArtifactImport, import: { ...staticArtifactImport.import, slug: "different-artifact-site" } }, 409)
120120
const conflictStateAfter = await (await fetch(`${origin}/?phase=r2-state`)).json()
121121
if (conflicting.status !== "conflict" || conflictStateAfter.version !== duplicateStateBefore.version || conflictStateAfter.pointer?.revision !== duplicateStateBefore.pointer?.revision) throw new Error("Conflicting static artifact replay changed canonical state.")
@@ -394,18 +394,35 @@ async function createPost(adminHtml) {
394394
return { id: post.id, slug: post.slug, route: `${link.pathname}${link.search}`, title }
395395
}
396396

397-
async function importStaticArtifact(input, expectedStatus = 201) {
397+
async function importStaticArtifact(input, expectedStatus = coordinator === "d1" ? 202 : 201) {
398398
const response = await fetch(`${origin}/?phase=operator-static-artifact-import`, {
399399
method: "POST",
400400
headers: { authorization: `Bearer ${operatorToken}`, "content-type": "application/json" },
401401
body: JSON.stringify(input),
402402
})
403403
const body = await response.text()
404404
if (response.status !== expectedStatus) throw new Error(`Static artifact import failed: ${response.status} ${body}.\nWorker output:\n${output}`)
405-
const result = JSON.parse(body)
406-
if (expectedStatus === 201 && (result.status !== "imported" || result.staticSiteImporterVersion !== "1.3.4" || !result.themeSlug
405+
let result = JSON.parse(body)
406+
if (expectedStatus === 202) {
407+
if (typeof result.operationId !== "string") throw new Error(`Queued static artifact import omitted its operation ID: ${body}.`)
408+
for (let tick = 0; tick < 10; tick++) {
409+
await runScheduledCron()
410+
const operationResponse = await fetch(`${origin}/?phase=operator-static-artifact-operation&operationId=${encodeURIComponent(result.operationId)}`, {
411+
headers: { authorization: `Bearer ${operatorToken}` },
412+
})
413+
const operationBody = await operationResponse.text()
414+
if (!operationResponse.ok) throw new Error(`Queued static artifact operation read failed: ${operationResponse.status} ${operationBody}.`)
415+
const operation = JSON.parse(operationBody)
416+
if (operation.state === "succeeded") {
417+
result = operation.receipt?.ssiResult
418+
break
419+
}
420+
if (operation.state === "failed") throw new Error(`Queued static artifact operation failed: ${operationBody}.`)
421+
}
422+
}
423+
if (expectedStatus !== 409 && (!result || typeof result !== "object" || result.status !== "imported" || result.staticSiteImporterVersion !== "1.3.4" || !result.themeSlug
407424
|| !result.pages || !Object.keys(result.pages).length || Object.values(result.quality ?? {}).some((count) => count !== 0)
408-
|| !response.headers.get("x-wp-codebox-canonical-revision") || !response.headers.get("x-wp-codebox-canonical-version"))) {
425+
|| (expectedStatus === 201 && (!response.headers.get("x-wp-codebox-canonical-revision") || !response.headers.get("x-wp-codebox-canonical-version"))))) {
409426
throw new Error(`Static artifact import returned invalid evidence: ${body}.`)
410427
}
411428
return result
@@ -759,10 +776,11 @@ async function assertCoordinatorAdoption() {
759776
if (!adoption.ok || !adopted.adopted || adopted.version !== before.version || adopted.pointer?.revision !== before.pointer?.revision) {
760777
throw new Error(`Exact coordinator adoption failed: status=${adoption.status} payload=${JSON.stringify(adopted)}.`)
761778
}
779+
const divergentRevision = "00000000-0000-4000-8000-000000000000"
762780
const divergent = await fetch(`${origin}/?phase=operator-adopt`, {
763781
method: "POST",
764782
headers: { authorization: `Bearer ${operatorToken}`, "content-type": "application/json" },
765-
body: JSON.stringify({ pointer: before.pointer, version: before.version + 1 }),
783+
body: JSON.stringify({ pointer: { ...before.pointer, revision: divergentRevision, manifestKey: before.pointer.manifestKey.replace(before.pointer.revision, divergentRevision) }, version: before.version }),
766784
})
767785
if (divergent.status !== 409) throw new Error(`Divergent coordinator adoption was not rejected: ${divergent.status} ${await divergent.text()}.`)
768786
await assertCoordinatorBackend()

0 commit comments

Comments
 (0)