2023-10-03 12:16:33 +00:00
|
|
|
import "https://esm.sh/fake-indexeddb@4.0.2/auto";
|
2023-05-23 18:53:53 +00:00
|
|
|
import { assertEquals } from "../../test_deps.ts";
|
2023-10-03 12:16:33 +00:00
|
|
|
import { DataStore } from "../../plugos/lib/datastore.ts";
|
|
|
|
import { IndexedDBKvPrimitives } from "../../plugos/lib/indexeddb_kv_primitives.ts";
|
|
|
|
import { DataStoreSpacePrimitives } from "./datastore_space_primitives.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
Deno.test("DataStoreSpacePrimitives", {
|
|
|
|
sanitizeResources: false,
|
|
|
|
sanitizeOps: false,
|
|
|
|
}, async () => {
|
|
|
|
const db = new IndexedDBKvPrimitives("test");
|
|
|
|
await db.init();
|
|
|
|
|
|
|
|
const space = new DataStoreSpacePrimitives(new DataStore(db));
|
2023-05-23 18:53:53 +00:00
|
|
|
const files = await space.fetchFileList();
|
|
|
|
assertEquals(files, []);
|
|
|
|
// Write text file
|
|
|
|
const fileMeta = await space.writeFile(
|
|
|
|
"test.txt",
|
|
|
|
stringToBytes("Hello World"),
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
(await space.readFile("test.txt")).data,
|
|
|
|
stringToBytes("Hello World"),
|
|
|
|
);
|
|
|
|
const fbContent = (await space.readFile("test.txt"))
|
|
|
|
.data;
|
|
|
|
assertEquals(new TextDecoder().decode(fbContent), "Hello World");
|
|
|
|
assertEquals(await space.fetchFileList(), [fileMeta]);
|
|
|
|
const buf = new Uint8Array([1, 2, 3, 4, 5]);
|
|
|
|
// Write binary file
|
|
|
|
await space.writeFile("test.bin", buf);
|
|
|
|
const fMeta = await space.getFileMeta("test.bin");
|
|
|
|
assertEquals(fMeta.size, 5);
|
|
|
|
assertEquals((await space.fetchFileList()).length, 2);
|
|
|
|
|
|
|
|
await space.deleteFile("test.bin");
|
|
|
|
assertEquals(await space.fetchFileList(), [fileMeta]);
|
2023-10-03 12:16:33 +00:00
|
|
|
|
|
|
|
db.close();
|
2023-05-23 18:53:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function stringToBytes(str: string): Uint8Array {
|
|
|
|
return new TextEncoder().encode(str);
|
|
|
|
}
|