Repository navigation
Description
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 (createInvoiceon a forced DB error,getPaymentStatuson a not-found row). The success path (a real DB write) is untested.SquarePaymentProvider— 1 test total: only the early "not connected" guard clause.SquarePaymentProvideris 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.getPaymentStatusis 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.createInvoicenever calls the Stripe API at all — it only writes a localinvoicesrow and fabricatesexternal_invoice_id: 'stripe-${job.id}'. This is documented inCONTRIBUTING.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_keyderived fromjob.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,
createInvoicethrows — 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
SquarePaymentProvider.createInvoice— the priority. MockcallSquareApi(orfetch, whichever@/lib/squarewraps) and the admin Supabase client. Cover:- full success path: all three calls succeed, correct
invoicesrow written, correctInvoiceResultreturned, 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
invoicesrow behind getPaymentStatus— found byid, found byexternal_invoice_id, not found
- full success path: all three calls succeed, correct
StripePaymentProvider— add the success-path test forcreateInvoice(real DB write, not just the fallback) and covergetPaymentStatus's found case (currently only not-found is tested).ManualPaymentProvider— addgetPaymentStatuscoverage (found/not-found/paid).- Use the same fake-Supabase-query-builder pattern established this session; mock
@/lib/square'scallSquareApi/decryptSecret/getStoredSquareConnectiondirectly rather than modeling HTTP.
Acceptance criteria
-
SquarePaymentProvider.createInvoicehas success-path coverage plus each of its distinct failure modes (order/invoice/publish failure), not just the pre-flight connection guard. -
SquarePaymentProvider.getPaymentStatusis tested. -
StripePaymentProvider.createInvoicehas a success-path test in addition to the existing fallback test. -
ManualPaymentProvider.getPaymentStatusis tested. -
npm run lint,npx tsc --noEmit,npm test,npm run buildall 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;
getPaymentStatusis 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).
Activity