103 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-02-04 14:12:00 +11:00
import { expect } from "chai";
import debug from "debug";
2021-05-19 11:00:43 +10:00
2022-04-01 12:19:51 +11:00
import { makeLogFileName, NOISE_KEY_1, Nwaku } from "../../test_utils";
2022-02-11 17:27:15 +11:00
import { delay } from "../../test_utils/delay";
import { createWaku } from "../create_waku";
import { waitForRemotePeer } from "../wait_for_remote_peer";
import { Protocols, Waku } from "../waku";
2022-02-04 14:12:00 +11:00
import { WakuMessage } from "../waku_message";
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";
2022-02-04 14:12:00 +11:00
describe("Waku Light Push [node only]", () => {
2021-05-19 11:00:43 +10:00
let waku: Waku;
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 });
2021-05-19 11:00:43 +10:00
waku = await createWaku({
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!";
const message = await WakuMessage.fromUtf8String(
messageText,
TestContentTopic
);
2021-05-19 11:00:43 +10:00
const pushResponse = await waku.lightPush.push(message);
2021-05-19 11:00:43 +10:00
expect(pushResponse?.isSuccess).to.be.true;
let msgs: WakuMessage[] = [];
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(message.contentTopic);
expect(msgs[0].version).to.equal(message.version);
expect(msgs[0].payloadAsUtf8).to.equal(messageText);
});
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 });
waku = await createWaku({
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!";
const message = await WakuMessage.fromUtf8String(
messageText,
TestContentTopic
);
log("Send message via lightpush");
const pushResponse = await waku.lightPush.push(message, {
peerId: nimPeerId,
pubSubTopic: customPubSubTopic,
});
log("Ack received", pushResponse);
expect(pushResponse?.isSuccess).to.be.true;
let msgs: WakuMessage[] = [];
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(message.contentTopic);
expect(msgs[0].version).to.equal(message.version);
expect(msgs[0].payloadAsUtf8).to.equal(messageText);
});
2021-05-19 11:00:43 +10:00
});