db4e552594cb | atagen | 2026-10-26 04:43 UTC | 1 | import assert from 'node:assert/strict'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 2 | import { test } from 'node:test'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 3 | import { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 4 | arrivalIn, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 5 | badgeTotal, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 6 | isRoomWatched, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 7 | notifyingCount, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 8 | planNotification, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 9 | sourceFromTimeline, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 10 | type NotificationSource |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 11 | } from '../src/lib/notification-model.ts'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 12 | import type { RoomSummary, TimelineItem } from '../src/lib/types.ts'; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 13 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 14 | const NOW = 1_000_000_000; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 15 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 16 | function room(extra: Partial<RoomSummary> = {}): RoomSummary { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 17 | return { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 18 | room_id: '!r:x', |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 19 | name: 'General', |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 20 | membership: 'Joined', |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 21 | unread_messages: 0, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 22 | unread_mentions: 0, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 23 | unread_notifications: 0, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 24 | space: false, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 25 | direct: false, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 26 | notification_mode: null, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 27 | latest_event_timestamp_ms: NOW - 1000, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 28 | avatar: 'mxc://x/room', |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 29 | ...extra |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 30 | } as RoomSummary; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 31 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 32 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 33 | function source(extra: Partial<NotificationSource> = {}): NotificationSource { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 34 | return { senderId: '@bob:x', senderName: 'Bob', senderAvatar: 'mxc://x/bob', kind: 'Text', body: 'hi', undecryptable: false, highlight: false, ...extra }; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 35 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 36 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 37 | test('counts only notifying unread per mode', () => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 38 | assert.equal(notifyingCount(room({ unread_notifications: 0, unread_messages: 9 })), 9); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 39 | assert.equal(notifyingCount(room({ direct: true, unread_messages: 2, unread_mentions: 1 })), 3); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 40 | assert.equal(notifyingCount(room({ notification_mode: 'Mute', unread_mentions: 4 })), 0); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 41 | assert.equal(notifyingCount(room({ notification_mode: 'MentionsAndKeywordsOnly', unread_notifications: 5, unread_mentions: 1 })), 1); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 42 | assert.equal(notifyingCount(room({ space: true, unread_mentions: 1 })), 0); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 43 | assert.equal(notifyingCount(room({ membership: 'Invited', unread_mentions: 1 })), 0); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 44 | }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 45 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 46 | test('badge sums notifying counts across rooms', () => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 47 | const rooms = [room({ unread_messages: 2 }), room({ direct: true, unread_messages: 1 }), room({ notification_mode: 'Mute', unread_messages: 9 })]; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 48 | assert.equal(badgeTotal(rooms), 3); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 49 | assert.equal(badgeTotal([]), 0); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 50 | }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 51 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 52 | test('arrival needs a known previous room and a count increase', () => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 53 | const before = room({ unread_messages: 1 }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 54 | assert.equal(arrivalIn(undefined, before, NOW), null); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 55 | assert.equal(arrivalIn(before, before, NOW), null); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 56 | assert.equal(arrivalIn(before, room({ unread_messages: 0 }), NOW), null); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 57 | assert.deepEqual(arrivalIn(before, room({ unread_messages: 2 }), NOW), { mention: false }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 58 | assert.deepEqual(arrivalIn(before, room({ unread_messages: 2, unread_mentions: 1 }), NOW), { mention: true }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 59 | }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 60 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 61 | test('arrival ignores stale backfill', () => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 62 | const next = room({ unread_messages: 1, latest_event_timestamp_ms: NOW - 10 * 60 * 1000 }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 63 | assert.equal(arrivalIn(room(), next, NOW), null); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 64 | }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 65 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 66 | test('only the focused open room is watched', () => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 67 | assert.equal(isRoomWatched('!a', '!a', true, true), true); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 68 | assert.equal(isRoomWatched('!a', '!a', false, true), false); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 69 | assert.equal(isRoomWatched('!a', '!a', true, false), false); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 70 | assert.equal(isRoomWatched('!a', '!b', true, true), false); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 71 | }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 72 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 73 | test('group notification prefixes the sender and tags per room', () => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 74 | const plan = planNotification(room(), source(), { mention: false }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 75 | assert.equal(plan.title, 'General'); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 76 | assert.equal(plan.body, 'Bob: hi'); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 77 | assert.equal(plan.tag, 'room:!r:x'); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 78 | assert.equal(plan.renotify, false); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 79 | assert.equal(plan.iconMxc, 'mxc://x/room'); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 80 | }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 81 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 82 | test('direct notification titles the sender and uses their avatar', () => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 83 | const plan = planNotification(room({ direct: true, name: 'Bob' }), source(), { mention: false }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 84 | assert.equal(plan.title, 'Bob'); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 85 | assert.equal(plan.body, 'hi'); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 86 | assert.equal(plan.iconMxc, 'mxc://x/bob'); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 87 | }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 88 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 89 | test('mentions and keyword highlights renotify', () => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 90 | assert.equal(planNotification(room(), source(), { mention: true }).renotify, true); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 91 | assert.equal(planNotification(room(), source({ highlight: true }), { mention: false }).renotify, true); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 92 | }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 93 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 94 | test('undecryptable and missing content get a generic body', () => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 95 | assert.equal(planNotification(room(), source({ undecryptable: true, body: null }), { mention: false }).body, 'Bob: New messages'); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 96 | assert.equal(planNotification(room({ direct: true }), null, { mention: false }).body, 'New message'); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 97 | }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 98 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 99 | test('media and emotes get readable previews', () => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 100 | assert.equal(planNotification(room({ direct: true }), source({ kind: 'Image', body: 'cat.png' }), { mention: false }).body, 'Sent an image'); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 101 | assert.equal(planNotification(room(), source({ kind: 'Emote', body: 'waves' }), { mention: false }).body, 'Bob waves'); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 102 | }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 103 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 104 | test('falls back to the user id local part when no display name is known', () => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 105 | assert.equal(planNotification(room(), source({ senderName: null }), { mention: false }).body, 'bob: hi'); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 106 | }); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 107 | |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 108 | test('timeline source skips own newest and non-message items', () => { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 109 | const message = (own: boolean, body: string): TimelineItem => |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 110 | ({ |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 111 | kind: 'event', |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 112 | event: { |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 113 | own, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 114 | sender: { user_id: '@bob:x', display_name: 'Bob', avatar: null, name_ambiguous: false }, |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 115 | content: { kind: 'Message', message: { kind: 'Text', body } } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 116 | } |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 117 | }) as TimelineItem; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 118 | const membership = { kind: 'event', event: { own: false, sender: { user_id: '@c:x' }, content: { kind: 'RoomCreated' } } } as TimelineItem; |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 119 | assert.equal(sourceFromTimeline([message(false, 'a'), membership])?.body, 'a'); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 120 | assert.equal(sourceFromTimeline([message(false, 'a'), message(true, 'b')]), null); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 121 | assert.equal(sourceFromTimeline(undefined), null); |
db4e552594cb | atagen | 2026-10-26 04:43 UTC | 122 | }); |