|
| 1 | +// Copyright (c) 2026 IOTA Stiftung |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { describe, expect, it } from 'vitest'; |
| 5 | + |
| 6 | +import { at9am, buildEvent } from './buildEvent'; |
| 7 | + |
| 8 | +const MS_PER_DAY = 24 * 60 * 60 * 1000; |
| 9 | + |
| 10 | +describe('at9am', () => { |
| 11 | + it('sets hours to 9 and zeroes minutes, seconds, milliseconds', () => { |
| 12 | + const d = new Date(2026, 2, 15, 14, 30, 45, 500); |
| 13 | + const result = at9am(d); |
| 14 | + expect(result.getHours()).toBe(9); |
| 15 | + expect(result.getMinutes()).toBe(0); |
| 16 | + expect(result.getSeconds()).toBe(0); |
| 17 | + expect(result.getMilliseconds()).toBe(0); |
| 18 | + }); |
| 19 | + |
| 20 | + it('does not mutate the input date', () => { |
| 21 | + const d = new Date(2026, 2, 15, 14, 0, 0, 0); |
| 22 | + at9am(d); |
| 23 | + expect(d.getHours()).toBe(14); |
| 24 | + }); |
| 25 | +}); |
| 26 | + |
| 27 | +describe('buildEvent', () => { |
| 28 | + const expiration = new Date(2026, 5, 15); // June 15, 2026 |
| 29 | + |
| 30 | + it('returns 4 alerts', () => { |
| 31 | + const event = buildEvent('test.iota', expiration); |
| 32 | + expect(event.alerts).toHaveLength(4); |
| 33 | + }); |
| 34 | + |
| 35 | + it('sets event date to the expiration date', () => { |
| 36 | + const event = buildEvent('test.iota', expiration); |
| 37 | + expect(event.date).toBe(expiration); |
| 38 | + }); |
| 39 | + |
| 40 | + it('title includes the name', () => { |
| 41 | + const event = buildEvent('test.iota', expiration); |
| 42 | + expect(event.title).toContain('test'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('description references iotanames.com', () => { |
| 46 | + const event = buildEvent('test.iota', expiration); |
| 47 | + expect(event.description).toContain('iotanames.com'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('alerts are ordered from earliest to latest', () => { |
| 51 | + const event = buildEvent('test.iota', expiration); |
| 52 | + const times = event.alerts!.map((a) => a.triggerAt.getTime()); |
| 53 | + expect(times).toEqual([...times].sort((a, b) => a - b)); |
| 54 | + }); |
| 55 | + |
| 56 | + it('all alert triggers are at 09:00', () => { |
| 57 | + const event = buildEvent('test.iota', expiration); |
| 58 | + for (const alert of event.alerts!) { |
| 59 | + expect(alert.triggerAt.getHours()).toBe(9); |
| 60 | + expect(alert.triggerAt.getMinutes()).toBe(0); |
| 61 | + expect(alert.triggerAt.getSeconds()).toBe(0); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + it('one-month alert is roughly 30 days before expiration', () => { |
| 66 | + const event = buildEvent('test.iota', expiration); |
| 67 | + const diffDays = (expiration.getTime() - event.alerts![0].triggerAt.getTime()) / MS_PER_DAY; |
| 68 | + expect(diffDays).toBeGreaterThanOrEqual(28); |
| 69 | + expect(diffDays).toBeLessThanOrEqual(31); |
| 70 | + }); |
| 71 | + |
| 72 | + it('one-week alert is exactly 7 days before expiration (at 9am)', () => { |
| 73 | + const event = buildEvent('test.iota', expiration); |
| 74 | + const expected = new Date(expiration); |
| 75 | + expected.setDate(expected.getDate() - 7); |
| 76 | + expected.setHours(9, 0, 0, 0); |
| 77 | + expect(event.alerts![1].triggerAt.getTime()).toBe(expected.getTime()); |
| 78 | + }); |
| 79 | + |
| 80 | + it('one-day alert is exactly 1 day before expiration (at 9am)', () => { |
| 81 | + const event = buildEvent('test.iota', expiration); |
| 82 | + const expected = new Date(expiration); |
| 83 | + expected.setDate(expected.getDate() - 1); |
| 84 | + expected.setHours(9, 0, 0, 0); |
| 85 | + expect(event.alerts![2].triggerAt.getTime()).toBe(expected.getTime()); |
| 86 | + }); |
| 87 | + |
| 88 | + it('expiry-day alert is on the expiration date at 9am', () => { |
| 89 | + const event = buildEvent('test.iota', expiration); |
| 90 | + const expected = new Date(expiration); |
| 91 | + expected.setHours(9, 0, 0, 0); |
| 92 | + expect(event.alerts![3].triggerAt.getTime()).toBe(expected.getTime()); |
| 93 | + }); |
| 94 | + |
| 95 | + it('one-month alert clamps correctly for names expiring on the 31st (Feb overflow)', () => { |
| 96 | + const march31 = new Date(2026, 2, 31); // March 31 |
| 97 | + const event = buildEvent('test.iota', march31); |
| 98 | + const oneMonthAlert = event.alerts![0].triggerAt; |
| 99 | + // Should be Feb 28 at 9am (2026 is not a leap year), not March 3 |
| 100 | + expect(oneMonthAlert.getMonth()).toBe(1); // February |
| 101 | + expect(oneMonthAlert.getDate()).toBe(28); |
| 102 | + }); |
| 103 | +}); |
0 commit comments