77 lines
3.0 KiB
TypeScript
77 lines
3.0 KiB
TypeScript
import { mkdtempSync, rmSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { Drive } from "@suaveplan/drive";
|
|
import { createNodeFsStorage } from "@suaveplan/storage-file";
|
|
import { createStorageUriResolver } from "@suaveplan/storage-uri";
|
|
import {
|
|
afterEach,
|
|
beforeEach,
|
|
describe,
|
|
expect,
|
|
it,
|
|
} from "@suaveplan/testing/runner";
|
|
import { driveUriPlugin } from "../../src/plugins/drive/drive.js";
|
|
|
|
describe("drive-roundtrip integration", () => {
|
|
let rootDir: string;
|
|
let drive: Drive;
|
|
|
|
beforeEach(async () => {
|
|
rootDir = mkdtempSync(join(tmpdir(), "drive-uri-roundtrip-"));
|
|
const fileStorage = createNodeFsStorage({ rootDir, mkdirp: true });
|
|
const storage = Object.assign(fileStorage, { id: "default" });
|
|
drive = new Drive(storage);
|
|
await drive.initialize();
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await drive.dispose();
|
|
rmSync(rootDir, { recursive: true, force: true });
|
|
});
|
|
|
|
it("writes a file via Drive.write then reads it via resolver.read('drive://')", {
|
|
timeout: 30000,
|
|
}, async () => {
|
|
const plugin = driveUriPlugin({ drive, driveId: "test-drive" });
|
|
const resolver = createStorageUriResolver([plugin]);
|
|
const payload = new Uint8Array([0x4d, 0x4f, 0x56, 0x21]);
|
|
await drive.write("folder/clip.mov", payload);
|
|
const bytes = await resolver.read("drive://test-drive/folder/clip.mov");
|
|
expect(Array.from(bytes)).toEqual(Array.from(payload));
|
|
});
|
|
|
|
it("resolver.open streams the file via Drive.createReadStream", {
|
|
timeout: 30000,
|
|
}, async () => {
|
|
const plugin = driveUriPlugin({ drive, driveId: "test-drive" });
|
|
const resolver = createStorageUriResolver([plugin]);
|
|
const payload = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
|
|
await drive.write("clip.dat", payload);
|
|
const stream = await resolver.open("drive://test-drive/clip.dat");
|
|
const reader = stream.getReader();
|
|
const chunks: number[] = [];
|
|
for (;;) {
|
|
const { value, done } = await reader.read();
|
|
if (done) break;
|
|
if (value !== undefined) chunks.push(...value);
|
|
}
|
|
reader.releaseLock();
|
|
expect(chunks).toEqual(Array.from(payload));
|
|
});
|
|
|
|
it("resolver.remove deletes the file", { timeout: 30000 }, async () => {
|
|
const plugin = driveUriPlugin({ drive, driveId: "test-drive" });
|
|
const resolver = createStorageUriResolver([plugin]);
|
|
await drive.write("doomed.bin", new Uint8Array([42]));
|
|
const removed = await resolver.remove("drive://test-drive/doomed.bin");
|
|
expect(removed).toBe(true);
|
|
// A subsequent read should yield STORAGE_URI.NOT_FOUND (the
|
|
// adapter translates Drive's not-found into undefined, and the
|
|
// resolver maps undefined to NOT_FOUND).
|
|
await expect(
|
|
resolver.read("drive://test-drive/doomed.bin")
|
|
).rejects.toBeDefined();
|
|
});
|
|
});
|