|
| 1 | +import { |
| 2 | + BAN_NOTICE_TICK_MS, |
| 3 | + formatBanRemaining, |
| 4 | + formatChatBanNotice, |
| 5 | + getChatBanInfo, |
| 6 | +} from './chatBanNotice'; |
| 7 | + |
| 8 | +// Stands in for react-intl's formatMessage: returns defaultMessage with {placeholder} filled, |
| 9 | +// which is what the real one does for these descriptors. |
| 10 | +const fmt = ( |
| 11 | + descriptor: { id: string; defaultMessage: string }, |
| 12 | + values?: Record<string, string | number>, |
| 13 | +) => { |
| 14 | + let out = descriptor.defaultMessage; |
| 15 | + Object.entries(values || {}).forEach(([k, v]) => { |
| 16 | + out = out.replace(new RegExp(`\\{${k}\\}`, 'g'), String(v)); |
| 17 | + }); |
| 18 | + return out; |
| 19 | +}; |
| 20 | + |
| 21 | +const NOW = 1_800_000_000_000; |
| 22 | +const mins = (n: number) => NOW + n * 60_000; |
| 23 | +const hours = (n: number) => NOW + n * 3_600_000; |
| 24 | +const days = (n: number) => NOW + n * 86_400_000; |
| 25 | + |
| 26 | +describe('getChatBanInfo', () => { |
| 27 | + it('extracts a live ban from the thrown error', () => { |
| 28 | + expect(getChatBanInfo({ bannedUntil: hours(48), reason: 'spray' }, NOW)).toEqual({ |
| 29 | + bannedUntil: hours(48), |
| 30 | + reason: 'spray', |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + it('ignores an expired ban', () => { |
| 35 | + expect(getChatBanInfo({ bannedUntil: NOW - 1 }, NOW)).toBeNull(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('returns null for ordinary failures', () => { |
| 39 | + expect(getChatBanInfo(new Error('network'), NOW)).toBeNull(); |
| 40 | + expect(getChatBanInfo(null, NOW)).toBeNull(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('tolerates a ban with no reason, so older servers still work', () => { |
| 44 | + expect(getChatBanInfo({ bannedUntil: hours(1) }, NOW)?.reason).toBeUndefined(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('rejects a non-finite expiry', () => { |
| 48 | + // Infinity survives an isNaN check and is also > now, so both original guards passed it. |
| 49 | + // The banner would then show an endless duration and never fire onExpire. |
| 50 | + expect(getChatBanInfo({ bannedUntil: Infinity }, NOW)).toBeNull(); |
| 51 | + expect(getChatBanInfo({ bannedUntil: 'Infinity' }, NOW)).toBeNull(); |
| 52 | + expect(getChatBanInfo({ bannedUntil: -Infinity }, NOW)).toBeNull(); |
| 53 | + expect(getChatBanInfo({ bannedUntil: 'not-a-number' }, NOW)).toBeNull(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('drops a non-string reason rather than rendering it', () => { |
| 57 | + expect( |
| 58 | + getChatBanInfo({ bannedUntil: hours(1), reason: { a: 1 } }, NOW)?.reason, |
| 59 | + ).toBeUndefined(); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('formatBanRemaining', () => { |
| 64 | + it('scales the unit to the magnitude', () => { |
| 65 | + expect(formatBanRemaining(mins(30), NOW, fmt)).toContain('30 minutes'); |
| 66 | + expect(formatBanRemaining(hours(5), NOW, fmt)).toContain('5 hours'); |
| 67 | + expect(formatBanRemaining(days(2), NOW, fmt)).toContain('2 days'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('never promises an unlock that has not happened', () => { |
| 71 | + expect(formatBanRemaining(NOW + 30_000, NOW, fmt)).toBe('in under a minute'); |
| 72 | + expect(formatBanRemaining(NOW - 10_000, NOW, fmt)).toBe('in under a minute'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('never phrases a count as 1', () => { |
| 76 | + [mins(59), mins(89), hours(1), hours(2), days(1), days(400)].forEach((until) => { |
| 77 | + const match = formatBanRemaining(until, NOW, fmt).match(/about (\d+)/); |
| 78 | + if (match) { |
| 79 | + expect(Number(match[1])).toBeGreaterThanOrEqual(2); |
| 80 | + } |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('renders a multi-year ban as days rather than degrading', () => { |
| 85 | + expect(formatBanRemaining(NOW + 3 * 365 * 86_400_000, NOW, fmt)).toContain('1095 days'); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe('formatChatBanNotice', () => { |
| 90 | + it('explains a spray timeout', () => { |
| 91 | + const text = formatChatBanNotice({ bannedUntil: hours(48), reason: 'spray' }, NOW, fmt); |
| 92 | + expect(text).toContain('same message went to several channels'); |
| 93 | + expect(text).toContain('You can still read chat.'); |
| 94 | + expect(text).toContain('2 days'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('explains a mass-DM ban differently', () => { |
| 98 | + const text = formatChatBanNotice({ bannedUntil: days(365), reason: 'mass-dm' }, NOW, fmt); |
| 99 | + expect(text).toContain('many people at once'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('falls back to generic copy for manual and unrecognised reasons', () => { |
| 103 | + ['manual', 'reason-from-a-newer-service', undefined].forEach((reason) => { |
| 104 | + const text = formatChatBanNotice({ bannedUntil: hours(3), reason }, NOW, fmt); |
| 105 | + expect(text).toContain('paused from posting'); |
| 106 | + expect(text).toContain('3 hours'); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it('never shows operator-facing detail to the banned user', () => { |
| 111 | + const text = formatChatBanNotice({ bannedUntil: hours(48), reason: 'spray' }, NOW, fmt); |
| 112 | + expect(text).not.toMatch(/\d{4}-\d{2}-\d{2}T/); |
| 113 | + expect(text).not.toContain('ecency_chat'); |
| 114 | + expect(text).not.toContain('@'); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +describe('BAN_NOTICE_TICK_MS', () => { |
| 119 | + it('stays inside the 32-bit setTimeout limit', () => { |
| 120 | + // A delay derived from bannedUntil overflows for long bans and fires almost immediately, |
| 121 | + // which would clear the notice for exactly the users who are most banned. |
| 122 | + expect(BAN_NOTICE_TICK_MS).toBeGreaterThan(0); |
| 123 | + expect(BAN_NOTICE_TICK_MS).toBeLessThan(2_147_483_647); |
| 124 | + }); |
| 125 | +}); |
0 commit comments