|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { MemoryNotificationOutbox } from './memory-outbox.js'; |
| 5 | +import { NotificationDispatcher } from './dispatcher.js'; |
| 6 | +import { renderDigest } from './digest-render.js'; |
| 7 | +import type { NotificationDeliveryRecord } from './outbox.js'; |
| 8 | +import type { MessagingChannel, Notification } from './channel.js'; |
| 9 | + |
| 10 | +function row(over: Partial<NotificationDeliveryRecord>): NotificationDeliveryRecord { |
| 11 | + return { |
| 12 | + id: 'd', notificationId: 'n', recipientId: 'u1', channel: 'inbox', |
| 13 | + payload: {}, partitionKey: 0, status: 'pending', attempts: 0, |
| 14 | + createdAt: 0, updatedAt: 0, ...over, |
| 15 | + }; |
| 16 | +} |
| 17 | + |
| 18 | +describe('renderDigest (P3b-2)', () => { |
| 19 | + it('collapses a group into one message with a per-item list', () => { |
| 20 | + const out = renderDigest([ |
| 21 | + row({ notificationId: 'a', payload: { title: 'Task A assigned', body: 'do A' }, topic: 'task.assigned' }), |
| 22 | + row({ notificationId: 'b', payload: { title: 'Task B assigned' }, topic: 'task.assigned' }), |
| 23 | + row({ notificationId: 'c', payload: { title: 'Mentioned in C' }, topic: 'mention' }), |
| 24 | + ]); |
| 25 | + expect(out.count).toBe(3); |
| 26 | + expect(out.title).toBe('You have 3 notifications'); |
| 27 | + expect(out.body).toBe('• Task A assigned\n• Task B assigned\n• Mentioned in C'); |
| 28 | + expect(out.items.map((i) => i.notificationId)).toEqual(['a', 'b', 'c']); |
| 29 | + expect(out.severity).toBe('info'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('uses the single item’s title when the window holds one notification', () => { |
| 33 | + const out = renderDigest([row({ payload: { title: 'Just one' } })]); |
| 34 | + expect(out.count).toBe(1); |
| 35 | + expect(out.title).toBe('Just one'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('falls back to the topic when an item has no title', () => { |
| 39 | + const out = renderDigest([row({ payload: {}, topic: 'system.alert' })]); |
| 40 | + expect(out.items[0].title).toBe('system.alert'); |
| 41 | + }); |
| 42 | +}); |
| 43 | + |
| 44 | +describe('MemoryNotificationOutbox — digest claim separation', () => { |
| 45 | + it('claim() skips batched rows; claimDigest() returns only them', async () => { |
| 46 | + const outbox = new MemoryNotificationOutbox(1, () => 1000); |
| 47 | + await outbox.enqueue({ notificationId: 'imm', recipientId: 'u1', channel: 'inbox', payload: {} }); |
| 48 | + await outbox.enqueue({ notificationId: 'g1', recipientId: 'u1', channel: 'inbox', payload: {}, digestKey: 'u1|inbox|w', notBefore: 500 }); |
| 49 | + |
| 50 | + const normal = await outbox.claim({ nodeId: 'n', limit: 10, claimTtlMs: 1000 }); |
| 51 | + expect(normal.map((r) => r.notificationId)).toEqual(['imm']); |
| 52 | + |
| 53 | + const digest = await outbox.claimDigest({ nodeId: 'n', limit: 10, claimTtlMs: 1000 }); |
| 54 | + expect(digest.map((r) => r.notificationId)).toEqual(['g1']); |
| 55 | + }); |
| 56 | + |
| 57 | + it('claimDigest() defers a batched row until its window opens', async () => { |
| 58 | + let now = 100; |
| 59 | + const outbox = new MemoryNotificationOutbox(1, () => now); |
| 60 | + await outbox.enqueue({ notificationId: 'g', recipientId: 'u1', channel: 'inbox', payload: {}, digestKey: 'u1|inbox|w', notBefore: 1000 }); |
| 61 | + expect(await outbox.claimDigest({ nodeId: 'n', limit: 10, claimTtlMs: 1000 })).toHaveLength(0); |
| 62 | + now = 1000; |
| 63 | + expect(await outbox.claimDigest({ nodeId: 'n', limit: 10, claimTtlMs: 1000 })).toHaveLength(1); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +/** A channel that records every notification it is asked to send. */ |
| 68 | +function recordingChannel(id: string): { channel: MessagingChannel; sent: Notification[] } { |
| 69 | + const sent: Notification[] = []; |
| 70 | + const channel: MessagingChannel = { |
| 71 | + id, |
| 72 | + async send(_ctx, req) { sent.push(req.notification); return { ok: true }; }, |
| 73 | + classifyError: () => 'retryable', |
| 74 | + }; |
| 75 | + return { channel, sent }; |
| 76 | +} |
| 77 | + |
| 78 | +function dispatcher(outbox: MemoryNotificationOutbox, channels: MessagingChannel[], now: () => number) { |
| 79 | + return new NotificationDispatcher({ |
| 80 | + nodeId: 'node-test', |
| 81 | + outbox, |
| 82 | + channels: { getChannel: (cid: string) => channels.find((c) => c.id === cid) }, |
| 83 | + channelContext: { logger: { info() {}, warn() {}, error() {} } }, |
| 84 | + rng: () => 0.5, |
| 85 | + now, |
| 86 | + partitionCount: 1, |
| 87 | + intervalMs: 10_000, |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +describe('NotificationDispatcher — digest collapse (P3b-2)', () => { |
| 92 | + it('collapses a window into one message at window time and sends normal rows immediately', async () => { |
| 93 | + let now = Date.UTC(2026, 0, 1, 9, 0); |
| 94 | + const windowAt = Date.UTC(2026, 0, 2, 0, 0); |
| 95 | + const outbox = new MemoryNotificationOutbox(1, () => now); |
| 96 | + const key = 'u1|inbox|2026-01-01'; |
| 97 | + for (let i = 0; i < 3; i++) { |
| 98 | + await outbox.enqueue({ notificationId: `n${i}`, recipientId: 'u1', channel: 'inbox', payload: { title: `Item ${i}` }, digestKey: key, notBefore: windowAt }); |
| 99 | + } |
| 100 | + await outbox.enqueue({ notificationId: 'imm', recipientId: 'u1', channel: 'inbox', payload: { title: 'Immediate' } }); |
| 101 | + |
| 102 | + const rec = recordingChannel('inbox'); |
| 103 | + const d = dispatcher(outbox, [rec.channel], () => now); |
| 104 | + |
| 105 | + // Before the window: only the immediate row sends; the batch waits. |
| 106 | + await d.tick(); |
| 107 | + expect(rec.sent.map((n) => n.title)).toEqual(['Immediate']); |
| 108 | + |
| 109 | + // At the window: the 3 batched rows collapse into one message. |
| 110 | + now = windowAt; |
| 111 | + await d.tick(); |
| 112 | + expect(rec.sent).toHaveLength(2); |
| 113 | + const digest = rec.sent[1]; |
| 114 | + expect(digest.title).toBe('You have 3 notifications'); |
| 115 | + expect(digest.payload?.digest).toBe(true); |
| 116 | + expect(digest.payload?.count).toBe(3); |
| 117 | + |
| 118 | + // All three batched rows are acked success by the single send. |
| 119 | + const success = await outbox.list({ status: 'success' }); |
| 120 | + expect(success.filter((r) => r.digestKey === key)).toHaveLength(3); |
| 121 | + }); |
| 122 | + |
| 123 | + it('re-defers the whole group when the digest send fails', async () => { |
| 124 | + let now = 1000; |
| 125 | + const outbox = new MemoryNotificationOutbox(1, () => now); |
| 126 | + const key = 'u1|inbox|w'; |
| 127 | + for (let i = 0; i < 2; i++) { |
| 128 | + await outbox.enqueue({ notificationId: `n${i}`, recipientId: 'u1', channel: 'inbox', payload: { title: `Item ${i}` }, digestKey: key, notBefore: 1000 }); |
| 129 | + } |
| 130 | + const failing: MessagingChannel = { id: 'inbox', async send() { return { ok: false, error: 'boom' }; }, classifyError: () => 'retryable' }; |
| 131 | + const d = dispatcher(outbox, [failing], () => now); |
| 132 | + |
| 133 | + await d.tick(); |
| 134 | + // Both rows go back to pending with a future next_attempt_at (retry), not success. |
| 135 | + const pending = await outbox.list({ status: 'pending' }); |
| 136 | + expect(pending.filter((r) => r.digestKey === key)).toHaveLength(2); |
| 137 | + expect(pending.every((r) => (r.nextAttemptAt ?? 0) > now)).toBe(true); |
| 138 | + }); |
| 139 | +}); |
0 commit comments