tests: use memLocalHistory for message_channel.spec.ts

This commit is contained in:
Danish Arora 2025-11-26 19:11:01 -05:00
parent 581430ab43
commit d52ea9fd2c
No known key found for this signature in database
GPG Key ID: 1C6EF37CDAE1426E

View File

@ -4,6 +4,7 @@ import { expect } from "chai";
import { DefaultBloomFilter } from "../bloom_filter/bloom.js";
import { MessageChannelEvent } from "./events.js";
import { MemLocalHistory } from "./mem_local_history.js";
import {
ContentMessage,
HistoryEntry,
@ -22,6 +23,28 @@ const callback = (_message: Message): Promise<{ success: boolean }> => {
return Promise.resolve({ success: true });
};
/**
* Test helper to create a MessageChannel with MemLocalHistory.
* This avoids localStorage pollution in tests and tests core functionality.
*/
const createTestChannel = (
channelId: string,
senderId: string,
options: {
causalHistorySize?: number;
possibleAcksThreshold?: number;
timeoutForLostMessagesMs?: number;
enableRepair?: boolean;
} = {}
): MessageChannel => {
return new MessageChannel(
channelId,
senderId,
options,
new MemLocalHistory()
);
};
const getBloomFilter = (channel: MessageChannel): DefaultBloomFilter => {
return channel["filter"] as DefaultBloomFilter;
};
@ -68,7 +91,7 @@ describe("MessageChannel", function () {
describe("sending a message ", () => {
beforeEach(() => {
channelA = new MessageChannel(channelId, "alice");
channelA = createTestChannel(channelId, "alice");
});
it("should increase lamport timestamp", async () => {
@ -171,8 +194,8 @@ describe("MessageChannel", function () {
describe("receiving a message", () => {
beforeEach(() => {
channelA = new MessageChannel(channelId, "alice");
channelB = new MessageChannel(channelId, "bob");
channelA = createTestChannel(channelId, "alice");
channelB = createTestChannel(channelId, "bob");
});
it("should increase lamport timestamp", async () => {
@ -187,8 +210,8 @@ describe("MessageChannel", function () {
// TODO: test is failing in CI, investigate in https://github.com/waku-org/js-waku/issues/2648
it.skip("should update lamport timestamp if greater than current timestamp and dependencies are met", async () => {
const testChannelA = new MessageChannel(channelId, "alice");
const testChannelB = new MessageChannel(channelId, "bob");
const testChannelA = createTestChannel(channelId, "alice");
const testChannelB = createTestChannel(channelId, "bob");
const timestampBefore = testChannelA["lamportTimestamp"];
@ -452,10 +475,10 @@ describe("MessageChannel", function () {
describe("reviewing ack status", () => {
beforeEach(() => {
channelA = new MessageChannel(channelId, "alice", {
channelA = createTestChannel(channelId, "alice", {
causalHistorySize: 2
});
channelB = new MessageChannel(channelId, "bob", { causalHistorySize: 2 });
channelB = createTestChannel(channelId, "bob", { causalHistorySize: 2 });
});
it("should mark all messages in causal history as acknowledged", async () => {
@ -661,10 +684,10 @@ describe("MessageChannel", function () {
describe("Sweeping incoming buffer", () => {
beforeEach(() => {
channelA = new MessageChannel(channelId, "alice", {
channelA = createTestChannel(channelId, "alice", {
causalHistorySize: 2
});
channelB = new MessageChannel(channelId, "bob", { causalHistorySize: 2 });
channelB = createTestChannel(channelId, "bob", { causalHistorySize: 2 });
});
it("should detect messages with missing dependencies", async () => {
@ -746,7 +769,7 @@ describe("MessageChannel", function () {
it("should mark a message as irretrievably lost if timeout is exceeded", async () => {
// Create a channel with very very short timeout
const channelC: MessageChannel = new MessageChannel(channelId, "carol", {
const channelC = createTestChannel(channelId, "carol", {
timeoutForLostMessagesMs: 10
});
@ -789,7 +812,7 @@ describe("MessageChannel", function () {
let lostMessages: HistoryEntry[] = [];
// Create a channel with very short timeout
const channelC: MessageChannel = new MessageChannel(channelId, "carol", {
const channelC = createTestChannel(channelId, "carol", {
timeoutForLostMessagesMs: 10
});
@ -853,7 +876,7 @@ describe("MessageChannel", function () {
it("should remove messages without delivering if timeout is exceeded", async () => {
const causalHistorySize = channelA["causalHistorySize"];
// Create a channel with very very short timeout
const channelC: MessageChannel = new MessageChannel(channelId, "carol", {
const channelC = createTestChannel(channelId, "carol", {
timeoutForLostMessagesMs: 10
});
@ -1043,10 +1066,10 @@ describe("MessageChannel", function () {
describe("Sweeping outgoing buffer", () => {
beforeEach(() => {
channelA = new MessageChannel(channelId, "alice", {
channelA = createTestChannel(channelId, "alice", {
causalHistorySize: 2
});
channelB = new MessageChannel(channelId, "bob", { causalHistorySize: 2 });
channelB = createTestChannel(channelId, "bob", { causalHistorySize: 2 });
});
it("should partition messages based on acknowledgement status", async () => {
@ -1088,10 +1111,10 @@ describe("MessageChannel", function () {
describe("Sync messages", () => {
beforeEach(() => {
channelA = new MessageChannel(channelId, "alice", {
channelA = createTestChannel(channelId, "alice", {
causalHistorySize: 2
});
channelB = new MessageChannel(channelId, "bob", { causalHistorySize: 2 });
channelB = createTestChannel(channelId, "bob", { causalHistorySize: 2 });
const message = utf8ToBytes("first message in channel");
channelA["localHistory"].push(
new ContentMessage(
@ -1115,7 +1138,7 @@ describe("MessageChannel", function () {
});
it("should not be sent when there is no history", async () => {
const channelC = new MessageChannel(channelId, "carol", {
const channelC = createTestChannel(channelId, "carol", {
causalHistorySize: 2
});
const res = await channelC.pushOutgoingSyncMessage(async (_msg) => {
@ -1160,7 +1183,7 @@ describe("MessageChannel", function () {
});
it("should update ack status of messages in outgoing buffer", async () => {
const channelC = new MessageChannel(channelId, "carol", {
const channelC = createTestChannel(channelId, "carol", {
causalHistorySize: 2
});
for (const m of messagesA) {
@ -1185,7 +1208,7 @@ describe("MessageChannel", function () {
describe("Ephemeral messages", () => {
beforeEach(() => {
channelA = new MessageChannel(channelId, "alice");
channelA = createTestChannel(channelId, "alice");
});
it("should be sent without a timestamp, causal history, or bloom filter", async () => {
@ -1208,7 +1231,7 @@ describe("MessageChannel", function () {
});
it("should be delivered immediately if received", async () => {
const channelB = new MessageChannel(channelId, "bob");
const channelB = createTestChannel(channelId, "bob");
// Track initial state
const localHistoryBefore = channelB["localHistory"].length;