Repository navigation
Description
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:
- dispatcher mutation authorization (partially covered already by Centralize API role authorization and enforce a tested permission matrix #49/Centralize API role authorization and enforce a tested permission matrix (issue #49) #54)
- job lifecycle mutation (partially covered —
PATCH /api/jobs/[id]tested,POST /api/jobsis not) - quote mutation
- customer acceptance
- public intake abuse controls
- cross-tenant isolation (partially covered — needs broadening beyond the one existing case)
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:
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.POST /api/quotes/[id]/decline— token-only auth, state machine transition back toin_progress. Cover: success path, invalid/expired token, already-declined/wrong-state rejection.POST /api/quotes/[id]/line-itemsandPATCH/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.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.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.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.POST /api/jobs/[id]/retry-sms— role auth, bulk conditional update ofsms_outboxrows. Cover: success, cross-tenant/cross-job rejection.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.tscovering 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 waysrc/lib/__tests__/smsOutbox.test.tsand existingroute.test.tsfiles already do. -
npm run lint,npx tsc --noEmit,npm test,npm run buildall 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).
Activity
Address review: cross-tenant technician isolation + expired-token cas…
Fix cross-tenant technician assignment on POST /api/jobs (#58)