g1t/services/runner/src/backup.test.ts

36 lines1,261 bytesCodeBlame
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import type { BackupClaim } from "@g1t/contracts";
5
6import { backupEnv, backupPace, backupSandboxName } from "./backup.ts";
7
8const claim: BackupClaim = {
9 jobId: "bkp_01",
10 token: "secret-token",
11 repoId: "repo_1",
12 path: { namespace: "acme", name: "rocket" },
13};
14
15test("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
26test("one sandbox per job", () => {
27 assert.equal(backupSandboxName(claim), "backup-bkp_01");
28 assert.notEqual(backupSandboxName({ ...claim, jobId: "bkp_02" }), backupSandboxName(claim));
29});
30
31test("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});