const assert = require('node:assert/strict'); const test = require('node:test'); const { AlertSchedulerService } = require('../src/services/alertSchedulerService'); function createLogger() { return { error() {} }; } test('scheduled delivery claims a due alert, sends it, then records delivery', async () => { const calls = []; const pool = { async query(sql, params = []) { const normalized = String(sql).replace(/\s+/g, ' ').trim(); calls.push({ normalized, params }); if (normalized.startsWith('WITH due AS')) { return { rows: [{ id: 17, route_id: 3, route_number: '4', notification_route_number: '4', alert_type: 'detour', title: 'Объезд', message: 'Через центр', start_time: new Date(), end_time: null, is_active: true }] }; } return { rows: [] }; } }; const notifications = []; const service = new AlertSchedulerService({ pool, notificationService: { enabled: true, async sendAlertCreated(alert) { notifications.push(alert); return { sent: true, messageId: 'test-message' }; } }, logger: createLogger() }); const result = await service.dispatchDueAlerts(); assert.deepEqual(result, { dispatched: 1, claimed: 1 }); assert.equal(notifications.length, 1); assert.equal(notifications[0].route_number, '4'); assert.equal(calls.length, 2); assert.match(calls[1].normalized, /^UPDATE alerts SET notification_sent_at/); }); test('scheduler leaves the database untouched while notifications are disabled', async () => { let queries = 0; const service = new AlertSchedulerService({ pool: { async query() { queries += 1; return { rows: [] }; } }, notificationService: { enabled: false }, logger: createLogger() }); assert.deepEqual(await service.dispatchDueAlerts(), { dispatched: 0, skipped: 'disabled' }); assert.equal(queries, 0); });