g1t/services/og/src/screenshot.test.ts

56 lines2,625 bytesCodeBlame
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { KEEP_DAYS, RETRY_AFTER_MS, attemptKey, expired, isCurrent, parseShot, shotKey, shouldAttempt } from "./screenshot.ts";
5
6const commit = "abc1234def5678abc1234def5678abc1234def56";
7
8test("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
27test("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
32test("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});
40
41test("a screenshot is current when it shows the commit and was taken after the deploy", () => {
42 const kept = { commit, capturedAt: "2026-10-06T10:00:00Z" };
43 assert.ok(isCurrent(kept, { host: "a.g1t.page", commit }));
44 assert.ok(isCurrent(kept, { host: "a.g1t.page", commit, since: "2026-10-06T09:00:00Z" }));
45 // Resumed (deployed again at the same commit) after it was taken.
46 assert.ok(!isCurrent(kept, { host: "a.g1t.page", commit, since: "2026-10-06T11:00:00Z" }));
47 assert.ok(!isCurrent(kept, { host: "a.g1t.page", commit: "f".repeat(40) }));
48 assert.deepEqual(parseShot({ host: "a-b.g1t.page", commit, since: "2026-10-06T11:00:00Z" })?.since, "2026-10-06T11:00:00Z");
49 assert.equal(parseShot({ host: "a-b.g1t.page", commit, since: "nope" })?.since, undefined);
50});
51
52test("kept objects expire after KEEP_DAYS", () => {
53 const now = Date.parse("2026-10-06T00:00:00Z");
54 assert.ok(expired(new Date(now - (KEEP_DAYS + 1) * 86_400_000), now));
55 assert.ok(!expired(new Date(now - 86_400_000), now));
56});