Skip to content

Commit ce5181d

Browse files
committed
test: add helper utilities to reduce duplication
1 parent 26de2d1 commit ce5181d

5 files changed

Lines changed: 108 additions & 173 deletions

File tree

packages/tests/src/utils/nodes.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import {
22
CreateNodeOptions,
33
IWaku,
44
LightNode,
5-
Protocols
5+
Protocols,
6+
RelayNode
67
} from "@waku/interfaces";
8+
import { createRelayNode, RelayCreateOptions } from "@waku/relay";
79
import { createLightNode } from "@waku/sdk";
810
import {
911
contentTopicToPubsubTopic,
@@ -14,13 +16,57 @@ import {
1416
import { Context } from "mocha";
1517
import pRetry from "p-retry";
1618

17-
import { NOISE_KEY_1 } from "../constants.js";
19+
import {
20+
DefaultTestNetworkConfig,
21+
DefaultTestRoutingInfo,
22+
NOISE_KEY_1
23+
} from "../constants.js";
1824
import { ServiceNodesFleet } from "../lib/index.js";
1925
import { DEFAULT_DISCOVERIES_ENABLED } from "../lib/runNodes.js";
26+
import { ServiceNode } from "../lib/service_node.js";
2027
import { Args } from "../types.js";
2128

29+
import { makeLogFileName } from "./log_file.js";
2230
import { waitForConnections } from "./waitForConnections.js";
2331

32+
export async function startServiceNode(
33+
context: Context,
34+
options: Args = {}
35+
): Promise<ServiceNode> {
36+
const node = new ServiceNode(makeLogFileName(context));
37+
await node.start({ filter: true, store: true, lightpush: true, ...options });
38+
return node;
39+
}
40+
41+
export async function startLightNode(
42+
options: CreateNodeOptions = {}
43+
): Promise<LightNode> {
44+
const node = await createLightNode({
45+
staticNoiseKey: NOISE_KEY_1,
46+
networkConfig: DefaultTestNetworkConfig,
47+
...options
48+
});
49+
await node.start();
50+
return node;
51+
}
52+
53+
export async function startRelayNode(
54+
options: Partial<RelayCreateOptions> = {}
55+
): Promise<RelayNode> {
56+
const relayOptions: RelayCreateOptions = {
57+
staticNoiseKey: NOISE_KEY_1,
58+
networkConfig: DefaultTestNetworkConfig,
59+
routingInfos: [DefaultTestRoutingInfo],
60+
...options
61+
} as RelayCreateOptions;
62+
63+
const node = await createRelayNode(relayOptions);
64+
if (relayOptions.autoStart === false) {
65+
await node.start();
66+
}
67+
return node;
68+
}
69+
2470
/**
2571
* Runs both js-waku and nwaku nodes.
2672
*

packages/tests/tests/enr.node.spec.ts

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import { EnrDecoder } from "@waku/enr";
22
import type { RelayNode } from "@waku/interfaces";
33
import { Protocols } from "@waku/interfaces";
4-
import { createRelayNode } from "@waku/relay";
54
import { expect } from "chai";
65

76
import {
87
afterEachCustom,
98
DefaultTestClusterId,
109
DefaultTestContentTopic,
11-
DefaultTestNetworkConfig,
1210
DefaultTestNumShardsInCluster,
1311
DefaultTestRoutingInfo,
14-
makeLogFileName,
15-
NOISE_KEY_1,
1612
ServiceNode,
13+
startRelayNode,
14+
startServiceNode,
1715
tearDownNodes
1816
} from "../src/index.js";
1917

@@ -27,8 +25,7 @@ describe("ENR Interop: ServiceNode", function () {
2725

2826
it("Relay", async function () {
2927
this.timeout(20_000);
30-
nwaku = new ServiceNode(makeLogFileName(this));
31-
await nwaku.start({
28+
nwaku = await startServiceNode(this, {
3229
relay: true,
3330
store: false,
3431
filter: false,
@@ -39,12 +36,7 @@ describe("ENR Interop: ServiceNode", function () {
3936
});
4037
const multiAddrWithId = await nwaku.getMultiaddrWithId();
4138

42-
waku = await createRelayNode({
43-
staticNoiseKey: NOISE_KEY_1,
44-
networkConfig: DefaultTestNetworkConfig,
45-
routingInfos: [DefaultTestRoutingInfo]
46-
});
47-
await waku.start();
39+
waku = await startRelayNode();
4840
await waku.dial(multiAddrWithId);
4941
await waku.waitForPeers([Protocols.Relay]);
5042

@@ -64,8 +56,7 @@ describe("ENR Interop: ServiceNode", function () {
6456

6557
it("Relay + Store", async function () {
6658
this.timeout(20_000);
67-
nwaku = new ServiceNode(makeLogFileName(this));
68-
await nwaku.start({
59+
nwaku = await startServiceNode(this, {
6960
relay: true,
7061
store: true,
7162
filter: false,
@@ -76,12 +67,7 @@ describe("ENR Interop: ServiceNode", function () {
7667
});
7768
const multiAddrWithId = await nwaku.getMultiaddrWithId();
7869

79-
waku = await createRelayNode({
80-
staticNoiseKey: NOISE_KEY_1,
81-
networkConfig: DefaultTestNetworkConfig,
82-
routingInfos: [DefaultTestRoutingInfo]
83-
});
84-
await waku.start();
70+
waku = await startRelayNode();
8571
await waku.dial(multiAddrWithId);
8672
await waku.waitForPeers([Protocols.Relay]);
8773

@@ -101,8 +87,7 @@ describe("ENR Interop: ServiceNode", function () {
10187

10288
it("All", async function () {
10389
this.timeout(20_000);
104-
nwaku = new ServiceNode(makeLogFileName(this));
105-
await nwaku.start({
90+
nwaku = await startServiceNode(this, {
10691
relay: true,
10792
store: true,
10893
filter: true,
@@ -113,12 +98,7 @@ describe("ENR Interop: ServiceNode", function () {
11398
});
11499
const multiAddrWithId = await nwaku.getMultiaddrWithId();
115100

116-
waku = await createRelayNode({
117-
staticNoiseKey: NOISE_KEY_1,
118-
networkConfig: DefaultTestNetworkConfig,
119-
routingInfos: [DefaultTestRoutingInfo]
120-
});
121-
await waku.start();
101+
waku = await startRelayNode();
122102
await waku.dial(multiAddrWithId);
123103
await waku.waitForPeers([Protocols.Relay]);
124104

packages/tests/tests/metadata.spec.ts

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { MetadataCodec } from "@waku/core";
22
import type { LightNode } from "@waku/interfaces";
3-
import { createLightNode } from "@waku/sdk";
43
import { decodeRelayShard } from "@waku/utils";
54
import chai, { expect } from "chai";
65
import chaiAsPromised from "chai-as-promised";
76

87
import {
98
afterEachCustom,
10-
beforeEachCustom,
119
delay,
12-
makeLogFileName,
1310
ServiceNode,
11+
startLightNode,
12+
startServiceNode,
1413
tearDownNodes
1514
} from "../src/index.js";
1615

@@ -21,10 +20,6 @@ describe("Metadata Protocol", function () {
2120
let waku: LightNode;
2221
let nwaku1: ServiceNode;
2322

24-
beforeEachCustom(this, async () => {
25-
nwaku1 = new ServiceNode(makeLogFileName(this.ctx) + "1");
26-
});
27-
2823
afterEachCustom(this, async () => {
2924
await tearDownNodes([nwaku1], waku);
3025
});
@@ -35,7 +30,7 @@ describe("Metadata Protocol", function () {
3530
const shards = [1];
3631
const numShardsInCluster = 8;
3732

38-
await nwaku1.start({
33+
nwaku1 = await startServiceNode(this, {
3934
relay: true,
4035
discv5Discovery: true,
4136
peerExchange: true,
@@ -47,10 +42,9 @@ describe("Metadata Protocol", function () {
4742
const nwaku1Ma = await nwaku1.getMultiaddrWithId();
4843
const nwaku1PeerId = await nwaku1.getPeerId();
4944

50-
waku = await createLightNode({
45+
waku = await startLightNode({
5146
networkConfig: { clusterId, numShardsInCluster }
5247
});
53-
await waku.start();
5448
await waku.libp2p.dialProtocol(nwaku1Ma, MetadataCodec);
5549

5650
if (!waku.libp2p.services.metadata) {
@@ -80,7 +74,7 @@ describe("Metadata Protocol", function () {
8074
const shards = [1];
8175
const numShardsInCluster = 8;
8276

83-
await nwaku1.start({
77+
nwaku1 = await startServiceNode(this, {
8478
relay: true,
8579
discv5Discovery: true,
8680
peerExchange: true,
@@ -91,10 +85,9 @@ describe("Metadata Protocol", function () {
9185

9286
const nwaku1Ma = await nwaku1.getMultiaddrWithId();
9387

94-
waku = await createLightNode({
88+
waku = await startLightNode({
9589
networkConfig: { clusterId: custerIdJsWaku, numShardsInCluster }
9690
});
97-
await waku.start();
9891
await waku.libp2p.dialProtocol(nwaku1Ma, MetadataCodec);
9992

10093
// ensure the connection is closed from the other side
@@ -115,7 +108,7 @@ describe("Metadata Protocol", function () {
115108
const shards = [1];
116109
const numShardsInCluster = 8;
117110

118-
await nwaku1.start({
111+
nwaku1 = await startServiceNode(this, {
119112
relay: true,
120113
discv5Discovery: true,
121114
peerExchange: true,
@@ -127,10 +120,9 @@ describe("Metadata Protocol", function () {
127120
const nwaku1Ma = await nwaku1.getMultiaddrWithId();
128121
const nwaku1PeerId = await nwaku1.getPeerId();
129122

130-
waku = await createLightNode({
123+
waku = await startLightNode({
131124
networkConfig: { clusterId, numShardsInCluster }
132125
});
133-
await waku.start();
134126
await waku.libp2p.dialProtocol(nwaku1Ma, MetadataCodec);
135127

136128
// delay to ensure the connection is estabilished and shardInfo is updated
@@ -153,7 +145,7 @@ describe("Metadata Protocol", function () {
153145
const shards = [1];
154146
const numShardsInCluster = 0; //static sharding
155147

156-
await nwaku1.start({
148+
nwaku1 = await startServiceNode(this, {
157149
relay: true,
158150
discv5Discovery: true,
159151
peerExchange: true,
@@ -165,7 +157,7 @@ describe("Metadata Protocol", function () {
165157
const nwaku1Ma = await nwaku1.getMultiaddrWithId();
166158
const nwaku1PeerId = await nwaku1.getPeerId();
167159

168-
waku = await createLightNode({
160+
waku = await startLightNode({
169161
networkConfig: {
170162
clusterId,
171163
numShardsInCluster
@@ -174,7 +166,6 @@ describe("Metadata Protocol", function () {
174166
pingKeepAlive: 1
175167
}
176168
});
177-
await waku.start();
178169
await waku.libp2p.dialProtocol(nwaku1Ma, MetadataCodec);
179170

180171
// delay to ensure the connection is estabilished, shardInfo is updated, and there is a ping
@@ -193,7 +184,7 @@ describe("Metadata Protocol", function () {
193184
const contentTopic = "/foo/1/bar/proto";
194185
const numShardsInCluster = 0;
195186

196-
await nwaku1.start({
187+
nwaku1 = await startServiceNode(this, {
197188
relay: true,
198189
discv5Discovery: true,
199190
peerExchange: true,
@@ -205,10 +196,9 @@ describe("Metadata Protocol", function () {
205196
const nwaku1Ma = await nwaku1.getMultiaddrWithId();
206197
const nwaku1PeerId = await nwaku1.getPeerId();
207198

208-
waku = await createLightNode({
199+
waku = await startLightNode({
209200
networkConfig: { clusterId, numShardsInCluster }
210201
});
211-
await waku.start();
212202
await waku.libp2p.dialProtocol(nwaku1Ma, MetadataCodec);
213203

214204
if (!waku.libp2p.services.metadata) {
@@ -239,7 +229,7 @@ describe("Metadata Protocol", function () {
239229
const contentTopic = ["/foo/1/bar/proto"];
240230
const numShardsInCluster = 0;
241231

242-
await nwaku1.start({
232+
nwaku1 = await startServiceNode(this, {
243233
relay: true,
244234
discv5Discovery: true,
245235
peerExchange: true,
@@ -250,13 +240,12 @@ describe("Metadata Protocol", function () {
250240

251241
const nwaku1Ma = await nwaku1.getMultiaddrWithId();
252242

253-
waku = await createLightNode({
243+
waku = await startLightNode({
254244
networkConfig: {
255245
clusterId: clusterIdJSWaku,
256246
numShardsInCluster
257247
}
258248
});
259-
await waku.start();
260249
await waku.libp2p.dialProtocol(nwaku1Ma, MetadataCodec);
261250

262251
// ensure the connection is closed from the other side
@@ -278,7 +267,7 @@ describe("Metadata Protocol", function () {
278267
const contentTopic = ["/foo/1/bar/proto"];
279268
const numShardsInCluster = 0;
280269

281-
await nwaku1.start({
270+
nwaku1 = await startServiceNode(this, {
282271
relay: true,
283272
discv5Discovery: true,
284273
peerExchange: true,
@@ -289,10 +278,9 @@ describe("Metadata Protocol", function () {
289278
const nwaku1Ma = await nwaku1.getMultiaddrWithId();
290279
const nwaku1PeerId = await nwaku1.getPeerId();
291280

292-
waku = await createLightNode({
281+
waku = await startLightNode({
293282
networkConfig: { clusterId, numShardsInCluster }
294283
});
295-
await waku.start();
296284
await waku.libp2p.dialProtocol(nwaku1Ma, MetadataCodec);
297285

298286
// delay to ensure the connection is estabilished and shardInfo is updated
@@ -316,7 +304,7 @@ describe("Metadata Protocol", function () {
316304
const contentTopic = ["/foo/1/bar/proto"];
317305
const numShardsInCluster = 0;
318306

319-
await nwaku1.start({
307+
nwaku1 = await startServiceNode(this, {
320308
relay: true,
321309
discv5Discovery: true,
322310
peerExchange: true,
@@ -327,7 +315,7 @@ describe("Metadata Protocol", function () {
327315
const nwaku1Ma = await nwaku1.getMultiaddrWithId();
328316
const nwaku1PeerId = await nwaku1.getPeerId();
329317

330-
waku = await createLightNode({
318+
waku = await startLightNode({
331319
networkConfig: {
332320
clusterId,
333321
numShardsInCluster
@@ -336,7 +324,6 @@ describe("Metadata Protocol", function () {
336324
pingKeepAlive: 1
337325
}
338326
});
339-
await waku.start();
340327
await waku.libp2p.dialProtocol(nwaku1Ma, MetadataCodec);
341328

342329
// delay to ensure the connection is estabilished, shardInfo is updated, and there is a ping

0 commit comments

Comments
 (0)