|
| 1 | +import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest' |
| 2 | +import express from 'express' |
| 3 | +import type { Server } from 'node:http' |
| 4 | +import { AddressInfo } from 'node:net' |
| 5 | +import notificationsRouter from '../../routes/notifications' |
| 6 | +import { setupMemoryMongo, teardownMemoryMongo, clearCollections } from './test-support' |
| 7 | +import { createDispatcher } from '../push' |
| 8 | +import { matchBlock, type ParsedBlock } from './matcher' |
| 9 | +import { isNotificationsEnabled, buildDispatcher } from './config' |
| 10 | +import type { PushData, PushSendResult } from '../push/types' |
| 11 | + |
| 12 | +const XPUB = |
| 13 | + 'ToEA6m5JcxaE72JyzLAvHoonWB3GgAxzt5jADQC9Pc43q4V3omj94DdKe98KkRgY2nqwbNHxWCceM3o62VhFVe7HCnAW7yBGEvfbfwrYsK3N7t7' |
| 14 | +const RECEIVE_0 = 'FQVANvQqVsLwkwBnAJ5oPDYrqcfXLak7Bf' |
| 15 | +const DEVICE_TOKEN = 'device-token-abc' |
| 16 | +const TXID = 'a'.repeat(64) |
| 17 | + |
| 18 | +let server: Server |
| 19 | +let baseUrl: string |
| 20 | + |
| 21 | +describe('notifications end-to-end', () => { |
| 22 | + beforeAll(async () => { |
| 23 | + await setupMemoryMongo() |
| 24 | + const app = express() |
| 25 | + app.use(express.json()) |
| 26 | + app.use('/api/notifications', notificationsRouter) |
| 27 | + server = app.listen(0) |
| 28 | + baseUrl = `http://127.0.0.1:${(server.address() as AddressInfo).port}` |
| 29 | + }, 60000) |
| 30 | + |
| 31 | + afterAll(async () => { |
| 32 | + await new Promise<void>((resolve) => server.close(() => resolve())) |
| 33 | + await teardownMemoryMongo() |
| 34 | + }) |
| 35 | + |
| 36 | + beforeEach(async () => { |
| 37 | + await clearCollections() |
| 38 | + }) |
| 39 | + |
| 40 | + it('registers via the route, matches a block, and dispatches exactly one content-free push', async () => { |
| 41 | + // 1. Register the wallet's watch-only xpub through the real route. |
| 42 | + const res = await fetch(`${baseUrl}/api/notifications/register`, { |
| 43 | + method: 'POST', |
| 44 | + headers: { 'content-type': 'application/json' }, |
| 45 | + body: JSON.stringify({ |
| 46 | + xpub: XPUB, |
| 47 | + scriptType: 'p2pkh', |
| 48 | + gapLimit: 5, |
| 49 | + network: 'mainnet', |
| 50 | + deviceToken: DEVICE_TOKEN, |
| 51 | + platform: 'android', |
| 52 | + confirmations: 1, |
| 53 | + events: ['incoming_pending', 'incoming_confirmed', 'outgoing_confirmed'], |
| 54 | + }), |
| 55 | + }) |
| 56 | + expect(res.status).toBe(200) |
| 57 | + const { subscriptionId } = (await res.json()) as { subscriptionId: string } |
| 58 | + |
| 59 | + // 2. A real dispatcher over a mock FCM sender that captures what is sent. |
| 60 | + const sent: Array<{ token: string; data: PushData }> = [] |
| 61 | + const dispatch = createDispatcher({ |
| 62 | + fcm: async (token, data): Promise<PushSendResult> => { |
| 63 | + sent.push({ token, data }) |
| 64 | + return { deadToken: false } |
| 65 | + }, |
| 66 | + }) |
| 67 | + |
| 68 | + // 3. Feed a synthetic block paying the registered index-0 receive address. |
| 69 | + const block: ParsedBlock = { |
| 70 | + network: 'mainnet', |
| 71 | + depth: 0, |
| 72 | + txs: [{ txid: TXID, outputAddresses: [RECEIVE_0], inputAddresses: [] }], |
| 73 | + } |
| 74 | + await matchBlock(block, { dispatch }) |
| 75 | + |
| 76 | + // 4. Exactly one push, to the registered device, carrying ONLY the wake signal. |
| 77 | + expect(sent).toHaveLength(1) |
| 78 | + expect(sent[0].token).toBe(DEVICE_TOKEN) |
| 79 | + expect(Object.keys(sent[0].data).sort()).toEqual(['event', 'subscriptionId', 'txid']) |
| 80 | + expect(sent[0].data).toEqual({ txid: TXID, event: 'incoming_pending', subscriptionId }) |
| 81 | + // No amount/address/balance ever transits the provider. |
| 82 | + expect(JSON.stringify(sent[0].data).toLowerCase()).not.toMatch(/amount|address|balance|value|fair/) |
| 83 | + }) |
| 84 | + |
| 85 | + it('is disabled and dispatcher-less when no provider credentials are present', () => { |
| 86 | + expect(isNotificationsEnabled({})).toBe(false) |
| 87 | + expect(buildDispatcher({})).toBeNull() |
| 88 | + }) |
| 89 | + |
| 90 | + it('enables and builds a dispatcher from a structurally valid FCM service account', () => { |
| 91 | + const serviceAccount = JSON.stringify({ |
| 92 | + client_email: 'notifier@example.iam.gserviceaccount.com', |
| 93 | + private_key: 'unused-at-construction', |
| 94 | + project_id: 'fair-explorer', |
| 95 | + }) |
| 96 | + expect(isNotificationsEnabled({ FCM_SERVICE_ACCOUNT_JSON: serviceAccount })).toBe(true) |
| 97 | + expect(typeof buildDispatcher({ FCM_SERVICE_ACCOUNT_JSON: serviceAccount })).toBe('function') |
| 98 | + }) |
| 99 | +}) |
0 commit comments