|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { NotificationManager } from '../../../src'; |
| 4 | + |
| 5 | +describe('NotificationManager', () => { |
| 6 | + beforeEach(() => { |
| 7 | + vi.useFakeTimers(); |
| 8 | + }); |
| 9 | + |
| 10 | + it('sorts notifications with the configured comparator', () => { |
| 11 | + const manager = new NotificationManager({ |
| 12 | + sortComparator: (a, b) => { |
| 13 | + const aTimed = !!a.duration; |
| 14 | + const bTimed = !!b.duration; |
| 15 | + |
| 16 | + if (aTimed !== bTimed) return aTimed ? -1 : 1; |
| 17 | + return a.createdAt - b.createdAt; |
| 18 | + }, |
| 19 | + }); |
| 20 | + |
| 21 | + vi.setSystemTime(new Date('2026-03-13T10:00:00.000Z')); |
| 22 | + manager.addInfo({ |
| 23 | + message: 'persistent', |
| 24 | + origin: { emitter: 'test' }, |
| 25 | + options: { duration: 0 }, |
| 26 | + }); |
| 27 | + |
| 28 | + vi.setSystemTime(new Date('2026-03-13T10:00:01.000Z')); |
| 29 | + manager.addInfo({ message: 'timed', origin: { emitter: 'test' } }); |
| 30 | + |
| 31 | + expect(manager.notifications.map(({ message }) => message)).toEqual([ |
| 32 | + 'timed', |
| 33 | + 'persistent', |
| 34 | + ]); |
| 35 | + }); |
| 36 | + |
| 37 | + it('stores severity as undefined by default', () => { |
| 38 | + const manager = new NotificationManager(); |
| 39 | + |
| 40 | + manager.add({ |
| 41 | + message: 'plain', |
| 42 | + origin: { emitter: 'test' }, |
| 43 | + }); |
| 44 | + |
| 45 | + expect(manager.notifications[0]).toMatchObject({ |
| 46 | + duration: undefined, |
| 47 | + severity: undefined, |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('stores configured duration for severity-based notifications', () => { |
| 52 | + const manager = new NotificationManager(); |
| 53 | + |
| 54 | + manager.addInfo({ |
| 55 | + message: 'timed', |
| 56 | + origin: { emitter: 'test' }, |
| 57 | + }); |
| 58 | + |
| 59 | + expect(manager.notifications[0]).toMatchObject({ |
| 60 | + duration: 3000, |
| 61 | + severity: 'info', |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('stores tags on the notification', () => { |
| 66 | + const manager = new NotificationManager(); |
| 67 | + |
| 68 | + manager.addError({ |
| 69 | + message: 'tagged', |
| 70 | + origin: { emitter: 'test' }, |
| 71 | + options: { tags: ['composer', 'upload'] }, |
| 72 | + }); |
| 73 | + |
| 74 | + expect(manager.notifications[0]).toMatchObject({ |
| 75 | + severity: 'error', |
| 76 | + tags: ['composer', 'upload'], |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('starts removal timeout only when startTimeout is triggered', () => { |
| 81 | + const manager = new NotificationManager(); |
| 82 | + |
| 83 | + const notificationId = manager.addInfo({ |
| 84 | + message: 'timed', |
| 85 | + origin: { emitter: 'test' }, |
| 86 | + options: { duration: 1000 }, |
| 87 | + }); |
| 88 | + |
| 89 | + vi.advanceTimersByTime(1000); |
| 90 | + expect(manager.notifications).toHaveLength(1); |
| 91 | + |
| 92 | + manager.startTimeout(notificationId); |
| 93 | + vi.advanceTimersByTime(999); |
| 94 | + expect(manager.notifications).toHaveLength(1); |
| 95 | + |
| 96 | + vi.advanceTimersByTime(1); |
| 97 | + expect(manager.notifications).toHaveLength(0); |
| 98 | + }); |
| 99 | + |
| 100 | + it('restarts timeout when startTimeout is called again for the same notification', () => { |
| 101 | + const manager = new NotificationManager(); |
| 102 | + |
| 103 | + const notificationId = manager.addInfo({ |
| 104 | + message: 'timed', |
| 105 | + origin: { emitter: 'test' }, |
| 106 | + options: { duration: 1000 }, |
| 107 | + }); |
| 108 | + |
| 109 | + manager.startTimeout(notificationId); |
| 110 | + vi.advanceTimersByTime(500); |
| 111 | + |
| 112 | + manager.startTimeout(notificationId); |
| 113 | + vi.advanceTimersByTime(999); |
| 114 | + expect(manager.notifications).toHaveLength(1); |
| 115 | + |
| 116 | + vi.advanceTimersByTime(1); |
| 117 | + expect(manager.notifications).toHaveLength(0); |
| 118 | + }); |
| 119 | + |
| 120 | + it('treats missing duration as a no-op unless an override is provided', () => { |
| 121 | + const manager = new NotificationManager(); |
| 122 | + |
| 123 | + const notificationId = manager.addInfo({ |
| 124 | + message: 'persistent', |
| 125 | + origin: { emitter: 'test' }, |
| 126 | + options: { duration: 0 }, |
| 127 | + }); |
| 128 | + |
| 129 | + manager.startTimeout(notificationId); |
| 130 | + vi.advanceTimersByTime(1000); |
| 131 | + expect(manager.notifications).toHaveLength(1); |
| 132 | + |
| 133 | + manager.startTimeout(notificationId, 250); |
| 134 | + vi.advanceTimersByTime(249); |
| 135 | + expect(manager.notifications).toHaveLength(1); |
| 136 | + |
| 137 | + vi.advanceTimersByTime(1); |
| 138 | + expect(manager.notifications).toHaveLength(0); |
| 139 | + }); |
| 140 | +}); |
0 commit comments