123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705 |
- ///<reference path="server/index.d.ts"/>
- import { expect, test } from "@playwright/test";
- const url = "http://localhost:8085";
- test.beforeEach(async ({ page }) => {
- await page.goto(url);
- await page.waitForFunction(() => window.setup != null);
- });
- test.describe("PrivateDirectory", () => {
- test("lookupNode can fetch file added to directory", async ({ page }) => {
- const result = await page.evaluate(async () => {
- const {
- wnfs: { PrivateDirectory, PrivateForest },
- mock: { MemoryBlockStore, Rng },
- } = await window.setup();
- const rng = new Rng();
- const store = new MemoryBlockStore();
- const initialForest = new PrivateForest(rng);
- const root = new PrivateDirectory(initialForest.emptyName(), new Date(), rng);
- var { rootDir, forest } = await root.write(
- ["text.txt"],
- true,
- new Uint8Array([1, 2, 3, 4, 5]),
- new Date(),
- initialForest,
- store,
- rng,
- );
- return await rootDir.lookupNode("text.txt", true, forest, store);
- });
- expect(result).toBeDefined();
- });
- test("getNode can fetch node from root dir", async ({ page }) => {
- const [result0, result1] = await page.evaluate(async (): Promise<any[]> => {
- const {
- wnfs: { PrivateDirectory, PrivateForest },
- mock: { MemoryBlockStore, Rng },
- } = await window.setup();
- const rng = new Rng();
- const store = new MemoryBlockStore();
- const initialForest = new PrivateForest(rng);
- const root = new PrivateDirectory(initialForest.emptyName(), new Date(), rng);
- var { rootDir, forest } = await root.mkdir(
- ["pictures", "dogs"],
- true,
- new Date(),
- initialForest,
- store,
- rng,
- );
- var { rootDir, forest } = await rootDir.write(
- ["pictures", "cats", "tabby.png"],
- true,
- new Uint8Array([1, 2, 3, 4, 5]),
- new Date(),
- forest,
- store,
- rng,
- );
- var result0 = await rootDir.getNode(["pictures", "cats", "tabby.png"], true, forest, store);
- var result1 = await rootDir.getNode(["pictures", "dogs", "bingo.png"], true, forest, store);
- console.log(result0);
- console.log(result1);
- return [result0, result1];
- });
- expect(result0).toBeDefined();
- expect(result1).toBeUndefined();
- });
- test("lookupNode cannot fetch file not added to directory", async ({ page }) => {
- const result = await page.evaluate(async () => {
- const {
- wnfs: { PrivateDirectory, PrivateForest },
- mock: { MemoryBlockStore, Rng },
- } = await window.setup();
- const rng = new Rng();
- const store = new MemoryBlockStore();
- const initialForest = new PrivateForest(rng);
- const root = new PrivateDirectory(initialForest.emptyName(), new Date(), rng);
- return await root.lookupNode("Unknown", true, initialForest, store);
- });
- expect(result).toBe(undefined);
- });
- test("mkdir can create new directory", async ({ page }) => {
- const result = await page.evaluate(async () => {
- const {
- wnfs: { PrivateDirectory, PrivateForest },
- mock: { MemoryBlockStore, Rng },
- } = await window.setup();
- const rng = new Rng();
- const store = new MemoryBlockStore();
- const initialForest = new PrivateForest(rng);
- const root = new PrivateDirectory(initialForest.emptyName(), new Date(), rng);
- var { rootDir, forest } = await root.mkdir(
- ["pictures", "cats"],
- true,
- new Date(),
- initialForest,
- store,
- rng,
- );
- var { rootDir, forest } = await rootDir.write(
- ["pictures", "cats", "tabby.png"],
- true,
- new Uint8Array([1, 2, 3, 4, 5]),
- new Date(),
- forest,
- store,
- rng,
- );
- var result = await rootDir.getNode(["pictures", "cats", "tabby.png"], true, forest, store);
- return result;
- });
- expect(result).toBeDefined();
- });
- test("ls can list children under directory", async ({ page }) => {
- const result = await page.evaluate(async () => {
- const {
- wnfs: { PrivateDirectory, PrivateForest },
- mock: { MemoryBlockStore, Rng },
- } = await window.setup();
- const rng = new Rng();
- const store = new MemoryBlockStore();
- const initialForest = new PrivateForest(rng);
- const root = new PrivateDirectory(initialForest.emptyName(), new Date(), rng);
- var { rootDir, forest } = await root.mkdir(
- ["pictures", "dogs"],
- true,
- new Date(),
- initialForest,
- store,
- rng,
- );
- var { rootDir, forest } = await rootDir.write(
- ["pictures", "cats", "tabby.png"],
- true,
- new Uint8Array([1, 2, 3, 4, 5]),
- new Date(),
- forest,
- store,
- rng,
- );
- var { result } = await rootDir.ls(["pictures"], true, forest, store);
- return result;
- });
- expect(result.length).toBe(2);
- expect(result[0].name).toBe("cats");
- expect(result[1].name).toBe("dogs");
- });
- test("rm can remove children from directory", async ({ page }) => {
- const result = await page.evaluate(async () => {
- const {
- wnfs: { PrivateDirectory, PrivateForest },
- mock: { MemoryBlockStore, Rng },
- } = await window.setup();
- const rng = new Rng();
- const store = new MemoryBlockStore();
- const initialForest = new PrivateForest(rng);
- const root = new PrivateDirectory(initialForest.emptyName(), new Date(), rng);
- var { rootDir, forest } = await root.write(
- ["pictures", "dogs", "billie.jpeg"],
- true,
- new Uint8Array([1, 2, 3, 4, 5]),
- new Date(),
- initialForest,
- store,
- rng,
- );
- var { rootDir, forest } = await rootDir.write(
- ["pictures", "cats", "tabby.png"],
- true,
- new Uint8Array([1, 2, 3, 4, 5]),
- new Date(),
- forest,
- store,
- rng,
- );
- var { rootDir, forest } = await rootDir.rm(["pictures", "cats"], true, forest, store, rng);
- var { result } = await rootDir.ls(["pictures"], true, forest, store);
- return result;
- });
- expect(result.length).toEqual(1);
- expect(result[0].name).toEqual("dogs");
- });
- test("basicMv can move content between directories", async ({ page }) => {
- const [imagesContent, picturesContent] = await page.evaluate(async () => {
- const {
- wnfs: { PrivateDirectory, PrivateForest },
- mock: { MemoryBlockStore, Rng },
- } = await window.setup();
- const rng = new Rng();
- const store = new MemoryBlockStore();
- const initialForest = new PrivateForest(rng);
- const root = new PrivateDirectory(initialForest.emptyName(), new Date(), rng);
- var { rootDir, forest } = await root.write(
- ["pictures", "cats", "luna.jpeg"],
- true,
- new Uint8Array([1, 2, 3, 4, 5]),
- new Date(),
- initialForest,
- store,
- rng,
- );
- var { rootDir, forest } = await rootDir.write(
- ["pictures", "cats", "tabby.png"],
- true,
- new Uint8Array([1, 2, 3, 4, 5]),
- new Date(),
- forest,
- store,
- rng,
- );
- var { rootDir, forest } = await rootDir.mkdir(
- ["images"],
- true,
- new Date(),
- forest,
- store,
- rng,
- );
- var { rootDir, forest } = await rootDir.basicMv(
- ["pictures", "cats"],
- ["images", "cats"],
- true,
- new Date(),
- forest,
- store,
- rng,
- );
- var { result: imagesContent, forest } = await rootDir.ls(["images"], true, forest, store);
- var { result: picturesContent, forest } = await rootDir.ls(["pictures"], true, forest, store);
- return [imagesContent, picturesContent];
- });
- expect(imagesContent.length).toEqual(1);
- expect(picturesContent.length).toEqual(0);
- expect(imagesContent[0].name).toEqual("cats");
- });
- test("cp can copy content between directories", async ({ page }) => {
- const [imagesContent, picturesContent] = await page.evaluate(async () => {
- const {
- wnfs: { PrivateDirectory, PrivateForest },
- mock: { MemoryBlockStore, Rng },
- } = await window.setup();
- const rng = new Rng();
- const store = new MemoryBlockStore();
- const initialForest = new PrivateForest(rng);
- const root = new PrivateDirectory(initialForest.emptyName(), new Date(), rng);
- var { rootDir, forest } = await root.write(
- ["pictures", "cats", "luna.jpeg"],
- true,
- new Uint8Array([1, 2, 3, 4, 5]),
- new Date(),
- initialForest,
- store,
- rng,
- );
- var { rootDir, forest } = await rootDir.write(
- ["pictures", "cats", "tabby.png"],
- true,
- new Uint8Array([1, 2, 3, 4, 5]),
- new Date(),
- forest,
- store,
- rng,
- );
- var { rootDir, forest } = await rootDir.mkdir(
- ["images"],
- true,
- new Date(),
- forest,
- store,
- rng,
- );
- var { rootDir, forest } = await rootDir.cp(
- ["pictures", "cats"],
- ["images", "cats"],
- true,
- new Date(),
- forest,
- store,
- rng,
- );
- var { result: imagesContent, forest } = await rootDir.ls(["images"], true, forest, store);
- var { result: picturesContent, forest } = await rootDir.ls(["pictures"], true, forest, store);
- return [imagesContent, picturesContent];
- });
- expect(imagesContent.length).toEqual(1);
- expect(picturesContent.length).toEqual(1);
- expect(imagesContent[0].name).toEqual("cats");
- expect(picturesContent[0].name).toEqual("cats");
- });
- });
- test.describe("PrivateFile", () => {
- test("empty can create empty file", async ({ page }) => {
- const result = await page.evaluate(async () => {
- const {
- wnfs: { PrivateFile, PrivateForest },
- mock: { Rng },
- } = await window.setup();
- const rng = new Rng();
- const forest = new PrivateForest(rng);
- const file = new PrivateFile(forest.emptyName(), new Date(), rng);
- return file.getId();
- });
- expect(result).toBeDefined();
- });
- test("withContent can create file with content", async ({ page }) => {
- const result = await page.evaluate(async () => {
- const {
- wnfs: { PrivateFile, PrivateForest },
- mock: { MemoryBlockStore, Rng },
- } = await window.setup();
- const rng = new Rng();
- const forest = new PrivateForest(rng);
- const store = new MemoryBlockStore();
- const [file] = await PrivateFile.withContent(
- forest.emptyName(),
- new Date(),
- new Uint8Array([1, 2, 3, 4, 5]),
- forest,
- store,
- rng,
- );
- return file.getId();
- });
- expect(result).toBeDefined();
- });
- test("getContent can fetch file's entire content", async ({ page }) => {
- const [length, type] = await page.evaluate(async () => {
- const {
- wnfs: { PrivateFile, PrivateForest },
- mock: { MemoryBlockStore, Rng },
- } = await window.setup();
- const rng = new Rng();
- const initialForest = new PrivateForest(rng);
- const store = new MemoryBlockStore();
- var [file, forest] = await PrivateFile.withContent(
- initialForest.emptyName(),
- new Date(),
- new Uint8Array([1, 2, 3, 4, 5]),
- initialForest,
- store,
- rng,
- );
- let content = await file.getContent(forest, store);
- return [content.length, content.constructor.name, content];
- });
- expect(length).toEqual(5);
- expect(type).toEqual("Uint8Array");
- });
- test("readAt can fetch file's partial content", async ({ page }) => {
- const [length, type, content] = await page.evaluate(async () => {
- const {
- wnfs: { PrivateFile, PrivateForest },
- mock: { MemoryBlockStore, Rng },
- } = await window.setup();
- const rng = new Rng();
- const initialForest = new PrivateForest(rng);
- const store = new MemoryBlockStore();
- var [file, forest] = await PrivateFile.withContent(
- initialForest.emptyName(),
- new Date(),
- new Uint8Array([1, 2, 3, 4, 5]),
- initialForest,
- store,
- rng,
- );
- let content = await file.readAt(1, 3, forest, store);
- return [content.length, content.constructor.name, content];
- });
- expect(length).toEqual(3);
- expect(type).toEqual("Uint8Array");
- expect(new Uint8Array(Object.values(content))).toEqual(new Uint8Array([2, 3, 4]));
- });
- test("getSize returns the exact content size", async ({ page }) => {
- const size = await page.evaluate(async () => {
- const {
- wnfs: { PrivateFile, PrivateForest },
- mock: { MemoryBlockStore, Rng },
- } = await window.setup();
- const rng = new Rng();
- const initialForest = new PrivateForest(rng);
- const store = new MemoryBlockStore();
- var [file, forest] = await PrivateFile.withContent(
- initialForest.emptyName(),
- new Date(),
- new Uint8Array(2 * 1024 * 1024),
- initialForest,
- store,
- rng,
- );
- return await file.getSize(forest, store);
- });
- expect(size).toEqual(2 * 1024 * 1024);
- });
- test("A PrivateDirectory has the correct metadata", async ({ page }) => {
- const result = await page.evaluate(async () => {
- const {
- wnfs: { PrivateDirectory, PrivateForest },
- mock: { Rng },
- } = await window.setup();
- const rng = new Rng();
- const forest = new PrivateForest(rng);
- const time = new Date();
- return new PrivateDirectory(forest.emptyName(), time, rng).metadata();
- });
- expect(result.created).not.toBeUndefined();
- });
- test("A PrivateFile has the correct metadata", async ({ page }) => {
- const result = await page.evaluate(async () => {
- const {
- wnfs: { PrivateFile, PrivateForest },
- mock: { Rng },
- } = await window.setup();
- const rng = new Rng();
- const forest = new PrivateForest(rng);
- const time = new Date();
- return new PrivateFile(forest.emptyName(), time, rng).metadata();
- });
- expect(result.created).not.toBeUndefined();
- });
- });
- test.describe("PrivateNode", () => {
- test("load returns what was stored", async ({ page }) => {
- const [metadataBefore, metadataAfter] = await page.evaluate(async () => {
- const {
- wnfs: { PrivateFile, PrivateNode, PrivateForest },
- mock: { MemoryBlockStore, Rng },
- } = await window.setup();
- const rng = new Rng();
- const store = new MemoryBlockStore();
- const forest = new PrivateForest(rng);
- const time = new Date();
- const file = new PrivateFile(forest.emptyName(), time, rng);
- const node = file.asNode();
- const [privateRef, newForest] = await node.store(forest, store, rng);
- const fetched = await PrivateNode.load(privateRef, newForest, store);
- const metadataBefore = node.asFile().metadata();
- const metadataAfter = fetched.asFile().metadata();
- return [metadataBefore, metadataAfter];
- });
- expect(metadataBefore).toBeDefined();
- expect(metadataAfter).toBeDefined();
- expect(metadataBefore.created).toEqual(metadataAfter.created);
- expect(metadataBefore.modified).toEqual(metadataAfter.modified);
- });
- test("searchLatest finds latest", async ({ page }) => {
- const [lsResultBefore, lsResultAfter] = await page.evaluate(async () => {
- const {
- wnfs: { PrivateDirectory, PrivateNode, PrivateForest },
- mock: { MemoryBlockStore, Rng },
- } = await window.setup();
- const rng = new Rng();
- const store = new MemoryBlockStore();
- const forest0 = new PrivateForest(rng);
- const time = new Date();
- // Create a root directory and store
- const rootDir0 = new PrivateDirectory(forest0.emptyName(), time, rng);
- const [accessKey, forest1] = await rootDir0.store(forest0, store, rng);
- // Write something to the directory and store it
- const { rootDir: rootDir1, forest: forest2 } = await rootDir0.write(
- ["some", "file.txt"],
- true,
- new Uint8Array([0]),
- time,
- forest1,
- store,
- rng,
- );
- const [_, forest3] = await rootDir1.asNode().store(forest2, store, rng);
- // loading back the *old* directory using its access key should give an empty directory:
- const oldNode = await PrivateNode.load(accessKey, forest3, store);
- const { result: lsResultBefore } = await oldNode.asDir().ls([], false, forest3, store);
- // loading back the directory with search latest should work:
- const latestNode = await oldNode.searchLatest(forest3, store);
- const { result: lsResultAfter } = await latestNode.asDir().ls([], false, forest3, store);
- return [lsResultBefore, lsResultAfter];
- });
- expect(lsResultBefore).toBeDefined();
- expect(lsResultAfter).toBeDefined();
- expect(lsResultBefore).toEqual([]);
- expect(lsResultAfter.length).toEqual(1);
- expect(lsResultAfter[0].name).toEqual("some");
- });
- });
- test.describe("PrivateForest", () => {
- test("store returns a PrivateRef", async ({ page }) => {
- const result = await page.evaluate(async () => {
- const {
- wnfs: { PrivateFile, PrivateForest },
- mock: { MemoryBlockStore, Rng, CID },
- } = await window.setup();
- const rng = new Rng();
- const store = new MemoryBlockStore();
- const forest = new PrivateForest(rng);
- const time = new Date();
- const file = new PrivateFile(forest.emptyName(), time, rng);
- const node = file.asNode();
- const [privateRef, _] = await node.store(forest, store, rng);
- return {
- // Need to be converted to arrays so they can be passed as JSON
- label: Array.from(privateRef.getLabel()),
- temporalKey: Array.from(privateRef.getTemporalKey()),
- contentCid: CID.decode(privateRef.getContentCid()).toString(),
- };
- });
- expect(result.label.length).toEqual(32);
- expect(result.temporalKey.length).toEqual(32);
- expect(result.contentCid).toBeDefined();
- });
- test("diff gets changes in forests", async ({ page }) => {
- const changes = await page.evaluate(async () => {
- const {
- wnfs: { PrivateFile, PrivateDirectory, PrivateForest },
- mock: { MemoryBlockStore, Rng },
- } = await window.setup();
- const rng = new Rng();
- const store = new MemoryBlockStore();
- const time = new Date();
- var mainForest: any = new PrivateForest(rng);
- var otherForest: any = mainForest;
- const file = new PrivateFile(mainForest.emptyName(), time, rng).asNode();
- const dir = new PrivateDirectory(mainForest.emptyName(), time, rng).asNode();
- var [_, mainForest] = await file.store(mainForest, store, rng);
- var [_, otherForest] = await dir.store(otherForest, store, rng);
- const diff = await mainForest.diff(otherForest, store);
- return diff.map((change: any) => change.getChangeType());
- });
- expect(changes.length).toEqual(2);
- expect(changes).toContain("add");
- expect(changes).toContain("remove");
- });
- test("merge combines changes in forests", async ({ page }) => {
- const result = await page.evaluate(async () => {
- const {
- wnfs: { PrivateFile, PrivateDirectory, PrivateForest, PrivateNode },
- mock: { MemoryBlockStore, Rng },
- } = await window.setup();
- const rng = new Rng();
- const store = new MemoryBlockStore();
- const time = new Date();
- var mainForest: any = new PrivateForest(rng);
- var otherForest: any = mainForest;
- const file = new PrivateFile(mainForest.emptyName(), time, rng).asNode();
- const dir = new PrivateDirectory(mainForest.emptyName(), time, rng).asNode();
- var [_, mainForest] = await file.store(mainForest, store, rng);
- var [privateRef, otherForest] = await dir.store(otherForest, store, rng);
- const mergeForest = await mainForest.merge(otherForest, store);
- return await PrivateNode.load(privateRef, mergeForest, store);
- });
- expect(result).toBeDefined();
- });
- });
- test.describe("AccessKey", () => {
- test("can encode / decode an access key", async ({ page }) => {
- const [metadataBefore, metadataAfter] = await page.evaluate(async () => {
- const {
- wnfs: { AccessKey, PrivateFile, PrivateNode, PrivateForest },
- mock: { MemoryBlockStore, Rng },
- } = await window.setup();
- const rng = new Rng();
- const store = new MemoryBlockStore();
- const forest = new PrivateForest(rng);
- const time = new Date();
- const file = new PrivateFile(forest.emptyName(), time, rng);
- const node = file.asNode();
- const [accessKey, newForest] = await node.store(forest, store, rng);
- const encodedAccessKey = accessKey.toBytes();
- const decodedAccessKey = AccessKey.fromBytes(encodedAccessKey);
- const fetched = await PrivateNode.load(decodedAccessKey, newForest, store);
- const metadataBefore = node.asFile().metadata();
- const metadataAfter = fetched.asFile().metadata();
- return [metadataBefore, metadataAfter];
- });
- expect(metadataBefore).toBeDefined();
- expect(metadataAfter).toBeDefined();
- expect(metadataBefore.created).toEqual(metadataAfter.created);
- expect(metadataBefore.modified).toEqual(metadataAfter.modified);
- });
- });
|