Key points (each is said and shown in the video):
- Grace’s report, reproduced: on a freshly seeded shop, book-1 has 18 on hand. Saving an adjustment of -20 from the staff desk’s Shelf count form leaves
book-1: -2 on hand, 0 reserved. - Your map places the cause in
adjustStock(), at the line that adds the adjustment. Your check script,node ~/checks/shelf-count.mjs, callsadjustStock()directly with -20 and printsActual: book-1 onHand -2, but it doesn’t show what the staff desk’s real path handsadjustStock(). So treat the placement as a claim, and check it on that path. - A short method for debugging code you didn’t write:
- State the expected and actual results exactly. Expected: the adjustment is refused, and book-1 stays at 18. Actual:
book-1: -2 on hand. - Form a hypothesis that a check could prove wrong: the count goes wrong inside
adjustStock(), not before it. Then the form and the route should pass on exactly what staff typed, -20, and any other value arriving atadjustStock()would prove the hypothesis wrong. - Check it with a real value, such as a logged number. Rereading the code isn’t a check.
- This path has three short parts. A real bug can cross eight, and reading each closely first can take hours. Let a few logged values choose the one part worth reading.
- The adjustment travels through three parts: the staff desk’s form in
public/admin/app.js, the/api/stock/adjustroute inserver.js, andadjustStock()insrc/stock/adjustments.js, which changes the stock row. Each place where one part hands the value to the next is a seam. - In
handleForm(), the form’stransformfunction turns what staff typed into what gets sent, so the form’s log line printstransform(values).adjustStock()gets two log lines. Before the adding line, one logs what comes in: the adjustment, and the count it starts from, which onlyadjustStock()can see. After it, the other logs what goes out to the stock row. That pushes the adding line from line 7 down to line 8. - The form’s log line runs in the browser, so it appears in the browser’s Console: open the developer tools with F12, or Cmd+Option+I on macOS, and pick the Console tab before you save. The lines of the route and of
adjustStock()run in Node, so they print in the terminal that runs the shop. - The run: the form sends -20, what staff typed, and the route gets the same number. Neither one changes the value, so both are ruled out as the place it goes wrong. Whether either should refuse -20 waits for the next lesson.
adjustStock()gets 18 and -20 and hands back -2, a count below zero, so that’s where the count goes wrong. - Only then read that one function closely: one line adds the adjustment to the count, and nothing compares the result with zero. The map’s placement is now a finding, with logged values behind it.
- The rule: check the value at each seam first, then read closely only the part where a good value turns bad.
Final code: the log lines, one at each seam.
In handleForm(), public/admin/app.js, new line 58:
const values = Object.fromEntries(new FormData(form));
console.log('form sends', transform(values));
In the /api/stock/adjust branch of server.js, new line 62:
} else if (url.pathname === '/api/stock/adjust') {
console.log('route gets', input);
result = await runCommand(state => adjustStock(state, input.bookId, input.delta));
In src/stock/adjustments.js, new lines 7 and 9:
export function adjustStock(state, bookId, delta) {
if (!Number.isSafeInteger(delta)) throw new Error('Adjustment must be an integer');
let row = findStock(state, bookId);
console.log('adjustStock gets', delta, 'on hand', row.onHand);
row.onHand += delta;
console.log('adjustStock leaves', row.onHand);
return row;
}
What the run printed, in the browser’s Console:
form sends {bookId: 'book-1', delta: -20}
And in the terminal that runs the shop:
route gets { bookId: 'book-1', delta: -20 }
adjustStock gets -20 on hand 18
adjustStock leaves -2
Keep these log lines for now. The next lesson takes them out.
Next: Test a Hypothesis at a Seam.