pr_01m47d24b0e6n91zwymwxg0vpx/services/og/src/screenshot.test.ts
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { RETRY_AFTER_MS, attemptKey, parseShot, shotKey, shouldAttempt } from "./screenshot.ts"; |
| 5 | |
| 6 | const commit = "abc1234def5678abc1234def5678abc1234def56"; |
| 7 | |
| 8 | test("only apps on g1t.page, with a commit, are taken", () => { |
| 9 | assert.deepEqual(parseShot({ host: "Web-Acme.g1t.page", commit: commit.toUpperCase() }), { host: "web-acme.g1t.page", commit }); |
| 10 | for (const host of [ |
| 11 | "web-acme.g1t.sh", |
| 12 | "example.com", |
| 13 | "g1t.page", |
| 14 | "a.b.g1t.page", |
| 15 | "domains.g1t.page", |
| 16 | "-web.g1t.page", |
| 17 | "web-acme.g1t.page.evil.com", |
| 18 | "web acme.g1t.page", |
| 19 | ]) { |
| 20 | assert.equal(parseShot({ host, commit }), null, host); |
| 21 | } |
| 22 | assert.equal(parseShot({ host: "web-acme.g1t.page", commit: "main" }), null); |
| 23 | assert.equal(parseShot({ host: "web-acme.g1t.page" }), null); |
| 24 | assert.equal(parseShot(null), null); |
| 25 | }); |
| 26 | |
| 27 | test("keys are by hostname", () => { |
| 28 | assert.equal(shotKey("web-acme.g1t.page"), "production/web-acme.g1t.page.jpg"); |
| 29 | assert.equal(attemptKey("web-acme.g1t.page"), "attempts/web-acme.g1t.page"); |
| 30 | }); |
| 31 | |
| 32 | test("a failed attempt at a commit waits before it is tried again", () => { |
| 33 | const now = Date.parse("2026-10-05T12:00:00Z"); |
| 34 | assert.equal(shouldAttempt(null, commit, now), true); |
| 35 | const just = { commit, at: new Date(now - 1000).toISOString() }; |
| 36 | assert.equal(shouldAttempt(just, commit, now), false); |
| 37 | assert.equal(shouldAttempt(just, "fff0000", now), true); |
| 38 | assert.equal(shouldAttempt({ commit, at: new Date(now - RETRY_AFTER_MS).toISOString() }, commit, now), true); |
| 39 | }); |