49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import fs from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
import {
|
|
deleteCatalogFiles,
|
|
readPage,
|
|
saveCatalogPages,
|
|
StorageError,
|
|
} from "../../server/storage";
|
|
import { TINY_JPEG, TINY_PNG } from "./helpers";
|
|
|
|
const temps: string[] = [];
|
|
|
|
async function tempDir() {
|
|
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "dc-storage-"));
|
|
temps.push(dir);
|
|
return dir;
|
|
}
|
|
|
|
afterEach(async () => {
|
|
await Promise.all(temps.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })));
|
|
});
|
|
|
|
describe("saveCatalogPages", () => {
|
|
it("writes numbered jpegs and can read them back", async () => {
|
|
const dir = await tempDir();
|
|
await saveCatalogPages(dir, "abc", [TINY_JPEG, TINY_JPEG]);
|
|
const page1 = await readPage(dir, "abc", 1);
|
|
expect(page1.equals(TINY_JPEG)).toBe(true);
|
|
const page2 = await readPage(dir, "abc", 2);
|
|
expect(page2.equals(TINY_JPEG)).toBe(true);
|
|
});
|
|
|
|
it("rejects non-jpeg pages", async () => {
|
|
const dir = await tempDir();
|
|
await expect(saveCatalogPages(dir, "abc", [TINY_PNG])).rejects.toBeInstanceOf(
|
|
StorageError,
|
|
);
|
|
});
|
|
|
|
it("deletes files so later reads fail", async () => {
|
|
const dir = await tempDir();
|
|
await saveCatalogPages(dir, "abc", [TINY_JPEG]);
|
|
await deleteCatalogFiles(dir, "abc");
|
|
await expect(readPage(dir, "abc", 1)).rejects.toBeInstanceOf(StorageError);
|
|
});
|
|
});
|