logos-delivery-js/packages/tests/tests/light_push.node.spec.ts

110 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-11-03 22:40:42 +11:00
import { bytesToUtf8, utf8ToBytes } from "@waku/byte-utils";
import { createEncoder, waitForRemotePeer } from "@waku/core";
import { createLightNode } from "@waku/create";
import type { WakuLight } from "@waku/interfaces";
2022-11-01 19:33:33 +11:00
import { Protocols } from "@waku/interfaces";
2022-02-04 14:12:00 +11:00
import { expect } from "chai";
import debug from "debug";
2021-05-19 11:00:43 +10:00
import {
delay,
makeLogFileName,
MessageRpcResponse,
NOISE_KEY_1,
Nwaku,
} from "../src/index.js";
2021-05-19 11:00:43 +10:00
const log = debug("waku:test:lightpush");
2022-02-04 14:12:00 +11:00
const TestContentTopic = "/test/1/waku-light-push/utf8";
const TestEncoder = createEncoder(TestContentTopic);
2022-02-04 14:12:00 +11:00
describe("Waku Light Push [node only]", () => {
let waku: WakuLight;
2022-04-01 12:19:51 +11:00
let nwaku: Nwaku;
2021-05-19 11:00:43 +10:00
afterEach(async function () {
2022-04-01 12:19:51 +11:00
!!nwaku && nwaku.stop();
2022-02-04 14:12:00 +11:00
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
2021-05-19 11:00:43 +10:00
});
2022-02-04 14:12:00 +11:00
it("Push successfully", async function () {
this.timeout(15_000);
2021-05-19 11:00:43 +10:00
2022-04-01 12:19:51 +11:00
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({ lightpush: true, relay: true });
2021-05-19 11:00:43 +10:00
waku = await createLightNode({
2021-05-19 11:00:43 +10:00
staticNoiseKey: NOISE_KEY_1,
});
await waku.start();
2022-04-01 12:19:51 +11:00
await waku.dial(await nwaku.getMultiaddrWithId());
await waitForRemotePeer(waku, [Protocols.LightPush]);
2021-05-19 11:00:43 +10:00
2022-02-04 14:12:00 +11:00
const messageText = "Light Push works!";
2021-05-19 11:00:43 +10:00
const pushResponse = await waku.lightPush.push(TestEncoder, {
payload: utf8ToBytes(messageText),
});
expect(pushResponse.recipients.length).to.eq(1);
2021-05-19 11:00:43 +10:00
let msgs: MessageRpcResponse[] = [];
2021-05-19 11:00:43 +10:00
while (msgs.length === 0) {
await delay(200);
2022-04-01 12:19:51 +11:00
msgs = await nwaku.messages();
2021-05-19 11:00:43 +10:00
}
expect(msgs[0].contentTopic).to.equal(TestContentTopic);
expect(bytesToUtf8(new Uint8Array(msgs[0].payload))).to.equal(messageText);
2021-05-19 11:00:43 +10:00
});
2022-02-04 14:12:00 +11:00
it("Push on custom pubsub topic", async function () {
this.timeout(15_000);
2022-02-04 14:12:00 +11:00
const customPubSubTopic = "/waku/2/custom-dapp/proto";
2022-04-01 12:19:51 +11:00
nwaku = new Nwaku(makeLogFileName(this));
await nwaku.start({
lightpush: true,
topics: customPubSubTopic,
relay: true,
});
waku = await createLightNode({
pubSubTopic: customPubSubTopic,
staticNoiseKey: NOISE_KEY_1,
});
await waku.start();
2022-04-01 12:19:51 +11:00
await waku.dial(await nwaku.getMultiaddrWithId());
await waitForRemotePeer(waku, [Protocols.LightPush]);
2022-04-01 12:19:51 +11:00
const nimPeerId = await nwaku.getPeerId();
2022-02-04 14:12:00 +11:00
const messageText = "Light Push works!";
log("Send message via lightpush");
const pushResponse = await waku.lightPush.push(
TestEncoder,
{ payload: utf8ToBytes(messageText) },
{
peerId: nimPeerId,
pubSubTopic: customPubSubTopic,
}
);
log("Ack received", pushResponse);
expect(pushResponse.recipients[0].toString()).to.eq(nimPeerId.toString());
let msgs: MessageRpcResponse[] = [];
log("Waiting for message to show in nwaku");
while (msgs.length === 0) {
await delay(200);
msgs = await nwaku.messages(customPubSubTopic);
}
expect(msgs[0].contentTopic).to.equal(TestContentTopic);
expect(bytesToUtf8(new Uint8Array(msgs[0].payload))!).to.equal(messageText);
});
2021-05-19 11:00:43 +10:00
});