UNPKG

ts-mls

Version:

[![CI](https://github.com/LukaJCB/ts-mls/actions/workflows/ci.yml/badge.svg)](https://github.com/LukaJCB/ts-mls/actions/workflows/ci.yml) [![npm version](https://badge.fury.io/js/ts-mls.svg)](https://badge.fury.io/js/ts-mls) [![Coverage Status](https://co

41 lines 2 kB
import { makePskIndex } from "../../src/clientState"; import { createApplicationMessage } from "../../src/createMessage"; import { processPrivateMessage } from "../../src/processMessages"; import { UsageError } from "../../src/mlsError"; export async function testEveryoneCanMessageEveryone(clients, impl) { const encoder = new TextEncoder(); const updatedGroups = [...clients]; for (const [senderIndex, senderState] of updatedGroups.entries()) { const messageText = `Hello from member ${senderIndex}`; const encodedMessage = encoder.encode(messageText); const { privateMessage, newState: newSenderState } = await createApplicationMessage(senderState, encodedMessage, impl); updatedGroups[senderIndex] = newSenderState; for (const [receiverIndex, receiverGroup] of updatedGroups.entries()) { if (receiverIndex === senderIndex) continue; const result = await processPrivateMessage(receiverGroup, privateMessage, makePskIndex(receiverGroup, {}), impl); if (result.kind === "newState") { throw new Error(`Expected application message for member ${receiverIndex} from ${senderIndex}`); } expect(result.message).toStrictEqual(encodedMessage); updatedGroups[receiverIndex] = result.newState; } } return { updatedGroups }; } export async function cannotMessageAnymore(state, impl) { await expect(createApplicationMessage(state, new TextEncoder().encode("hello"), impl)).rejects.toThrow(UsageError); } export function shuffledIndices(arr) { const indices = arr.map((_, i) => i); for (let i = indices.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [indices[i], indices[j]] = [indices[j], indices[i]]; } return indices; } export function getRandomElement(arr) { const index = Math.floor(Math.random() * arr.length); return arr[index]; } //# sourceMappingURL=common.js.map