Skip to content

Harden payment provider tests, especially the live Square integration #60

Closed

Description

@diese-tech

Parent discovery

Tracked under #53 — Discovery: Half-Shell codebase audit and technical-debt dependency map. Follow-up requested directly by the repo owner after #58 (behavior-level regression coverage) merged, scoping the "payment processing/handling" testing item.

Problem

src/lib/__tests__/payments.test.ts has 8 test cases total across all three payment providers, and none of them exercise a real success path:

  • StripePaymentProvider — 2 tests, both only prove the DB-error fallback path (createInvoice on a forced DB error, getPaymentStatus on a not-found row). The success path (a real DB write) is untested.
  • SquarePaymentProvider — 1 test total: only the early "not connected" guard clause. SquarePaymentProvider is the app's one live, real integration (three sequential Square API calls: create order → create invoice → publish invoice, each with a job.id-derived idempotency key, then a DB write) — currently the most complex and most production-critical payment code in the repo, and the least tested.
  • ManualPaymentProvider — 1 test, DB-error fallback only. getPaymentStatus is untested.

This matters more than most test gaps because PATCH /api/quotes/[id]/accept (#58) already proved invoice-creation failure is swallowed non-fatally at the route layer — which is correct customer-facing behavior, but means a broken payment provider currently fails silently from the customer's perspective. The provider-level tests are the only place that would actually catch a regression before it reaches production.

Notable facts already confirmed during investigation

  • Stripe is stubbed, not a bug: StripePaymentProvider.createInvoice never calls the Stripe API at all — it only writes a local invoices row and fabricates external_invoice_id: 'stripe-${job.id}'. This is documented in CONTRIBUTING.md's known limitations ("Stripe is stubbed... Square is the only live payment provider"). Testing effort here should match that reality — close the coverage gap on what code actually exists, not pretend there's real Stripe API logic to test.
  • Square already uses idempotency keys correctly — each of its three API calls passes a deterministic idempotency_key derived from job.id (swiftdispatch-order-..., swiftdispatch-invoice-..., swiftdispatch-invoice-publish-...). This is good practice already in place, not a gap.
  • Square has no partial-failure recovery: if order creation succeeds but invoice creation (or publish) fails, createInvoice throws — no DB row is ever written, so there's no local record of the Square-side order that now exists. This is worth a test proving the current (accepted) behavior explicitly, per the same "document don't necessarily fix" latitude used for SMS's at-least-once contract in Add stale SMS outbox lease recovery and worker resilience #51 — unless investigation during this issue turns up an easy, in-scope fix.

Scope

  1. SquarePaymentProvider.createInvoice — the priority. Mock callSquareApi (or fetch, whichever @/lib/square wraps) and the admin Supabase client. Cover:
    • full success path: all three calls succeed, correct invoices row written, correct InvoiceResult returned, each call's idempotency key is what's expected
    • missing/invalid Square connection (already covered — keep it)
    • missing access token, missing location id (currently unreachable via getStoredSquareConnection's shape but worth confirming the guard clauses fire if they ever are)
    • order-creation failure, invoice-creation failure, publish failure — each throws, and (explicitly asserted) leaves no invoices row behind
    • getPaymentStatus — found by id, found by external_invoice_id, not found
  2. StripePaymentProvider — add the success-path test for createInvoice (real DB write, not just the fallback) and cover getPaymentStatus's found case (currently only not-found is tested).
  3. ManualPaymentProvider — add getPaymentStatus coverage (found/not-found/paid).
  4. Use the same fake-Supabase-query-builder pattern established this session; mock @/lib/square's callSquareApi/decryptSecret/getStoredSquareConnection directly rather than modeling HTTP.

Acceptance criteria

  • SquarePaymentProvider.createInvoice has success-path coverage plus each of its distinct failure modes (order/invoice/publish failure), not just the pre-flight connection guard.
  • SquarePaymentProvider.getPaymentStatus is tested.
  • StripePaymentProvider.createInvoice has a success-path test in addition to the existing fallback test.
  • ManualPaymentProvider.getPaymentStatus is tested.
  • npm run lint, npx tsc --noEmit, npm test, npm run build all pass.
  • No production code changes unless a test surfaces an actual bug (same discipline as Centralize API role authorization and enforce a tested permission matrix #49–Add behavior-level regression coverage for 8 untested mutation seams #59) — the partial-failure-leaves-no-record behavior should be documented as an explicit, asserted test case rather than silently left uncovered, and only changed if it turns out to be a quick, in-scope fix.

Out of scope

  • Building real Stripe API integration (a feature request, not a test-coverage issue).
  • A Square webhook endpoint for push-based payment status updates (SwiftDispatch has none today; getPaymentStatus is presumably polled elsewhere — a real architecture question, not scoped here).
  • The remaining lower-risk admin CRUD route tests deferred by Add behavior-level regression coverage for untested production mutation seams #58.
  • Load/stress test refresh (separate, smaller follow-up item).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions