Files
MikrotikManager/backend/src/services/system-database-operation.test.ts
T

41 lines
3.0 KiB
TypeScript

import assert from "node:assert/strict"
import {test} from "node:test"
import {createDatabaseSchemaReadiness,executeDatabaseOperation} from "./system-database-operation.js"
function fixture(fail: string[] = []) {
const calls:string[]=[]
const step=(name:string)=>async()=>{calls.push(name);if(fail.includes(name))throw Error(name+" failed")}
return {calls,deps:{stopListener:step("stop listener"),stopScheduler:step("stop scheduler"),startListener:step("start listener"),startScheduler:step("start scheduler")},run:async()=>{await step("command")();return {cleanupError:"cleanup warning"}}}
}
test("confirmed command survives independent restart failures",async()=>{
for(const fail of [[],["start listener"],["start scheduler"],["start listener","start scheduler"]]){
const f=fixture(fail);const result=await executeDatabaseOperation(f.run,f.deps)
assert.equal(result.cleanupError,"cleanup warning");assert.equal(!!result.listenerError,fail.includes("start listener"));assert.equal(!!result.schedulerError,fail.includes("start scheduler"))
assert.deepEqual(f.calls,["stop listener","stop scheduler","command","start listener","start scheduler"])
}
})
test("core failure survives restart failures without command retry",async()=>{
const f=fixture(["command","start listener","start scheduler"])
await assert.rejects(executeDatabaseOperation(f.run,f.deps),/command failed/)
assert.equal(f.calls.filter(s=>s==="command").length,1);assert.deepEqual(f.calls.slice(-2),["start listener","start scheduler"])
})
test("stop failure prevents command and still attempts both restarts",async()=>{
for(const step of ["stop listener","stop scheduler"]){const f=fixture([step,"start listener"]);await assert.rejects(executeDatabaseOperation(f.run,f.deps),new RegExp(step+" failed"));assert.equal(f.calls.includes("command"),false);assert.deepEqual(f.calls.slice(-2),["start listener","start scheduler"])}
})
test("unconfirmed schema blocks both restarts across subsequent operations",async()=>{
const readiness=createDatabaseSchemaReadiness();readiness.beginReplacement()
for(let i=0;i<2;i++){
const f=fixture();const result=await executeDatabaseOperation(f.run,{...f.deps,beforeRestart:readiness.assertReady})
assert.match(result.listenerError!,/не подтверждена/);assert.match(result.schedulerError!,/не подтверждена/)
assert.deepEqual(f.calls,["stop listener","stop scheduler","command"])
}
readiness.confirmSchema();const f=fixture();await executeDatabaseOperation(f.run,{...f.deps,beforeRestart:readiness.assertReady})
assert.deepEqual(f.calls.slice(-2),["start listener","start scheduler"])
})
test("readiness refusal preserves primary failure and is checked once before either restart",async()=>{
const f=fixture(["command"]);let checks=0
await assert.rejects(executeDatabaseOperation(f.run,{...f.deps,beforeRestart:()=>{checks++;throw Error("schema unknown")}}),/command failed/)
assert.equal(checks,1);assert.deepEqual(f.calls,["stop listener","stop scheduler","command"])
})