Skip to content

Integration Tests Webhook Pipeline ​

File: client_backend/src/tests/webhook-pipeline.test.ts

Why ​

The webhook pipeline is the most critical path in the system. Every call flows through:

participant_joined → start-recording → participant_left → credit deduction → transcription-sync → analysis-sync

If any step breaks, you get stuck contacts, missing recordings, wrong billing, or lost analysis data. These tests simulate every step by directly writing to MongoDB (same operations the real webhooks perform) and verify the final state.

What It Tests ​

Single Call Lifecycle (3 tests) ​

  • Full answered call: verifies all timestamps, recording data, transcription, analysis, and credit deduction are stored correctly
  • Rejected call: verifies status=failed, no billing duration, no credit charge
  • Not-picked call (timeout): same as rejected but with CONNECTION_TIMEOUT disconnect reason

Billing Duration Computation (5 tests) ​

  • Uses callAnsweredAt when available (precise billing start)
  • Falls back to participantJoinedAt when callAnsweredAt is missing
  • Returns null for unanswered calls (no billing)
  • Returns null when join timestamp is missing
  • Ceils fractional seconds (1.5s → 2s)

Credit Deduction (3 tests) ​

  • 3-minute call: customer charged 9 credits (3 min × 3 cr/min), org charged 3 credits (3 min × 1 cr/min)
  • Partial minutes rounded up: 90s → 2 minutes billed
  • Idempotency: second deduction with same serviceRef is skipped (webhook retry safe)

Batch Simulation (2 tests) ​

  • 50 concurrent calls: all complete with correct status, billing, transcription, and analysis
  • Mixed outcomes (10 answered + 3 rejected + 2 timeout): correct status counts and only answered calls billed

Edge Cases (2 tests) ​

  • Very short call (< 1 second): still computes a duration and completes
  • Test call (isTestCall: true): billing duration computed but credit deduction skipped by webhook controller

How To Run ​

bash
cd client_backend
npx vitest --run src/tests/webhook-pipeline.test.ts

Expected output: 15 passed (15)

Database ​

Tests connect to {config.dbName}_test (e.g., Oktivo-Dev_test), NOT the production database. All test data is cleaned up in afterAll.

Test data uses timestamped UIDs (org-{timestamp}-{nanoid}) so multiple test runs don't collide.

Known Issues ​

  • Mongoose duplicate schema index warnings for orgUID, ownerUID, trunkUID cosmetic only, doesn't affect tests
  • ServicePricing legacy indexes: The collection may have legacy unique indexes from older schema versions. If you see E11000 duplicate key error mentioning serviceType or organizationId, run POST /internal/scripts/fix-service-pricing-indexes to drop the legacy indexes. See SERVICE-PRICING.md for details.

Last updated: