Files
2026-05-27 23:47:53 +01:00

85 lines
2.8 KiB
TypeScript

import { promises as fs } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import {
afterAll,
beforeAll,
describe,
expect,
it,
} from "@suaveplan/testing/runner";
import {
prepareFileCommand,
validateFileCommand,
} from "../../src/commands/files/files.js";
import { serveFileCommand } from "../../src/commands/files/serve-file.js";
import { createRootKeysCommand } from "../../src/commands/keys/keys.js";
const PW = "intpw1234";
function tmp(label: string): string {
return path.join(
tmpdir(),
`sk-int-${label}-${Date.now()}-${Math.random().toString(36).slice(2)}`
);
}
describe("integration: prepare → validate → serve round-trip", () => {
let dir = "";
let input = "";
let originalBytes = new Uint8Array();
const preparedDir = (): string => path.join(dir, "prepared");
beforeAll(async () => {
dir = tmp("rt");
await fs.mkdir(dir, { recursive: true });
await createRootKeysCommand(PW, { output: dir });
input = path.join(dir, "data.bin");
// 1 MiB + 1 byte to exercise multi-packet and an odd tail packet.
const size = 1_048_577;
originalBytes = new Uint8Array(size);
for (let i = 0; i < size; i++) {
originalBytes[i] = (i * 31 + 7) & 0xff;
}
await fs.writeFile(input, originalBytes);
});
afterAll(async () => {
await fs.rm(dir, { recursive: true, force: true });
});
it("prepares + validates + serves a binary file byte-identical to the input", {
timeout: 120000,
}, async () => {
const manifest = await prepareFileCommand(
input,
path.join(dir, "root.vault"),
PW,
{ output: preparedDir(), packetSize: 256 * 1024 }
);
expect(manifest.totalPackets).toBe(5);
expect(manifest.originalSize).toBe(originalBytes.byteLength);
const validation = await validateFileCommand(preparedDir());
expect(validation.isComplete).toBe(true);
expect(validation.validPackets).toBe(manifest.totalPackets);
expect(validation.invalidPackets).toEqual([]);
const reassembledPath = path.join(dir, "out.bin");
const result = await serveFileCommand(preparedDir(), {
output: reassembledPath,
});
expect(result.bytesWritten).toBe(originalBytes.byteLength);
const reassembled = await fs.readFile(reassembledPath);
expect(reassembled.byteLength).toBe(originalBytes.byteLength);
// Byte-identical comparison.
for (let i = 0; i < originalBytes.byteLength; i++) {
if (reassembled[i] !== originalBytes[i]) {
throw new Error(
`byte mismatch at offset ${i}: expected ${originalBytes[i]} got ${reassembled[i]}`
);
}
}
});
});