Skip to content

Commit 1ec0c20

Browse files
authored
chore: new relay tests (#1649)
* make relay folder * make relay folder * adjust message collector for relay * small fix * small fix * small fix * split tests more * small fixes * small fix * new test * fix pubsubtopic name * new subscribe tests * new subscribe tests * new tests * small fix after ci run * small fix after ci run2 * fix skipped test * added issue for skipped test
1 parent 80a33b9 commit 1ec0c20

16 files changed

+1222
-829
lines changed

packages/tests/src/constants.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export const TEST_STRING = [
5050
{ description: "JSON", value: '{"user":"admin","password":"123456"}' },
5151
{ description: "shell command", value: "`rm -rf /`" },
5252
{ description: "escaped characters", value: "\\n\\t\\0" },
53-
{ description: "unicode special characters", value: "\u202Ereverse" }
53+
{ description: "unicode special characters", value: "\u202Ereverse" },
54+
{ description: "emoji", value: "🤫 🤥 😶 😶‍🌫️ 😐 😑 😬 🫨 🫠 🙄 😯 😦 😧 😮" }
5455
];
5556

5657
export const TEST_TIMESTAMPS = [

packages/tests/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export * from "./log_file.js";
1212
export * from "./node/node.js";
1313
export * from "./teardown.js";
1414
export * from "./message_collector.js";
15+
export * from "./utils.js";

packages/tests/src/message_collector.ts

+33-44
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class MessageCollector {
113113
expectedVersion?: number;
114114
expectedMeta?: Uint8Array;
115115
expectedEphemeral?: boolean;
116-
expectedTimestamp?: bigint;
116+
expectedTimestamp?: bigint | number;
117117
checkTimestamp?: boolean; // Used to determine if we need to check the timestamp
118118
}
119119
): void {
@@ -148,6 +148,38 @@ export class MessageCollector {
148148
);
149149
}
150150

151+
const shouldCheckTimestamp =
152+
options.checkTimestamp !== undefined ? options.checkTimestamp : true;
153+
if (shouldCheckTimestamp && message.timestamp) {
154+
// In we send timestamp in the request we assert that it matches the timestamp in the response +- 1 sec
155+
// We take the 1s deviation because there are some ms diffs in timestamps, probably because of conversions
156+
let timestampAsNumber: number;
157+
158+
if (message.timestamp instanceof Date) {
159+
timestampAsNumber = message.timestamp.getTime();
160+
} else {
161+
timestampAsNumber = Number(message.timestamp) / 1_000_000;
162+
}
163+
164+
let lowerBound: number;
165+
let upperBound: number;
166+
167+
// Define the bounds based on the expectedTimestamp
168+
if (options.expectedTimestamp !== undefined) {
169+
lowerBound = Number(options.expectedTimestamp) - 1000;
170+
upperBound = Number(options.expectedTimestamp) + 1000;
171+
} else {
172+
upperBound = Date.now();
173+
lowerBound = upperBound - 10000;
174+
}
175+
176+
if (timestampAsNumber < lowerBound || timestampAsNumber > upperBound) {
177+
throw new AssertionError(
178+
`Message timestamp not within the expected range. Expected between: ${lowerBound} and ${upperBound}. Got: ${timestampAsNumber}`
179+
);
180+
}
181+
}
182+
151183
if (this.isMessageRpcResponse(message)) {
152184
// nwaku message specific assertions
153185
const receivedMessageText = message.payload
@@ -158,37 +190,6 @@ export class MessageCollector {
158190
options.expectedMessageText,
159191
`Message text mismatch. Expected: ${options.expectedMessageText}. Got: ${receivedMessageText}`
160192
);
161-
162-
if (message.timestamp) {
163-
// In we send timestamp in the request we assert that it matches the timestamp in the response +- 1 sec
164-
// We take the 1s deviation because there are some ms diffs in timestamps, probably because of conversions
165-
if (options.expectedTimestamp !== undefined) {
166-
const lowerBound =
167-
BigInt(options.expectedTimestamp) - BigInt(1000000000);
168-
const upperBound =
169-
BigInt(options.expectedTimestamp) + BigInt(1000000000);
170-
171-
if (
172-
message.timestamp < lowerBound ||
173-
message.timestamp > upperBound
174-
) {
175-
throw new AssertionError(
176-
`Message timestamp not within the expected range. Expected between: ${lowerBound} and ${upperBound}. Got: ${message.timestamp}`
177-
);
178-
}
179-
}
180-
// In we don't send timestamp in the request we assert that the timestamp in the response is between now and (now-10s)
181-
else {
182-
const now = BigInt(Date.now()) * BigInt(1_000_000);
183-
const tenSecondsAgo = now - BigInt(10_000_000_000);
184-
185-
if (message.timestamp < tenSecondsAgo || message.timestamp > now) {
186-
throw new AssertionError(
187-
`Message timestamp not within the expected range. Expected between: ${tenSecondsAgo} and ${now}. Got: ${message.timestamp}`
188-
);
189-
}
190-
}
191-
}
192193
} else {
193194
// js-waku message specific assertions
194195
expect(message.pubsubTopic).to.eq(
@@ -205,18 +206,6 @@ export class MessageCollector {
205206
}. Got: ${bytesToUtf8(message.payload)}`
206207
);
207208

208-
const shouldCheckTimestamp =
209-
options.checkTimestamp !== undefined ? options.checkTimestamp : true;
210-
if (shouldCheckTimestamp && message.timestamp) {
211-
const now = Date.now();
212-
const tenSecondsAgo = now - 10000;
213-
expect(message.timestamp.getTime()).to.be.within(
214-
tenSecondsAgo,
215-
now,
216-
`Message timestamp not within the expected range. Expected between: ${tenSecondsAgo} and ${now}. Got: ${message.timestamp.getTime()}`
217-
);
218-
}
219-
220209
expect([
221210
options.expectedMeta,
222211
undefined,

packages/tests/src/utils.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { createDecoder, createEncoder, Decoder, Encoder } from "@waku/core";
2+
3+
// Utility to generate test data for multiple topics tests.
4+
export function generateTestData(topicCount: number): {
5+
contentTopics: string[];
6+
encoders: Encoder[];
7+
decoders: Decoder[];
8+
} {
9+
const contentTopics = Array.from(
10+
{ length: topicCount },
11+
(_, i) => `/test/${i + 1}/waku-multi`
12+
);
13+
const encoders = contentTopics.map((topic) =>
14+
createEncoder({ contentTopic: topic })
15+
);
16+
const decoders = contentTopics.map((topic) => createDecoder(topic));
17+
return {
18+
contentTopics,
19+
encoders,
20+
decoders
21+
};
22+
}

packages/tests/tests/filter/push.node.spec.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe("Waku Filter V2: FilterPush", function () {
8888
});
8989
});
9090

91-
it("Check received message with invalid timestamp is not received", async function () {
91+
it("Check message with invalid timestamp is not received", async function () {
9292
await subscription.subscribe([TestDecoder], messageCollector.callback);
9393
await delay(400);
9494

@@ -105,7 +105,7 @@ describe("Waku Filter V2: FilterPush", function () {
105105
expect(await messageCollector.waitForMessages(1)).to.eq(false);
106106
});
107107

108-
it("Check received message on other pubsub topic is not received", async function () {
108+
it("Check message on other pubsub topic is not received", async function () {
109109
await subscription.subscribe([TestDecoder], messageCollector.callback);
110110
await delay(400);
111111

@@ -121,7 +121,7 @@ describe("Waku Filter V2: FilterPush", function () {
121121
expect(await messageCollector.waitForMessages(1)).to.eq(false);
122122
});
123123

124-
it("Check received message with no pubsub topic is not received", async function () {
124+
it("Check message with no pubsub topic is not received", async function () {
125125
await subscription.subscribe([TestDecoder], messageCollector.callback);
126126
await delay(400);
127127

@@ -136,7 +136,7 @@ describe("Waku Filter V2: FilterPush", function () {
136136
expect(await messageCollector.waitForMessages(1)).to.eq(false);
137137
});
138138

139-
it("Check received message with no content topic is not received", async function () {
139+
it("Check message with no content topic is not received", async function () {
140140
await subscription.subscribe([TestDecoder], messageCollector.callback);
141141
await delay(400);
142142

@@ -151,7 +151,7 @@ describe("Waku Filter V2: FilterPush", function () {
151151
expect(await messageCollector.waitForMessages(1)).to.eq(false);
152152
});
153153

154-
it("Check received message with no payload is not received", async function () {
154+
it("Check message with no payload is not received", async function () {
155155
await subscription.subscribe([TestDecoder], messageCollector.callback);
156156
await delay(400);
157157

@@ -171,7 +171,7 @@ describe("Waku Filter V2: FilterPush", function () {
171171
}
172172
});
173173

174-
it("Check received message with non string payload is not received", async function () {
174+
it("Check message with non string payload is not received", async function () {
175175
await subscription.subscribe([TestDecoder], messageCollector.callback);
176176
await delay(400);
177177

@@ -187,7 +187,7 @@ describe("Waku Filter V2: FilterPush", function () {
187187
expect(await messageCollector.waitForMessages(1)).to.eq(false);
188188
});
189189

190-
it("Check received message with extra parameter is not received", async function () {
190+
it("Check message with extra parameter is not received", async function () {
191191
await subscription.subscribe([TestDecoder], messageCollector.callback);
192192
await delay(400);
193193

@@ -226,7 +226,7 @@ describe("Waku Filter V2: FilterPush", function () {
226226
});
227227

228228
// Will be skipped until https://github.com/waku-org/js-waku/issues/1464 si done
229-
it.skip("Check received message received after jswaku node is restarted", async function () {
229+
it.skip("Check message received after jswaku node is restarted", async function () {
230230
// Subscribe and send message
231231
await subscription.subscribe([TestDecoder], messageCollector.callback);
232232
await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes("M1") });
@@ -259,7 +259,7 @@ describe("Waku Filter V2: FilterPush", function () {
259259
});
260260

261261
// Will be skipped until https://github.com/waku-org/js-waku/issues/1464 si done
262-
it.skip("Check received message received after nwaku node is restarted", async function () {
262+
it.skip("Check message received after nwaku node is restarted", async function () {
263263
await subscription.subscribe([TestDecoder], messageCollector.callback);
264264
await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes("M1") });
265265
expect(await messageCollector.waitForMessages(1)).to.eq(true);

packages/tests/tests/filter/subscribe.node.spec.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { expect } from "chai";
1212

1313
import {
1414
delay,
15+
generateTestData,
1516
makeLogFileName,
1617
MessageCollector,
1718
NimGoNode,
@@ -20,7 +21,6 @@ import {
2021
} from "../../src/index.js";
2122

2223
import {
23-
generateTestData,
2424
messagePayload,
2525
messageText,
2626
runNodes,
@@ -295,7 +295,9 @@ describe("Waku Filter V2: Subscribe", function () {
295295

296296
// Check if all messages were received.
297297
// Since there are overlapping topics, there should be 6 messages in total (2 from the first set + 4 from the second set).
298-
expect(await messageCollector.waitForMessages(6)).to.eq(true);
298+
expect(await messageCollector.waitForMessages(6, { exact: true })).to.eq(
299+
true
300+
);
299301
});
300302

301303
it("Refresh subscription", async function () {
@@ -307,7 +309,9 @@ describe("Waku Filter V2: Subscribe", function () {
307309
await waku.lightPush.send(TestEncoder, { payload: utf8ToBytes("M2") });
308310

309311
// Confirm both messages were received.
310-
expect(await messageCollector.waitForMessages(2)).to.eq(true);
312+
expect(await messageCollector.waitForMessages(2, { exact: true })).to.eq(
313+
true
314+
);
311315
messageCollector.verifyReceivedMessage(0, {
312316
expectedMessageText: "M1",
313317
expectedContentTopic: TestContentTopic

packages/tests/tests/filter/unsubscribe.node.spec.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ import type { IFilterSubscription, LightNode } from "@waku/interfaces";
33
import { utf8ToBytes } from "@waku/utils/bytes";
44
import { expect } from "chai";
55

6-
import { MessageCollector, NimGoNode, tearDownNodes } from "../../src/index.js";
7-
86
import {
97
generateTestData,
8+
MessageCollector,
9+
NimGoNode,
10+
tearDownNodes
11+
} from "../../src/index.js";
12+
13+
import {
1014
messagePayload,
1115
messageText,
1216
runNodes,

packages/tests/tests/filter/utils.ts

+1-28
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
createDecoder,
3-
createEncoder,
4-
Decoder,
5-
Encoder,
6-
waitForRemotePeer
7-
} from "@waku/core";
1+
import { createDecoder, createEncoder, waitForRemotePeer } from "@waku/core";
82
import { IFilterSubscription, LightNode, Protocols } from "@waku/interfaces";
93
import { createLightNode } from "@waku/sdk";
104
import { Logger } from "@waku/utils";
@@ -21,27 +15,6 @@ export const TestDecoder = createDecoder(TestContentTopic);
2115
export const messageText = "Filtering works!";
2216
export const messagePayload = { payload: utf8ToBytes(messageText) };
2317

24-
// Utility to generate test data for multiple topics tests.
25-
export function generateTestData(topicCount: number): {
26-
contentTopics: string[];
27-
encoders: Encoder[];
28-
decoders: Decoder[];
29-
} {
30-
const contentTopics = Array.from(
31-
{ length: topicCount },
32-
(_, i) => `/test/${i + 1}/waku-multi`
33-
);
34-
const encoders = contentTopics.map((topic) =>
35-
createEncoder({ contentTopic: topic })
36-
);
37-
const decoders = contentTopics.map((topic) => createDecoder(topic));
38-
return {
39-
contentTopics,
40-
encoders,
41-
decoders
42-
};
43-
}
44-
4518
// Utility to validate errors related to pings in the subscription.
4619
export async function validatePingError(
4720
subscription: IFilterSubscription

packages/tests/tests/light-push/index.node.spec.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ describe("Waku Light Push", function () {
188188
Date.now() + 3600000
189189
].forEach((testItem) => {
190190
it(`Push message with custom timestamp: ${testItem}`, async function () {
191-
const customTimeNanos = BigInt(testItem) * BigInt(1000000);
192191
const pushResponse = await waku.lightPush.send(TestEncoder, {
193192
payload: utf8ToBytes(messageText),
194193
timestamp: new Date(testItem)
@@ -198,7 +197,7 @@ describe("Waku Light Push", function () {
198197
expect(await messageCollector.waitForMessages(1)).to.eq(true);
199198
messageCollector.verifyReceivedMessage(0, {
200199
expectedMessageText: messageText,
201-
expectedTimestamp: customTimeNanos,
200+
expectedTimestamp: testItem,
202201
expectedContentTopic: TestContentTopic
203202
});
204203
});

0 commit comments

Comments
 (0)