import test from "node:test"; import assert from "node:assert/strict"; import { CHUNK_SIZE, MessageType } from "../../shared/protocol/constants.js"; import { obscureSpeech, speechReadability } from "../../shared/speech/proximity.js"; import { broadcastSpeech } from "../src/chat/speech.js"; test("speech stays clear until half a chunk, then falls to zero at one chunk", () => { assert.equal(speechReadability(0), 1); assert.equal(speechReadability(CHUNK_SIZE * 0.5), 1); assert.equal(speechReadability(CHUNK_SIZE * 0.75), 0.5); assert.equal(speechReadability(CHUNK_SIZE), 0); assert.equal(speechReadability(CHUNK_SIZE + 4), 0); }); test("obscured speech is deterministic and hides letters with distance", () => { const source = "Hello there"; assert.equal(obscureSpeech(source, 1, 7), source); assert.equal(obscureSpeech(source, 0, 7), ""); const far = obscureSpeech(source, 0.4, 7); assert.equal(obscureSpeech(source, 0.4, 7), far); assert.match(far, /\./); assert.match(far, / /); }); test("broadcastSpeech trims, caps length, and only sends audible bubbles", () => { function session (id, entityId) { const sent = []; return { id, entityId, characterId: entityId, socket: { readyState: 1, send (raw) { sent.push(JSON.parse(raw)); } }, sent }; } const speaker = session(1, 10); const near = session(2, 20); const mid = session(3, 30); const silent = session(4, 40); const positions = new Map([ [10, { tileX: 0, tileY: 0 }], [20, { tileX: 4, tileY: 0 }], [30, { tileX: 12, tileY: 0 }], [40, { tileX: 16, tileY: 0 }] ]); const simulation = { settings: { chatMaxCharacters: 8 }, positionOf (id) { return positions.get(id) ?? null; } }; const sessions = new Map([ [speaker.id, speaker], [near.id, near], [mid.id, mid], [silent.id, silent] ]); broadcastSpeech(sessions, simulation, speaker, " hello world "); assert.equal(speaker.sent.length, 1); assert.equal(speaker.sent[0].type, MessageType.chatBubble); assert.equal(speaker.sent[0].text, "hello wo"); assert.equal(near.sent[0].text, "hello wo"); assert.equal( mid.sent[0].text, obscureSpeech("hello wo", speechReadability(12), speaker.sent[0].messageId ^ mid.id) ); assert.equal(silent.sent.length, 0); for (const entry of sessions.values()) { entry.sent.length = 0; } broadcastSpeech(sessions, simulation, speaker, " "); assert.equal(speaker.sent.length, 0); });