25 lines
1.8 KiB
TypeScript
25 lines
1.8 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { test } from "node:test"
|
|
import { MikrotikClient, MikrotikError } from "./mikrotik.js"
|
|
import { BackupRestoreUnknownError, importBackupOnce } from "./backup-restore.js"
|
|
const makeClient = () => new MikrotikClient({ host: "router.invalid", port: 80, useSsl: false, verifySsl: false, username: "test", password: "test" })
|
|
test("import does not fallback after ambiguous failure or command rejection", async () => {
|
|
for (const error of [new Error("connection lost after apply"), new Error("Invalid JSON"), new MikrotikError(500, "/import", "error"), new MikrotikError(400, "/import", "script failed"), new MikrotikError(401, "/import", "unauthorized")]) {
|
|
const client = makeClient(), calls: string[] = []
|
|
client.post = async <T>(endpoint: string): Promise<T> => { calls.push(endpoint); throw error }
|
|
await assert.rejects(client.importUploadedFile("config.rsc"), error); assert.deepEqual(calls, ["/import"])
|
|
}
|
|
})
|
|
test("unsupported endpoint retains one fallback and successful import is not repeated", async () => {
|
|
for (const status of [200, 404, 405]) {
|
|
const client = makeClient(), calls: string[] = []
|
|
client.post = async <T>(endpoint: string): Promise<T> => { calls.push(endpoint); if (endpoint === "/import" && status !== 200) throw new MikrotikError(status, endpoint, "unsupported"); return { ok: true } as T }
|
|
await client.importUploadedFile("config.rsc"); assert.deepEqual(calls, status === 200 ? ["/import"] : ["/import", "/execute"])
|
|
}
|
|
})
|
|
test("failed import has unknown outcome and no application retry", async () => {
|
|
let calls = 0
|
|
await assert.rejects(importBackupOnce("config.rsc", async () => { calls++; throw new Error("lost response") }), BackupRestoreUnknownError)
|
|
assert.equal(calls, 1); await importBackupOnce("config.rsc", async () => ({}))
|
|
})
|