import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; import { DenoKVBlobService, DenoKVFileService, FileMetaData, FileService, } from "../src/fileservice.ts"; import { asyncIterableToArray } from "../src/utils.ts"; import { generateLargeText } from "./utils.ts"; import { StringReader } from "https://deno.land/std@0.191.0/io/mod.ts"; Deno.test("DenoKVBlobService happy path", async (t) => { localStorage.clear(); const kv = await Deno.openKv(":memory:"); const bs = new DenoKVBlobService(kv, 50); function toUint8Array(s: string): Uint8Array { return new TextEncoder().encode(s); } await t.step("read write small text", async () => { const size = await bs.persistBlob("0002", toUint8Array("hello world")); const content = await bs.readAllBlob("0002"); assertEquals(content, toUint8Array("hello world")); assertEquals(await bs.getNumberOfBlobs("0002"), 1); assertEquals(size, "hello world".length); }); await t.step("read write large text", async () => { const largeText = toUint8Array(generateLargeText(33, 100)); const size = await bs.persistBlob("0001", largeText); const content = await bs.readAllBlob("0001"); assertEquals(content, largeText); assertEquals(await bs.getNumberOfBlobs("0001"), 21); assertEquals(size, largeText.length); }); await t.step("read write large file", async () => { const fileName = 'tests/examples/teapot.obj'; const f = await Deno.open(fileName); const size = await bs.persistReader("large-stream", f); assertEquals(await bs.getNumberOfBlobs("large-stream"), 1030); assertEquals((await Deno.readFile(fileName)).length, size); f.close(); }); kv.close(); }); Deno.test("DenoKVFileService happy path xxx ", async (t) => { const kv = await Deno.openKv(":memory:"); const fs: FileService = new DenoKVFileService(kv); const fileName1 = "didier/file1.txt"; const fileName2 = "didier/file2.txt"; let file1: FileMetaData | undefined; let file2: FileMetaData | undefined; await t.step("Create 2 files xyzabc", async () => { file1 = await fs.createFromFullContent(fileName1, "bob le chef"); file2 = await fs.createFromFullContent(fileName2, "le geant vert ☀️"); assertEquals(file1.path, fileName1); assertEquals(file2.contentSize, 47); }); await t.step("Get file metadata", async () => { const ret = await fs.getMetaDataByID(file1!.id); assertEquals(ret, file1); }); await t.step("List files", async () => { const expected = [file1, file2]; const ret = await fs.list(); assertEquals(await asyncIterableToArray(ret), expected); }); await t.step("Get File by path", async () => { const ret = await fs.getRawContentByPath(fileName1); assertEquals(new TextDecoder().decode(ret!), "bob le chef"); }); await t.step("Get File by Id", async () => { const ret = await fs.getRawContentByPath(fileName1); assertEquals(new TextDecoder().decode(ret!), "bob le chef"); }); await t.step("Delete File", async () => { await fs.deleteByPath(fileName1); await fs.deleteByPath("404 not found"); }); await t.step("List after delete", async () => { const expected = [file2]; const ret = await fs.list(); assertEquals(await asyncIterableToArray(ret), expected); }); await t.step("Create File from reader", async () => { const newPath = "boris/streamed-file.txt"; const ret = await fs.createFromReader(newPath, new StringReader("hello world 18")); assertEquals(ret.contentSize, 40); const s = await fs.getRawContentByPath(newPath); assertEquals(new TextDecoder().decode(s!), "hello world 18") }); await t.step("Rename file", async () => { const newPath = "boris/file2.txt"; const ret = await fs.renameById(file2!.id, newPath); assertEquals(ret?.path, newPath); assertEquals( new TextDecoder().decode((await fs.getRawContentByPath(newPath))!), 'le geant vert ☀️', ); }); kv.close(); }); Deno.test("DenoKVFileService happy path with byte array", async (t) => { const kv = await Deno.openKv(":memory:"); const fs: FileService = new DenoKVFileService(kv); await t.step("Create and read 1 binary file", async () => { const inputContent = new Uint8Array([240]); const file = await fs.createFromFullContent("hello", inputContent); assertEquals(file.path, "hello"); assertEquals(file.contentSize, 27); const persistedContent = await fs.getRawContentByPath("hello"); assertEquals(persistedContent, inputContent); }); kv.close(); });