-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestBackupsAndCronsExtended.js
More file actions
101 lines (81 loc) · 3.2 KB
/
testBackupsAndCronsExtended.js
File metadata and controls
101 lines (81 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import BosBase from "bosbase";
function createStubClient() {
const pb = new BosBase("https://backups.test");
const calls = [];
pb.send = async (path, options = {}) => {
calls.push({ path, options });
if (path === "/api/backups" && options.method === "POST") {
return {};
}
if (path === "/api/backups/upload" && options.method === "POST") {
return {};
}
if (path.startsWith("/api/backups/") && path.endsWith("/restore")) {
return {};
}
if (path.startsWith("/api/backups/") && options.method === "DELETE") {
return {};
}
if (path.startsWith("/api/crons/") && options.method === "POST") {
return {};
}
throw new Error(`Unexpected request: ${path} ${options.method || ""}`);
};
return { pb, calls };
}
async function main() {
const { pb, calls } = createStubClient();
try {
console.log("[INFO] Testing BackupService create/upload/delete/restore helpers...");
const created = await pb.backups.create("nightly");
if (!created) {
throw new Error("backups.create did not resolve to true");
}
const createCall = calls.find(
(c) => c.path === "/api/backups" && c.options.method === "POST",
);
if (!createCall || createCall.options.body?.name !== "nightly") {
throw new Error("backups.create did not send expected payload");
}
await pb.backups.upload({ file: "blob" });
const uploadCall = calls.find((c) => c.path === "/api/backups/upload");
if (!uploadCall || uploadCall.options.method !== "POST") {
throw new Error("backups.upload did not hit /api/backups/upload");
}
await pb.backups.delete("nightly.bak");
const deleteCall = calls.find(
(c) => c.path.startsWith("/api/backups/") && c.options.method === "DELETE",
);
if (!deleteCall || !deleteCall.path.endsWith("nightly.bak")) {
throw new Error("backups.delete did not encode key");
}
await pb.backups.restore("nightly.bak");
const restoreCall = calls.find((c) => c.path.endsWith("/restore"));
if (!restoreCall || restoreCall.options.method !== "POST") {
throw new Error("backups.restore did not POST to restore endpoint");
}
const url = pb.backups.getDownloadURL("TOKEN123", "folder/backup.zip");
const encodedKey = encodeURIComponent("folder/backup.zip");
if (!url.includes("TOKEN123") || !url.includes(encodedKey)) {
throw new Error("getDownloadURL did not build expected download url");
}
console.log("[SUCCESS] BackupService helpers verified with stubbed client");
console.log("[INFO] Testing CronService run helper...");
await pb.crons.run("job with spaces");
const cronCall = calls.find((c) => c.path.startsWith("/api/crons/"));
if (
!cronCall ||
!cronCall.path.endsWith("job%20with%20spaces") ||
cronCall.options.method !== "POST"
) {
throw new Error("crons.run did not POST to encoded cron path");
}
console.log("[SUCCESS] CronService run helper verified");
console.log("\n========== Backup and Cron helper coverage completed ==========");
} catch (error) {
console.error("[ERROR] Backup/Cron helper coverage failed:");
console.error(error?.stack || error);
process.exit(1);
}
}
main();