feat(service): P5.3 inbound adapter pipeline (ACK-fast webhook + RawEventProjector) + traceId

POST /v1/adapters/:type/webhook verifies HMAC (fail-closed) -> stores raw ->
emits raw.received -> 202. RawEventProjector consumes it off the relay, normalizes
via the adapter, reuses IngestService (traceId threaded raw->interaction).
Deduped on (channelType, externalEventId). rawBody enabled. 48 tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-01 13:44:23 +05:30
parent b4a1832a5c
commit 29b64c3c4e
12 changed files with 320 additions and 1 deletions
@@ -0,0 +1,75 @@
import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest';
import { PrismaClient } from '@prisma/client';
import { resetDb } from '../test-utils/reset-db';
import { makeFakePorts } from '@insignia/iios-testkit';
import { IIOS_EVENTS, type CloudEvent } from '@insignia/iios-contracts';
import { signedFixture, emailFixture } from '@insignia/iios-adapter-sdk';
import { AdapterRegistry } from './adapter.registry';
import { InboundService } from './inbound.service';
import { RawEventProjector } from './raw-event.projector';
import { IngestService } from '../interactions/ingest.service';
import { ActorResolver } from '../identity/actor.resolver';
import { OutboxBus } from '../outbox/outbox.bus';
import type { PrismaService } from '../prisma/prisma.service';
const url = process.env.DATABASE_URL ?? 'postgresql://iios:iios@localhost:5434/iios?schema=public';
const prisma = new PrismaClient({ datasources: { db: { url } } });
const asService = prisma as unknown as PrismaService;
const SECRET = 'dev-adapter-secret'; // AdapterRegistry default
const registry = () => new AdapterRegistry();
const inbound = () => new InboundService(asService, registry());
const projector = () =>
new RawEventProjector(asService, new OutboxBus(), registry(), new IngestService(asService, makeFakePorts(), new ActorResolver(asService)));
async function rawReceivedEvents(): Promise<CloudEvent[]> {
const rows = await prisma.iiosOutboxEvent.findMany({ where: { eventType: IIOS_EVENTS.rawReceived }, orderBy: { createdAt: 'asc' } });
return rows.map((r) => r.cloudEvent as unknown as CloudEvent);
}
beforeAll(async () => { await prisma.$connect(); });
afterAll(async () => { await prisma.$disconnect(); });
beforeEach(async () => { await resetDb(prisma); });
describe('Adapter inbound (P5)', () => {
it('signed webhook → raw stored, then projector normalizes → interaction with the text + traceId', async () => {
const { body, headers } = signedFixture(emailFixture, SECRET);
const ack = await inbound().receive('WEBHOOK', body, headers);
expect(ack.status).toBe('RECEIVED');
const raw = await prisma.iiosInboundRawEvent.findUniqueOrThrow({ where: { id: ack.rawEventId } });
expect(raw.signatureStatus).toBe('VERIFIED');
await projector().onRawReceived((await rawReceivedEvents())[0]!);
const after = await prisma.iiosInboundRawEvent.findUniqueOrThrow({ where: { id: ack.rawEventId } });
expect(after.status).toBe('NORMALIZED');
expect(after.interactionId).toBeTruthy();
const interaction = await prisma.iiosInteraction.findUniqueOrThrow({
where: { id: after.interactionId! },
include: { parts: true },
});
expect(interaction.parts[0]?.bodyText).toBe(emailFixture.text);
expect(interaction.traceId).toBe(raw.traceId); // trace flows raw → interaction
});
it('bad signature → REJECTED, nothing ingested (fail-closed)', async () => {
const { body } = signedFixture(emailFixture, SECRET);
await expect(inbound().receive('WEBHOOK', body, { 'x-iios-signature': 'sha256=bad' })).rejects.toThrow();
expect(await prisma.iiosInboundRawEvent.count({ where: { status: 'REJECTED' } })).toBe(1);
expect(await prisma.iiosInteraction.count()).toBe(0);
});
it('replays: same provider eventId twice → one raw event, one interaction', async () => {
const { body, headers } = signedFixture(emailFixture, SECRET);
const svc = inbound();
await svc.receive('WEBHOOK', body, headers);
const second = await svc.receive('WEBHOOK', body, headers);
expect(second.deduped).toBe(true);
await projector().onRawReceived((await rawReceivedEvents())[0]!);
expect(await prisma.iiosInboundRawEvent.count()).toBe(1);
expect(await prisma.iiosInteraction.count()).toBe(1);
});
});