| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { expiresIn, formatBytes, legacyArtifactsWorthAsking } from "./artifacts.ts"; |
| 5 | |
| 6 | test("sizes read as KB, MB or GB", () => { |
| 7 | assert.equal(formatBytes(10), "1 KB"); |
| 8 | assert.equal(formatBytes(412_870), "403 KB"); |
| 9 | assert.equal(formatBytes(18_734_120), "17.9 MB"); |
| 10 | assert.equal(formatBytes(5 * 1024 ** 3), "5.00 GB"); |
| 11 | }); |
| 12 | |
| 13 | test("expiry reads from now", () => { |
| 14 | const now = Date.parse("2026-10-08T12:00:00Z"); |
| 15 | assert.equal(expiresIn("2026-10-22T12:00:00Z", now), "expires in 14 days"); |
| 16 | assert.equal(expiresIn("2026-10-09T13:00:00Z", now), "expires tomorrow"); |
| 17 | assert.equal(expiresIn("2026-10-08T20:00:00Z", now), "expires today"); |
| 18 | assert.equal(expiresIn("2026-10-08T11:00:00Z", now), "expired"); |
| 19 | assert.equal(expiresIn(null, now), ""); |
| 20 | }); |
| 21 | |
| 22 | test("KV is asked for an old, finished run's artifacts only until they have expired", () => { |
| 23 | const old = { createdAt: "2026-10-07T12:00:00Z", status: "completed" }; |
| 24 | const now = Date.parse("2026-10-10T00:00:00Z"); |
| 25 | assert.ok(legacyArtifactsWorthAsking(old, now)); |
| 26 | // Still going: its artifacts are in R2, and its page refreshes. |
| 27 | assert.ok(!legacyArtifactsWorthAsking({ ...old, status: "in_progress" }, now)); |
| 28 | // Made after the move to R2. |
| 29 | assert.ok(!legacyArtifactsWorthAsking({ ...old, createdAt: "2026-10-09T08:00:00Z" }, now)); |
| 30 | // Every KV artifact has expired. |
| 31 | assert.ok(!legacyArtifactsWorthAsking(old, Date.parse("2026-10-22T00:00:00Z"))); |
| 32 | }); |