10 lines
2.2 KiB
TypeScript
10 lines
2.2 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import {test} from "node:test"
|
|
import {executeSystemDatabaseRestore} from "./system-database-restore-execution.js"
|
|
const dump=Buffer.from("PGDMPfixture")
|
|
function fixture(){const calls:string[]=[];let failWrite=false,failRun=false,failRemove=false;const deps={allocate:async()=>{calls.push("allocate");return "fixture.dump"},write:async()=>{calls.push("write");if(failWrite)throw Error("write failed")},run:async(args:string[])=>{calls.push("run");assert.ok(args.includes("--single-transaction"));assert.deepEqual(args.slice(-3),["-d","postgres://fixture","fixture.dump"]);if(failRun)throw Error("restore failed")},remove:async()=>{calls.push("remove");if(failRemove)throw Error("cleanup failed")}};return {calls,deps,setWrite:()=>failWrite=true,setRun:()=>failRun=true,setRemove:()=>failRemove=true}}
|
|
test("invalid archive never allocates or runs command",async()=>{const f=fixture();for(const data of [Buffer.alloc(0),Buffer.from("sqlite")])await assert.rejects(executeSystemDatabaseRestore(data,"postgres://fixture",f.deps),/PGDMP/);assert.deepEqual(f.calls,[])})
|
|
test("restore runs one transactional command and cleans file after success",async()=>{const f=fixture();assert.deepEqual(await executeSystemDatabaseRestore(dump,"postgres://fixture",f.deps),{});assert.deepEqual(f.calls,["allocate","write","run","remove"])})
|
|
test("write or restore failure always cleans temporary file and never retries",async()=>{for(const step of ["write","run"]){const f=fixture();if(step==="write")f.setWrite();else f.setRun();await assert.rejects(executeSystemDatabaseRestore(dump,"postgres://fixture",f.deps),/failed/);assert.deepEqual(f.calls,step==="write"?["allocate","write","remove"]:["allocate","write","run","remove"])}})
|
|
test("cleanup failure keeps confirmed restore result; main failure is preserved",async()=>{const f=fixture();f.setRemove();assert.deepEqual(await executeSystemDatabaseRestore(dump,"postgres://fixture",f.deps),{cleanupError:"Не удалось удалить временный файл восстановления"});const bad=fixture();bad.setRun();bad.setRemove();await assert.rejects(executeSystemDatabaseRestore(dump,"postgres://fixture",bad.deps),/restore failed; Не удалось удалить/)})
|