js-waku/packages/run/tests/basic.spec.ts

121 lines
3.4 KiB
TypeScript
Raw Normal View History

import { execSync } from "child_process";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
import { Protocols } from "@waku/sdk";
import { expect } from "chai";
import {
DEFAULT_NODE1_REST_PORT,
DEFAULT_NODE2_REST_PORT
} from "../src/constants.js";
import { WakuTestClient } from "../src/test-client.js";
import { getProjectName } from "../src/utils.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const packageRoot = join(__dirname, "..");
describe("Waku Run - Basic Test", function () {
this.timeout(90000);
let client: WakuTestClient;
before(async function () {
// Step 1: Start the nodes
const projectName = getProjectName(packageRoot);
execSync(`docker compose --project-name ${projectName} up -d`, {
cwd: packageRoot,
stdio: "inherit",
env: { ...process.env, COMPOSE_PROJECT_NAME: projectName }
});
// Wait for nodes to be ready
const maxRetries = 30;
const retryDelay = 2000;
let ready = false;
for (let i = 0; i < maxRetries; i++) {
try {
await fetch(
`http://127.0.0.1:${DEFAULT_NODE1_REST_PORT}/debug/v1/info`
);
await fetch(
`http://127.0.0.1:${DEFAULT_NODE2_REST_PORT}/debug/v1/info`
);
ready = true;
break;
} catch {
await new Promise((resolve) => setTimeout(resolve, retryDelay));
}
}
if (!ready) {
throw new Error("Nodes failed to start within expected time");
}
// Nodes automatically connect via --staticnode configuration
// cspell:ignore staticnode
// Wait for nwaku nodes to connect to each other
let connected = false;
for (let i = 0; i < 15; i++) {
try {
const peers = await fetch(
`http://127.0.0.1:${DEFAULT_NODE1_REST_PORT}/admin/v1/peers`
).then((r) => r.json());
if (peers.length > 0 && peers[0].connected === "Connected") {
connected = true;
break;
}
} catch {
// Ignore errors
}
await new Promise((resolve) => setTimeout(resolve, 1000));
}
if (!connected) {
throw new Error("Nwaku nodes failed to connect to each other");
}
});
after(async function () {
// Step 4: Stop the nodes
if (client) {
await client.stop();
}
const projectName = getProjectName(packageRoot);
execSync(`docker compose --project-name ${projectName} down`, {
cwd: packageRoot,
stdio: "inherit",
env: { ...process.env, COMPOSE_PROJECT_NAME: projectName }
});
});
it("should connect to both nodes and send lightpush message to both peers", async function () {
// Step 2: Connect to nodes via js-waku using WakuTestClient
client = new WakuTestClient({
contentTopic: "/test/1/basic/proto"
});
await client.start();
// Wait for both peers to be connected
await client.waku!.waitForPeers([Protocols.LightPush]);
const connectedPeers = client.waku!.libp2p.getPeers().length;
expect(connectedPeers).to.equal(
2,
"Should be connected to both nwaku nodes"
);
// Step 3: Send lightpush message - it should be sent to both peers
const result = await client.sendTestMessage("Hello Waku!");
expect(result.success).to.be.true;
expect(result.messagesSent).to.equal(
2,
"Message should be sent to both peers"
);
expect(result.failures).to.equal(0, "Should have no failures");
});
});