From 5742164567b507dd5433ebaf55949c8aa7f73d71 Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Thu, 19 Sep 2024 16:34:57 -0700 Subject: [PATCH] subscribe test --- tests/fireproof/all-gateway.test.ts | 48 +++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/fireproof/all-gateway.test.ts b/tests/fireproof/all-gateway.test.ts index e33fdb61..24d16a73 100644 --- a/tests/fireproof/all-gateway.test.ts +++ b/tests/fireproof/all-gateway.test.ts @@ -297,6 +297,54 @@ describe("noop Gateway", function () { }); }); +describe("noop Gateway subscribe", function () { + let db: Database; + + let metaStore: ExtendedStore; + + let metaGateway: ExtendedGateway; + + afterEach(async function () { + await db.close(); + await db.destroy(); + }); + beforeEach(async function () { + db = new Database("test-gateway-" + Math.random().toString(36).substring(7)); + + // Extract stores from the loader + metaStore = (await db.blockstore.loader?.metaStore()) as unknown as ExtendedStore; + + metaGateway = metaStore?.gateway; + }); + it("should subscribe to meta Gateway", async function () { + const metaUrl = await metaGateway?.buildUrl(metaStore?._url, "main"); + await metaGateway?.start(metaStore?._url); + + if (metaGateway.subscribe) { + let resolve: () => void; + let didCall = false; + const p = new Promise((r) => { + resolve = r; + }); + + const metaSubscribeResult = await metaGateway?.subscribe?.(metaUrl?.Ok(), async (data: Uint8Array) => { + const decodedData = new TextDecoder().decode(data); + expect(decodedData).toContain("parents"); + didCall = true + resolve(); + }); + expect(metaSubscribeResult?.Ok()).toBeTruthy(); + const ok = await db.put({ _id: "key1", hello: "world1" }); + expect(ok).toBeTruthy(); + expect(ok.id).toBe("key1"); + await p + expect(didCall).toBeTruthy(); + } + }); + + }); +}); + describe("Gateway", function () { let db: Database; // let carStore: ExtendedStore;