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";
|
2022-08-01 15:43:43 +10:00
|
|
|
import { createWaku } from "../create_waku";
|
2022-07-25 12:33:08 +10:00
|
|
|
import { waitForRemotePeer } from "../wait_for_remote_peer";
|
2022-08-01 15:43:43 +10:00
|
|
|
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
|
|
|
|
2022-09-05 21:30:29 +10:00
|
|
|
const log = debug("waku:test:lightpush");
|
2022-01-31 15:30:49 +11:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const TestContentTopic = "/test/1/waku-light-push/utf8";
|
2021-07-28 11:19:24 +10:00
|
|
|
|
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 () {
|
2022-04-01 12:59:59 +11:00
|
|
|
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
|
|
|
|
2022-06-23 14:03:52 +10:00
|
|
|
waku = await createWaku({
|
2021-05-19 11:00:43 +10:00
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
|
});
|
2022-06-23 14:03:52 +10:00
|
|
|
await waku.start();
|
2022-04-01 12:19:51 +11:00
|
|
|
await waku.dial(await nwaku.getMultiaddrWithId());
|
2022-07-25 12:33:08 +10:00
|
|
|
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-07-28 11:19:24 +10:00
|
|
|
const message = await WakuMessage.fromUtf8String(
|
|
|
|
|
messageText,
|
|
|
|
|
TestContentTopic
|
|
|
|
|
);
|
2021-05-19 11:00:43 +10:00
|
|
|
|
2021-06-16 23:37:13 +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);
|
|
|
|
|
});
|
2021-06-09 12:25:56 +10:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
it("Push on custom pubsub topic", async function () {
|
2022-04-01 12:59:59 +11:00
|
|
|
this.timeout(15_000);
|
2021-06-09 12:25:56 +10:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const customPubSubTopic = "/waku/2/custom-dapp/proto";
|
2021-06-09 12:25:56 +10:00
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
nwaku = new Nwaku(makeLogFileName(this));
|
|
|
|
|
await nwaku.start({ lightpush: true, topics: customPubSubTopic });
|
2021-06-09 12:25:56 +10:00
|
|
|
|
2022-06-23 14:03:52 +10:00
|
|
|
waku = await createWaku({
|
2021-08-20 10:12:19 +10:00
|
|
|
pubSubTopic: customPubSubTopic,
|
2021-06-09 12:25:56 +10:00
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
|
});
|
2022-06-23 14:03:52 +10:00
|
|
|
await waku.start();
|
2022-04-01 12:19:51 +11:00
|
|
|
await waku.dial(await nwaku.getMultiaddrWithId());
|
2022-07-25 12:33:08 +10:00
|
|
|
await waitForRemotePeer(waku, [Protocols.LightPush]);
|
2021-06-09 12:25:56 +10:00
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
const nimPeerId = await nwaku.getPeerId();
|
2021-06-09 12:25:56 +10:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const messageText = "Light Push works!";
|
2021-07-28 11:19:24 +10:00
|
|
|
const message = await WakuMessage.fromUtf8String(
|
|
|
|
|
messageText,
|
|
|
|
|
TestContentTopic
|
|
|
|
|
);
|
2021-06-09 12:25:56 +10:00
|
|
|
|
2022-09-05 21:30:29 +10:00
|
|
|
log("Send message via lightpush");
|
2021-06-16 23:37:13 +10:00
|
|
|
const pushResponse = await waku.lightPush.push(message, {
|
|
|
|
|
peerId: nimPeerId,
|
2022-06-15 11:12:06 +10:00
|
|
|
pubSubTopic: customPubSubTopic,
|
2021-06-16 23:37:13 +10:00
|
|
|
});
|
2022-09-05 21:30:29 +10:00
|
|
|
log("Ack received", pushResponse);
|
2021-06-09 12:25:56 +10:00
|
|
|
expect(pushResponse?.isSuccess).to.be.true;
|
|
|
|
|
|
|
|
|
|
let msgs: WakuMessage[] = [];
|
|
|
|
|
|
2022-09-05 21:30:29 +10:00
|
|
|
log("Waiting for message to show in nwaku");
|
2021-06-09 12:25:56 +10:00
|
|
|
while (msgs.length === 0) {
|
|
|
|
|
await delay(200);
|
2022-07-25 17:11:36 +10:00
|
|
|
msgs = await nwaku.messages(customPubSubTopic);
|
2021-06-09 12:25:56 +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);
|
|
|
|
|
});
|
2021-05-19 11:00:43 +10:00
|
|
|
});
|