In the last lesson, a log line at each seam settled where Grace’s negative shelf count goes wrong. The form sent delta: -20, the /api/stock/adjust route got the same -20, and adjustStock() got -20 with 18 on hand and left -2. That doesn’t yet tell you why. Was -20 the wrong value, or the right value that the next part understood differently?
In this lesson, you’ll write down what each side of that seam believes the value means and turn the gap into a hypothesis that a logged run could prove wrong. You’ll check Code Mentor’s account of the same seam claim by claim and create a debugging record in map.md that a teammate could pick up. Start with what the sender and the receiver each assume.
Name What Each Side Assumes
A seam, as the last lesson put it, is a place where one part of the system hands a value to the next, and each part has its own idea of what that value means. When the two ideas differ, both parts can look right on their own while the bug lives between them. So before you read any line closely, state the problem exactly, in values someone else could check.
Here’s the problem, in the terms of a fresh npm run seed:
- Expected: An adjustment of -20 for book-1, which has 18 copies on hand, is refused, and the count stays at 18. Ada’s rule in
docs/ARCHITECTURE.mdsays that stock never goes negative, and you set the expected result from it when you reproduced the report. - Actual: The staff desk says
Saved book-1, and the Shelf count list showsbook-1: -2 on hand, 0 reserved.
Before you open the code, take out the last lesson’s log lines, so your files and their line numbers match the ones this lesson shows. From your second-draft-books folder, run:
$ git restore public/admin/app.js server.js src/stock/adjustments.js
The git restore command discards your uncommitted changes to the files you name and puts each one back as it was in the last commit. So app.js, server.js, and adjustments.js lose the four log lines you added in Locate the Fault at the Seams.
The sender is the staff desk’s form. Here are lines 39 to 43 of public/admin/index.html, which render it:
<section><h2>Shelf count</h2><p>Use signed adjustments after counting a shelf.</p>
<form id="stock-form">
<label>Book ID <input name="bookId" value="book-1" required></label>
<label>Adjustment <input name="delta" type="number" value="1" required></label>
<button>Save count adjustment</button>
The heading says Shelf count, but the box is labeled Adjustment, and the line under the heading asks for a signed adjustment, which is a change such as -2 for two damaged copies, not the number you counted. The box has no min, so it accepts negative integers. Lines 80 to 82 of public/admin/app.js send the entered adjustment on:
handleForm('#stock-form', '/api/stock/adjust', input => ({
bookId: input.bookId, delta: Number(input.delta)
}), '#stock-message');
Line 81 turns the typed text into a number with Number() and sends it as delta. Line 62 of server.js passes input.delta to adjustStock() without looking at it. The receiver is adjustStock() in src/stock/adjustments.js, with line 5 broken in two:
import { findStock } from './inventory.js';
// Staff use this write path to correct a shelf count after a delivery.
export function adjustStock(state, bookId, delta) {
if (!Number.isSafeInteger(delta))
throw new Error('Adjustment must be an integer');
let row = findStock(state, bookId);
row.onHand += delta;
return row;
}
Line 5 lets delta through only if it’s a safe integer: a number with no fractional part, negative or positive, small enough for JavaScript to store exactly. So -20 passes, while 1.5 or the string '-20' would make line 5 throw. Line 7 adds delta to onHand. Put the four parts in a row, from the form to the stock row in data/shop.json: