| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import type { BackupClaim } from "@g1t/contracts"; |
| 5 | |
| 6 | import { backupEnv, backupPace, backupSandboxName } from "./backup.ts"; |
| 7 | |
| 8 | const claim: BackupClaim = { |
| 9 | jobId: "bkp_01", |
| 10 | token: "secret-token", |
| 11 | repoId: "repo_1", |
| 12 | path: { namespace: "acme", name: "rocket" }, |
| 13 | }; |
| 14 | |
| 15 | test("the sandbox gets its job and token, and no git credential", () => { |
| 16 | const env = backupEnv(claim, "https://api.g1t.sh"); |
| 17 | assert.deepEqual(env, { |
| 18 | MODE: "backup", |
| 19 | G1T_API: "https://api.g1t.sh", |
| 20 | BACKUP_JOB: "bkp_01", |
| 21 | BACKUP_TOKEN: "secret-token", |
| 22 | }); |
| 23 | assert.ok(!("G1T_TOKEN" in env) && !("GIT_REMOTE" in env)); |
| 24 | }); |
| 25 | |
| 26 | test("one sandbox per job", () => { |
| 27 | assert.equal(backupSandboxName(claim), "backup-bkp_01"); |
| 28 | assert.notEqual(backupSandboxName({ ...claim, jobId: "bkp_02" }), backupSandboxName(claim)); |
| 29 | }); |
| 30 | |
| 31 | test("the pace comes from the variables, and 0 turns backups off", () => { |
| 32 | assert.deepEqual(backupPace(undefined, undefined), { perSweep: 4, running: 6 }); |
| 33 | assert.deepEqual(backupPace("2", "3"), { perSweep: 2, running: 3 }); |
| 34 | assert.deepEqual(backupPace("0", "6"), { perSweep: 0, running: 6 }); |
| 35 | assert.deepEqual(backupPace("many", "-1"), { perSweep: 4, running: 6 }); |
| 36 | }); |