29 lines
2.3 KiB
TypeScript
29 lines
2.3 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { mkdtemp, writeFile, mkdir, rm, lstat } from "node:fs/promises"
|
|
import os from "node:os"
|
|
import path from "node:path"
|
|
import { withBackupLocalFileStates } from "./backup-local-file-state.js"
|
|
import type { BackupItemDto } from "@mmapp/contracts/backups"
|
|
|
|
const directory = await mkdtemp(path.join(os.tmpdir(), "mm-backup-state-"))
|
|
const row = (filename: string, storage: BackupItemDto["storage"] = "local"): BackupItemDto => ({ id: filename, filename, serverId: 1, serverName: "fixture", sizeBytes: 0, createdAt: "2026-10-14T00:00:00Z", kind: "manual", storage })
|
|
try {
|
|
await writeFile(path.join(directory, "available.rsc"), "# own fixture\n")
|
|
await mkdir(path.join(directory, "directory.rsc"))
|
|
const rows = [row("available.rsc"), row("missing.rsc"), row("missing-both.rsc", "both"), row("remote.rsc", "s3"), row("directory.rsc"), row("../outside.rsc")]
|
|
const result = await withBackupLocalFileStates(rows, directory)
|
|
assert.deepEqual(result.map(r => r.localFileStatus), ["available", "missing", "missing", "not-stored", "unknown", "unknown"])
|
|
assert(rows.every(r => r.localFileStatus === undefined))
|
|
assert.equal((await lstat(path.join(directory, "available.rsc"))).isFile(), true)
|
|
const denied = await withBackupLocalFileStates([row("denied.rsc")], directory, (async () => { throw Object.assign(new Error("denied"), { code: "EACCES" }) }) as typeof lstat)
|
|
assert.equal(denied[0].localFileStatus, "unknown")
|
|
let active = 0, max = 0
|
|
const inspect = (async () => { active++; max = Math.max(max, active); await new Promise(r => setTimeout(r, 1)); active--; return lstat(path.join(directory, "available.rsc")) }) as typeof lstat
|
|
const many = await withBackupLocalFileStates(Array.from({ length: 100 }, (_, i) => row(`${i}.rsc`)), directory, inspect)
|
|
assert.equal(many.length, 100); assert(max <= 8); assert.equal(active, 0)
|
|
let touched = false
|
|
await withBackupLocalFileStates([row("remote-only.rsc", "s3"), row("../outside.rsc")], directory, (async () => { touched = true; throw Error("unexpected") }) as typeof lstat)
|
|
assert.equal(touched, false)
|
|
console.log("backup local states: real file/missing/both/remote/directory/path, permission error and bounded100 passed")
|
|
} finally { await rm(directory, { recursive: true, force: true }) }
|