Skip to content

Run and Test the Real Project

The Course
•
20 min study

Move beyond the simulation

Download Review Queue 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/batch.test.mjs proves that a stale or inaccessible last edit rolls back the earlier edit, and duplicate ids or oversized batches are rejected.
  • 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 the transaction matters

The browser app makes a batch all-or-nothing by editing a draft. The SQLite project does it with a transaction around the same edit calls:

JavaScript
applyBatch(actor, input) {
  const valid = validateBatch(input);
  if (!valid.ok) return valid;
  db.exec("BEGIN IMMEDIATE");
  try {
    const items = [];
    for (const edit of valid.edits) {
      const result = this.edit(actor, edit.id, edit.version, edit);
      if (!result.ok) {
        db.exec("ROLLBACK");
        return result;
      }
      items.push(result.item);
    }
    db.exec("COMMIT");
    return { ok: true, items };
  } catch (error) {
    db.exec("ROLLBACK");
    throw error;
  }
},

In a temporary copy of the project, delete the four db.exec lines in applyBatch (begin, both rollbacks, commit), so each edit commits on its own. Before running anything, predict:

  1. Which tests in tests/batch.test.mjs fail, and which still pass?
  2. For a failing test, what title does the assertion find in storage?
  3. Why does the memory version of the same test still pass?

Then run node --test tests/batch.test.mjs, compare, and restore the lines.

Hint 1: the idea

Without a transaction, every UPDATE is final the moment it runs. A batch whose last edit is refused has already saved the edits before it, and nothing takes them back.

Hint 2: a first step

Read an invalid last edit rolls back earlier edits: which edit comes first in its batch, and what does it set the title to? Then look at which repository each test creates: sqlite(":memory:") or memory().

Show one solution

Two tests fail: SQLite: an invalid last edit rolls back earlier edits, and the HTTP adapter test, which sends a stale batch through the real PATCH handler. The first finds item 1 titled Must roll back where it expected Read about server boundaries: the first edit committed, the second was refused with conflict, and the caller was told the batch failed while half of it was saved.

The memory version still passes because its all-or-nothing behaviour comes from the draft array, not from SQL, and you didn't change it. That's why the same contract test runs against both adapters: each can break on its own.

BEGIN IMMEDIATE also takes the write lock at the start, so another request can't change a row between this batch's version checks and its writes. Each UPDATE still carries the version in its WHERE clause, so a check and its write are one statement even inside the transaction.

Was this article helpful?

Your response is saved on this device.