Capstone implementation and tests
Prerequisite: node-capstone-requirements-and-architecture. Objective: Integrate reviewed catalog and orders. Each example is independent. Use Node.js 22.16.0 in a disposable directory; save as main.mjs and run node main.mjs. No external service is contacted.
How it works
Use a composite tenant-and-book key, parameterized SQL and a bounded quantity. A retry with the same tenant and idempotency key returns the existing result only if the original request matches; otherwise report conflict. An order for a different tenant must not reveal or mutate its stock. Place the stock update, order insert and event insert in one transaction, roll back on failure, and test actual state. The separate capstone fixture includes native HTTP and real SQLite; it is an isolated teaching application, not a production-ready database architecture.
Prove rollback of a real SQLite mutation
import {DatabaseSync} from 'node:sqlite';const db=new DatabaseSync(':memory:');db.exec('CREATE TABLE stock(q INTEGER CHECK(q>=0)); INSERT INTO stock VALUES(3)');db.exec('BEGIN');try{db.exec('UPDATE stock SET q=1; INSERT INTO stock VALUES(-1)');db.exec('COMMIT')}catch{db.exec('ROLLBACK')}console.log(db.prepare('SELECT q FROM stock').get().q);db.close();Expected output:
3The failed constraint rolls back the earlier update in the same transaction.
Reject reuse of a key with changed payload
function replay(old,next){return old.bookId===next.bookId&&old.quantity===next.quantity?'replay':'conflict';}console.log(replay({bookId:'b1',quantity:2},{bookId:'b1',quantity:3}));Expected output:
conflictA key is tied to the original request semantics.
Common mistake
Do not check stock in one unprotected operation and decrement it later; do not accept duplicate keys with different payloads.
Practice
Run the capstone regression suite; add a test for a new order after an insufficient-stock rejection.
Reference candidate: node-capstone-implementation-and-tests-lookup (route unverified). Documentation anchor: https://nodejs.org/download/release/v22.21.0/docs/api/sqlite.html (related documentation, not exact-version validation).
Administrative lab: staged-capstone/README.md. Run only the supplied synthetic fixture; no production deployment authorized.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Capstone implementation and tests — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
What belongs in the order transaction?
What should changed payload with reused key produce?
What verifies rollback?