|
| 1 | +import { |
| 2 | + createDecoder, |
| 3 | + createEncoder, |
| 4 | + DefaultPubSubTopic, |
| 5 | + waitForRemotePeer, |
| 6 | +} from "@waku/core"; |
| 7 | +import { createLightNode } from "@waku/create"; |
| 8 | +import type { LightNode } from "@waku/interfaces"; |
| 9 | +import { Protocols } from "@waku/interfaces"; |
| 10 | +import { toAsyncIterator } from "@waku/utils"; |
| 11 | +import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes"; |
| 12 | +import { expect } from "chai"; |
| 13 | + |
| 14 | +import { makeLogFileName, NOISE_KEY_1, Nwaku } from "../src/index.js"; |
| 15 | + |
| 16 | +const TestContentTopic = "/test/1/waku-filter"; |
| 17 | +const TestEncoder = createEncoder({ contentTopic: TestContentTopic }); |
| 18 | +const TestDecoder = createDecoder(TestContentTopic); |
| 19 | + |
| 20 | +describe("Util: toAsyncIterator", () => { |
| 21 | + let waku: LightNode; |
| 22 | + let nwaku: Nwaku; |
| 23 | + |
| 24 | + beforeEach(async function () { |
| 25 | + this.timeout(15000); |
| 26 | + nwaku = new Nwaku(makeLogFileName(this)); |
| 27 | + await nwaku.start({ filter: true, lightpush: true, relay: true }); |
| 28 | + waku = await createLightNode({ |
| 29 | + staticNoiseKey: NOISE_KEY_1, |
| 30 | + libp2p: { addresses: { listen: ["/ip4/0.0.0.0/tcp/0/ws"] } }, |
| 31 | + }); |
| 32 | + await waku.start(); |
| 33 | + await waku.dial(await nwaku.getMultiaddrWithId()); |
| 34 | + await waitForRemotePeer(waku, [Protocols.Filter, Protocols.LightPush]); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(async () => { |
| 38 | + try { |
| 39 | + await nwaku.stop(); |
| 40 | + await waku.stop(); |
| 41 | + } catch (err) { |
| 42 | + console.log("Failed to stop", err); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + it("creates an iterator", async function () { |
| 47 | + const messageText = "hey, what's up?"; |
| 48 | + const sent = { payload: utf8ToBytes(messageText) }; |
| 49 | + |
| 50 | + const { iterator } = await toAsyncIterator(waku.filter, TestDecoder); |
| 51 | + |
| 52 | + await waku.lightPush.send(TestEncoder, sent); |
| 53 | + const { value } = await iterator.next(); |
| 54 | + |
| 55 | + expect(value.contentTopic).to.eq(TestContentTopic); |
| 56 | + expect(value.pubSubTopic).to.eq(DefaultPubSubTopic); |
| 57 | + expect(bytesToUtf8(value.payload)).to.eq(messageText); |
| 58 | + }); |
| 59 | + |
| 60 | + it("handles multiple messages", async function () { |
| 61 | + const { iterator } = await toAsyncIterator(waku.filter, TestDecoder); |
| 62 | + |
| 63 | + await waku.lightPush.send(TestEncoder, { |
| 64 | + payload: utf8ToBytes("Filtering works!"), |
| 65 | + }); |
| 66 | + await waku.lightPush.send(TestEncoder, { |
| 67 | + payload: utf8ToBytes("Filtering still works!"), |
| 68 | + }); |
| 69 | + |
| 70 | + let result = await iterator.next(); |
| 71 | + expect(bytesToUtf8(result.value.payload)).to.eq("Filtering works!"); |
| 72 | + |
| 73 | + result = await iterator.next(); |
| 74 | + expect(bytesToUtf8(result.value.payload)).to.eq("Filtering still works!"); |
| 75 | + }); |
| 76 | + |
| 77 | + it("unsubscribes", async function () { |
| 78 | + const { iterator, stop } = await toAsyncIterator(waku.filter, TestDecoder); |
| 79 | + |
| 80 | + await waku.lightPush.send(TestEncoder, { |
| 81 | + payload: utf8ToBytes("This should be received"), |
| 82 | + }); |
| 83 | + |
| 84 | + await stop(); |
| 85 | + |
| 86 | + await waku.lightPush.send(TestEncoder, { |
| 87 | + payload: utf8ToBytes("This should not be received"), |
| 88 | + }); |
| 89 | + |
| 90 | + let result = await iterator.next(); |
| 91 | + expect(result.done).to.eq(true); |
| 92 | + expect(bytesToUtf8(result.value.payload)).to.eq("This should be received"); |
| 93 | + |
| 94 | + result = await iterator.next(); |
| 95 | + expect(result.value).to.eq(undefined); |
| 96 | + expect(result.done).to.eq(true); |
| 97 | + }); |
| 98 | +}); |
0 commit comments