diff --git a/.gitignore b/.gitignore index 4dfa574a2..ba4ed19f9 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ logs smoke/package.json smoke/pnpm-lock.yaml smoke/react/package.json +smoke/react/pnpm-lock.yaml diff --git a/.prettierignore b/.prettierignore index 869a70976..0b497349b 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1 +1,2 @@ **/pnpm-lock.yaml +scripts/ diff --git a/package-fireproof-core.json b/package-fireproof-core.json index bf7ed1769..f7a54f88e 100644 --- a/package-fireproof-core.json +++ b/package-fireproof-core.json @@ -13,17 +13,11 @@ "require": "./index.cjs", "script": "./index.global.js", "types": "./index.d.ts" - }, - "./blockstore": { - "import": "./blockstore/index.js", - "require": "./blockstore/index.cjs", - "script": "./blockstore/index.global.js", - "types": "./blockstore/index.d.ts" } }, "scripts": {}, "keywords": ["database", "JSON", "document", "IPLD", "CID", "IPFS"], - "contributors": ["J Chris Anderson", "Alan Shaw", "Travis Vachon", "Mikeal Rogers"], + "contributors": ["J Chris Anderson", "Alan Shaw", "Travis Vachon", "Mikeal Rogers", "Meno Abels"], "author": "J Chris Anderson", "license": "Apache-2.0 OR MIT", "homepage": "https://use-fireproof.com", diff --git a/src/crdt.ts b/src/crdt.ts index 5653544a1..f1ca83117 100644 --- a/src/crdt.ts +++ b/src/crdt.ts @@ -29,6 +29,7 @@ import type { IndexKeyType, DocWithId, DocTypes, + Falsy, } from "./types.js"; import { index, type Index } from "./indexer.js"; import { CRDTClock } from "./crdt-clock.js"; @@ -142,10 +143,10 @@ export class CRDT { return await getBlock(this.blockstore, cidString); } - async get(key: string): Promise | null> { + async get(key: string): Promise | Falsy> { await this.ready(); const result = await getValueFromCrdt(this.blockstore, this.clock.head, key); - if (result.del) return null; + if (result.del) return undefined; return result; } diff --git a/src/index.ts b/src/index.ts index 8d83f1c65..3f16b47ee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,11 @@ export * from "./database.js"; export * from "./types.js"; -export { CRDT } from "./crdt.js"; +export * from "./crdt.js"; export * from "./indexer.js"; + +export * as blockstore from "./blockstore/index.js"; + +export * as bs from "./blockstore/index.js"; +export * as rt from "./runtime/index.js"; diff --git a/src/indexer.ts b/src/indexer.ts index ea75f72f5..cd3fd7547 100644 --- a/src/indexer.ts +++ b/src/indexer.ts @@ -15,6 +15,7 @@ import { IndexTransactionMeta, } from "./types.js"; import { BaseBlockstore } from "./blockstore/index.js"; + import { bulkIndex, indexEntriesForChanges, diff --git a/src/runtime/node-sys-container.ts b/src/runtime/node-sys-container.ts index 33e0280a0..a4eebff9d 100644 --- a/src/runtime/node-sys-container.ts +++ b/src/runtime/node-sys-container.ts @@ -6,7 +6,7 @@ export async function createNodeSysContainer(): Promise { const nodeURL = "node:url"; const nodeFS = "node:fs"; const fs = (await saveImport(nodeFS)).promises; - const assert = "assert"; + // const assert = "assert"; const path = await saveImport(nodePath); return { state: "node", @@ -18,6 +18,6 @@ export async function createNodeSysContainer(): Promise { readdir: fs.readdir as NodeMap["readdir"], readfile: fs.readFile as NodeMap["readfile"], writefile: fs.writeFile as NodeMap["writefile"], - assert: (await saveImport(assert)).default, + // assert: (await saveImport(assert)).default, }; } diff --git a/src/runtime/sys-container.ts b/src/runtime/sys-container.ts index c926c8ed4..e5eebca49 100644 --- a/src/runtime/sys-container.ts +++ b/src/runtime/sys-container.ts @@ -12,7 +12,7 @@ export interface NodeMap { dirname: (path: string) => string; homedir: () => string; fileURLToPath: (url: string | URL) => string; - assert: (condition: unknown, message?: string | Error) => void; + // assert: (condition: unknown, message?: string | Error) => void; mkdir: (path: PathLike, options?: { recursive: boolean }) => Promise; readdir: (path: PathLike, options?: unknown) => Promise; @@ -26,9 +26,9 @@ export interface NodeMap { writefile: (path: PathLike, data: Uint8Array | string) => Promise; } -export function assert(condition: unknown, message?: string | Error): asserts condition { - SysContainer.freight?.assert(condition, message); -} +// export function assert(condition: unknown, message?: string | Error): asserts condition { +// SysContainer.freight?.assert(condition, message); +// } const onceStart = new ResolveOnce(); @@ -53,15 +53,15 @@ class sysContainer { } return url.pathname; }, - assert: (condition: unknown, message?: string | Error) => { - if (!condition) { - if (message instanceof Error) { - throw message; - } else { - throw new Error(message); - } - } - }, + // assert: (condition: unknown, message?: string | Error) => { + // if (!condition) { + // if (message instanceof Error) { + // throw message; + // } else { + // throw new Error(message); + // } + // } + // }, mkdir: () => Promise.reject(new Error("SysContainer:mkdir is not available in seeded state")), readdir: () => Promise.reject(new Error("SysContainer:readdir is not available in seeded state")), rm: () => Promise.reject(new Error("SysContainer:rm is not available in seeded state")), diff --git a/tests/storage-engine/loader.test.ts b/tests/blockstore/loader.test.ts similarity index 60% rename from tests/storage-engine/loader.test.ts rename to tests/blockstore/loader.test.ts index d0ca57498..ef7e69608 100644 --- a/tests/storage-engine/loader.test.ts +++ b/tests/blockstore/loader.test.ts @@ -3,20 +3,10 @@ import { sha256 as hasher } from "multiformats/hashes/sha2"; import { BlockView } from "multiformats"; import { encode } from "multiformats/block"; import { CID } from "multiformats/cid"; - import { MemoryBlockstore } from "@web3-storage/pail/block"; +import { CRDTMeta, IndexTransactionMeta, bs, rt } from "@fireproof/core"; -import { assert, matches, equals } from "../helpers.js"; - -import { parseCarFile } from "@fireproof/core/blockstore"; - -import { CarTransaction, CompactionFetcher, EncryptedBlockstore, Loader } from "@fireproof/core/blockstore"; - -import { AnyAnyLink, AnyLink, CarGroup, TransactionMeta } from "@fireproof/core/blockstore"; -import { SysContainer } from "@fireproof/core/runtime"; -import { CRDTMeta, IndexTransactionMeta } from "use-fireproof"; - -class MyMemoryBlockStore extends EncryptedBlockstore { +class MyMemoryBlockStore extends bs.EncryptedBlockstore { readonly memblock = new MemoryBlockstore(); constructor() { const ebOpts = { @@ -30,12 +20,12 @@ class MyMemoryBlockStore extends EncryptedBlockstore { close(): Promise { return this.loader.close(); } - loader: Loader = new Loader("MyMemoryBlockStore", {}); - readonly transactions = new Set(); + loader = new bs.Loader("MyMemoryBlockStore", {}); + readonly transactions = new Set(); // readonly lastTxMeta?: TransactionMeta; readonly compacting: boolean = false; - override async put(cid: AnyAnyLink, block: Uint8Array): Promise { + override async put(cid: bs.AnyAnyLink, block: Uint8Array): Promise { return this.memblock.put(cid, block); } @@ -44,22 +34,22 @@ class MyMemoryBlockStore extends EncryptedBlockstore { // } // eslint-disable-next-line @typescript-eslint/no-unused-vars - getFile(car: AnyLink, cid: AnyLink, isPublic?: boolean): Promise { + getFile(car: bs.AnyLink, cid: bs.AnyLink, isPublic?: boolean): Promise { throw new Error("Method not implemented."); } compact(): Promise { throw new Error("Method not implemented."); } // eslint-disable-next-line @typescript-eslint/no-unused-vars - defaultCompact(blocks: CompactionFetcher): Promise { + defaultCompact(blocks: bs.CompactionFetcher): Promise { throw new Error("Method not implemented."); } } describe("basic Loader simple", function () { - let loader: Loader; + let loader: bs.Loader; let block: BlockView; - let t: CarTransaction; + let t: bs.CarTransaction; afterEach(async function () { await loader.close(); @@ -68,10 +58,10 @@ describe("basic Loader simple", function () { beforeEach(async function () { const testDbName = "test-loader-commit"; - await SysContainer.start(); + await rt.SysContainer.start(); const mockM = new MyMemoryBlockStore(); - t = new CarTransaction(mockM as EncryptedBlockstore); - loader = new Loader(testDbName, { public: true }); + t = new bs.CarTransaction(mockM as bs.EncryptedBlockstore); + loader = new bs.Loader(testDbName, { public: true }); await loader.ready(); block = await encode({ value: { hello: "world" }, @@ -82,30 +72,30 @@ describe("basic Loader simple", function () { await mockM.put(block.cid, block.bytes); }); it("should have an empty car log", function () { - equals(loader.carLog.length, 0); + expect(loader.carLog.length).toBe(0); }); it("should commit", async function () { const carGroup = await loader.commit(t, { head: [block.cid] }); - equals(loader.carLog.length, 1); + expect(loader.carLog.length).toBe(1); const reader = await loader.loadCar(carGroup[0]); - assert(reader); - const parsed = await parseCarFile(reader); - assert(parsed.cars); - equals(parsed.cars.length, 0); - assert(parsed.meta); - assert(parsed.meta.head); + expect(reader).toBeTruthy(); + const parsed = await bs.parseCarFile(reader); + expect(parsed.cars).toBeTruthy(); + expect(parsed.cars.length).toBe(0); + expect(parsed.meta).toBeTruthy(); + expect(parsed.meta.head).toBeTruthy(); }); }); describe("basic Loader with two commits", function () { - let loader: Loader; + let loader: bs.Loader; let block: BlockView; let block2: BlockView; let block3: BlockView; let block4: BlockView; - let t: CarTransaction; - let carCid: CarGroup; - let carCid0: CarGroup; + let t: bs.CarTransaction; + let carCid: bs.CarGroup; + let carCid0: bs.CarGroup; afterEach(async function () { await loader.close(); @@ -113,10 +103,10 @@ describe("basic Loader with two commits", function () { }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); const mockM = new MyMemoryBlockStore(); - t = new CarTransaction(mockM); - loader = new Loader("test-loader-two-commit", { public: true }); + t = new bs.CarTransaction(mockM); + loader = new bs.Loader("test-loader-two-commit", { public: true }); block = await encode({ value: { hello: "world" }, hasher, @@ -150,60 +140,60 @@ describe("basic Loader with two commits", function () { }); it("should have a car log", function () { - equals(loader.carLog.length, 2); - equals(loader.carLog[0].toString(), carCid.toString()); - equals(loader.carLog[1].toString(), carCid0.toString()); + expect(loader.carLog.length).toBe(2); + expect(loader.carLog[0].toString()).toBe(carCid.toString()); + expect(loader.carLog[1].toString()).toBe(carCid0.toString()); }); it("should commit", async function () { const reader = await loader.loadCar(carCid[0]); - assert(reader); - const parsed = await parseCarFile(reader); - assert(parsed.cars); - equals(parsed.compact.length, 0); - equals(parsed.cars.length, 1); - assert(parsed.meta); - assert(parsed.meta.head); + expect(reader).toBeTruthy(); + const parsed = await bs.parseCarFile(reader); + expect(parsed.cars).toBeTruthy(); + expect(parsed.compact.length).toBe(0); + expect(parsed.cars.length).toBe(1); + expect(parsed.meta).toBeTruthy(); + expect(parsed.meta.head).toBeTruthy(); }); it("should compact", async function () { const compactCid = await loader.commit(t, { head: [block2.cid] }, { compact: true }); - equals(loader.carLog.length, 1); + expect(loader.carLog.length).toBe(1); const reader = await loader.loadCar(compactCid[0]); - assert(reader); - const parsed = await parseCarFile(reader); - assert(parsed.cars); - equals(parsed.compact.length, 2); - equals(parsed.cars.length, 0); - assert(parsed.meta); - assert(parsed.meta.head); + expect(reader).toBeTruthy(); + const parsed = await bs.parseCarFile(reader); + expect(parsed.cars).toBeTruthy(); + expect(parsed.compact.length).toBe(2); + expect(parsed.cars.length).toBe(0); + expect(parsed.meta).toBeTruthy(); + expect(parsed.meta.head).toBeTruthy(); }); it("compact should erase old files", async function () { const cs = await loader.carStore(); await loader.commit(t, { head: [block2.cid] }, { compact: true }); - equals(loader.carLog.length, 1); + expect(loader.carLog.length).toBe(1); await loader.commit(t, { head: [block3.cid] }, { compact: false }); - equals(loader.carLog.length, 2); - assert(await cs.load(carCid[0])); + expect(loader.carLog.length).toBe(2); + expect(await cs.load(carCid[0])).toBeTruthy(); await loader.commit(t, { head: [block3.cid] }, { compact: true }); - equals(loader.carLog.length, 1); + expect(loader.carLog.length).toBe(1); const e0 = await cs.load(carCid[0]).catch((e) => e); - assert(e0 instanceof Error, "e should be an instance of Error"); + expect(e0 instanceof Error).toBeTruthy(); await loader.commit(t, { head: [block4.cid] }, { compact: false }); - equals(loader.carLog.length, 2); + expect(loader.carLog.length).toBe(2); const e = await loader.loadCar(carCid[0]).catch((e) => e); - assert(e); - assert(e instanceof Error, "e should be Error"); - matches(e.message, "missing car file"); + expect(e).toBeTruthy(); + expect(e instanceof Error).toBeTruthy(); + expect(e.message).toMatch("missing car file"); }, 10000); }); describe("basic Loader with index commits", function () { let block: BlockView; - let ib: EncryptedBlockstore; + let ib: bs.EncryptedBlockstore; let indexerResult: IndexTransactionMeta; let cid: CID; // let indexMap: Map; @@ -215,9 +205,9 @@ describe("basic Loader with index commits", function () { beforeEach(async function () { const name = "test-loader-index" + Math.random(); - await SysContainer.start(); + await rt.SysContainer.start(); // t = new CarTransaction() - ib = new EncryptedBlockstore({ name }); + ib = new bs.EncryptedBlockstore({ name }); block = await encode({ value: { hello: "world" }, hasher, @@ -241,8 +231,8 @@ describe("basic Loader with index commits", function () { }); it("should start with an empty car log", function () { - assert(ib.loader); - equals(ib.loader.carLog.length, 0); + expect(ib.loader).toBeTruthy(); + expect(ib.loader.carLog.length).toBe(0); }); it("should commit the index metadata", async function () { @@ -253,22 +243,23 @@ describe("basic Loader with index commits", function () { } /* , indexMap */, ); - assert(carCid); - assert(ib.loader); + expect(carCid).toBeTruthy(); + expect(ib.loader).toBeTruthy(); const carLog = ib.loader.carLog; - equals(carLog.length, 1); - const reader = await ib.loader.loadCar(carCid[0]); - assert(reader); - const parsed = await parseCarFile(reader); - assert(parsed.cars); - equals(parsed.cars.length, 0); - assert(parsed.meta); - assert(parsed.meta.indexes); + expect(carLog.length).toBe(1); + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const reader = await ib.loader.loadCar(carCid![0]); + expect(reader).toBeTruthy(); + const parsed = await bs.parseCarFile(reader); + expect(parsed.cars).toBeTruthy(); + expect(parsed.cars.length).toBe(0); + expect(parsed.meta).toBeTruthy(); + expect(parsed.meta.indexes).toBeTruthy(); const indexes = parsed.meta.indexes; - assert(indexes); - assert(indexes.hello); - equals(indexes.hello.map, "(doc) => doc.hello"); - equals(indexes.hello.name, "hello"); + expect(indexes).toBeTruthy(); + expect(indexes.hello).toBeTruthy(); + expect(indexes.hello.map).toBe("(doc) => doc.hello"); + expect(indexes.hello.name).toBe("hello"); }); }); diff --git a/tests/blockstore/store.test.ts b/tests/blockstore/store.test.ts new file mode 100644 index 000000000..99d7fbc6a --- /dev/null +++ b/tests/blockstore/store.test.ts @@ -0,0 +1,145 @@ +import { CID } from "multiformats"; +import { rt, bs } from "@fireproof/core"; + +const decoder = new TextDecoder("utf-8"); + +describe("DataStore", function () { + let store: bs.DataStore; + let raw: bs.TestStore; + + afterEach(async () => { + await store.close(); + await store.destroy(); + }); + + beforeEach(async () => { + await rt.SysContainer.start(); + store = await bs.toStoreRuntime().makeDataStore({ name: "test" } as bs.Loadable); + raw = await bs.testStoreFactory(store.url); + }); + + it("should have a name", function () { + expect(store.name).toEqual("test"); + }); + + it("should save a car", async function () { + const car: bs.AnyBlock = { + cid: "cidKey" as unknown as CID, + bytes: new Uint8Array([55, 56, 57]), + }; + await store.save(car); + const data = await raw.get(car.cid.toString()); + expect(decoder.decode(data)).toEqual(decoder.decode(car.bytes)); + }); +}); + +describe("DataStore with a saved car", function () { + let store: bs.DataStore; + let raw: bs.TestStore; + let car: bs.AnyBlock; + + afterEach(async () => { + await store.close(); + await store.destroy(); + }); + + beforeEach(async function () { + await rt.SysContainer.start(); + store = await bs.toStoreRuntime().makeDataStore({ name: "test2" } as bs.Loadable); + raw = await bs.testStoreFactory(store.url); + car = { + cid: "cid" as unknown as CID, + bytes: new Uint8Array([55, 56, 57, 80]), + }; + await store.save(car); + }); + + it("should have a car", async function () { + const data = await raw.get(car.cid.toString()); + expect(decoder.decode(data)).toEqual(decoder.decode(car.bytes)); + }); + + it("should load a car", async function () { + const loaded = await store.load(car.cid); + expect(loaded.cid).toEqual(car.cid); + expect(loaded.bytes.constructor.name).toEqual("Uint8Array"); + expect(loaded.bytes.toString()).toEqual(car.bytes.toString()); + }); + + it("should remove a car", async function () { + await store.remove(car.cid); + const error = (await store.load(car.cid).catch((e: Error) => e)) as Error; + expect(error.message).toMatch("ENOENT"); + // matches(error.message, "ENOENT"); + }); +}); + +describe("MetaStore", function () { + let store: bs.MetaStore; + let raw: bs.TestStore; + + afterEach(async () => { + await store.close(); + await store.destroy(); + }); + + beforeEach(async function () { + await rt.SysContainer.start(); + store = await bs.toStoreRuntime().makeMetaStore({ name: "test" } as unknown as bs.Loader); + raw = await bs.testStoreFactory(store.url); + }); + + it("should have a name", function () { + expect(store.name).toEqual("test"); + }); + + it("should save a header", async function () { + const cid = CID.parse("bafybeia4luuns6dgymy5kau5rm7r4qzrrzg6cglpzpogussprpy42cmcn4"); + const h: bs.DbMeta = { + cars: [cid], + key: undefined, + }; + await store.save(h); + const file = await raw.get("main"); + const header = JSON.parse(decoder.decode(file)); + expect(header).toBeTruthy(); + expect(header.cars).toBeTruthy(); + expect(header.cars[0]["/"]).toEqual(cid.toString()); + }); +}); + +describe("MetaStore with a saved header", function () { + let store: bs.MetaStore; + let raw: bs.TestStore; + let cid: CID; + + afterEach(async () => { + await store.close(); + await store.destroy(); + }); + + beforeEach(async function () { + await rt.SysContainer.start(); + store = await bs.toStoreRuntime().makeMetaStore({ name: "test-saved-header" } as unknown as bs.Loader); + raw = await bs.testStoreFactory(store.url); + cid = CID.parse("bafybeia4luuns6dgymy5kau5rm7r4qzrrzg6cglpzpogussprpy42cmcn4"); + await store.save({ cars: [cid], key: undefined }); + }); + + it("should have a header", async function () { + const data = decoder.decode(await raw.get("main")); + expect(data).toMatch(/car/); + const header = JSON.parse(data); + expect(header).toBeTruthy(); + expect(header.cars).toBeTruthy(); + expect(header.cars[0]["/"]).toEqual(cid.toString()); + }); + + it("should load a header", async function () { + const loadeds = (await store.load()) as bs.DbMeta[]; + const loaded = loadeds[0]; + expect(loaded).toBeTruthy(); + expect(loaded.cars).toBeTruthy(); + expect(loaded.cars.toString()).toEqual(cid.toString()); + }); +}); diff --git a/tests/storage-engine/transaction.test.ts b/tests/blockstore/transaction.test.ts similarity index 58% rename from tests/storage-engine/transaction.test.ts rename to tests/blockstore/transaction.test.ts index 9ee939a47..19ff9c76d 100644 --- a/tests/storage-engine/transaction.test.ts +++ b/tests/blockstore/transaction.test.ts @@ -1,79 +1,76 @@ import { CID } from "multiformats"; - -import { assert, equals, matches, equalsJSON } from "../helpers.js"; -import { EncryptedBlockstore, BaseBlockstore, CarTransaction } from "@fireproof/core/blockstore"; - -import { AnyAnyLink, AnyBlock, AnyLink } from "@fireproof/core/blockstore"; +// import { matches, equalsJSON } from "../helpers.js"; +import { bs } from "@fireproof/core"; describe("Fresh TransactionBlockstore", function () { - let blocks: BaseBlockstore; + let blocks: bs.BaseBlockstore; beforeEach(function () { - blocks = new BaseBlockstore(); + blocks = new bs.BaseBlockstore(); }); it("should not have a name", function () { // @ts-expect-error - name is not set on BaseBlockstore - assert(!blocks.name); + expect(blocks.name).toBeFalsy(); }); it("should not have a loader", function () { // @ts-expect-error - loader is not set on BaseBlockstore - assert(!blocks.loader); + expect(blocks.loader).toBeFalsy(); }); it("should not put", async function () { const value = new TextEncoder().encode("value"); - const e = await blocks.put("key" as unknown as AnyLink, value).catch((e) => e); - matches(e.message, /transaction/g); + const e = await blocks.put("key" as unknown as bs.AnyLink, value).catch((e) => e); + expect(e.message).toMatch(/transaction/g); }); it("should yield a transaction", async function () { const txR = await blocks.transaction(async (tblocks) => { - assert(tblocks); - assert(tblocks instanceof CarTransaction); + expect(tblocks).toBeTruthy(); + expect(tblocks instanceof bs.CarTransaction).toBeTruthy(); return { head: [] }; }); - assert(txR); - assert(txR.t); - equalsJSON(txR.meta, { head: [] }); + expect(txR).toBeTruthy(); + expect(txR.t).toBeTruthy(); + expect(txR.meta).toEqual({ head: [] }); }); }); describe("TransactionBlockstore with name", function () { - let blocks: EncryptedBlockstore; + let blocks: bs.EncryptedBlockstore; beforeEach(function () { - blocks = new EncryptedBlockstore({ name: "test" }); + blocks = new bs.EncryptedBlockstore({ name: "test" }); }); it("should have a name", function () { - equals(blocks.name, "test"); + expect(blocks.name).toEqual("test"); }); it("should have a loader", function () { - assert(blocks.loader); + expect(blocks.loader).toBeTruthy(); }); it("should get from loader", async function () { const bytes = new TextEncoder().encode("bytes"); - assert(blocks.loader); + expect(blocks.loader).toBeTruthy(); blocks.loader.getBlock = async (cid) => { return { cid, bytes }; }; - const value = await blocks.get("key" as unknown as AnyAnyLink); - equalsJSON(value, { cid: "key" as unknown as AnyAnyLink, bytes }); + const value = await blocks.get("key" as unknown as bs.AnyAnyLink); + expect(value).toEqual({ cid: "key" as unknown as bs.AnyAnyLink, bytes }); }); }); describe("A transaction", function () { - let tblocks: CarTransaction; - let blocks: EncryptedBlockstore; + let tblocks: bs.CarTransaction; + let blocks: bs.EncryptedBlockstore; beforeEach(async function () { - blocks = new EncryptedBlockstore({ name: "test" }); - tblocks = new CarTransaction(blocks); + blocks = new bs.EncryptedBlockstore({ name: "test" }); + tblocks = new bs.CarTransaction(blocks); blocks.transactions.add(tblocks); }); it("should put and get", async function () { const cid = CID.parse("bafybeia4luuns6dgymy5kau5rm7r4qzrrzg6cglpzpogussprpy42cmcn4"); const bytes = new TextEncoder().encode("bytes"); await tblocks.put(cid, bytes); - assert(blocks.transactions.has(tblocks)); + expect(blocks.transactions.has(tblocks)).toBeTruthy(); const got = await tblocks.get(cid); - assert(got); - equals(got.cid, cid); - equals(got.bytes, bytes); + expect(got).toBeTruthy(); + expect(got?.cid).toEqual(cid); + expect(got?.bytes).toEqual(bytes); }); }); @@ -82,7 +79,7 @@ function asUInt8Array(str: string) { } describe("TransactionBlockstore with a completed transaction", function () { - let blocks: BaseBlockstore; + let blocks: bs.BaseBlockstore; let cid: CID; let cid2: CID; @@ -90,7 +87,7 @@ describe("TransactionBlockstore with a completed transaction", function () { cid = CID.parse("bafybeia4luuns6dgymy5kau5rm7r4qzrrzg6cglpzpogussprpy42cmcn4"); cid2 = CID.parse("bafybeibgouhn5ktecpjuovt52zamzvm4dlve5ak7x6d5smms3itkhplnhm"); - blocks = new BaseBlockstore(); + blocks = new bs.BaseBlockstore(); await blocks.transaction(async (tblocks) => { await tblocks.put(cid, asUInt8Array("value")); await tblocks.put(cid2, asUInt8Array("value2")); @@ -104,22 +101,22 @@ describe("TransactionBlockstore with a completed transaction", function () { }); it("should have transactions", async function () { const ts = blocks.transactions; - equals(ts.size, 2); + expect(ts.size).toEqual(2); }); it("should get", async function () { - const value = (await blocks.get(cid)) as AnyBlock; - equals(value.cid, cid); - equals(value.bytes.toString(), asUInt8Array("value").toString()); + const value = (await blocks.get(cid)) as bs.AnyBlock; + expect(value.cid).toEqual(cid); + expect(value.bytes.toString()).toEqual(asUInt8Array("value").toString()); - const value2 = (await blocks.get(cid2)) as AnyBlock; - equals(value2.bytes.toString(), asUInt8Array("value2").toString()); + const value2 = (await blocks.get(cid2)) as bs.AnyBlock; + expect(value2.bytes.toString()).toEqual(asUInt8Array("value2").toString()); }); it("should yield entries", async function () { const blz = []; for await (const blk of blocks.entries()) { blz.push(blk); } - equals(blz.length, 2); + expect(blz.length).toEqual(2); }); }); diff --git a/tests/fireproof/crdt.test.ts b/tests/fireproof/crdt.test.ts index 67e0d5661..b98121ed3 100644 --- a/tests/fireproof/crdt.test.ts +++ b/tests/fireproof/crdt.test.ts @@ -1,13 +1,11 @@ -import { assert, equals, matches, notEquals, itSkip } from "../helpers.js"; +import { itSkip } from "../helpers.js"; import { uuidv4 } from "uuidv7"; import { CRDT } from "@fireproof/core"; -import { parseCarFile } from "@fireproof/core/blockstore"; +import { bs, rt } from "@fireproof/core"; import { CRDTMeta, DocValue } from "@fireproof/core"; import { Index, index } from "@fireproof/core"; -import { AnyBlock, EncryptedBlockstore, Loader } from "@fireproof/core/blockstore"; -import { SysContainer } from "@fireproof/core/runtime"; describe("Fresh crdt", function () { let crdt: CRDT<{ hello: string } | { points: number }>; @@ -16,17 +14,17 @@ describe("Fresh crdt", function () { await crdt.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); crdt = new CRDT(); }); it("should have an empty head", async function () { const head = crdt.clock.head; - equals(head.length, 0); + expect(head.length).toBe(0); }); it("should accept put and return results", async function () { const didPut = await crdt.bulk([{ id: "hello", value: { hello: "world" } }]); const head = didPut.head; - equals(head.length, 1); + expect(head.length).toBe(1); }); it("should accept multi-put and return results", async function () { const didPut = await crdt.bulk([ @@ -34,7 +32,7 @@ describe("Fresh crdt", function () { { id: "king", value: { points: 10 } }, ]); const head = didPut.head; - equals(head.length, 1); + expect(head.length).toBe(1); }); }); @@ -52,40 +50,40 @@ describe("CRDT with one record", function () { }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); crdt = new CRDT(`test@${uuidv4()}`); firstPut = await crdt.bulk([{ id: "hello", value: { hello: "world" } }]); }); it("should have a one-element head", async function () { const head = crdt.clock.head; - equals(head.length, 1); + expect(head.length).toBe(1); }); it("should return the head", async function () { - equals(firstPut.head.length, 1); + expect(firstPut.head.length).toBe(1); }); it("return the record on get", async function () { - const got = await crdt.get("hello"); - assert(got); - equals(got.doc.hello, "world"); + const got = (await crdt.get("hello")) as DocValue; + expect(got).toBeTruthy(); + expect(got.doc.hello).toBe("world"); }); it("should accept another put and return results", async function () { const didPut = await crdt.bulk([{ id: "nice", value: { nice: "data" } }]); const head = didPut.head; - equals(head.length, 1); + expect(head.length).toBe(1); const { doc } = (await crdt.get("nice")) as DocValue; - equals(doc.nice, "data"); + expect(doc.nice).toBe("data"); }); it("should allow for a delete", async function () { const didDel = await crdt.bulk([{ id: "hello", del: true }]); - assert(didDel.head); + expect(didDel.head).toBeTruthy(); const got = await crdt.get("hello"); - assert(!got); + expect(got).toBeFalsy(); }); it("should offer changes", async function () { const { result } = await crdt.changes([]); - equals(result.length, 1); - equals(result[0].id, "hello"); - equals(result[0].value?.hello, "world"); + expect(result.length).toBe(1); + expect(result[0].id).toBe("hello"); + expect(result[0].value?.hello).toBe("world"); }); }); @@ -101,7 +99,7 @@ describe("CRDT with a multi-write", function () { await crdt.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); crdt = new CRDT(); firstPut = await crdt.bulk([ { id: "ace", value: { points: 11 } }, @@ -110,31 +108,31 @@ describe("CRDT with a multi-write", function () { }); it("should have a one-element head", async function () { const head = crdt.clock.head; - equals(head.length, 1); - equals(firstPut.head.length, 1); + expect(head.length).toBe(1); + expect(firstPut.head.length).toBe(1); }); it("return the records on get", async function () { const { doc } = (await crdt.get("ace")) as DocValue; - equals(doc.points, 11); + expect(doc.points).toBe(11); - const got2 = await crdt.get("king"); - assert(got2); - equals(got2.doc.points, 10); + const got2 = (await crdt.get("king")) as DocValue; + expect(got2).toBeTruthy(); + expect(got2.doc.points).toBe(10); }); it("should accept another put and return results", async function () { const didPut = await crdt.bulk([{ id: "queen", value: { points: 10 } }]); const head = didPut.head; - equals(head.length, 1); - const got = await crdt.get("queen"); - assert(got); - equals(got.doc.points, 10); + expect(head.length).toBe(1); + const got = (await crdt.get("queen")) as DocValue; + expect(got).toBeTruthy(); + expect(got.doc.points).toBe(10); }); it("should offer changes", async function () { const { result } = await crdt.changes([]); - equals(result.length, 2); - equals(result[0].id, "ace"); - equals(result[0].value?.points, 11); - equals(result[1].id, "king"); + expect(result.length).toBe(2); + expect(result[0].id).toBe("ace"); + expect(result[0].value?.points).toBe(11); + expect(result[1].id).toBe("king"); }); it("should offer changes since", async function () { /** @type {CRDTMeta} */ @@ -142,13 +140,13 @@ describe("CRDT with a multi-write", function () { { id: "queen", value: { points: 10 } }, { id: "jack", value: { points: 10 } }, ]); - assert(secondPut.head); + expect(secondPut.head).toBeTruthy(); const { result: r2, head: h2 } = await crdt.changes(); - equals(r2.length, 4); + expect(r2.length).toBe(4); const { result: r3 } = await crdt.changes(firstPut.head); - equals(r3.length, 2); + expect(r3.length).toBe(2); const { result: r4 } = await crdt.changes(h2); - equals(r4.length, 0); + expect(r4.length).toBe(0); }); }); @@ -165,7 +163,7 @@ describe("CRDT with two multi-writes", function () { await crdt.destroy(); }); beforeEach(async () => { - await SysContainer.start(); + await rt.SysContainer.start(); crdt = new CRDT(); firstPut = await crdt.bulk([ { id: "ace", value: { points: 11 } }, @@ -178,30 +176,30 @@ describe("CRDT with two multi-writes", function () { }); it("should have a one-element head", async function () { const head = crdt.clock.head; - equals(head.length, 1); - equals(firstPut.head.length, 1); - equals(secondPut.head.length, 1); - notEquals(firstPut.head[0], secondPut.head[0]); + expect(head.length).toBe(1); + expect(firstPut.head.length).toBe(1); + expect(secondPut.head.length).toBe(1); + expect(firstPut.head[0]).not.toBe(secondPut.head[0]); }); it("return the records on get", async function () { const ret = await crdt.get("ace"); expect(ret).not.toBeNull(); const { doc } = ret as DocValue; - equals(doc.points, 11); + expect(doc.points).toBe(11); for (const key of ["king", "queen", "jack"]) { const { doc } = (await crdt.get(key)) as DocValue; - equals(doc.points, 10); + expect(doc.points).toBe(10); } }); it("should offer changes", async function () { const { result } = await crdt.changes(); - equals(result.length, 4); - equals(result[0].id, "ace"); - equals(result[0].value?.points, 11); - equals(result[1].id, "king"); - equals(result[2].id, "queen"); - equals(result[3].id, "jack"); + expect(result.length).toBe(4); + expect(result[0].id).toBe("ace"); + expect(result[0].value?.points).toBe(11); + expect(result[1].id).toBe("king"); + expect(result[2].id).toBe("queen"); + expect(result[3].id).toBe("jack"); }); }); @@ -212,7 +210,7 @@ describe("Compact a named CRDT with writes", function () { await crdt.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); crdt = new CRDT("named-crdt-compaction"); for (let i = 0; i < 10; i++) { const bulk = [ @@ -224,38 +222,38 @@ describe("Compact a named CRDT with writes", function () { }); it("has data", async function () { const got = (await crdt.get("ace")) as DocValue; - assert(got.doc); - equals(got.doc.points, 11); + expect(got.doc).toBeTruthy(); + expect(got.doc.points).toBe(11); }); it("should start with blocks", async function () { - const blz: AnyBlock[] = []; + const blz: bs.AnyBlock[] = []; for await (const blk of crdt.blockstore.entries()) { blz.push(blk); } - equals(blz.length, 13); + expect(blz.length).toBe(13); }); it("should start with changes", async function () { const { result } = await crdt.changes(); - equals(result.length, 2); - equals(result[0].id, "ace"); + expect(result.length).toBe(2); + expect(result[0].id).toBe("ace"); }); itSkip("should have fewer blocks after compact", async function () { await crdt.compact(); - const blz: AnyBlock[] = []; + const blz: bs.AnyBlock[] = []; for await (const blk of crdt.blockstore.entries()) { blz.push(blk); } - equals(blz.length, 23); + expect(blz.length).toBe(23); }); it("should have data after compact", async function () { await crdt.compact(); const got = (await crdt.get("ace")) as DocValue; - assert(got.doc); - equals(got.doc.points, 11); + expect(got.doc).toBeTruthy(); + expect(got.doc.points).toBe(11); }); it("should have changes after compact", async function () { const chs = await crdt.changes(); - equals(chs.result[0].id, "ace"); + expect(chs.result[0].id).toBe("ace"); }); }); @@ -267,7 +265,7 @@ describe("CRDT with an index", function () { await crdt.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); crdt = new CRDT(); await crdt.bulk([ { id: "ace", value: { points: 11 } }, @@ -277,24 +275,24 @@ describe("CRDT with an index", function () { }); it("should query the data", async function () { const got = await idx.query({ range: [9, 12] }); - equals(got.rows.length, 2); - equals(got.rows[0].id, "king"); - equals(got.rows[0].key, 10); + expect(got.rows.length).toBe(2); + expect(got.rows[0].id).toBe("king"); + expect(got.rows[0].key).toBe(10); }); it("should register the index", async function () { const rIdx = await index({ _crdt: crdt }, "points"); - assert(rIdx); - equals(rIdx.name, "points"); + expect(rIdx).toBeTruthy(); + expect(rIdx.name).toBe("points"); const got = await rIdx.query({ range: [9, 12] }); - equals(got.rows.length, 2); - equals(got.rows[0].id, "king"); - equals(got.rows[0].key, 10); + expect(got.rows.length).toBe(2); + expect(got.rows[0].id).toBe("king"); + expect(got.rows[0].key).toBe(10); }); it("creating a different index with same name should not work", async function () { const e = await index({ _crdt: crdt }, "points", (doc) => doc._id) .query() .catch((err) => err); - matches(e.message, /cannot apply/); + expect(e.message).toMatch(/cannot apply/); }); }); @@ -302,8 +300,8 @@ describe("Loader with a committed transaction", function () { interface CRDTTestType { readonly foo: string; } - let loader: Loader; - let blockstore: EncryptedBlockstore; + let loader: bs.Loader; + let blockstore: bs.EncryptedBlockstore; let crdt: CRDT; let done: CRDTMeta; const dbname = "test-loader"; @@ -312,9 +310,9 @@ describe("Loader with a committed transaction", function () { await crdt.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); crdt = new CRDT(dbname); - blockstore = crdt.blockstore as EncryptedBlockstore; + blockstore = crdt.blockstore as bs.EncryptedBlockstore; if (!blockstore.loader) { throw new Error("no loader"); } @@ -322,23 +320,23 @@ describe("Loader with a committed transaction", function () { done = await crdt.bulk([{ id: "foo", value: { foo: "bar" } }]); }); it("should have a name", function () { - equals(loader.name, dbname); + expect(loader.name).toBe(dbname); }); it("should commit a transaction", function () { - assert(done.head); - // assert(done.cars); - equals(loader.carLog.length, 1); + expect(done.head).toBeTruthy(); + // expect(done.cars).toBeTruthy(); + expect(loader.carLog.length).toBe(1); }); it("can load the car", async function () { const blk = loader.carLog[0][0]; - assert(blk); + expect(blk).toBeTruthy(); const reader = await loader.loadCar(blk); - assert(reader); - const parsed = await parseCarFile(reader); - assert(parsed.cars); - equals(parsed.cars.length, 0); - assert(parsed.meta); - assert(parsed.meta.head); + expect(reader).toBeTruthy(); + const parsed = await bs.parseCarFile(reader); + expect(parsed.cars).toBeTruthy(); + expect(parsed.cars.length).toBe(0); + expect(parsed.meta).toBeTruthy(); + expect(parsed.meta.head).toBeTruthy(); }); }); @@ -346,9 +344,9 @@ describe("Loader with two committed transactions", function () { interface CRDTTestType { readonly foo: string; } - let loader: Loader; + let loader: bs.Loader; let crdt: CRDT; - let blockstore: EncryptedBlockstore; + let blockstore: bs.EncryptedBlockstore; let done1: CRDTMeta; let done2: CRDTMeta; afterEach(async function () { @@ -356,9 +354,9 @@ describe("Loader with two committed transactions", function () { await crdt.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); crdt = new CRDT("test-loader"); - blockstore = crdt.blockstore as EncryptedBlockstore; + blockstore = crdt.blockstore as bs.EncryptedBlockstore; if (!blockstore.loader) { throw new Error("no loader"); } @@ -367,29 +365,29 @@ describe("Loader with two committed transactions", function () { done2 = await crdt.bulk([{ id: "orange", value: { foo: "bar" } }]); }); it("should commit two transactions", function () { - assert(done1.head); - // assert(done1.cars); - assert(done2.head); - // assert(done2.cars); - notEquals(done1.head, done2.head); - // notEquals(done1.cars, done2.cars); - // equals(blockstore.transactions.size, 2) - equals(loader.carLog.length, 2); - //equals(loader.carLog.indexOf(done1.cars), 1) - // equals(loader.carLog.map((cs) => cs.toString()).indexOf(done1.cars.toString()), 1); - //equals(loader.carLog.indexOf(done2.cars), 0) - // equals(loader.carLog.map((cs) => cs.toString()).indexOf(done2.cars.toString()), 0); + expect(done1.head).toBeTruthy(); + // expect(done1.cars).toBeTruthy(); + expect(done2.head).toBeTruthy(); + // expect(done2.cars).toBeTruthy(); + expect(done1.head).not.toBe(done2.head); + // expect(done1.cars).not.toBe(done2.cars); + // expect(blockstore.transactions.size).toBe(2); + expect(loader.carLog.length).toBe(2); + // expect(loader.carLog.indexOf(done1.cars)).toBe(1); + // expect(loader.carLog.map((cs) => cs.toString()).indexOf(done1.cars.toString())).toBe(1); + // expect(loader.carLog.indexOf(done2.cars)).toBe(0); + // expect(loader.carLog.map((cs) => cs.toString()).indexOf(done2.cars.toString())).toBe(0); }); it("can load the car", async function () { const blk = loader.carLog[0][0]; - assert(blk); + expect(blk).toBeTruthy(); const reader = await loader.loadCar(blk); - assert(reader); - const parsed = await parseCarFile(reader); - assert(parsed.cars); - equals(parsed.cars.length, 1); - assert(parsed.meta); - assert(parsed.meta.head); + expect(reader).toBeTruthy(); + const parsed = await bs.parseCarFile(reader); + expect(parsed.cars).toBeTruthy(); + expect(parsed.cars.length).toBe(1); + expect(parsed.meta).toBeTruthy(); + expect(parsed.meta.head).toBeTruthy(); }); }); @@ -397,8 +395,8 @@ describe("Loader with many committed transactions", function () { interface Doc { foo: string; } - let loader: Loader; - let blockstore: EncryptedBlockstore; + let loader: bs.Loader; + let blockstore: bs.EncryptedBlockstore; let crdt: CRDT; let dones: CRDTMeta[]; const count = 10; @@ -407,9 +405,9 @@ describe("Loader with many committed transactions", function () { await crdt.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); crdt = new CRDT("test-loader-many"); - blockstore = crdt.blockstore as EncryptedBlockstore; + blockstore = crdt.blockstore as bs.EncryptedBlockstore; if (!blockstore.loader) { throw new Error("no loader"); } @@ -422,21 +420,21 @@ describe("Loader with many committed transactions", function () { }); it("should commit many transactions", function () { for (const done of dones) { - assert(done.head); - // assert(done.cars); + expect(done.head).toBeTruthy(); + // expect(done.cars).toBeTruthy(); } - equals(blockstore.transactions.size, 0); // cleaned up on commit - equals(loader.carLog.length, count); + expect(blockstore.transactions.size).toBe(0); // cleaned up on commit + expect(loader.carLog.length).toBe(count); }); it("can load the car", async function () { const blk = loader.carLog[2][0]; - // assert(dones[5].cars); + // expect(dones[5].cars).toBeTruthy(); const reader = await loader.loadCar(blk); - assert(reader); - const parsed = await parseCarFile(reader); - assert(parsed.cars); - equals(parsed.cars.length, 7); - assert(parsed.meta); - assert(parsed.meta.head); + expect(reader).toBeTruthy(); + const parsed = await bs.parseCarFile(reader); + expect(parsed.cars).toBeTruthy(); + expect(parsed.cars.length).toBe(7); + expect(parsed.meta).toBeTruthy(); + expect(parsed.meta.head).toBeTruthy(); }); }); diff --git a/tests/fireproof/database.test.ts b/tests/fireproof/database.test.ts index 50a97eb04..fc38c4c86 100644 --- a/tests/fireproof/database.test.ts +++ b/tests/fireproof/database.test.ts @@ -1,7 +1,5 @@ -import { assert, equals, notEquals, matches, buildBlobFiles, FileWithCid } from "../helpers.js"; -import { Database, DbResponse, DocFileMeta, DocWithId } from "@fireproof/core"; -import { SysContainer } from "@fireproof/core/runtime"; -import { EncryptedBlockstore } from "@fireproof/core/blockstore"; +import { buildBlobFiles, FileWithCid } from "../helpers.js"; +import { rt, bs, Database, DbResponse, DocFileMeta, DocWithId, DocFiles } from "@fireproof/core"; describe("basic Database", () => { let db: Database; @@ -10,28 +8,28 @@ describe("basic Database", () => { await db.destroy(); }); beforeEach(async () => { - await SysContainer.start(); + await rt.SysContainer.start(); db = new Database(); }); it("should put", async () => { /** @type {Doc} */ const doc = { _id: "hello", value: "world" }; const ok = await db.put(doc); - equals(ok.id, "hello"); + expect(ok.id).toBe("hello"); }); it("get missing should throw", async () => { const e = await db.get("missing").catch((e) => e); - matches(e.message, /Not found/); + expect(e.message).toMatch(/Not found/); }); it("del missing should result in deleted state", async () => { await db.del("missing"); const e = await db.get("missing").catch((e) => e); - matches(e.message, /Not found/); + expect(e.message).toMatch(/Not found/); }); it("has no changes", async () => { const { rows } = await db.changes([]); - equals(rows.length, 0); + expect(rows.length).toBe(0); }); }); @@ -45,44 +43,44 @@ describe("basic Database with record", function () { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); db = new Database(); const ok = await db.put({ _id: "hello", value: "world" }); - equals(ok.id, "hello"); + expect(ok.id).toBe("hello"); }); it("should get", async function () { const doc = await db.get("hello"); - assert(doc); - equals(doc._id, "hello"); - equals(doc.value, "world"); + expect(doc).toBeTruthy(); + expect(doc._id).toBe("hello"); + expect(doc.value).toBe("world"); }); it("should update", async function () { const ok = await db.put({ _id: "hello", value: "universe" }); - equals(ok.id, "hello"); + expect(ok.id).toBe("hello"); const doc = await db.get("hello"); - assert(doc); - equals(doc._id, "hello"); - equals(doc.value, "universe"); + expect(doc).toBeTruthy(); + expect(doc._id).toBe("hello"); + expect(doc.value).toBe("universe"); }); it("should del last record", async function () { const ok = await db.del("hello"); - equals(ok.id, "hello"); + expect(ok.id).toBe("hello"); const e = await db.get("hello").catch((e) => e); - matches(e.message, /Not found/); + expect(e.message).toMatch(/Not found/); }); it("has changes", async function () { const { rows } = await db.changes([]); - equals(rows.length, 1); - equals(rows[0].key, "hello"); - equals(rows[0].value._id, "hello"); + expect(rows.length).toBe(1); + expect(rows[0].key).toBe("hello"); + expect(rows[0].value._id).toBe("hello"); }); it("is not persisted", async function () { const db2 = new Database(); const { rows } = await db2.changes([]); - equals(rows.length, 0); + expect(rows.length).toBe(0); const doc = await db2.get("hello").catch((e) => e); - assert(doc.message); + expect(doc.message).toBeTruthy(); await db2.close(); await db2.destroy(); }); @@ -98,86 +96,86 @@ describe("named Database with record", function () { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); db = new Database("test-db-name"); /** @type {Doc} */ const doc = { _id: "hello", value: "world" }; const ok = await db.put(doc); - equals(ok.id, "hello"); + expect(ok.id).toBe("hello"); }); it("should get", async function () { const doc = await db.get("hello"); - assert(doc); - equals(doc._id, "hello"); - equals(doc.value, "world"); + expect(doc).toBeTruthy(); + expect(doc._id).toBe("hello"); + expect(doc.value).toBe("world"); }); it("should update", async function () { const ok = await db.put({ _id: "hello", value: "universe" }); - equals(ok.id, "hello"); + expect(ok.id).toBe("hello"); const doc = await db.get("hello"); - assert(doc); - equals(doc._id, "hello"); - equals(doc.value, "universe"); + expect(doc).toBeTruthy(); + expect(doc._id).toBe("hello"); + expect(doc.value).toBe("universe"); }); it("should del last record", async function () { const ok = await db.del("hello"); - equals(ok.id, "hello"); + expect(ok.id).toBe("hello"); const e = await db.get("hello").catch((e) => e); - matches(e.message, /Not found/); + expect(e.message).toMatch(/Not found/); }); it("has changes", async function () { const { rows } = await db.changes([]); - equals(rows.length, 1); - equals(rows[0].key, "hello"); - equals(rows[0].value._id, "hello"); + expect(rows.length).toBe(1); + expect(rows[0].key).toBe("hello"); + expect(rows[0].value._id).toBe("hello"); }); it("should have a key", async function () { const { rows } = await db.changes([]); - equals(rows.length, 1); - const blocks = db._crdt.blockstore as EncryptedBlockstore; + expect(rows.length).toBe(1); + const blocks = db._crdt.blockstore as bs.EncryptedBlockstore; const loader = blocks.loader; - assert(loader); + expect(loader).toBeTruthy(); await loader.ready(); - equals(loader.key?.length, 64); - equals(loader.keyId?.length, 64); - notEquals(loader.key, loader.keyId); + expect(loader.key?.length).toBe(64); + expect(loader.keyId?.length).toBe(64); + expect(loader.key).not.toBe(loader.keyId); }); it("should work right with a sequence of changes", async function () { const numDocs = 10; for (let i = 0; i < numDocs; i++) { const doc = { _id: `id-${i}`, hello: "world" }; const ok = await db.put(doc); - equals(ok.id, `id-${i}`); + expect(ok.id).toBe(`id-${i}`); } const { rows } = await db.changes([]); - equals(rows.length, numDocs + 1); + expect(rows.length).toBe(numDocs + 1); const ok6 = await db.put({ _id: `id-${6}`, hello: "block" }); - equals(ok6.id, `id-${6}`); + expect(ok6.id).toBe(`id-${6}`); for (let i = 0; i < numDocs; i++) { const id = `id-${i}`; const doc = await db.get<{ hello: string }>(id); - assert(doc); - equals(doc._id, id); - equals(doc.hello.length, 5); + expect(doc).toBeTruthy(); + expect(doc._id).toBe(id); + expect(doc.hello.length).toBe(5); } const { rows: rows2 } = await db.changes([]); - equals(rows2.length, numDocs + 1); + expect(rows2.length).toBe(numDocs + 1); const ok7 = await db.del(`id-${7}`); - equals(ok7.id, `id-${7}`); + expect(ok7.id).toBe(`id-${7}`); const { rows: rows3 } = await db.changes([]); - equals(rows3.length, numDocs + 1); - equals(rows3[numDocs].key, `id-${7}`); - equals(rows3[numDocs].value._deleted, true); + expect(rows3.length).toBe(numDocs + 1); + expect(rows3[numDocs].key).toBe(`id-${7}`); + expect(rows3[numDocs].value._deleted).toBe(true); // test limit const { rows: rows4 } = await db.changes([], { limit: 5 }); - equals(rows4.length, 5); + expect(rows4.length).toBe(5); }); it("should work right after compaction", async function () { @@ -185,18 +183,18 @@ describe("named Database with record", function () { for (let i = 0; i < numDocs; i++) { const doc = { _id: `id-${i}`, hello: "world" }; const ok = await db.put(doc); - equals(ok.id, `id-${i}`); + expect(ok.id).toBe(`id-${i}`); } const { rows } = await db.changes([]); - equals(rows.length, numDocs + 1); + expect(rows.length).toBe(numDocs + 1); await db.compact(); const { rows: rows3 } = await db.changes([], { dirty: true }); - equals(rows3.length, numDocs + 1); + expect(rows3.length).toBe(numDocs + 1); const { rows: rows4 } = await db.changes([], { dirty: false }); - equals(rows4.length, numDocs + 1); + expect(rows4.length).toBe(numDocs + 1); }); }); @@ -223,7 +221,7 @@ describe("basic Database parallel writes / public", function () { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); db = new Database("test-parallel-writes", { public: true }); for (let i = 0; i < 10; i++) { const doc = { _id: `id-${i}`, hello: "world" }; @@ -233,25 +231,25 @@ describe("basic Database parallel writes / public", function () { }); it("should have one head", function () { const crdt = db._crdt; - equals(crdt.clock.head.length, 1); + expect(crdt.clock.head.length).toBe(1); }); it("should write all", async function () { for (let i = 0; i < 10; i++) { const id = `id-${i}`; const doc = await db.get<{ hello: string }>(id); - assert(doc); - equals(doc._id, id); - equals(doc.hello, "world"); + expect(doc).toBeTruthy(); + expect(doc._id).toBe(id); + expect(doc.hello).toBe("world"); } }); it("should del all", async function () { for (let i = 0; i < 10; i++) { const id = `id-${i}`; const ok = await db.del(id); - equals(ok.id, id); + expect(ok.id).toBe(id); const e = await db.get(id).catch((e) => e); - matches(e.message, /Not found/); + expect(e.message).toMatch(/Not found/); } }); it("should delete all in parallel", async function () { @@ -264,30 +262,30 @@ describe("basic Database parallel writes / public", function () { for (let i = 0; i < 10; i++) { const id = `id-${i}`; const e = await db.get(id).catch((e) => e); - matches(e.message, /Not found/); + expect(e.message).toMatch(/Not found/); } }); it("has changes", async function () { const { rows, clock } = await db.changes([]); - equals(clock[0], db._crdt.clock.head[0]); - equals(rows.length, 10); + expect(clock[0]).toBe(db._crdt.clock.head[0]); + expect(rows.length).toBe(10); // rows.sort((a, b) => a.key.localeCompare(b.key)); for (let i = 0; i < 10; i++) { - equals(rows[i].key, "id-" + i); - assert(rows[i].clock, "The clock head is missing"); + expect(rows[i].key).toBe("id-" + i); + expect(rows[i].clock).toBeTruthy(); } }); it("should not have a key", async function () { const { rows } = await db.changes([]); - equals(rows.length, 10); - assert(db.opts.public); - assert(db._crdt.opts.public); - const blocks = db._crdt.blockstore as EncryptedBlockstore; + expect(rows.length).toBe(10); + expect(db.opts.public).toBeTruthy(); + expect(db._crdt.opts.public).toBeTruthy(); + const blocks = db._crdt.blockstore as bs.EncryptedBlockstore; const loader = blocks.loader; - assert(loader); + expect(loader).toBeTruthy(); await loader.ready(); - equals(loader.key, undefined); - equals(loader.keyId, undefined); + expect(loader.key).toBeUndefined(); + expect(loader.keyId).toBeUndefined(); }); }); @@ -302,7 +300,7 @@ describe("basic Database with subscription", function () { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); db = new Database(); didRun = 0; waitForSub = new Promise((resolve) => { @@ -316,23 +314,23 @@ describe("basic Database with subscription", function () { }); it("should run on put", async function () { const all = await db.allDocs(); - equals(all.rows.length, 0); + expect(all.rows.length).toBe(0); const doc = { _id: "hello", message: "world" }; - equals(didRun, 0); + expect(didRun).toBe(0); const ok = await db.put(doc); await waitForSub; - assert(didRun); - assert(lastDoc); - assert(lastDoc._id); - equals(ok.id, "hello"); - equals(didRun, 1); + expect(didRun).toBeTruthy(); + expect(lastDoc).toBeTruthy(); + expect(lastDoc._id).toBe("hello"); + expect(ok.id).toBe("hello"); + expect(didRun).toBe(1); }); it("should unsubscribe", async function () { unsubscribe(); const doc = { _id: "hello", message: "again" }; const ok = await db.put(doc); - equals(ok.id, "hello"); - equals(didRun, 0); + expect(ok.id).toBe("hello"); + expect(didRun).toBe(0); }); }); @@ -345,7 +343,7 @@ describe("basic Database with no update subscription", function () { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); db = new Database(); didRun = 0; @@ -355,20 +353,20 @@ describe("basic Database with no update subscription", function () { }); it("should run on put", async function () { const all = await db.allDocs(); - equals(all.rows.length, 0); + expect(all.rows.length).toBe(0); /** @type {Doc} */ const doc = { _id: "hello", message: "world" }; - equals(didRun, 0); + expect(didRun).toBe(0); const ok = await db.put(doc); - equals(ok.id, "hello"); - equals(didRun, 1); + expect(ok.id).toBe("hello"); + expect(didRun).toBe(1); }); it("should unsubscribe", async function () { unsubscribe(); const doc = { _id: "hello", message: "again" }; const ok = await db.put(doc); - equals(ok.id, "hello"); - equals(didRun, 0); + expect(ok.id).toBe("hello"); + expect(didRun).toBe(0); }); }); @@ -382,7 +380,7 @@ describe("database with files input", () => { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); imagefiles = await buildBlobFiles(); db = new Database("fireproof-with-images"); const doc = { @@ -397,38 +395,38 @@ describe("database with files input", () => { }); it("Should upload images", async function () { - equals(result.id, "images-main"); + expect(result.id).toBe("images-main"); }); it("Should fetch the images", async function () { const doc = await db.get(result.id); - const files = doc._files; - assert(files); + const files = doc._files as DocFiles; + expect(files).toBeTruthy(); const keys = Object.keys(files); let fileMeta = files[keys[0]] as DocFileMeta; - assert(fileMeta); - assert(imagefiles[0].file.type); - assert(fileMeta.type); - equals(fileMeta.type, imagefiles[0].file.type); - equals(fileMeta.size, imagefiles[0].file.size); - equals(fileMeta.cid.toString(), imagefiles[0].cid); - equals(typeof fileMeta.file, "function"); + expect(fileMeta).toBeTruthy(); + expect(imagefiles[0].file.type).toBeTruthy(); + expect(fileMeta.type).toBeTruthy(); + expect(fileMeta.type).toBe(imagefiles[0].file.type); + expect(fileMeta.size).toBe(imagefiles[0].file.size); + expect(fileMeta.cid.toString()).toBe(imagefiles[0].cid); + expect(typeof fileMeta.file).toBe("function"); let file = (await fileMeta.file?.()) as File; - equals(file.type, imagefiles[0].file.type); - equals(file.size, imagefiles[0].file.size); - // equals(file.name, 'image.jpg') // see https://github.com/fireproof-storage/fireproof/issues/70 + expect(file.type).toBe(imagefiles[0].file.type); + expect(file.size).toBe(imagefiles[0].file.size); + // expect(file.name).toBe('image.jpg') // see https://github.com/fireproof-storage/fireproof/issues/70 fileMeta = files[keys[1]] as DocFileMeta; - equals(fileMeta.type, imagefiles[1].file.type); - equals(fileMeta.size, imagefiles[1].file.size); - equals(fileMeta.cid.toString(), imagefiles[1].cid); - equals(typeof fileMeta.file, "function"); + expect(fileMeta.type).toBe(imagefiles[1].file.type); + expect(fileMeta.size).toBe(imagefiles[1].file.size); + expect(fileMeta.cid.toString()).toBe(imagefiles[1].cid); + expect(typeof fileMeta.file).toBe("function"); file = (await fileMeta.file?.()) as File; - equals(file.type, imagefiles[1].file.type); - equals(file.size, imagefiles[1].file.size); - // equals(file.name, 'fireproof.png') // see https://github.com/fireproof-storage/fireproof/issues/70 + expect(file.type).toBe(imagefiles[1].file.type); + expect(file.size).toBe(imagefiles[1].file.size); + // expect(file.name).toBe('fireproof.png') // see https://github.com/fireproof-storage/fireproof/issues/70 }); it("should update the file document data without changing the files", async function () { @@ -439,30 +437,30 @@ describe("database with files input", () => { let files = doc._files || {}; let keys = Object.keys(files); let fileMeta = files[keys[0]] as DocFileMeta; - equals(fileMeta.type, imagefiles[0].file.type); - equals(fileMeta.size, imagefiles[0].file.size); - equals(fileMeta.cid.toString(), imagefiles[0].cid); - equals(typeof fileMeta.file, "function"); + expect(fileMeta.type).toBe(imagefiles[0].file.type); + expect(fileMeta.size).toBe(imagefiles[0].file.size); + expect(fileMeta.cid.toString()).toBe(imagefiles[0].cid); + expect(typeof fileMeta.file).toBe("function"); let file = (await fileMeta.file?.()) as File; - equals(file.type, imagefiles[0].file.type); - equals(file.size, imagefiles[0].file.size); + expect(file.type).toBe(imagefiles[0].file.type); + expect(file.size).toBe(imagefiles[0].file.size); doc.type = "images"; const r2 = await db.put(doc); - equals(r2.id, "images-main"); + expect(r2.id).toBe("images-main"); const readDoc = await db.get(r2.id); - equals(readDoc.type, "images"); + expect(readDoc.type).toBe("images"); files = readDoc._files || {}; keys = Object.keys(files); fileMeta = files[keys[0]] as DocFileMeta; - equals(fileMeta.type, imagefiles[0].file.type); - equals(fileMeta.size, imagefiles[0].file.size); - equals(fileMeta.cid.toString(), imagefiles[0].cid); - equals(typeof fileMeta.file, "function"); + expect(fileMeta.type).toBe(imagefiles[0].file.type); + expect(fileMeta.size).toBe(imagefiles[0].file.size); + expect(fileMeta.cid.toString()).toBe(imagefiles[0].cid); + expect(typeof fileMeta.file).toBe("function"); file = (await fileMeta.file?.()) as File; - equals(file.type, imagefiles[0].file.type); - equals(file.size, imagefiles[0].file.size); + expect(file.type).toBe(imagefiles[0].file.type); + expect(file.size).toBe(imagefiles[0].file.size); }); }); diff --git a/tests/fireproof/fireproof.test.ts b/tests/fireproof/fireproof.test.ts index 9f7d7035f..953424ace 100644 --- a/tests/fireproof/fireproof.test.ts +++ b/tests/fireproof/fireproof.test.ts @@ -1,12 +1,10 @@ -import { assert, equals, notEquals, equalsJSON, sleep, itSkip } from "../helpers.js"; +import { sleep, itSkip } from "../helpers.js"; import { CID } from "multiformats/cid"; -import { fireproof, Database, index, DbResponse, IndexRows, DocWithId, Index, MapFn } from "@fireproof/core"; -import { AnyLink, EncryptedBlockstore } from "@fireproof/core/blockstore"; -import { SysContainer } from "@fireproof/core/runtime"; +import { bs, rt, fireproof, Database, index, DbResponse, IndexRows, DocWithId, Index, MapFn } from "@fireproof/core"; -export function carLogIncludesGroup(list: AnyLink[], cid: CID) { +export function carLogIncludesGroup(list: bs.AnyLink[], cid: CID) { return list.some((c) => c.equals(cid)); } @@ -32,31 +30,31 @@ describe("dreamcode", function () { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); db = fireproof("test-db"); ok = await db.put({ _id: "test-1", text: "fireproof", dream: true }); doc = await db.get(ok.id); result = await db.query("text", { range: ["a", "z"] }); }); it("should put", function () { - assert(ok); - equals(ok.id, "test-1"); + expect(ok).toBeTruthy(); + expect(ok.id).toBe("test-1"); }); it("should get", function () { - equals(doc.text, "fireproof"); + expect(doc.text).toBe("fireproof"); }); it("should query", function () { - assert(result); - assert(result.rows); - equals(result.rows.length, 1); - equals(result.rows[0].key, "fireproof"); + expect(result).toBeTruthy(); + expect(result.rows).toBeTruthy(); + expect(result.rows.length).toBe(1); + expect(result.rows[0].key).toBe("fireproof"); }); it("should query with function", async function () { const result = await db.query((doc) => doc.dream); - assert(result); - assert(result.rows); - equals(result.rows.length, 1); - equals(result.rows[0].key, true); + expect(result).toBeTruthy(); + expect(result.rows).toBeTruthy(); + expect(result.rows.length).toBe(1); + expect(result.rows[0].key).toBe(true); }); }); @@ -75,7 +73,7 @@ describe("public API", function () { }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); db = fireproof("test-api"); // index = index(db, 'test-index', (doc) => doc.foo) ok = await db.put({ _id: "test", foo: "bar" }); @@ -83,21 +81,21 @@ describe("public API", function () { query = await db.query((doc) => doc.foo); }); it("should be a database instance", function () { - assert(db); - assert(db instanceof Database); + expect(db).toBeTruthy(); + expect(db instanceof Database).toBeTruthy(); }); it("should put", function () { - assert(ok); - equals(ok.id, "test"); + expect(ok).toBeTruthy(); + expect(ok.id).toBe("test"); }); it("should get", function () { - equals(doc.foo, "bar"); + expect(doc.foo).toBe("bar"); }); it("should query", function () { - assert(query); - assert(query.rows); - equals(query.rows.length, 1); - equals(query.rows[0].key, "bar"); + expect(query).toBeTruthy(); + expect(query.rows).toBeTruthy(); + expect(query.rows.length).toBe(1); + expect(query.rows[0].key).toBe("bar"); }); }); @@ -111,39 +109,39 @@ describe("basic database", function () { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); db = new Database("test-basic"); }); it("can put with id", async function () { const ok = await db.put({ _id: "test", foo: "bar" }); - assert(ok); - equals(ok.id, "test"); + expect(ok).toBeTruthy(); + expect(ok.id).toBe("test"); }); it("can put without id", async function () { const ok = await db.put({ foo: "bam" }); - assert(ok); + expect(ok).toBeTruthy(); const got = await db.get(ok.id); - equals(got.foo, "bam"); + expect(got.foo).toBe("bam"); }); it("can define an index", async function () { const ok = await db.put({ _id: "test", foo: "bar" }); - assert(ok); + expect(ok).toBeTruthy(); const idx = index(db, "test-index", (doc) => doc.foo); const result = await idx.query(); - assert(result); - assert(result.rows); - equals(result.rows.length, 1); - equals(result.rows[0].key, "bar"); + expect(result).toBeTruthy(); + expect(result.rows).toBeTruthy(); + expect(result.rows.length).toBe(1); + expect(result.rows[0].key).toBe("bar"); }); it("can define an index with a default function", async function () { const ok = await db.put({ _id: "test", foo: "bar" }); - assert(ok); + expect(ok).toBeTruthy(); const idx = index(db, "foo"); const result = await idx.query(); - assert(result); - assert(result.rows); - equals(result.rows.length, 1); - equals(result.rows[0].key, "bar"); + expect(result).toBeTruthy(); + expect(result.rows).toBeTruthy(); + expect(result.rows.length).toBe(1); + expect(result.rows[0].key).toBe("bar"); }); }); @@ -155,17 +153,17 @@ describe("benchmarking with compaction", function () { }); beforeEach(async function () { // erase the existing test data - await SysContainer.start(); + await rt.SysContainer.start(); db = new Database("test-benchmark-compaction", { autoCompact: 3, public: true }); }); itSkip( "passing: insert during compaction", async function () { const ok = await db.put({ _id: "test", foo: "fast" }); - assert(ok); - equals(ok.id, "test"); - assert(db._crdt.clock.head); - equals(db._crdt.clock.head.length, 1); + expect(ok).toBeTruthy(); + expect(ok.id).toBe("test"); + expect(db._crdt.clock.head).toBeTruthy(); + expect(db._crdt.clock.head.length).toBe(1); const numDocs = 20000; const batchSize = 500; @@ -188,9 +186,9 @@ describe("benchmarking with compaction", function () { }), ); } - const blocks = db._crdt.blockstore as EncryptedBlockstore; + const blocks = db._crdt.blockstore as bs.EncryptedBlockstore; const loader = blocks.loader; - assert(loader); + expect(loader).toBeTruthy(); const label = `write ${i} log ${loader.carLog.length}`; console.time(label); db.put({ @@ -218,7 +216,7 @@ describe("benchmarking a database", function () { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); // erase the existing test data db = new Database("test-benchmark", { autoCompact: 100000, public: true }); // db = new Database(null, {autoCompact: 100000}) @@ -233,11 +231,11 @@ describe("benchmarking a database", function () { "passing: insert and read many records", async () => { const ok = await db.put({ _id: "test", foo: "fast" }); - assert(ok); - equals(ok.id, "test"); + expect(ok).toBeTruthy(); + expect(ok.id).toBe("test"); - assert(db._crdt.clock.head); - equals(db._crdt.clock.head.length, 1); + expect(db._crdt.clock.head).toBeTruthy(); + expect(db._crdt.clock.head.length).toBe(1); const numDocs = 2500; const batchSize = 500; @@ -256,7 +254,7 @@ describe("benchmarking a database", function () { }) .then((ok) => { db.get<{ fire: string }>(`test${i + j}`).then((doc) => { - assert(doc.fire); + expect(doc.fire).toBeTruthy(); }); return ok; }), @@ -275,19 +273,19 @@ describe("benchmarking a database", function () { console.time("open new DB"); const newDb = new Database("test-benchmark", { autoCompact: 100000, public: true }); const doc = await newDb.get<{ foo: string }>("test"); - equals(doc.foo, "fast"); + expect(doc.foo).toBe("fast"); console.timeEnd("open new DB"); console.time("changes"); const result = await db.changes(); // takes 1.5 seconds (doesn't have to load blocks from cars) console.timeEnd("changes"); - equals(result.rows.length, numDocs + 1); + expect(result.rows.length).toBe(numDocs + 1); // this takes 1 minute w 1000 docs console.time("changes new DB"); const result2 = await newDb.changes(); console.timeEnd("changes new DB"); - equals(result2.rows.length, numDocs + 1); + expect(result2.rows.length).toBe(numDocs + 1); await sleep(1000); @@ -303,11 +301,10 @@ describe("benchmarking a database", function () { await db.put({ _id: "compacted-test", foo: "bar" }); // console.log('car log length', db._crdt.blockstore.loader.carLog.length) - const blocks = db._crdt.blockstore as EncryptedBlockstore; + const blocks = db._crdt.blockstore as bs.EncryptedBlockstore; const loader = blocks.loader; - assert(loader); - - equals(loader.carLog.length, 2); + expect(loader).toBeTruthy(); + expect(loader.carLog.length).toBe(2); // console.time('allDocs new DB') // takes forever on 5k // const allDocsResult = await newDb.allDocs() @@ -318,18 +315,18 @@ describe("benchmarking a database", function () { console.time("compacted reopen again"); const newDb2 = new Database("test-benchmark", { autoCompact: 100000, public: true }); const doc21 = await newDb2.get("test"); - equals(doc21.foo, "fast"); - const blocks2 = newDb2._crdt.blockstore as EncryptedBlockstore; + expect(doc21.foo).toBe("fast"); + const blocks2 = newDb2._crdt.blockstore as bs.EncryptedBlockstore; const loader2 = blocks2.loader; - assert(loader2); + expect(loader2).toBeTruthy(); - equals(loader2.carLog.length, 2); + expect(loader2.carLog.length).toBe(2); const doc2 = await newDb2.get("compacted-test"); - equals(doc2.foo, "bar"); + expect(doc2.foo).toBe("bar"); - equals(doc2.foo, "bar"); + expect(doc2.foo).toBe("bar"); console.timeEnd("compacted reopen again"); await sleep(100); @@ -337,7 +334,7 @@ describe("benchmarking a database", function () { console.time("compacted changes new DB2"); const result3 = await newDb2.changes(); console.timeEnd("compacted changes new DB2"); - equals(result3.rows.length, numDocs + 2); + expect(result3.rows.length).toBe(numDocs + 2); console.time("compacted newDb2 insert and read 100 records"); const ops2: Promise[] = []; @@ -351,7 +348,7 @@ describe("benchmarking a database", function () { }) .then(() => { newDb2.get<{ fire: number }>(`test${i}`).then((doc) => { - assert(doc.fire); + expect(doc.fire).toBeTruthy(); }); }); ops2.push(ok); @@ -381,49 +378,49 @@ describe("Reopening a database", function () { }); beforeEach(async function () { // erase the existing test data - await SysContainer.start(); + await rt.SysContainer.start(); db = new Database("test-reopen", { autoCompact: 100000 }); const ok = await db.put({ _id: "test", foo: "bar" }); - assert(ok); - equals(ok.id, "test"); + expect(ok).toBeTruthy(); + expect(ok.id).toBe("test"); - assert(db._crdt.clock.head); - equals(db._crdt.clock.head.length, 1); + expect(db._crdt.clock.head).toBeDefined(); + expect(db._crdt.clock.head.length).toBe(1); }); it("should persist data", async function () { const doc = await db.get("test"); - equals(doc.foo, "bar"); + expect(doc.foo).toBe("bar"); }); it("should have the same data on reopen", async function () { const db2 = new Database("test-reopen"); const doc = await db2.get("test"); - equals(doc.foo, "bar"); - assert(db2._crdt.clock.head); - equals(db2._crdt.clock.head.length, 1); - equalsJSON(db2._crdt.clock.head, db._crdt.clock.head); + expect(doc.foo).toBe("bar"); + expect(db2._crdt.clock.head).toBeDefined(); + expect(db2._crdt.clock.head.length).toBe(1); + expect(db2._crdt.clock.head).toEqual(db._crdt.clock.head); await db2.close(); }); it("should have a car in the car log", async function () { await db._crdt.ready(); - const blocks = db._crdt.blockstore as EncryptedBlockstore; + const blocks = db._crdt.blockstore as bs.EncryptedBlockstore; const loader = blocks.loader; - assert(loader); - assert(loader.carLog); - equals(loader.carLog.length, 1); + expect(loader).toBeDefined(); + expect(loader.carLog).toBeDefined(); + expect(loader.carLog.length).toBe(1); }); it("should have carlog after reopen", async function () { const db2 = new Database("test-reopen"); await db2._crdt.ready(); - const blocks = db2._crdt.blockstore as EncryptedBlockstore; + const blocks = db2._crdt.blockstore as bs.EncryptedBlockstore; const loader = blocks.loader; - assert(loader); - assert(loader.carLog); - equals(loader.carLog.length, 1); + expect(loader).toBeDefined(); + expect(loader.carLog).toBeDefined(); + expect(loader.carLog.length).toBe(1); await db2.close(); }); @@ -433,14 +430,14 @@ describe("Reopening a database", function () { const db = new Database("test-reopen"); // assert(db._crdt.xready()); await db._crdt.ready(); - const blocks = db._crdt.blockstore as EncryptedBlockstore; + const blocks = db._crdt.blockstore as bs.EncryptedBlockstore; const loader = blocks.loader; - equals(loader.carLog.length, i + 1); + expect(loader.carLog.length).toBe(i + 1); const ok = await db.put({ _id: `test${i}`, fire: "proof".repeat(50 * 1024) }); - assert(ok); - equals(loader.carLog.length, i + 2); + expect(ok).toBeTruthy(); + expect(loader.carLog.length).toBe(i + 2); const doc = await db.get(`test${i}`); - equals(doc.fire, "proof".repeat(50 * 1024)); + expect(doc.fire).toBe("proof".repeat(50 * 1024)); await db.close(); } }, 20000); @@ -455,20 +452,20 @@ describe("Reopening a database", function () { // assert(db._crdt.ready); await db._crdt.ready(); // console.timeEnd("db open"); - const blocks = db._crdt.blockstore as EncryptedBlockstore; + const blocks = db._crdt.blockstore as bs.EncryptedBlockstore; const loader = blocks.loader; - assert(loader); - equals(loader.carLog.length, i + 1); + expect(loader).toBeDefined(); + expect(loader.carLog.length).toBe(i + 1); // console.log('car log length', loader.carLog.length) // console.time("db put"); const ok = await db.put({ _id: `test${i}`, fire: "proof".repeat(50 * 1024) }); // console.timeEnd("db put"); - assert(ok); - equals(loader.carLog.length, i + 2); + expect(ok).toBeTruthy(); + expect(loader.carLog.length).toBe(i + 2); // console.time("db get"); const doc = await db.get(`test${i}`); // console.timeEnd("db get"); - equals(doc.fire, "proof".repeat(50 * 1024)); + expect(doc.fire).toBe("proof".repeat(50 * 1024)); } }, 200000, @@ -488,10 +485,10 @@ describe("Reopening a database with indexes", function () { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); db = fireproof("test-reopen-idx"); const ok = await db.put({ _id: "test", foo: "bar" }); - equals(ok.id, "test"); + expect(ok.id).toBe("test"); didMap = false; @@ -504,57 +501,57 @@ describe("Reopening a database with indexes", function () { it("should persist data", async function () { const doc = await db.get("test"); - equals(doc.foo, "bar"); + expect(doc.foo).toBe("bar"); const idx2 = index(db, "foo"); - assert(idx2 === idx, "same object"); + expect(idx2).toBe(idx); const result = await idx2.query(); - assert(result); - assert(result.rows); - equals(result.rows.length, 1); - equals(result.rows[0].key, "bar"); - assert(didMap); + expect(result).toBeTruthy(); + expect(result.rows).toBeTruthy(); + expect(result.rows.length).toBe(1); + expect(result.rows[0].key).toBe("bar"); + expect(didMap).toBeTruthy(); }); it("should reuse the index", async function () { const idx2 = index(db, "foo", mapFn); - assert(idx2 === idx, "same object"); + expect(idx2).toBe(idx); const result = await idx2.query(); - assert(result); - assert(result.rows); - equals(result.rows.length, 1); - equals(result.rows[0].key, "bar"); - assert(didMap); + expect(result).toBeTruthy(); + expect(result.rows).toBeTruthy(); + expect(result.rows.length).toBe(1); + expect(result.rows[0].key).toBe("bar"); + expect(didMap).toBeTruthy(); didMap = false; const r2 = await idx2.query(); - assert(r2); - assert(r2.rows); - equals(r2.rows.length, 1); - equals(r2.rows[0].key, "bar"); - assert(!didMap); + expect(r2).toBeTruthy(); + expect(r2.rows).toBeTruthy(); + expect(r2.rows.length).toBe(1); + expect(r2.rows[0].key).toBe("bar"); + expect(didMap).toBeFalsy(); }); it("should have the same data on reopen", async function () { const db2 = fireproof("test-reopen-idx"); const doc = await db2.get("test"); - equals(doc.foo, "bar"); - assert(db2._crdt.clock.head); - equals(db2._crdt.clock.head.length, 1); - equalsJSON(db2._crdt.clock.head, db._crdt.clock.head); + expect(doc.foo).toBe("bar"); + expect(db2._crdt.clock.head).toBeTruthy(); + expect(db2._crdt.clock.head.length).toBe(1); + expect(db2._crdt.clock.head).toEqual(db._crdt.clock.head); }); it("should have the same data on reopen after a query", async function () { const r0 = await idx.query(); - assert(r0); - assert(r0.rows); - equals(r0.rows.length, 1); - equals(r0.rows[0].key, "bar"); + expect(r0).toBeTruthy(); + expect(r0.rows).toBeTruthy(); + expect(r0.rows.length).toBe(1); + expect(r0.rows[0].key).toBe("bar"); const db2 = fireproof("test-reopen-idx"); const doc = await db2.get("test"); - equals(doc.foo, "bar"); - assert(db2._crdt.clock.head); - equals(db2._crdt.clock.head.length, 1); - equalsJSON(db2._crdt.clock.head, db._crdt.clock.head); + expect(doc.foo).toBe("bar"); + expect(db2._crdt.clock.head).toBeTruthy(); + expect(db2._crdt.clock.head.length).toBe(1); + expect(db2._crdt.clock.head).toEqual(db._crdt.clock.head); }); // it('should query the same data on reopen', async function () { @@ -580,25 +577,25 @@ describe("Reopening a database with indexes", function () { describe("basic js verify", function () { beforeAll(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); }); it("should include cids in arrays", async function () { const db = fireproof("test-verify"); const ok = await db.put({ _id: "test", foo: ["bar", "bam"] }); - equals(ok.id, "test"); + expect(ok.id).toBe("test"); const ok2 = await db.put({ _id: "test2", foo: ["bar", "bam"] }); - equals(ok2.id, "test2"); - const blocks = db._crdt.blockstore as EncryptedBlockstore; + expect(ok2.id).toBe("test2"); + const blocks = db._crdt.blockstore as bs.EncryptedBlockstore; const loader = blocks.loader; - assert(loader); + expect(loader).toBeTruthy(); const cid = loader.carLog[0][0]; const cid2 = db._crdt.clock.head[0]; - notEquals(cid, cid2); - assert(cid !== cid2); + expect(cid).not.toBe(cid2); + expect(cid).not.toBe(cid2); const cidList = [cid, cid2]; const cid3 = CID.parse(cid.toString()); - assert(!cidList.includes(cid3)); // sad trombone - assert(carLogIncludesGroup(cidList, cid3)); + expect(cidList.includes(cid3)).toBeFalsy(); // sad trombone + expect(carLogIncludesGroup(cidList, cid3)).toBeTruthy(); await db.close(); await db.destroy(); }); diff --git a/tests/fireproof/hello.test.ts b/tests/fireproof/hello.test.ts index 295a11234..227d1f056 100644 --- a/tests/fireproof/hello.test.ts +++ b/tests/fireproof/hello.test.ts @@ -1,11 +1,9 @@ -import { assert, equals } from "../helpers.js"; -import { fireproof as database, Database, DbResponse, DocWithId, index, Index, IndexRows } from "@fireproof/core"; -import { SysContainer } from "@fireproof/core/runtime"; +import { rt, fireproof as database, Database, DbResponse, DocWithId, index, Index, IndexRows } from "@fireproof/core"; describe("Hello World Test", function () { it("should pass the hello world test", function () { const result = database("hello"); // call to your library function - assert(result.name === "hello"); + expect(result.name).toBe("hello"); }); }); @@ -23,7 +21,7 @@ describe("public API", function () { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); db = database("test-public-api"); idx = index(db, "test-index", (doc) => doc.foo); ok = await db.put({ _id: "test", foo: "bar" }); @@ -31,24 +29,24 @@ describe("public API", function () { query = await idx.query(); }); it("should have a database", function () { - assert(db); - assert(db instanceof Database); + expect(db).toBeTruthy(); + expect(db instanceof Database).toBeTruthy(); }); it("should have an index", function () { - assert(idx); - assert(idx instanceof Index); + expect(idx).toBeTruthy(); + expect(idx instanceof Index).toBeTruthy(); }); it("should put", function () { - assert(ok); - equals(ok.id, "test"); + expect(ok).toBeTruthy(); + expect(ok.id).toBe("test"); }); it("should get", function () { - equals(doc.foo, "bar"); + expect(doc.foo).toBe("bar"); }); it("should query", function () { - assert(query); - assert(query.rows); - equals(query.rows.length, 1); - equals(query.rows[0].key, "bar"); + expect(query).toBeTruthy(); + expect(query.rows).toBeTruthy(); + expect(query.rows.length).toBe(1); + expect(query.rows[0].key).toBe("bar"); }); }); diff --git a/tests/fireproof/indexer.test.ts b/tests/fireproof/indexer.test.ts index 85f771ad0..e1d685ccf 100644 --- a/tests/fireproof/indexer.test.ts +++ b/tests/fireproof/indexer.test.ts @@ -1,7 +1,6 @@ -import { assert, equals, equalsJSON, itSkip } from "../helpers.js"; +import { itSkip } from "../helpers.js"; -import { Index, index, Database, CRDT, IndexRows } from "@fireproof/core"; -import { SysContainer } from "@fireproof/core/runtime"; +import { rt, Index, index, Database, CRDT, IndexRows } from "@fireproof/core"; interface TestType { readonly title: string; @@ -17,7 +16,7 @@ describe("basic Index", () => { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); db = new Database("test-indexer"); await db.put({ title: "amazing" }); await db.put({ title: "creative" }); @@ -28,67 +27,67 @@ describe("basic Index", () => { }); }); it("should have properties", function () { - equals(indexer.crdt, db._crdt); - equals(indexer.crdt.name, "test-indexer"); - equals(indexer.name, "hello"); - assert(indexer.mapFn); + expect(indexer.crdt).toBe(db._crdt); + expect(indexer.crdt.name).toBe("test-indexer"); + expect(indexer.name).toBe("hello"); + expect(indexer.mapFn).toBeTruthy(); }); it("should call the map function on first query", async function () { didMap = false; await indexer.query(); - assert(didMap); + expect(didMap).toBe(true); }); it("should not call the map function on second query", async function () { await indexer.query(); didMap = false; await indexer.query(); - assert(!didMap); + expect(didMap).toBe(false); }); it("should get results", async function () { const result = await indexer.query(); - assert(result); - assert(result.rows); - equals(result.rows.length, 3); + expect(result).toBeTruthy(); + expect(result.rows).toBeTruthy(); + expect(result.rows.length).toBe(3); }); it("should be in order", async function () { const { rows } = await indexer.query(); - equals(rows[0].key, "amazing"); + expect(rows[0].key).toBe("amazing"); }); it("should work with limit", async function () { const { rows } = await indexer.query({ limit: 1 }); - equals(rows.length, 1); + expect(rows.length).toBe(1); }); it("should work with descending", async function () { const { rows } = await indexer.query({ descending: true }); - equals(rows[0].key, "creative"); + expect(rows[0].key).toBe("creative"); }); it("should range query all", async function () { const { rows } = await indexer.query({ range: ["a", "z"] }); - equals(rows.length, 3); - equals(rows[0].key, "amazing"); + expect(rows.length).toBe(3); + expect(rows[0].key).toBe("amazing"); }); it("should range query all twice", async function () { const { rows } = await indexer.query({ range: ["a", "z"] }); - equals(rows.length, 3); - equals(rows[0].key, "amazing"); + expect(rows.length).toBe(3); + expect(rows[0].key).toBe("amazing"); const { rows: rows2 } = await indexer.query({ range: ["a", "z"] }); - equals(rows2.length, 3); - equals(rows2[0].key, "amazing"); + expect(rows2.length).toBe(3); + expect(rows2[0].key).toBe("amazing"); }); it("should range query", async function () { const { rows } = await indexer.query({ range: ["b", "d"] }); - equals(rows[0].key, "bazillas"); + expect(rows[0].key).toBe("bazillas"); }); it("should key query", async function () { const { rows } = await indexer.query({ key: "bazillas" }); - equals(rows.length, 1); + expect(rows.length).toBe(1); }); it("should include docs", async function () { const { rows } = await indexer.query({ includeDocs: true }); - assert(rows[0]); - assert(rows[0].id); - assert(rows[0].doc); - equals(rows[0].doc._id, rows[0].id); + expect(rows[0]).toBeTruthy(); + expect(rows[0].id).toBeTruthy(); + expect(rows[0].doc).toBeTruthy(); + expect(rows[0].doc?._id).toBe(rows[0].id); }); }); @@ -100,7 +99,7 @@ describe("Index query with compound key", function () { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); db = new Database("test-indexer"); await db.put({ title: "amazing", score: 1 }); await db.put({ title: "creative", score: 2 }); @@ -112,9 +111,9 @@ describe("Index query with compound key", function () { }); it("should prefix query", async function () { const { rows } = await indexer.query({ prefix: "creative" }); - equals(rows.length, 2); - equalsJSON(rows[0].key, ["creative", 2]); - equalsJSON(rows[1].key, ["creative", 20]); + expect(rows.length).toBe(2); + expect(rows[0].key).toEqual(["creative", 2]); + expect(rows[1].key).toEqual(["creative", 20]); }); }); @@ -126,7 +125,7 @@ describe("basic Index with map fun", function () { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); db = new Database("test-indexer"); await db.put({ title: "amazing" }); await db.put({ title: "creative" }); @@ -137,10 +136,10 @@ describe("basic Index with map fun", function () { }); it("should get results", async function () { const result = await indexer.query(); - assert(result); - assert(result.rows); - equals(result.rows.length, 3); - equals(result.rows[0].key, "amazing"); + expect(result).toBeTruthy(); + expect(result.rows).toBeTruthy(); + expect(result.rows.length).toBe(3); + expect(result.rows[0].key).toBe("amazing"); }); }); @@ -152,7 +151,7 @@ describe("basic Index with map fun with value", function () { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); db = new Database("test-indexer"); await db.put({ title: "amazing" }); await db.put({ title: "creative" }); @@ -163,21 +162,21 @@ describe("basic Index with map fun with value", function () { }); it("should get results", async function () { const result = await indexer.query(); - assert(result); - assert(result.rows); - equals(result.rows.length, 3); - equals(result.rows[0].key, "amazing"); + expect(result).toBeTruthy(); + expect(result.rows).toBeTruthy(); + expect(result.rows.length).toBe(3); + expect(result.rows[0].key).toBe("amazing"); // @jchris why is this not a object? - equals(result.rows[0].value, 7); + expect(result.rows[0].value).toBe(7); }); it("should include docs", async function () { const { rows } = await indexer.query({ includeDocs: true }); - assert(rows[0].doc); - equals(rows[0].doc._id, rows[0].id); - equals(rows.length, 3); - equals(rows[0].key, "amazing"); + expect(rows[0].doc).toBeTruthy(); + expect(rows[0].doc?._id).toBe(rows[0].id); + expect(rows.length).toBe(3); + expect(rows[0].key).toBe("amazing"); // @jchris why is this not a object? - equals(rows[0].value, 7); + expect(rows[0].value).toBe(7); }); }); @@ -189,7 +188,7 @@ describe("Index query with map and compound key", function () { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); db = new Database("test-indexer"); await db.put({ title: "amazing", score: 1 }); await db.put({ title: "creative", score: 2 }); @@ -201,9 +200,9 @@ describe("Index query with map and compound key", function () { }); it("should prefix query", async function () { const { rows } = await indexer.query({ prefix: "creative" }); - equals(rows.length, 2); - equalsJSON(rows[0].key, ["creative", 2]); - equalsJSON(rows[1].key, ["creative", 20]); + expect(rows.length).toBe(2); + expect(rows[0].key).toEqual(["creative", 2]); + expect(rows[1].key).toEqual(["creative", 20]); }); }); @@ -215,7 +214,7 @@ describe("basic Index with string fun", function () { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); db = new Database("test-indexer"); await db.put({ title: "amazing" }); await db.put({ title: "creative" }); @@ -224,14 +223,14 @@ describe("basic Index with string fun", function () { }); it("should get results", async function () { const result = await indexer.query(); - assert(result); - assert(result.rows); - equals(result.rows.length, 3); + expect(result).toBeTruthy(); + expect(result.rows).toBeTruthy(); + expect(result.rows.length).toBe(3); }); it("should include docs", async function () { const { rows } = await indexer.query(); - assert(rows.length); - assert(rows[0].doc); + expect(rows.length).toBeTruthy(); + expect(rows[0].doc).toBeTruthy(); }); }); @@ -251,7 +250,7 @@ describe("basic Index upon cold start", function () { await crdt.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); // db = database() crdt = new CRDT("test-indexer-cold", { persistIndexes: true }); await crdt.bulk([ @@ -267,63 +266,63 @@ describe("basic Index upon cold start", function () { indexer = await index({ _crdt: crdt }, "hello", mapFn); // new Index(db._crdt.indexBlockstore, db._crdt, 'hello', mapFn) result = await indexer.query(); - equalsJSON(indexer.indexHead, crdt.clock.head); + expect(indexer.indexHead).toEqual(crdt.clock.head); }); it("should call map on first query", function () { - assert(didMap); - equals(didMap, 3); + expect(didMap).toBeTruthy(); + expect(didMap).toEqual(3); }); it("should get results on first query", function () { - assert(result); - assert(result.rows); - equals(result.rows.length, 3); + expect(result).toBeTruthy(); + expect(result.rows).toBeTruthy(); + expect(result.rows.length).toEqual(3); }); it("should work on cold load", async function () { const crdt2 = new CRDT("test-indexer-cold", { persistIndexes: true }); const { result, head } = await crdt2.changes(); - assert(result); + expect(result).toBeTruthy(); await crdt2.ready(); const indexer2 = await index({ _crdt: crdt2 }, "hello", mapFn); await indexer2.xready(); const result2 = await indexer2.query(); - equalsJSON(indexer2.indexHead, head); - assert(result2); - equals(result2.rows.length, 3); - equalsJSON(indexer2.indexHead, head); + expect(indexer2.indexHead).toEqual(head); + expect(result2).toBeTruthy(); + expect(result2.rows.length).toEqual(3); + expect(indexer2.indexHead).toEqual(head); }); itSkip("should not rerun the map function on seen changes", async function () { didMap = 0; const crdt2 = new CRDT("test-indexer-cold", { persistIndexes: true }); const indexer2 = await index({ _crdt: crdt2 }, "hello", mapFn); const { result, head } = await crdt2.changes([]); - equals(result.length, 3); - equals(head.length, 1); + expect(result.length).toEqual(3); + expect(head.length).toEqual(1); const { result: ch2, head: h2 } = await crdt2.changes(head); - equals(ch2.length, 0); - equals(h2.length, 1); - equalsJSON(h2, head); + expect(ch2.length).toEqual(0); + expect(h2.length).toEqual(1); + expect(h2).toEqual(head); const result2 = await indexer2.query(); - equalsJSON(indexer2.indexHead, head); - assert(result2); - equals(result2.rows.length, 3); - equals(didMap, 0); + expect(indexer2.indexHead).toEqual(head); + expect(result2).toBeTruthy(); + expect(result2.rows.length).toEqual(3); + expect(didMap).toEqual(0); await crdt2.bulk([{ id: "abc4", value: { title: "despicable", score: 0 } }]); const { result: ch3, head: h3 } = await crdt2.changes(head); - equals(ch3.length, 1); - equals(h3.length, 1); + expect(ch3.length).toEqual(1); + expect(h3.length).toEqual(1); const result3 = await indexer2.query(); - assert(result3); - equals(result3.rows.length, 4); - equals(didMap, 1); + expect(result3).toBeTruthy(); + expect(result3.rows.length).toEqual(4); + expect(didMap).toEqual(1); }); it("should ignore meta when map function definiton changes", async function () { const crdt2 = new CRDT("test-indexer-cold"); const result = await index({ _crdt: crdt2 }, "hello", (doc) => doc.title.split("").reverse().join(""), ).query(); - equals(result.rows.length, 3); - equals(result.rows[0].key, "evitaerc"); // creative + expect(result.rows.length).toEqual(3); + expect(result.rows[0].key).toEqual("evitaerc"); // creative }); }); @@ -336,7 +335,7 @@ describe("basic Index with no data", function () { await db.destroy(); }); beforeEach(async function () { - await SysContainer.start(); + await rt.SysContainer.start(); db = new Database("test-indexer"); indexer = new Index(db._crdt, "hello", (doc) => { didMap = true; @@ -344,25 +343,25 @@ describe("basic Index with no data", function () { }); }); it("should have properties", function () { - equals(indexer.crdt, db._crdt); - equals(indexer.name, "hello"); - assert(indexer.mapFn); + expect(indexer.crdt).toEqual(db._crdt); + expect(indexer.name).toEqual("hello"); + expect(indexer.mapFn).toBeTruthy(); }); it("should not call the map function on first query", async function () { didMap = false; await indexer.query(); - assert(!didMap); + expect(didMap).toBeFalsy(); }); it("should not call the map function on second query", async function () { await indexer.query(); didMap = false; await indexer.query(); - assert(!didMap); + expect(didMap).toBeFalsy(); }); it("should get results", async function () { const result = await indexer.query(); - assert(result); - assert(result.rows); - equals(result.rows.length, 0); + expect(result).toBeTruthy(); + expect(result.rows).toBeTruthy(); + expect(result.rows.length).toEqual(0); }); }); diff --git a/tests/helpers.ts b/tests/helpers.ts index 7c2b862c9..7b1a630bc 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -1,9 +1,9 @@ -import { SysContainer, assert } from "@fireproof/core/runtime"; +import { rt } from "@fireproof/core"; import { toCryptoOpts } from "../src/runtime/crypto.js"; import { encodeFile } from "../src/runtime/files"; -export { dataDir } from "@fireproof/core/runtime"; -export { assert }; +const dataDir = rt.dataDir; +export { dataDir }; export function sleep(ms: number) { return new Promise((resolve) => setTimeout(resolve, ms)); @@ -22,67 +22,47 @@ export function itSkip(value: string, fn: () => unknown, options?: number) { console.warn("itSkip of " + value); } -export function equals(actual: T, expected: T) { - assert(actual === expected, `Expected '${actual}' to equal '${expected}'`); -} - -export function equalsJSON(actual: T, expected: T) { - equals(JSON.stringify(actual), JSON.stringify(expected)); -} +// -export function notEquals(actual: T, expected: T) { - assert(actual !== expected, `Expected '${actual} 'to not equal '${expected}'`); -} -interface ToStringFn { - toString: () => string; -} -export function matches(actual: TA, expected: TB | RegExp) { - if (expected instanceof RegExp) { - assert(actual.toString().match(expected), `Expected '${actual}' to match ${expected}`); - } else { - assert(actual.toString().match(expected.toString()), `Expected '${actual}' to match ${expected}`); - } -} +// // Function to copy a directory +// export async function copyDirectory(source: string, destination: string) { +// // Ensure the destination directory exists +// await rt.SysContainer.mkdir(destination, { recursive: true }); -// Function to copy a directory -export async function copyDirectory(source: string, destination: string) { - // Ensure the destination directory exists - await SysContainer.mkdir(destination, { recursive: true }); +// // Read the source directory +// const entries = await SysContainer.readdirent(source, { withFileTypes: true }); - // Read the source directory - const entries = await SysContainer.readdirent(source, { withFileTypes: true }); +// // Iterate through each entry in the directory +// for (const entry of entries) { +// const sourcePath = SysContainer.join(source, entry.name); +// const destinationPath = SysContainer.join(destination, entry.name); - // Iterate through each entry in the directory - for (const entry of entries) { - const sourcePath = SysContainer.join(source, entry.name); - const destinationPath = SysContainer.join(destination, entry.name); +// if (entry.isDirectory()) { +// // If the entry is a directory, copy it recursively +// await copyDirectory(sourcePath, destinationPath); +// } else if (entry.isFile()) { +// // If the entry is a file, copy it +// await SysContainer.copyFile(sourcePath, destinationPath); +// } +// } +// } - if (entry.isDirectory()) { - // If the entry is a directory, copy it recursively - await copyDirectory(sourcePath, destinationPath); - } else if (entry.isFile()) { - // If the entry is a file, copy it - await SysContainer.copyFile(sourcePath, destinationPath); - } - } -} - -export function getDirectoryName(url: string) { - let path: string; - try { - path = SysContainer.fileURLToPath(url); - } catch (e) { - path = url; - } - if (process && typeof process.cwd === "function") { - const cwd = process.cwd(); - if (cwd.endsWith("dist/esm")) { - path = "../../" + path; - } - } - const dir_name = SysContainer.dirname(path); - return dir_name; -} +// export function getDirectoryName(url: string) { +// let path: string; +// try { +// path = SysContainer.fileURLToPath(url); +// } catch (e) { +// path = url; +// } +// if (process && typeof process.cwd === "function") { +// const cwd = process.cwd(); +// if (cwd.endsWith("dist/esm")) { +// path = "../../" + path; +// } +// } +// const dir_name = SysContainer.dirname(path); +// return dir_name; +// } async function toFileWithCid(buffer: Uint8Array, name: string, opts: FilePropertyBag): Promise { return { diff --git a/tests/storage-engine/store.test.ts b/tests/storage-engine/store.test.ts deleted file mode 100644 index c5fc5516c..000000000 --- a/tests/storage-engine/store.test.ts +++ /dev/null @@ -1,157 +0,0 @@ -import { CID } from "multiformats"; - -import { matches, equals } from "../helpers.js"; - -import { MetaStore, DataStore, Loader, Loadable, testStoreFactory } from "@fireproof/core/blockstore"; - -import { toStoreRuntime } from "@fireproof/core/blockstore"; -import { AnyBlock, DbMeta } from "@fireproof/core/blockstore"; -import { SysContainer, assert } from "@fireproof/core/runtime"; -import { TestStore } from "@fireproof/core/blockstore"; - -const decoder = new TextDecoder("utf-8"); - -describe("DataStore", function () { - let store: DataStore; - let raw: TestStore; - - afterEach(async () => { - await store.close(); - await store.destroy(); - }); - - beforeEach(async () => { - await SysContainer.start(); - store = await toStoreRuntime().makeDataStore({ name: "test" } as Loadable); - raw = await testStoreFactory(store.url); - }); - - it("should have a name", function () { - equals(store.name, "test"); - }); - - it("should save a car", async function () { - const car: AnyBlock = { - cid: "cidKey" as unknown as CID, - bytes: new Uint8Array([55, 56, 57]), - }; - await store.save(car); - // const path = SysContainer.join(store.url.pathname, store.name, "data", car.cid + ".car"); - // const data = await SysContainer.readfile(path); - const data = await raw.get(car.cid.toString()); - equals(decoder.decode(data), decoder.decode(car.bytes)); - }); -}); - -describe("DataStore with a saved car", function () { - let store: DataStore; - let raw: TestStore; - let car: AnyBlock; - - afterEach(async () => { - await store.close(); - await store.destroy(); - }); - beforeEach(async function () { - await SysContainer.start(); - store = await toStoreRuntime().makeDataStore({ name: "test2" } as Loadable); - raw = await testStoreFactory(store.url); - car = { - cid: "cid" as unknown as CID, - bytes: new Uint8Array([55, 56, 57, 80]), - }; - await store.save(car); - }); - - it("should have a car", async function () { - // const path = SysContainer.join(store.url.pathname, store.name, "data", car.cid + ".car"); - // const data = await SysContainer.readfile(path); - const data = await raw.get(car.cid.toString()); - equals(decoder.decode(data), decoder.decode(car.bytes)); - }); - - it("should load a car", async function () { - const loaded = await store.load(car.cid); - equals(loaded.cid, car.cid); - equals(loaded.bytes.constructor.name, "Uint8Array"); - equals(loaded.bytes.toString(), car.bytes.toString()); - }); - - it("should remove a car", async function () { - await store.remove(car.cid); - const error = (await store.load(car.cid).catch((e: Error) => e)) as Error; - matches(error.message, "ENOENT"); - }); -}); - -describe("MetaStore", function () { - let store: MetaStore; - let raw: TestStore; - - afterEach(async () => { - await store.close(); - await store.destroy(); - }); - beforeEach(async function () { - await SysContainer.start(); - store = await toStoreRuntime().makeMetaStore({ name: "test" } as unknown as Loader); - raw = await testStoreFactory(store.url); - }); - - it("should have a name", function () { - equals(store.name, "test"); - }); - - it("should save a header", async function () { - const cid = CID.parse("bafybeia4luuns6dgymy5kau5rm7r4qzrrzg6cglpzpogussprpy42cmcn4"); - const h: DbMeta = { - cars: [cid], - key: undefined, - }; - await store.save(h); - const file = await raw.get("main"); - // SysContainer.rawDB.get(store.url, "meta", "main", 'json'); - const header = JSON.parse(decoder.decode(file)); - assert(header); - assert(header.cars); - equals(header.cars[0]["/"], cid.toString()); - }); -}); - -describe("MetaStore with a saved header", function () { - let store: MetaStore; - let raw: TestStore; - let cid: CID; - - afterEach(async () => { - await store.close(); - await store.destroy(); - }); - - beforeEach(async function () { - await SysContainer.start(); - store = await toStoreRuntime().makeMetaStore({ name: "test-saved-header" } as unknown as Loader); - raw = await testStoreFactory(store.url); - cid = CID.parse("bafybeia4luuns6dgymy5kau5rm7r4qzrrzg6cglpzpogussprpy42cmcn4"); - await store.save({ cars: [cid], key: undefined }); - }); - - it("should have a header", async function () { - // const path = SysContainer.join(dataDir(), store.name, "meta", "main.json"); - // const data = await SysContainer.readfile(path); - const data = decoder.decode(await raw.get("main")); - matches(data, /car/); - const header = JSON.parse(data); - assert(header); - assert(header.cars); - equals(header.cars[0]["/"], cid.toString()); - }); - - it("should load a header", async function () { - const loadeds = (await store.load()) as DbMeta[]; - const loaded = loadeds[0]; - assert(loaded); - assert(loaded.cars); - equals(loaded.cars.toString(), cid.toString()); - }); -}); diff --git a/tsconfig.json b/tsconfig.json index ca0c2086d..93aff99fb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -31,8 +31,8 @@ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ "paths": { "@fireproof/core": ["./src/index.js"], - "@fireproof/core/runtime": ["./src/runtime/index.js"], - "@fireproof/core/blockstore": ["./src/blockstore/index.js"], + // "@fireproof/core/runtime": ["./src/runtime/index.js"], + // "@fireproof/core/blockstore": ["./src/blockstore/index.js"], "use-fireproof": ["./src/react/index.ts"] } /* Specify a set of entries that re-map imports to additional lookup locations. */, // "rootDirs": ["./src", "./src-generated"], /* Allow multiple folders to be treated as one when resolving modules. */ diff --git a/tsup.config.ts b/tsup.config.ts index 063997696..163caa819 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -37,28 +37,6 @@ const LIBRARY_BUNDLES: readonly Options[] = [ footer: "declare module '@fireproof/core'", }, }, - { - ...LIBRARY_BUNDLE_OPTIONS, - name: "core/blockstore", - entry: ["src/blockstore/index.ts"], - platform: "browser", - outDir: "dist/fireproof-core/blockstore", - esbuildPlugins: [ - resolve({ - "../runtime/store-sql/store-sql.js": "../runtime/store-sql/not-impl.js", - "../runtime/store-file.js": "../runtime/store-file-not-impl.js", - // "./node-sys-container.js": "../runtime/store-file-not-impl.js", - // "node:fs": path.join(__dirname, './src/runtime/memory-sys-container.js'), - // "node:path": path.join(__dirname, './src/runtime/memory-sys-container.js'), - // "node:os": path.join(__dirname, './src/runtime/memory-sys-container.js'), - // "node:url": path.join(__dirname, './src/runtime/memory-sys-container.js'), - // "assert": path.join(__dirname, './src/runtime/memory-sys-container.js'), - }), - ], - dts: { - footer: "declare module '@fireproof/core/blockstore'", - }, - }, { ...LIBRARY_BUNDLE_OPTIONS, name: "use-fireproof",