Skip to content

Commit

Permalink
chore(tests): restructure & cleanup (#1796)
Browse files Browse the repository at this point in the history
* chore: restructure folder heirrarchy

* fix: imports
  • Loading branch information
danisharora099 authored Jan 17, 2024
1 parent 7a8ef87 commit 2e6d983
Show file tree
Hide file tree
Showing 50 changed files with 255 additions and 208 deletions.
10 changes: 2 additions & 8 deletions packages/tests/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
* @module
*/

export * from "./async_fs.js";
export * from "./utils/index.js";
export * from "./constants.js";
export * from "./delay.js";
export * from "./log_file.js";
export * from "./node/node.js";
export * from "./teardown.js";
export * from "./message_collector.js";
export * from "./utils.js";
export * from "./waitForRemotePeerWithCodec.js";
export * from "./lib/index.js";
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from "fs";
import { Logger } from "@waku/utils";
import Docker from "dockerode";

import { Args } from "./interfaces.js";
import { Args } from "../types.js";

const log = new Logger("test:docker");

Expand Down
2 changes: 2 additions & 0 deletions packages/tests/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./message_collector.js";
export * from "./service_node.js";
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes";
import { AssertionError, expect } from "chai";
import { equals } from "uint8arrays/equals";

import { MessageRpcResponse } from "./node/interfaces.js";

import { base64ToUtf8, delay, NimGoNode } from "./index.js";
import { base64ToUtf8, delay, ServiceNode } from "../index.js";
import { MessageRpcResponse } from "../types.js";

const log = new Logger("test:message-collector");

Expand All @@ -20,7 +19,7 @@ export class MessageCollector {
list: Array<MessageRpcResponse | DecodedMessage> = [];
callback: (msg: DecodedMessage) => void = () => {};

constructor(private nwaku?: NimGoNode) {
constructor(private nwaku?: ServiceNode) {
if (!this.nwaku) {
this.callback = (msg: DecodedMessage): void => {
log.info("Got a message");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import { bytesToHex, hexToBytes } from "@waku/utils/bytes";
import pRetry from "p-retry";
import portfinder from "portfinder";

import { existsAsync, mkdirAsync, openAsync } from "../async_fs.js";
import { delay } from "../delay.js";
import waitForLine from "../log_file.js";

import Dockerode from "./dockerode.js";
import {
Args,
KeyPair,
LogLevel,
MessageRpcQuery,
MessageRpcResponse
} from "./interfaces.js";
} from "../types.js";
import { existsAsync, mkdirAsync, openAsync } from "../utils/async_fs.js";
import { delay } from "../utils/delay.js";
import waitForLine from "../utils/log_file.js";

import Dockerode from "./dockerode.js";

const log = new Logger("test:node");

Expand All @@ -41,7 +41,7 @@ BigInt.prototype.toJSON = function toJSON() {
return Number(this);
};

export class NimGoNode {
export class ServiceNode {
private docker?: Dockerode;
private peerId?: PeerId;
private multiaddrWithId?: Multiaddr;
Expand Down Expand Up @@ -464,7 +464,3 @@ interface RpcInfoResponse {
listenAddresses: string[];
enrUri?: string;
}

export function base64ToUtf8(b64: string): string {
return Buffer.from(b64, "base64").toString("utf-8");
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ export async function waitForFile(path: string): Promise<void> {
}
} while (!found);
}

export * from "./log_file.js";
3 changes: 3 additions & 0 deletions packages/tests/src/utils/base64_utf8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function base64ToUtf8(b64: string): string {
return Buffer.from(b64, "base64").toString("utf-8");
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

export * from "./async_fs.js";
File renamed without changes.
6 changes: 6 additions & 0 deletions packages/tests/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from "./generate_test_data.js";
export * from "./teardown.js";
export * from "./random_array.js";
export * from "./wait_for_remote_peer_with_codec.js";
export * from "./delay.js";
export * from "./base64_utf8.js";
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Waku } from "@waku/interfaces";
import { Logger } from "@waku/utils";
import pRetry from "p-retry";

import { NimGoNode } from "./index.js";
import { ServiceNode } from "../lib/service_node.js";

const log = new Logger("test:teardown");

export async function tearDownNodes(
nwakuNodes: NimGoNode | NimGoNode[],
nwakuNodes: ServiceNode | ServiceNode[],
wakuNodes: Waku | Waku[]
): Promise<void> {
const nNodes = Array.isArray(nwakuNodes) ? nwakuNodes : [nwakuNodes];
Expand Down
12 changes: 6 additions & 6 deletions packages/tests/tests/connection_manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { createLightNode } from "@waku/sdk";
import { expect } from "chai";
import sinon, { SinonSpy, SinonStub } from "sinon";

import { delay } from "../dist/delay.js";
import { makeLogFileName, NimGoNode, tearDownNodes } from "../src/index.js";
import { delay } from "../src/index.js";
import { makeLogFileName, ServiceNode, tearDownNodes } from "../src/index.js";

const TEST_TIMEOUT = 10_000;
const DELAY_MS = 1_000;
Expand Down Expand Up @@ -486,15 +486,15 @@ describe("ConnectionManager", function () {

describe("Connection state", () => {
this.timeout(20_000);
let nwaku1: NimGoNode;
let nwaku2: NimGoNode;
let nwaku1: ServiceNode;
let nwaku2: ServiceNode;
let nwaku1PeerId: Multiaddr;
let nwaku2PeerId: Multiaddr;

beforeEach(async () => {
this.timeout(20_000);
nwaku1 = new NimGoNode(makeLogFileName(this.ctx) + "1");
nwaku2 = new NimGoNode(makeLogFileName(this.ctx) + "2");
nwaku1 = new ServiceNode(makeLogFileName(this.ctx) + "1");
nwaku2 = new ServiceNode(makeLogFileName(this.ctx) + "2");
await nwaku1.start({
filter: true
});
Expand Down
2 changes: 1 addition & 1 deletion packages/tests/tests/dns-peer-discovery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { createLightNode } from "@waku/sdk";
import { expect } from "chai";
import { MemoryDatastore } from "datastore-core/memory";

import { delay } from "../src/delay.js";
import { delay } from "../src/index.js";

const maxQuantity = 3;

Expand Down
18 changes: 11 additions & 7 deletions packages/tests/tests/enr.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import { Protocols } from "@waku/interfaces";
import { createRelayNode } from "@waku/sdk/relay";
import { expect } from "chai";

import { makeLogFileName, NOISE_KEY_1, tearDownNodes } from "../src/index.js";
import { NimGoNode } from "../src/node/node.js";
import {
makeLogFileName,
NOISE_KEY_1,
ServiceNode,
tearDownNodes
} from "../src/index.js";

describe("ENR Interop: NimGoNode", function () {
describe("ENR Interop: ServiceNode", function () {
let waku: RelayNode;
let nwaku: NimGoNode;
let nwaku: ServiceNode;

afterEach(async function () {
this.timeout(15000);
Expand All @@ -19,7 +23,7 @@ describe("ENR Interop: NimGoNode", function () {

it("Relay", async function () {
this.timeout(20_000);
nwaku = new NimGoNode(makeLogFileName(this));
nwaku = new ServiceNode(makeLogFileName(this));
await nwaku.start({
relay: true,
store: false,
Expand Down Expand Up @@ -51,7 +55,7 @@ describe("ENR Interop: NimGoNode", function () {

it("Relay + Store", async function () {
this.timeout(20_000);
nwaku = new NimGoNode(makeLogFileName(this));
nwaku = new ServiceNode(makeLogFileName(this));
await nwaku.start({
relay: true,
store: true,
Expand Down Expand Up @@ -83,7 +87,7 @@ describe("ENR Interop: NimGoNode", function () {

it("All", async function () {
this.timeout(20_000);
nwaku = new NimGoNode(makeLogFileName(this));
nwaku = new ServiceNode(makeLogFileName(this));
await nwaku.start({
relay: true,
store: true,
Expand Down
6 changes: 3 additions & 3 deletions packages/tests/tests/ephemeral.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import {
makeLogFileName,
NOISE_KEY_1,
NOISE_KEY_2,
ServiceNode,
tearDownNodes
} from "../src/index.js";
import { NimGoNode } from "../src/node/node.js";

const log = new Logger("test:ephemeral");

Expand All @@ -43,7 +43,7 @@ const TestDecoder = createDecoder(TestContentTopic);

describe("Waku Message Ephemeral field", () => {
let waku: LightNode;
let nwaku: NimGoNode;
let nwaku: ServiceNode;

let subscription: IFilterSubscription;

Expand All @@ -54,7 +54,7 @@ describe("Waku Message Ephemeral field", () => {

beforeEach(async function () {
this.timeout(15_000);
nwaku = new NimGoNode(makeLogFileName(this));
nwaku = new ServiceNode(makeLogFileName(this));
await nwaku.start({
filter: true,
lightpush: true,
Expand Down
20 changes: 10 additions & 10 deletions packages/tests/tests/filter/multiple_pubsub.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { expect } from "chai";
import {
makeLogFileName,
MessageCollector,
NimGoNode,
ServiceNode,
tearDownNodes
} from "../../src/index.js";

Expand All @@ -28,8 +28,8 @@ describe("Waku Filter V2: Multiple PubsubTopics", function () {
// Set the timeout for all tests in this suite. Can be overwritten at test level
this.timeout(30000);
let waku: LightNode;
let nwaku: NimGoNode;
let nwaku2: NimGoNode;
let nwaku: ServiceNode;
let nwaku2: ServiceNode;
let subscription: IFilterSubscription;
let messageCollector: MessageCollector;

Expand Down Expand Up @@ -121,7 +121,7 @@ describe("Waku Filter V2: Multiple PubsubTopics", function () {
await subscription.subscribe([customDecoder1], messageCollector.callback);

// Set up and start a new nwaku node with customPubsubTopic1
nwaku2 = new NimGoNode(makeLogFileName(this) + "2");
nwaku2 = new ServiceNode(makeLogFileName(this) + "2");
await nwaku2.start({
filter: true,
lightpush: true,
Expand Down Expand Up @@ -185,8 +185,8 @@ describe("Waku Filter V2 (Autosharding): Multiple PubsubTopics", function () {
// Set the timeout for all tests in this suite. Can be overwritten at test level
this.timeout(30000);
let waku: LightNode;
let nwaku: NimGoNode;
let nwaku2: NimGoNode;
let nwaku: ServiceNode;
let nwaku2: ServiceNode;
let subscription: IFilterSubscription;
let messageCollector: MessageCollector;

Expand Down Expand Up @@ -295,7 +295,7 @@ describe("Waku Filter V2 (Autosharding): Multiple PubsubTopics", function () {
await subscription.subscribe([customDecoder1], messageCollector.callback);

// Set up and start a new nwaku node with customPubsubTopic1
nwaku2 = new NimGoNode(makeLogFileName(this) + "2");
nwaku2 = new ServiceNode(makeLogFileName(this) + "2");
await nwaku2.start({
filter: true,
lightpush: true,
Expand Down Expand Up @@ -359,8 +359,8 @@ describe("Waku Filter V2 (Named sharding): Multiple PubsubTopics", function () {
// Set the timeout for all tests in this suite. Can be overwritten at test level
this.timeout(30000);
let waku: LightNode;
let nwaku: NimGoNode;
let nwaku2: NimGoNode;
let nwaku: ServiceNode;
let nwaku2: ServiceNode;
let subscription: IFilterSubscription;
let messageCollector: MessageCollector;

Expand Down Expand Up @@ -446,7 +446,7 @@ describe("Waku Filter V2 (Named sharding): Multiple PubsubTopics", function () {
await subscription.subscribe([customDecoder1], messageCollector.callback);

// Set up and start a new nwaku node with customPubsubTopic1
nwaku2 = new NimGoNode(makeLogFileName(this) + "2");
nwaku2 = new ServiceNode(makeLogFileName(this) + "2");
await nwaku2.start({
filter: true,
lightpush: true,
Expand Down
8 changes: 6 additions & 2 deletions packages/tests/tests/filter/ping.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import type { IFilterSubscription, LightNode } from "@waku/interfaces";
import { utf8ToBytes } from "@waku/utils/bytes";
import { expect } from "chai";

import { MessageCollector, NimGoNode, tearDownNodes } from "../../src/index.js";
import {
MessageCollector,
ServiceNode,
tearDownNodes
} from "../../src/index.js";

import {
runNodes,
Expand All @@ -17,7 +21,7 @@ describe("Waku Filter V2: Ping", function () {
// Set the timeout for all tests in this suite. Can be overwritten at test level
this.timeout(10000);
let waku: LightNode;
let nwaku: NimGoNode;
let nwaku: ServiceNode;
let subscription: IFilterSubscription;
let messageCollector: MessageCollector;

Expand Down
4 changes: 2 additions & 2 deletions packages/tests/tests/filter/push.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { expect } from "chai";
import {
delay,
MessageCollector,
NimGoNode,
ServiceNode,
tearDownNodes,
TEST_STRING,
TEST_TIMESTAMPS
Expand All @@ -26,7 +26,7 @@ describe("Waku Filter V2: FilterPush", function () {
// Set the timeout for all tests in this suite. Can be overwritten at test level
this.timeout(10000);
let waku: LightNode;
let nwaku: NimGoNode;
let nwaku: ServiceNode;
let subscription: IFilterSubscription;
let messageCollector: MessageCollector;

Expand Down
10 changes: 5 additions & 5 deletions packages/tests/tests/filter/subscribe.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
generateTestData,
makeLogFileName,
MessageCollector,
NimGoNode,
ServiceNode,
tearDownNodes,
TEST_STRING
} from "../../src/index.js";
Expand All @@ -35,8 +35,8 @@ describe("Waku Filter V2: Subscribe", function () {
// Set the timeout for all tests in this suite. Can be overwritten at test level
this.timeout(10000);
let waku: LightNode;
let nwaku: NimGoNode;
let nwaku2: NimGoNode;
let nwaku: ServiceNode;
let nwaku2: ServiceNode;
let subscription: IFilterSubscription;
let messageCollector: MessageCollector;

Expand Down Expand Up @@ -118,7 +118,7 @@ describe("Waku Filter V2: Subscribe", function () {

// Send a test message using the relay post method.
await nwaku.sendMessage(
NimGoNode.toMessageRpcQuery({
ServiceNode.toMessageRpcQuery({
contentTopic: TestContentTopic,
payload: utf8ToBytes(messageText)
})
Expand Down Expand Up @@ -370,7 +370,7 @@ describe("Waku Filter V2: Subscribe", function () {
await subscription.subscribe([TestDecoder], messageCollector.callback);

// Set up and start a new nwaku node
nwaku2 = new NimGoNode(makeLogFileName(this) + "2");
nwaku2 = new ServiceNode(makeLogFileName(this) + "2");
await nwaku2.start({
filter: true,
lightpush: true,
Expand Down
Loading

0 comments on commit 2e6d983

Please sign in to comment.