mirror of https://github.com/status-im/js-waku.git
new store tests
This commit is contained in:
parent
2ff19c7901
commit
9371477069
|
@ -1,9 +1,10 @@
|
|||
import { DecodedMessage, DefaultPubSubTopic } from "@waku/core";
|
||||
import { bytesToUtf8 } from "@waku/utils/bytes";
|
||||
import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes";
|
||||
import { AssertionError, expect } from "chai";
|
||||
import debug from "debug";
|
||||
|
||||
import { MessageRpcResponse } from "./node/interfaces.js";
|
||||
import { areUint8ArraysEqual } from "./utils.js";
|
||||
|
||||
import { base64ToUtf8, delay, NimGoNode } from "./index.js";
|
||||
|
||||
|
@ -36,9 +37,17 @@ export class MessageCollector {
|
|||
}
|
||||
|
||||
hasMessage(topic: string, text: string): boolean {
|
||||
return this.list.some(
|
||||
(message) => message.contentTopic === topic && message.payload === text
|
||||
);
|
||||
return this.list.some((message) => {
|
||||
if (message.contentTopic !== topic) {
|
||||
return false;
|
||||
}
|
||||
if (typeof message.payload === "string") {
|
||||
return message.payload === text;
|
||||
} else if (message.payload instanceof Uint8Array) {
|
||||
return areUint8ArraysEqual(message.payload, utf8ToBytes(text));
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
// Type guard to determine if a message is of type MessageRpcResponse
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
export function areUint8ArraysEqual(a: Uint8Array, b: Uint8Array): boolean {
|
||||
if (a.length !== b.length) return false;
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
if (a[i] !== b[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
|
@ -341,7 +341,7 @@ describe("Waku Filter V2: Subscribe", function () {
|
|||
}
|
||||
|
||||
// Check if both messages were received
|
||||
expect(messageCollector.hasMessage(TestContentTopic, "M1")).to.be.true;
|
||||
expect(messageCollector.hasMessage(newContentTopic, "M2")).to.be.true;
|
||||
expect(messageCollector.hasMessage(TestContentTopic, "M1")).to.eq(true);
|
||||
expect(messageCollector.hasMessage(newContentTopic, "M2")).to.eq(true);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -17,15 +17,15 @@ import {
|
|||
import {
|
||||
processMessages,
|
||||
sendMessages,
|
||||
startAndConnectLightNode
|
||||
startAndConnectLightNode,
|
||||
TestContentTopic,
|
||||
TestDecoder,
|
||||
totalMsgs
|
||||
} from "./utils.js";
|
||||
|
||||
const TestContentTopic = "/test/1/waku-store/utf8";
|
||||
const TestDecoder = createDecoder(TestContentTopic);
|
||||
const customContentTopic = "/test/2/waku-store/utf8";
|
||||
const customPubSubTopic = "/waku/2/custom-dapp/proto";
|
||||
const customTestDecoder = createDecoder(customContentTopic, customPubSubTopic);
|
||||
const totalMsgs = 20;
|
||||
|
||||
describe("Waku Store, custom pubsub topic", function () {
|
||||
this.timeout(15000);
|
||||
|
@ -110,6 +110,7 @@ describe("Waku Store, custom pubsub topic", function () {
|
|||
topic: [DefaultPubSubTopic],
|
||||
relay: true
|
||||
});
|
||||
await nwaku2.ensureSubscriptions([DefaultPubSubTopic]);
|
||||
|
||||
const totalMsgs = 10;
|
||||
await sendMessages(nwaku, totalMsgs, customContentTopic, customPubSubTopic);
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import {
|
||||
createCursor,
|
||||
createDecoder,
|
||||
createEncoder,
|
||||
DecodedMessage,
|
||||
DefaultPubSubTopic,
|
||||
PageDirection,
|
||||
|
@ -23,20 +22,28 @@ import {
|
|||
import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes";
|
||||
import { expect } from "chai";
|
||||
|
||||
import { makeLogFileName, NimGoNode, tearDownNodes } from "../../src/index.js";
|
||||
import {
|
||||
delay,
|
||||
makeLogFileName,
|
||||
MessageCollector,
|
||||
NimGoNode,
|
||||
tearDownNodes,
|
||||
TEST_STRING
|
||||
} from "../../src/index.js";
|
||||
import { areUint8ArraysEqual } from "../../src/utils.js";
|
||||
|
||||
import {
|
||||
log,
|
||||
messageText,
|
||||
processMessages,
|
||||
sendMessages,
|
||||
startAndConnectLightNode
|
||||
startAndConnectLightNode,
|
||||
TestContentTopic,
|
||||
TestDecoder,
|
||||
TestEncoder,
|
||||
totalMsgs
|
||||
} from "./utils.js";
|
||||
|
||||
const TestContentTopic = "/test/1/waku-store/utf8";
|
||||
const TestEncoder = createEncoder({ contentTopic: TestContentTopic });
|
||||
const TestDecoder = createDecoder(TestContentTopic);
|
||||
const totalMsgs = 20;
|
||||
|
||||
describe("Waku Store", function () {
|
||||
this.timeout(15000);
|
||||
let waku: LightNode;
|
||||
|
@ -54,7 +61,7 @@ describe("Waku Store", function () {
|
|||
await tearDownNodes([nwaku], [waku, waku2]);
|
||||
});
|
||||
|
||||
it("Generator", async function () {
|
||||
it("Query generator for multiple messages", async function () {
|
||||
await sendMessages(nwaku, totalMsgs, TestContentTopic, DefaultPubSubTopic);
|
||||
waku = await startAndConnectLightNode(nwaku);
|
||||
const messages = await processMessages(
|
||||
|
@ -64,13 +71,77 @@ describe("Waku Store", function () {
|
|||
);
|
||||
|
||||
expect(messages?.length).eq(totalMsgs);
|
||||
|
||||
// checking that the message with text 0 exists
|
||||
const result = messages?.findIndex((msg) => {
|
||||
return msg.payload[0]! === 0;
|
||||
});
|
||||
expect(result).to.not.eq(-1);
|
||||
});
|
||||
|
||||
it("Generator, no message returned", async function () {
|
||||
it("Query generator for multiple messages with different message text format", async function () {
|
||||
for (const testItem of TEST_STRING) {
|
||||
expect(
|
||||
await nwaku.sendMessage(
|
||||
NimGoNode.toMessageRpcQuery({
|
||||
payload: utf8ToBytes(testItem["value"]),
|
||||
contentTopic: TestContentTopic
|
||||
}),
|
||||
DefaultPubSubTopic
|
||||
)
|
||||
).to.be.true;
|
||||
await delay(1); // to ensure each timestamp is unique.
|
||||
}
|
||||
|
||||
waku = await startAndConnectLightNode(nwaku);
|
||||
const messageCollector = new MessageCollector();
|
||||
messageCollector.list = await processMessages(
|
||||
waku,
|
||||
[TestDecoder],
|
||||
DefaultPubSubTopic
|
||||
);
|
||||
TEST_STRING.forEach((testItem) => {
|
||||
expect(
|
||||
messageCollector.hasMessage(TestContentTopic, testItem["value"])
|
||||
).to.eq(true);
|
||||
});
|
||||
});
|
||||
|
||||
it("Query generator for multiple messages with different message content topic format", async function () {
|
||||
for (const testItem of TEST_STRING) {
|
||||
expect(
|
||||
await nwaku.sendMessage(
|
||||
NimGoNode.toMessageRpcQuery({
|
||||
payload: utf8ToBytes(messageText),
|
||||
contentTopic: testItem["value"]
|
||||
}),
|
||||
DefaultPubSubTopic
|
||||
)
|
||||
).to.be.true;
|
||||
await delay(1); // to ensure each timestamp is unique.
|
||||
}
|
||||
|
||||
waku = await startAndConnectLightNode(nwaku);
|
||||
|
||||
let localPromises: Promise<void>[] = [];
|
||||
for (const testItem of TEST_STRING) {
|
||||
for await (const msgPromises of waku.store.queryGenerator([
|
||||
createDecoder(testItem["value"])
|
||||
])) {
|
||||
const _promises = msgPromises.map(async (promise) => {
|
||||
const msg = await promise;
|
||||
if (msg) {
|
||||
areUint8ArraysEqual(msg.payload, utf8ToBytes(messageText));
|
||||
}
|
||||
});
|
||||
|
||||
localPromises = localPromises.concat(_promises);
|
||||
}
|
||||
await Promise.all(localPromises);
|
||||
}
|
||||
});
|
||||
|
||||
it("Query generator, no message returned", async function () {
|
||||
waku = await startAndConnectLightNode(nwaku);
|
||||
const messages = await processMessages(
|
||||
waku,
|
||||
|
|
|
@ -1,5 +1,12 @@
|
|||
import { Decoder, DefaultPubSubTopic, waitForRemotePeer } from "@waku/core";
|
||||
import { IMessage, LightNode, Protocols } from "@waku/interfaces";
|
||||
import {
|
||||
createDecoder,
|
||||
createEncoder,
|
||||
DecodedMessage,
|
||||
Decoder,
|
||||
DefaultPubSubTopic,
|
||||
waitForRemotePeer
|
||||
} from "@waku/core";
|
||||
import { LightNode, Protocols } from "@waku/interfaces";
|
||||
import { createLightNode } from "@waku/sdk";
|
||||
import { expect } from "chai";
|
||||
import debug from "debug";
|
||||
|
@ -8,6 +15,12 @@ import { delay, NimGoNode, NOISE_KEY_1 } from "../../src";
|
|||
|
||||
export const log = debug("waku:test:store");
|
||||
|
||||
export const TestContentTopic = "/test/1/waku-store/utf8";
|
||||
export const TestEncoder = createEncoder({ contentTopic: TestContentTopic });
|
||||
export const TestDecoder = createDecoder(TestContentTopic);
|
||||
export const totalMsgs = 20;
|
||||
export const messageText = "Store Push works!";
|
||||
|
||||
export async function sendMessages(
|
||||
instance: NimGoNode,
|
||||
numMessages: number,
|
||||
|
@ -24,16 +37,16 @@ export async function sendMessages(
|
|||
pubSubTopic
|
||||
)
|
||||
).to.be.true;
|
||||
await delay(1); // to ensure each timestamp is unique.
|
||||
}
|
||||
await delay(1); // to ensure each timestamp is unique.
|
||||
}
|
||||
|
||||
export async function processMessages(
|
||||
instance: LightNode,
|
||||
decoders: Array<Decoder>,
|
||||
expectedTopic: string
|
||||
): Promise<IMessage[]> {
|
||||
const localMessages: IMessage[] = [];
|
||||
): Promise<DecodedMessage[]> {
|
||||
const localMessages: DecodedMessage[] = [];
|
||||
let localPromises: Promise<void>[] = [];
|
||||
for await (const msgPromises of instance.store.queryGenerator(decoders)) {
|
||||
const _promises = msgPromises.map(async (promise) => {
|
||||
|
|
Loading…
Reference in New Issue