Skip to content

Run and Test the Real Project

The Course
•
20 min study

Move beyond the simulation

Download Study Library with its tests. Unzip it into a new folder. You need Node.js 22.13 or newer for node:sqlite; some Node versions still label that API experimental. This is a local learning adapter, not a recommendation to use a local file on ephemeral hosting.

Shell
npm install
npm test
npm run dev

Open the address Next prints. Add an item, stop the server, start it again and reload the direct item URL. The SQLite record should survive. Use a temporary folder when experimenting so the course's source tree stays clean.

What the tests prove

tests/repository.test.mjs runs the public repository contract against a fresh in-memory SQLite database. tests/memory.test.mjs checks the browser adapter with the same cases. tests/persistence.test.mjs reopens a temporary file and checks a rollback after a constraint failure.

  • Another owner and the same id in another tenant cannot read or edit item 1.
  • A first edit increments the version; a stale second edit cannot overwrite it.
  • Blank and overlong drafts do not add or alter records.
  • The output contains id, title and version, not ownership fields.
  • A committed row survives reopening; a failed transaction changes nothing.
  • tests/http.test.mjs calls the actual item handlers with Request objects and checks malformed JSON, status codes, private headers and public fields.

The project uses a factory so tests get independent repositories. Each test closes its connection. A module cache or leftover test database must not define the next test's starting state.

Production rendering is another check

Stop the development server before using the same port:

Shell
npm run build
npm start

Visit the list and a detail URL directly, reload a filtered search and submit an invalid form. The simulator cannot test Client Component server rendering, hydration, Next.js cache behaviour or real persistence. These commands and requests provide different evidence from the contract table.

Exercise

Prove a guard matters

Temporarily remove the version predicate from the SQLite UPDATE and the matching early stale-version check. Run the tests, identify the failing assertion, then restore both guards. Why is checking only before the UPDATE insufficient under concurrency?

Hint 1: the idea

Two callers can read the same version before either writes. The write itself must be conditional, not only the earlier JavaScript decision.

Hint 2: a first step

Read the stale-edit test's second request and its final stored-title check. Keep the experiment in a temporary local project, not valuable data.

Show one solution

The stale-edit test must fail when a second edit with version 1 overwrites a record already at version 2. The SQL condition and affected-row check protect the final write even when another request changes the row after an earlier read. Restore both checks and confirm the suite passes again.

Was this article helpful?

Your response is saved on this device.