23 lines
2.3 KiB
TypeScript
23 lines
2.3 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { test } from "node:test"
|
|
import type { BackupJobStatusResponse } from "@mmapp/contracts/backups"
|
|
import { backupJobResponseSchema } from "@mmapp/contracts/backups"
|
|
import { processBackupJob, recordBackupEvent } from "./backup-job-execution.js"
|
|
const item = { id: "backup", serverId: 1, serverName: "router", filename: "config.rsc", sizeBytes: 0, createdAt: "2026-10-01T00:00:00Z", kind: "manual" as const, storage: "local" as const }
|
|
const job = (): BackupJobStatusResponse => ({ id: "job", serverIds: ["1", "2"], status: "queued", requestedAt: "2026-10-01T00:00:00Z", total: 2, completed: 0, created: [], failures: [] })
|
|
test("event failure preserves done and actual created files", async () => {
|
|
const row = job(), warnings: unknown[] = []
|
|
await processBackupJob(row, ["1", "2"], undefined, { create: async id => ({ ...item, id, serverId: Number(id) }), event: input => recordBackupEvent(input, async () => { throw new Error("event unavailable") }, error => { warnings.push(error) }) })
|
|
assert.equal(row.status, "done"); assert.equal(row.created.length, 2); assert.equal(row.failures.length, 0); assert.equal(warnings.length, 1); backupJobResponseSchema.parse(row)
|
|
})
|
|
test("one failed device keeps partial files and completes every target", async () => {
|
|
const row = job()
|
|
await processBackupJob(row, ["1", "2"], undefined, { create: async id => { if (id === "2") throw new Error("device unavailable"); return item }, event: async input => { assert.equal(input.level, "warning") } })
|
|
assert.equal(row.status, "done"); assert.equal(row.completed, 2); assert.equal(row.created.length, 1); assert.deepEqual(row.failures, [{ serverId: "2", error: "device unavailable" }]); backupJobResponseSchema.parse(row)
|
|
})
|
|
test("S3 upload error remains a local copy and a warning rather than full success", async () => {
|
|
const row = job(); row.total = 1; row.serverIds = ["1"]
|
|
await processBackupJob(row, ["1"], undefined, { create: async () => ({ ...item, uploadError: "S3 unavailable" }), event: async input => { assert.equal(input.level, "warning"); assert.match(input.message, /ошибок загрузки: 1/) } })
|
|
assert.equal(row.status, "done"); assert.equal(row.created[0].storage, "local"); assert.equal(row.created[0].uploadError, "S3 unavailable"); backupJobResponseSchema.parse(row)
|
|
})
|