Skip to content

Add behavior-level regression coverage for untested production mutation seams #58

Closed

Description

@diese-tech

Parent discovery

Tracked under #53 — Discovery: Half-Shell codebase audit and technical-debt dependency map. Corresponds to April O'Neil's "coverage is utility-heavy, workflow-light" finding and the audit's Phase 3 item 6 ("Broader behavior-level regression coverage").

Problem

Only 4 of the 26 route handlers under src/app/api/**/route.ts have a route.test.ts: admin/technicians, admin/square/callback, jobs/[id], quotes. The other 22 — including the app's actual money path (quote acceptance + payment provider integration), its only public unauthenticated surface (customer intake), and technician field actions via SMS token links — have zero automated coverage of their behavior. Existing tests skew toward pure utilities and state primitives; the seams production traffic actually flows through are comparatively untested.

Goal

Extend this session's established test pattern (hand-built fake Supabase query-builder mocks — no live Supabase project in this environment, see src/app/api/jobs/[id]/route.test.ts and src/lib/__tests__/smsOutbox.test.ts for the established shape) to the highest-value untested seams identified by the audit:

In scope — Phase 1 (this issue)

Prioritized by risk (auth-bypass or money-path exposure first) and each with a concrete case to prove per route:

  1. POST /api/quotes/[id]/accept — dual auth (customer approval-token OR dispatcher/admin role), payment provider invoice creation, state machine transition. Cover: token path success, role path success, cross-tenant rejection, invalid/expired token rejection, payment-provider-failure handling.
  2. POST /api/quotes/[id]/decline — token-only auth, state machine transition back to in_progress. Cover: success path, invalid/expired token, already-declined/wrong-state rejection.
  3. POST /api/quotes/[id]/line-items and PATCH/DELETE /api/quotes/[id]/line-items/[itemId] — role auth + company ownership + quote-total recompute. Cover: create/update/delete success with correct total recompute, cross-tenant rejection, role rejection.
  4. POST /api/intake — public/unauthenticated, dual rate limiting (IP + company slug), multi-step writes (customer upsert, job + status_event insert, confirmation SMS enqueue, status-token signing). Cover: happy path, rate-limit-exceeded rejection (both IP and company-slug keys), invalid/unknown company slug, validation failure.
  5. GET /api/tech-action — token-gated (separate from role auth), state-machine-guarded, technician availability + customer SMS side effects, returns HTML not JSON. Cover: valid transition success, invalid transition rejection, invalid/expired token, technician-availability-update side effect.
  6. POST /api/jobs — dispatcher/admin role, zod validation, conditional technician-assignment + SMS side effects. Cover: success with and without technician assignment, validation rejection, role rejection.
  7. POST /api/jobs/[id]/retry-sms — role auth, bulk conditional update of sms_outbox rows. Cover: success, cross-tenant/cross-job rejection.
  8. POST /api/send-sms — role auth, SMS-consent gate, enqueue-failure fallback (still 200 with warning). Cover: success, consent-gate rejection, enqueue-failure fallback behavior.

Cross-tenant isolation (a company-B actor cannot read/mutate company-A's rows) should be one explicit test case per route above where it applies, not a separate pass — this directly answers Shredder's adversarial question #4 from the audit ("valid user attempting cross-company mutations").

Out of scope — deferred to a follow-up issue

Lower-risk, admin-only CRUD surfaces the audit itself treats as lower priority: admin/users, admin/templates(/[id]), admin/settings, admin/square/connect, admin/technicians/[id]/regenerate-pin, company, tech/login, intake/status. These are simpler (single-table CRUD, no payment/SMS/token complexity) and lower-traffic than the Phase 1 list above.

Acceptance criteria

  • Each Phase 1 route above has a route.test.ts covering its success path, its primary rejection path(s) named in the case list, and one cross-tenant-isolation case where the route is company-scoped.
  • Tests use the established fake-Supabase-query-builder pattern (no live Supabase project available) and mock external calls (Twilio sendSms, payment provider, rate limiter) the same way src/lib/__tests__/smsOutbox.test.ts and existing route.test.ts files already do.
  • npm run lint, npx tsc --noEmit, npm test, npm run build all pass.
  • No production route code changes unless a test surfaces an actual bug (in which case, fix it with a regression test, same as Centralize API role authorization and enforce a tested permission matrix #49–Add CI quality gates and protect main from unvalidated changes #52).

Out of scope (per audit's own non-goals)

  • Rewriting any of the tested routes' behavior.
  • A distinct end-to-end/browser test framework — this stays within the existing vitest + direct-handler-import pattern.
  • The lower-priority admin CRUD routes listed above (follow-up issue).

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