Files
MikrotikManager/backend/src/services/backup-storage-update.test.ts
T

54 lines
3.9 KiB
TypeScript

import assert from "node:assert/strict"
import { test } from "node:test"
import { assertBackupStorageConnectionChange, planBackupStorageUpdate } from "./backup-storage-update.js"
import { BackupStorageConflictError, withBackupStorageOperation, withBackupStorageSettingsUpdate } from "./backup-storage-operation.js"
const row = { provider: "s3" as const, s3Endpoint: "https://s3.example.test", s3Region: "us-east-1", s3Bucket: "bucket", s3Prefix: "prefix", s3AccessKeyId: "test-key", s3SecretAccessKey: "test-secret", s3ForcePathStyle: false, keepLocalCopy: true, lastTestAt: "2026-10-01T00:00:00Z", lastTestError: null, updatedAt: "2026-10-01T00:00:00Z" }
const now = "2026-10-01T00:01:00Z"
test("unchanged credentials and blank key preserve secret and last receipt", () => {
const result = planBackupStorageUpdate(row, { s3SecretAccessKey: "", s3Bucket: row.s3Bucket }, now)
assert.equal(result.s3SecretAccessKey, row.s3SecretAccessKey); assert.equal(result.lastTestAt, row.lastTestAt)
assert.equal(result.updatedAt, now)
})
test("every effective S3 configuration change invalidates previous test metadata", () => {
for (const patch of [{ provider: "local" as const }, { s3Endpoint: "https://other.example.test" }, { s3Region: "new" }, { s3Bucket: "new" }, { s3Prefix: "new" }, { s3AccessKeyId: "new" }, { s3SecretAccessKey: "new-test-secret" }, { s3ForcePathStyle: true }]) {
const result = planBackupStorageUpdate({ ...row, lastTestError: "old failure" }, patch, now)
assert.equal(result.lastTestAt, null); assert.equal(result.lastTestError, null)
}
})
test("local retention does not invent a connection change or discard a failed test", () => {
const result = planBackupStorageUpdate({ ...row, lastTestError: "failed" }, { keepLocalCopy: false }, now)
assert.equal(result.keepLocalCopy, false); assert.equal(result.lastTestAt, row.lastTestAt); assert.equal(result.lastTestError, "failed")
})
test("remote catalog prevents addressing existing keys through another S3 connection", () => {
for (const patch of [{ provider: "local" as const }, { s3Endpoint: "https://other.example.test" }, { s3Region: "new" }, { s3Bucket: "other" }, { s3ForcePathStyle: true }]) {
const next = planBackupStorageUpdate(row, patch, now)
assert.throws(() => assertBackupStorageConnectionChange(row, next, true), BackupStorageConflictError)
assert.doesNotThrow(() => assertBackupStorageConnectionChange(row, next, false))
}
for (const patch of [{ s3Prefix: "new" }, { s3AccessKeyId: "rotated" }, { s3SecretAccessKey: "rotated" }, { keepLocalCopy: false }]) {
assert.doesNotThrow(() => assertBackupStorageConnectionChange(row, planBackupStorageUpdate(row, patch, now), true))
}
assert.doesNotThrow(() => assertBackupStorageConnectionChange({ ...row, provider: "local" }, row, true))
assert.throws(() => assertBackupStorageConnectionChange({ ...row, provider: "local" }, { ...row, s3Bucket: "other" }, true), BackupStorageConflictError)
})
test("storage update rejects before mutation during upload, then sees the published catalog", async () => {
let release!: () => void
const uploaded = new Promise<void>(resolve => { release = resolve })
let hasRemoteEntries = false, updateStarted = false
const create = withBackupStorageOperation(async () => { await uploaded; hasRemoteEntries = true })
const update = withBackupStorageSettingsUpdate(async () => {
updateStarted = true
assertBackupStorageConnectionChange(row, planBackupStorageUpdate(row, { s3Bucket: "other" }, now), hasRemoteEntries)
})
const rejected = assert.rejects(update, BackupStorageConflictError)
await Promise.resolve(); assert.equal(updateStarted, false)
release(); await create; await rejected
assert.equal(updateStarted, false)
await assert.rejects(withBackupStorageSettingsUpdate(async () => {
assertBackupStorageConnectionChange(row, planBackupStorageUpdate(row, { s3Bucket: "other" }, now), hasRemoteEntries)
}), BackupStorageConflictError)
assert.equal(await withBackupStorageOperation(async () => "next operation"), "next operation")
})