add: tests

This commit is contained in:
danisharora099 2022-11-15 17:30:35 +05:30
parent 0169a0ccb1
commit 69b64af548
No known key found for this signature in database
GPG Key ID: FBD2BF500037F135
1 changed files with 134 additions and 137 deletions

View File

@ -40,75 +40,75 @@ describe("Waku Store", () => {
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e)); !!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
}); });
// it("Generator", async function () { it("Generator", async function () {
// this.timeout(1000_000); this.timeout(1000_000);
// const totalMsgs = 20; const totalMsgs = 20;
// for (let i = 0; i < totalMsgs; i++) { for (let i = 0; i < totalMsgs; i++) {
// expect( expect(
// await nwaku.sendMessage( await nwaku.sendMessage(
// Nwaku.toMessageRpcQuery({ Nwaku.toMessageRpcQuery({
// payload: utf8ToBytes(`Message ${i}`), payload: utf8ToBytes(`Message ${i}`),
// contentTopic: TestContentTopic, contentTopic: TestContentTopic,
// }) })
// ) )
// ).to.be.true; ).to.be.true;
// } }
// waku = await createFullNode({ waku = await createFullNode({
// staticNoiseKey: NOISE_KEY_1, staticNoiseKey: NOISE_KEY_1,
// }); });
// await waku.start(); await waku.start();
// await waku.dial(await nwaku.getMultiaddrWithId()); await waku.dial(await nwaku.getMultiaddrWithId());
// await waitForRemotePeer(waku, [Protocols.Store]); await waitForRemotePeer(waku, [Protocols.Store]);
// const messages: Message[] = []; const messages: Message[] = [];
// let promises: Promise<void>[] = []; let promises: Promise<void>[] = [];
// for await (const msgPromises of waku.store.queryGenerator([TestDecoder])) { for await (const msgPromises of waku.store.queryGenerator([TestDecoder])) {
// const _promises = msgPromises.map(async (promise) => { const _promises = msgPromises.map(async (promise) => {
// const msg = await promise; const msg = await promise;
// if (msg) { if (msg) {
// messages.push(msg); messages.push(msg);
// } }
// }); });
// promises = promises.concat(_promises); promises = promises.concat(_promises);
// } }
// await Promise.all(promises); await Promise.all(promises);
// expect(messages?.length).eq(totalMsgs); expect(messages?.length).eq(totalMsgs);
// const result = messages?.findIndex((msg) => { const result = messages?.findIndex((msg) => {
// return bytesToUtf8(msg.payload!) === "Message 0"; return bytesToUtf8(msg.payload!) === "Message 0";
// }); });
// expect(result).to.not.eq(-1); expect(result).to.not.eq(-1);
// }); });
// it("Generator, no message returned", async function () { it("Generator, no message returned", async function () {
// this.timeout(15_000); this.timeout(15_000);
// waku = await createFullNode({ waku = await createFullNode({
// staticNoiseKey: NOISE_KEY_1, staticNoiseKey: NOISE_KEY_1,
// }); });
// await waku.start(); await waku.start();
// await waku.dial(await nwaku.getMultiaddrWithId()); await waku.dial(await nwaku.getMultiaddrWithId());
// await waitForRemotePeer(waku, [Protocols.Store]); await waitForRemotePeer(waku, [Protocols.Store]);
// const messages: Message[] = []; const messages: Message[] = [];
// let promises: Promise<void>[] = []; let promises: Promise<void>[] = [];
// for await (const msgPromises of waku.store.queryGenerator([TestDecoder])) { for await (const msgPromises of waku.store.queryGenerator([TestDecoder])) {
// const _promises = msgPromises.map(async (promise) => { const _promises = msgPromises.map(async (promise) => {
// const msg = await promise; const msg = await promise;
// if (msg) { if (msg) {
// messages.push(msg); messages.push(msg);
// } }
// }); });
// promises = promises.concat(_promises); promises = promises.concat(_promises);
// } }
// await Promise.all(promises); await Promise.all(promises);
// expect(messages?.length).eq(0); expect(messages?.length).eq(0);
// }); });
it("Passing a cursor", async function () { it("Passing a cursor", async function () {
this.timeout(4_000); this.timeout(4_000);
@ -132,46 +132,43 @@ describe("Waku Store", () => {
await waku.dial(await nwaku.getMultiaddrWithId()); await waku.dial(await nwaku.getMultiaddrWithId());
await waitForRemotePeer(waku, [Protocols.Store]); await waitForRemotePeer(waku, [Protocols.Store]);
const messages: Message[] = [];
const query = waku.store.queryGenerator([TestDecoder]); const query = waku.store.queryGenerator([TestDecoder]);
// messages in reversed order (first message at last index)
const messages: Message[] = [];
for await (const page of query) { for await (const page of query) {
for await (const msg of page) { for await (const msg of page.reverse()) {
messages.push(msg as Message); messages.push(msg as Message);
} }
} }
// index 2 would mean the third last message sent
const cursorIndex = 2; const cursorIndex = 2;
const cursorMessage = messages[cursorIndex]; const cursorMessage = messages[cursorIndex];
// create cursor to extract messages after the 3rd index
const cursor = await createCursor( const cursor = await createCursor(
bytesToUtf8(cursorMessage.payload!), bytesToUtf8(cursorMessage.payload!),
BigInt(cursorMessage.timestamp!.getTime()) * BigInt(1000000), BigInt(cursorMessage.timestamp!.getTime()) * BigInt(1000000),
TestContentTopic TestContentTopic
); );
const val = await waku.store const messagesAfterCursor: Message[] = [];
.queryGenerator([TestDecoder], { cursor }) for await (const page of waku.store.queryGenerator([TestDecoder], {
.next(); cursor,
//realIndexOfTest = (cursor-pageSize+test+len)%len })) {
// the last message received on this page for await (const msg of page.reverse()) {
const testMessage = await val.value[10 - 1]; messagesAfterCursor.push(msg as Message);
}
}
// for (const msg of val.value) { const testMessage = messagesAfterCursor[0];
// const _msg = await msg;
// console.log({
// msg: bytesToUtf8(_msg.payload!),
// });
// }
// console.log({
// cursorMessage: bytesToUtf8(cursorMessage.payload!),
// testMessage: bytesToUtf8(testMessage.payload!),
// });
expect(messages?.length).be.eq(totalMsgs); expect(messages.length).be.eq(totalMsgs);
expect(testMessage).to.be.eq(messages[cursorIndex + 1]); expect(bytesToUtf8(testMessage.payload!)).to.be.eq(
bytesToUtf8(messages[cursorIndex + 1].payload!)
);
}); });
it("Callback on promise", async function () { it("Callback on promise", async function () {
@ -560,68 +557,68 @@ describe("Waku Store", () => {
}); });
}); });
// describe("Waku Store, custom pubsub topic", () => { describe("Waku Store, custom pubsub topic", () => {
// const customPubSubTopic = "/waku/2/custom-dapp/proto"; const customPubSubTopic = "/waku/2/custom-dapp/proto";
// let waku: WakuFull; let waku: WakuFull;
// let nwaku: Nwaku; let nwaku: Nwaku;
// beforeEach(async function () { beforeEach(async function () {
// this.timeout(15_000); this.timeout(15_000);
// nwaku = new Nwaku(makeLogFileName(this)); nwaku = new Nwaku(makeLogFileName(this));
// await nwaku.start({ await nwaku.start({
// persistMessages: true, persistMessages: true,
// store: true, store: true,
// topics: customPubSubTopic, topics: customPubSubTopic,
// }); });
// }); });
// afterEach(async function () { afterEach(async function () {
// !!nwaku && nwaku.stop(); !!nwaku && nwaku.stop();
// !!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e)); !!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
// }); });
// it("Generator, custom pubsub topic", async function () { it("Generator, custom pubsub topic", async function () {
// this.timeout(15_000); this.timeout(15_000);
// const totalMsgs = 20; const totalMsgs = 20;
// for (let i = 0; i < totalMsgs; i++) { for (let i = 0; i < totalMsgs; i++) {
// expect( expect(
// await nwaku.sendMessage( await nwaku.sendMessage(
// Nwaku.toMessageRpcQuery({ Nwaku.toMessageRpcQuery({
// payload: utf8ToBytes(`Message ${i}`), payload: utf8ToBytes(`Message ${i}`),
// contentTopic: TestContentTopic, contentTopic: TestContentTopic,
// }), }),
// customPubSubTopic customPubSubTopic
// ) )
// ).to.be.true; ).to.be.true;
// } }
// waku = await createFullNode({ waku = await createFullNode({
// staticNoiseKey: NOISE_KEY_1, staticNoiseKey: NOISE_KEY_1,
// pubSubTopic: customPubSubTopic, pubSubTopic: customPubSubTopic,
// }); });
// await waku.start(); await waku.start();
// await waku.dial(await nwaku.getMultiaddrWithId()); await waku.dial(await nwaku.getMultiaddrWithId());
// await waitForRemotePeer(waku, [Protocols.Store]); await waitForRemotePeer(waku, [Protocols.Store]);
// const messages: Message[] = []; const messages: Message[] = [];
// let promises: Promise<void>[] = []; let promises: Promise<void>[] = [];
// for await (const msgPromises of waku.store.queryGenerator([TestDecoder])) { for await (const msgPromises of waku.store.queryGenerator([TestDecoder])) {
// const _promises = msgPromises.map(async (promise) => { const _promises = msgPromises.map(async (promise) => {
// const msg = await promise; const msg = await promise;
// if (msg) { if (msg) {
// messages.push(msg); messages.push(msg);
// } }
// }); });
// promises = promises.concat(_promises); promises = promises.concat(_promises);
// } }
// await Promise.all(promises); await Promise.all(promises);
// expect(messages?.length).eq(totalMsgs); expect(messages?.length).eq(totalMsgs);
// const result = messages?.findIndex((msg) => { const result = messages?.findIndex((msg) => {
// return bytesToUtf8(msg.payload!) === "Message 0"; return bytesToUtf8(msg.payload!) === "Message 0";
// }); });
// expect(result).to.not.eq(-1); expect(result).to.not.eq(-1);
// }); });
// }); });