Skip to content

test: add category API route tests - #34

Merged
TechnicPotato merged 2 commits into
mainfrom
api-routes
Nov 30, 2026
Merged

TechnicPotato merged 2 commits into
mainfrom
api-routes

Conversation

@TechnicPotato

Copy link
Copy Markdown
Owner

Summary

Puts the category store behind HTTP (Phase 3, step 4). Requests are validated with the category Zod schemas at the boundary. The store's results become HTTP status codes in the routes, so the store stays free of HTTP.

Endpoint Success Failures
GET /api/categories 200, categories sorted by name (ignoring case) —
POST /api/categories 201, the new category 400 invalid JSON or name · 409 name taken (ignoring case)
PATCH /api/categories/[id] 200, the renamed category 400 invalid JSON or name · 404 unknown ID · 409 name taken
DELETE /api/categories/[id] 204 404 unknown ID

Error bodies use the same { error } shape as the task routes, and validation errors also list issues.

Notes

  • Store selection: lib/categories/store.ts picks the memory or Firestore store from TASK_STORE, the same variable as tasks. A shared variable means tasks and categories can't end up on different backends. Deleting a category will later move its tasks in one Firestore transaction, which needs both in the same database.
  • Exhaustive rename handling: PATCH switches on the failure reason, with a never check in default. If the store gains a new failure reason, this route won't compile until it handles it.
  • server-only: lib/firestore.ts (the Firestore SDK helpers) is now marked server-only, like the stores.

Testing

  • Route tests cover every row above: valid input, each validation rule, malformed JSON, conflicts and unknown IDs. Failed requests are also checked to leave stored categories unchanged.
  • Locally: tests, lint, typecheck and format check pass.

Not in this PR

  • categoryId on tasks (step 5), the category UI (step 6), and moving a deleted category's tasks to Uncategorised (step 8).

Add GET/POST /api/categories and PATCH/DELETE /api/categories/[id],
backed by a category store selected with TASK_STORE (shared with tasks
so both always use the same backend).

Store results map to HTTP statuses in the routes: name-taken → 409,
not-found → 404, invalid input → 400. Rename failures use an
exhaustive switch so a new failure reason won't compile until handled.

Also mark lib/firestore.ts server-only.
@TechnicPotato
TechnicPotato merged commit b846caf into main Nov 30, 2026
4 checks passed
@TechnicPotato
TechnicPotato deleted the api-routes branch November 30, 2026 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant