updates v2 tests

This commit is contained in:
danisharora099 2023-04-26 13:41:10 +05:30
parent 833b910915
commit a07437d81e
No known key found for this signature in database
GPG Key ID: FBD2BF500037F135

View File

@ -21,7 +21,7 @@ const TestEncoder = createEncoder({ contentTopic: TestContentTopic });
const TestDecoder = createDecoder(TestContentTopic);
describe("Waku Filter: V2", () => {
let waku: LightNode;
let waku: LightNode<true>;
let nwaku: Nwaku;
afterEach(async function () {
@ -67,9 +67,31 @@ describe("Waku Filter: V2", () => {
};
await waku.filter.subscribe([TestDecoder], callback);
// As the filter protocol does not cater for an ack of subscription
// we cannot know whether the subscription happened. Something we want to
// correct in future versions of the protocol.
await waku.lightPush.send(TestEncoder, message);
while (messageCount === 0) {
await delay(250);
}
expect(messageCount).to.eq(1);
});
it("modifies subscription", async function () {
this.timeout(10000);
let messageCount = 0;
const messageText = "Filtering works!";
const message = { payload: utf8ToBytes(messageText) };
const callback = (msg: DecodedMessage): void => {
log("Got a message");
messageCount++;
expect(msg.contentTopic).to.eq(TestContentTopic);
expect(msg.pubSubTopic).to.eq(DefaultPubSubTopic);
expect(bytesToUtf8(msg.payload)).to.eq(messageText);
};
await waku.filter.subscribe([TestDecoder], callback);
await delay(200);
await waku.lightPush.send(TestEncoder, message);
@ -77,6 +99,30 @@ describe("Waku Filter: V2", () => {
await delay(250);
}
expect(messageCount).to.eq(1);
// Modify subscription
messageCount = 0;
const newMessageText = "Filtering still works!";
const newMessage = { payload: utf8ToBytes(newMessageText) };
const newContentTopic = "/test/2/waku-filter";
const newEncoder = createEncoder({ contentTopic: newContentTopic });
const newDecoder = createDecoder(newContentTopic);
const newCallback = (msg: DecodedMessage): void => {
log("Got a message");
messageCount++;
expect(msg.contentTopic).to.eq(newContentTopic);
expect(msg.pubSubTopic).to.eq(DefaultPubSubTopic);
expect(bytesToUtf8(msg.payload)).to.eq(newMessageText);
};
await waku.filter.subscribe([newDecoder], newCallback);
await waku.lightPush.send(newEncoder, newMessage);
while (messageCount === 0) {
await delay(250);
}
expect(messageCount).to.eq(1);
});
it("handles multiple messages", async function () {
@ -89,7 +135,6 @@ describe("Waku Filter: V2", () => {
};
await waku.filter.subscribe(TestDecoder, callback);
await delay(200);
await waku.lightPush.send(TestEncoder, {
payload: utf8ToBytes("Filtering works!"),
});
@ -115,7 +160,46 @@ describe("Waku Filter: V2", () => {
});
await delay(100);
await unsubscribe();
await waku.lightPush.send(TestEncoder, {
payload: utf8ToBytes("This should not be received"),
});
await delay(100);
expect(messageCount).to.eq(1);
});
it("tests ping", async function () {
let messageCount = 0;
const callback = (): void => {
messageCount++;
};
await waku.filter.subscribe([TestDecoder], callback);
await delay(200);
await waku.lightPush.send(TestEncoder, {
payload: utf8ToBytes("This should be received"),
});
await delay(100);
await waku.filter.ping();
await waku.lightPush.send(TestEncoder, {
payload: utf8ToBytes("This should be received"),
});
await delay(100);
expect(messageCount).to.eq(2);
});
it("unsubscribes all", async function () {
let messageCount = 0;
const callback = (): void => {
messageCount++;
};
await waku.filter.subscribe([TestDecoder], callback);
await delay(200);
await waku.lightPush.send(TestEncoder, {
payload: utf8ToBytes("This should be received"),
});
await delay(100);
await waku.filter.unsubscribeAll();
await waku.lightPush.send(TestEncoder, {
payload: utf8ToBytes("This should not be received"),
});