React + Fastify · Hard · Cross Layer Consistency
The React screen and Fastify service already run separately. Their integration contains transport and state regressions. Own retry behavior at the HTTP boundary. A retryable upstream failure must not become duplicated state or an unlimited retry loop.
Task
Deliver one working browser-to-service flow, not two disconnected implementations. Make reservations independent: if Alpha API fails while Beta Web succeeds, restore only Alpha’s row and keep Beta’s confirmed count; retrying Alpha must reuse its original request ID. Make reservations atomic and idempotent within one service process, with deterministic concurrency and failure tests.
Contract
- Browser — Starting data:
initialRowsis the supplied project list. Do not mutate it.ididentifies a project;titleis its visible name;archivedhides it from the active list.seatsis the available seat count;versionidentifies the last server version of that project. - Browser — User action: The user presses Reserve on one project. One deliberate reservation is one intent; retrying it is not a new intent.
- Browser — Provided reservation API:
api.reserve(id, {expectedVersion, requestId, signal})returns a Promise of{id, seats, version}.makeRequestId()supplies a key for a new intent. An error may includelatest, the server’s newer project values. - Browser — Timing guarantees: API operations may resolve or reject in any order, even after cancellation. The fixture controls completion order; no real network is used.
- API — Request:
POST /projects/:id/reservations;:idis the project being reserved. Send the body as JSON withContent-Type: application/json. - API — JSON body:
quantity— seats requested; integer from1through10.expectedVersion— project version last read by the caller; positive integer.requestId— identity of this reservation attempt; 1–40 lowercase letters, digits or hyphens. Example:{"quantity": 1, "expectedVersion": 3, "requestId": "reserve-42"}. - API — Authentication:
Authorization: Bearer fixture-alphaidentifies tenantalpha;fixture-bravoidentifies tenantbravo. The supplied authentication helper resolves the tenant. A query/body field is not an identity source. - API — Request delivery and storage: The same request may arrive again after a timeout. Different requests may overlap. Repository operations from different requests may overlap, and a write may fail. Use the provided single-process exclusive boundary. No distributed storage is part of this task.
- Connecting the two layers:
web/src/api-client.jsis the browser-to-HTTP adapter.api.listreads the HTTP response’sitemsarray.api.reservemaps the HTTP receipt’sremainingfield to the component’sseatsfield.
Acceptance criteria
- Immediately subtract one displayed seat from the selected row.
- Show
Savingfor that row. Disable Reserve while it is pending or has zero seats. - Keep other rows interactive.
- Success: use the returned server values for that row and clear its pending/error state.
- Failure: restore that row’s pre-attempt values, or use the newer server values supplied with the error.
- On failure, show
Reservation failedfor that row and clear its pending state. - Never roll back another row’s successful reservation.
- Retry a failed intent with the same key. Clear that intent after success.
- At component unmount, cancel every pending operation and ignore later results.
- Return HTTP
200with{id, quantity, remaining, version}. remainingis the seat count after subtractingquantity;versionincreases by exactly one.- Concurrent requests must not reserve more seats than exist.
- The key is the authenticated tenant plus
requestId. - Same key and same project, quantity and expected version: return the original receipt without changing stock again.
- Check for that receipt before checking the project’s current version.
- Same key with different reservation data: HTTP
409, codeIDEMPOTENCY_CONFLICT. - Malformed body: HTTP
400, codeINVALID_RESERVATION. - Missing or unknown bearer token: HTTP
401, codeUNAUTHORIZED. - Missing, archived or another tenant’s project: HTTP
404, codeNOT_FOUND. - Version changed: HTTP
409, codeVERSION_CONFLICT. - Not enough seats: HTTP
409, codeINSUFFICIENT_SEATS. - Repository write failed: HTTP
503, codeRETRY_LATER. - Every error body is
{code: "THE_CODE"}. - Do not consume seats, increment the version or save a successful receipt.
- Retrying the same request must still be possible.
- URL-encode search text; it must not become extra query parameters.
- Reject failed HTTP responses with their status. Do not hide errors as an empty successful list.
- Retry HTTP 503 at most once, with the exact same request body and key.
- Do not retry HTTP 400, 401 or 409, or an aborted operation.
Constraints
- At most 200 rows; IDs are unique ASCII strings and seats are non-negative integers. Input props must not be mutated.
- Keep the Search projects label, data-id row identity and accessible status messages. Preserve per-row button semantics; changing search behavior is not part of this reservation task.
- At most 10000 projects in the repository; reservation JSON body at most 16 KiB.
- Use the supplied repository and authenticated tenant resolver. No external database, network service or production credential is needed.
- The reservation guarantee is single-process only. Do not claim distributed or durable idempotency from an in-memory store.
- All services run inside the test process on loopback. No external deployment, database or cloud account is required.
Editable files
web/src/ProjectApp.jsx— Repair the component state, lifecycle and API coordination.web/src/ProjectRows.jsx— Provided row rendering; preserve identity, accessibility and button semantics.web/src/helpers.js— Optional pure helpers; not a replacement for framework lifecycle handling.web/tests/candidate.test.js— Add DOM-level regression tests using fixture(t), deferred() and assert.server/src/app.js— Framework router/controller, middleware and error response integration.server/src/service.js— Repair filtering/pagination or the reservation consistency boundary.server/src/helpers.js— Optional helpers; import from service when needed.server/tests/candidate.test.js— Add the required regression tests here using fixture(t), assert and node:test.web/src/api-client.js— Repair URL encoding, error propagation and response DTO mapping.integration/candidate.test.js— Add cross-layer regressions; reuse the local integration fixture.