proofchain/packages/api/test/services/notifications.test.ts
caelum0x 315020a760 feat: Wave E — api platform surface (51 services, 288 routes, 19 indexers, 481 tests)
- routes across all domains: trade finance, compliance, dpp, logistics, commodities,
  energy, workforce, data + cross-cutting (auth/webhooks/exports/reports/admin/analytics)
- service layer per domain aggregating chain reads + infra repositories into typed DTOs
- indexer handlers mapping every contract group's events to repository upserts
- api + workspace typecheck green

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-08-01 00:19:24 +03:00

57 lines
2.1 KiB
TypeScript

/**
* Notifications service tests — list scoping, unread counting, and idempotent
* read-state transitions over an in-memory read model.
*/
import { describe, expect, it } from 'vitest';
import type { AppContext } from '../../src/context.js';
import { loadConfig } from '../../src/config/env.js';
import { createNotificationsService } from '../../src/services/notifications.js';
import { makeChain, makeDb, type Row } from '../routers-kit.js';
import { silentLogger } from '../helpers.js';
const config = loadConfig({ BASE_SEPOLIA_RPC_URL: 'https://sepolia.base.org' });
const RECIPIENT = `0x${'7'.repeat(40)}`;
const rows: Row[] = [
{ id: 'n1', recipient: RECIPIENT, kind: 'attested', read: false, created_at: '2026-01-03' },
{ id: 'n2', recipient: RECIPIENT, kind: 'settled', read: true, created_at: '2026-01-02' },
{ id: 'n3', recipient: RECIPIENT, kind: 'disputed', read: false, created_at: '2026-01-01' },
];
const makeCtx = (data: Record<string, readonly Row[]>): AppContext => ({
config,
logger: silentLogger,
chain: makeChain(),
db: makeDb(data),
});
const page = { limit: 25, offset: 0 };
describe('NotificationsService', () => {
it('lists newest-first and filters by read flag', async () => {
const svc = createNotificationsService(makeCtx({ notifications: rows }));
const all = await svc.list({ pagination: page });
expect(all.total).toBe(3);
expect(all.rows[0]?.id).toBe('n1');
const unread = await svc.list({ pagination: page, read: false });
expect(unread.total).toBe(2);
});
it('counts unread notifications for a recipient', async () => {
const svc = createNotificationsService(makeCtx({ notifications: rows }));
expect(await svc.unreadCount(RECIPIENT)).toBe(2);
});
it('marks an unread notification read (idempotently)', async () => {
const svc = createNotificationsService(makeCtx({ notifications: rows }));
const updated = await svc.markRead('n1');
expect(updated?.read).toBe(true);
const already = await svc.markRead('n2');
expect(already?.read).toBe(true);
expect(await svc.markRead('missing')).toBeNull();
});
});