| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { PAUSE_KEPT_MS, forgetPlatformPause, platformPause, readPause } from "../../../packages/contracts/src/platform.ts"; |
| 5 | import { STATIC_CARD, pausedCard } from "./paused.ts"; |
| 6 | |
| 7 | test("while renders are paused, a miss gets the cached brand card, kept a minute", async () => { |
| 8 | const brand = new Response("png", { headers: { "content-type": "image/png", "cache-control": "public, max-age=3600" } }); |
| 9 | const answer = pausedCard(brand); |
| 10 | assert.equal(answer.status, 200); |
| 11 | assert.equal(answer.headers.get("content-type"), "image/png"); |
| 12 | assert.equal(answer.headers.get("cache-control"), "public, max-age=60"); |
| 13 | assert.equal(await answer.text(), "png"); |
| 14 | }); |
| 15 | |
| 16 | test("with no brand card cached, a miss is sent to the static logo", () => { |
| 17 | const answer = pausedCard(undefined); |
| 18 | assert.equal(answer.status, 302); |
| 19 | assert.equal(answer.headers.get("location"), STATIC_CARD); |
| 20 | }); |
| 21 | |
| 22 | test("the platform pause is read once per 30 seconds, and nothing is paused when billing cannot say", async () => { |
| 23 | forgetPlatformPause(); |
| 24 | let calls = 0; |
| 25 | const billing = { |
| 26 | async fetch() { |
| 27 | calls += 1; |
| 28 | return new Response(JSON.stringify({ compute: false, schedules: true, indexing: false, renders: true })); |
| 29 | }, |
| 30 | }; |
| 31 | const now = 1_000_000; |
| 32 | assert.equal((await platformPause(billing, now)).renders, true); |
| 33 | assert.equal((await platformPause(billing, now + PAUSE_KEPT_MS - 1)).schedules, true); |
| 34 | assert.equal(calls, 1); |
| 35 | const down = { fetch: async () => new Response("no", { status: 500 }) }; |
| 36 | assert.deepEqual(await platformPause(down, now + PAUSE_KEPT_MS), { compute: false, schedules: false, indexing: false, renders: false }); |
| 37 | // No binding: never paused. |
| 38 | assert.equal((await platformPause(undefined)).renders, false); |
| 39 | assert.deepEqual(readPause({ renders: "yes", indexing: true }), { compute: false, schedules: false, indexing: true, renders: false }); |
| 40 | forgetPlatformPause(); |
| 41 | }); |