'use strict'; const test = require('node:test'); const assert = require('node:assert'); const { createTestServer } = require('./helpers'); const post = (body) => ({ method: 'POST', body }); const MIN = 60 * 1000; test('messages: send, inbox/outbox/thread, read, delete own copy', async (t0) => { const t = await createTestServer({ contract: true }); t0.after(() => t.close()); const a = await t.signup('alice'), b = await t.signup('bobby'), c = await t.signup('carol'); let r = await t.api('/messages', post({ toOperatorId: b.operatorId, text: 'hi‮ bob​!\u0007' }), a.token); assert.equal(r.status, 201); assert.equal(r.body.recipients, 1); const m1 = r.body.messageId; t.clock.advance(1000); r = await t.api('/messages', post({ toName: 'ALICE', text: 'hello back' }), b.token); assert.equal(r.status, 201); const m2 = r.body.messageId; t.clock.advance(1000); r = await t.api('/messages', post({ toOperatorId: b.operatorId, text: 'third' }), c.token); const m3 = r.body.messageId; r = await t.api('/messages/inbox', {}, b.token); assert.deepEqual(r.body.items.map((m) => m.id), [m3, m1], 'newest first'); assert.equal(r.body.unread, 2); assert.equal(r.body.items[1].text, 'hi bob!', 'control and bidi characters are stripped'); assert.equal(r.body.items[1].from.name, 'alice'); assert.equal(r.body.items[1].direction, 'in'); assert.equal(r.body.items[1].readAt, null); // cursor: page of one r = await t.api('/messages/inbox?limit=1', {}, b.token); assert.equal(r.body.items.length, 1); assert.equal(r.body.next, m3); r = await t.api('/messages/inbox?limit=1&after=' + r.body.next, {}, b.token); assert.deepEqual(r.body.items.map((m) => m.id), [m1]); assert.equal(r.body.next, null); r = await t.api('/messages/outbox', {}, a.token); assert.deepEqual(r.body.items.map((m) => m.id), [m1]); assert.equal(r.body.items[0].to.name, 'bobby'); assert.equal(r.body.items[0].direction, 'out'); // thread alice <-> bobby, both directions, excludes carol r = await t.api('/messages/thread/' + b.operatorId, {}, a.token); assert.deepEqual(r.body.items.map((m) => m.id), [m2, m1]); assert.deepEqual(r.body.items.map((m) => m.direction), ['in', 'out']); r = await t.api('/messages/thread/' + a.operatorId, {}, b.token); assert.deepEqual(r.body.items.map((m) => m.id), [m2, m1]); // read r = await t.api('/messages/read', post({ ids: [m1, 99999] }), b.token); assert.equal(r.body.changed, true); assert.equal(r.body.count, 1); r = await t.api('/messages/read', post({ ids: [m1] }), b.token); assert.equal(r.body.changed, false); r = await t.api('/messages/inbox?unread=true', {}, b.token); assert.deepEqual(r.body.items.map((m) => m.id), [m3]); r = await t.api('/messages/read', post({ all: true }), b.token); assert.equal(r.body.count, 1); r = await t.api('/messages/inbox', {}, b.token); assert.equal(r.body.unread, 0); assert.ok(r.body.items.every((m) => m.readAt === t.clock.now())); r = await t.api('/messages/' + m1, {}, b.token); assert.equal(r.body.message.text, 'hi bob!'); assert.equal((await t.api('/messages/' + m1, {}, c.token)).status, 404, 'strangers cannot read it'); // delete own copy only r = await t.api('/messages/' + m1, { method: 'DELETE' }, b.token); assert.equal(r.body.changed, true); r = await t.api('/messages/' + m1, { method: 'DELETE' }, b.token); assert.equal(r.body.changed, false); assert.equal((await t.api('/messages/' + m1, {}, c.token).then((x) => x.status)), 404); assert.equal((await t.api('/messages/inbox', {}, b.token)).body.items.some((m) => m.id === m1), false); assert.equal((await t.api('/messages/outbox', {}, a.token)).body.items.length, 1, 'the sender keeps theirs'); r = await t.api('/messages/' + m1, { method: 'DELETE' }, a.token); assert.equal(r.body.changed, true); assert.equal(t.srv.db.get('SELECT COUNT(*) AS n FROM messages WHERE id = ?', m1).n, 0, 'purged when nobody has a copy'); assert.equal((await t.api('/messages/' + m1, { method: 'DELETE' }, a.token)).status, 404); // notifications for the recipient r = await t.api('/notifications', {}, b.token); assert.ok(r.body.items.some((n) => n.type === 'message' && n.payload.from.name === 'alice')); }); test('messages: validation, self, empty, too long', async (t0) => { const t = await createTestServer({ contract: true }); t0.after(() => t.close()); const a = await t.signup('alice'), b = await t.signup('bobby'); const send = (body) => t.api('/messages', post(body), a.token); assert.equal((await send({ text: 'no target' })).status, 422); assert.equal((await send({ toOperatorId: b.operatorId, toName: 'bobby', text: 'two targets' })).status, 422); assert.equal((await send({ toOperatorId: a.operatorId, text: 'self' })).status, 422); assert.equal((await send({ toOperatorId: b.operatorId, text: '' })).status, 422); assert.equal((await send({ toOperatorId: b.operatorId, text: '‮​ \u0007' })).status, 422, 'nothing left after stripping'); assert.equal((await send({ toOperatorId: b.operatorId, text: 'x'.repeat(2001) })).status, 422); assert.equal((await send({ toOperatorId: b.operatorId, text: 'x'.repeat(2000) })).status, 201); assert.equal((await send({ toName: 'nobody', text: 'x' })).status, 404); assert.equal((await send({ toOperatorId: 999999, text: 'x' })).status, 404); assert.equal((await send({ toAlliance: true, text: 'x' })).body.error.code, 'not_in_alliance'); assert.equal((await t.api('/messages', post({ toOperatorId: b.operatorId, text: 'x' }))).status, 401); }); test('messages: per-sender rate limit follows the clock', async (t0) => { const t = await createTestServer({ contract: true }); t0.after(() => t.close()); const a = await t.signup('alice'), b = await t.signup('bobby'); for (let i = 0; i < 15; i++) { const r = await t.api('/messages', post({ toOperatorId: b.operatorId, text: 'm' + i }), a.token); assert.equal(r.status, 201, 'message ' + i); } let r = await t.api('/messages', post({ toOperatorId: b.operatorId, text: 'one more' }), a.token); assert.equal(r.status, 429); assert.equal(r.body.error.code, 'rate_limited'); assert.ok(r.body.error.details.retryAfter > 0 && r.body.error.details.retryAfter <= 600); assert.equal(r.headers.get('retry-after'), String(r.body.error.details.retryAfter)); // bobby is not affected by alice's limit assert.equal((await t.api('/messages', post({ toOperatorId: a.operatorId, text: 'hey' }), b.token)).status, 201); t.clock.advance(11 * MIN); assert.equal((await t.api('/messages', post({ toOperatorId: b.operatorId, text: 'again' }), a.token)).status, 201); // hourly cap: 60 per hour for (let i = 0; i < 100; i++) { t.clock.advance(11 * MIN / 15 * 1); // ~44 s apart: within the 10-minute window never more than 15 r = await t.api('/messages', post({ toOperatorId: b.operatorId, text: 'h' + i }), a.token); if (r.status === 429) break; } assert.equal(r.status, 429); assert.equal(r.body.error.details.windowMs, 60 * MIN, 'the hourly window is the one that bites'); }); test('messages: alliance broadcast, block list, and the feed', async (t0) => { const t = await createTestServer({ contract: true }); t0.after(() => t.close()); const lead = await t.signupPeering('leader'), m1 = await t.signupPeering('memb1'), m2 = await t.signupPeering('memb2'), out = await t.signup('outsider'); const id = (await t.api('/alliances', post({ name: 'Chat Club', tag: 'CHAT' }), lead.token)).body.allianceId; for (const u of [m1, m2]) { const inv = await t.api('/alliances/' + id + '/invite', post({ operatorId: u.operatorId }), lead.token); await t.api('/alliance-invites/' + inv.body.inviteId + '/accept', { method: 'POST' }, u.token); } // m2 blocks m1 let r = await t.api('/messages/blocks', post({ operatorId: m1.operatorId }), m2.token); assert.equal(r.body.changed, true); r = await t.api('/messages/blocks', post({ operatorId: m1.operatorId }), m2.token); assert.equal(r.body.changed, false); r = await t.api('/messages/blocks', {}, m2.token); assert.deepEqual(r.body.items.map((b) => b.name), ['memb1']); r = await t.api('/messages', post({ toAlliance: true, text: 'meeting at 8' }), m1.token); assert.equal(r.status, 201); assert.equal(r.body.kind, 'alliance'); assert.equal(r.body.recipients, 1, 'lead only: m2 blocked the sender'); r = await t.api('/messages', post({ toAlliance: true, text: 'from the lead' }), lead.token); assert.equal(r.body.recipients, 2); r = await t.api('/messages/alliance', {}, lead.token); assert.deepEqual(r.body.items.map((m) => m.text), ['from the lead', 'meeting at 8']); assert.equal(r.body.items[0].alliance.tag, 'CHAT'); r = await t.api('/messages/alliance', {}, m2.token); assert.deepEqual(r.body.items.map((m) => m.text), ['from the lead']); r = await t.api('/messages/alliance', {}, m1.token); assert.equal(r.body.items.length, 2, 'the sender sees their own broadcast and the lead\'s'); assert.equal((await t.api('/messages/alliance', {}, out.token)).body.error.code, 'not_in_alliance'); assert.equal((await t.api('/messages/inbox', {}, out.token)).body.items.length, 0); // blocked DMs are accepted silently and never delivered r = await t.api('/messages', post({ toOperatorId: m2.operatorId, text: 'dm to the blocker' }), m1.token); assert.equal(r.status, 201); assert.equal(r.body.recipients, 0); assert.equal((await t.api('/messages/inbox', {}, m2.token)).body.items.filter((m) => m.text === 'dm to the blocker').length, 0); assert.equal((await t.api('/messages/outbox', {}, m1.token)).body.items[0].text, 'dm to the blocker'); r = await t.api('/messages/blocks/' + m1.operatorId, { method: 'DELETE' }, m2.token); assert.equal(r.body.changed, true); r = await t.api('/messages', post({ toOperatorId: m2.operatorId, text: 'now it arrives' }), m1.token); assert.equal(r.body.recipients, 1); // the broadcast stays readable after the alliance is gone await t.api('/alliances/' + id + '/disband', { method: 'POST' }, lead.token); r = await t.api('/messages/inbox', {}, lead.token); assert.ok(r.body.items.some((m) => m.text === 'meeting at 8' && m.alliance.tag === 'CHAT')); });