In the last course, you placed Grace’s note about a shelf count of minus three in stock/, at line 7 of adjustStock(), and marked “Stock never goes negative” as only assumed on that path in your map.md. That told you where to look. It didn’t give you a way to make the bug happen.
In this lesson, you’ll turn Grace’s report into steps that make the wrong count appear every time, cut those steps down to what matters, and find out why most adjustments come out fine. You’ll finish with your first saved check script. Start with the report itself and what it does and doesn’t tell you.
Split the Report Before You Trust It
A bug report mixes facts with guesses, and if you investigate the guesses first, you start in the wrong place. Here’s Grace’s note again, with a line she’s added since:
After this morning’s shelf count, someone saved an adjustment of -20 for Small Maps, and the staff desk’s Shelf count now says
book-1: -3 on hand, 0 reserved. A shelf can’t hold minus three books. Staff save adjustments every day, and most come out fine, so I think the form plays up now and then.
Before you open any code, split the note into three parts: what staff did, what they saw, and what they concluded. You use the first two to reproduce the bug, and the third is one hypothesis among several.
If you’re arriving at this course directly, you don’t have a map.md yet, so create an empty one in your home folder, outside the shop’s repository, and keep this course’s notes in it. Write your split in map.md before you compare it with this one:
- Did: Saved an adjustment of -20 for
book-1, Small Maps, on the Shelf count form. - Saw:
book-1: -3 on hand, 0 reserved, while other adjustments looked right. - Concluded: The form “plays up now and then,” and counts shouldn’t go below zero.
The split also shows what’s missing. Grace doesn’t say what the count was before the adjustment or what else happened to Small Maps that morning. Note both questions for her. You can start without the answers, because the seed data gives you a known starting count.
Your split also needs an expected result. That isn’t what Grace assumed, but what the shop should do. Ada, the teammate who wrote the shop’s architecture notes, left a rule that covers it. Ada’s rule in docs/ARCHITECTURE.md (line 27) says “Stock never goes negative,” so the shop should refuse an adjustment that would leave fewer than zero copies and keep the count unchanged.
You’ve already read line 7 of adjustStock(), which adds the adjustment to the count without checking the result, so you may want to change it now. But until you can make the wrong count appear, you can’t tell whether a change fixes Grace’s bug, a different one, or nothing. Her conclusion deserves the same care. The button below opens Code Mentor with a question ready to send about what else could explain what she saw:
Treat each explanation in the reply as another hypothesis, and test it against a run.
Write Steps That Fail Every Time
A reproduction is a written set of steps, starting from a known state, that makes the bug happen every time someone follows them. Your known state is the seed data. If you’re arriving at this course directly, clone the shop and pin the course’s snapshot first. Otherwise, open a terminal in your second-draft-books folder, stop the server with Ctrl+C if it’s running, and skip the first two commands:
git checkout 3d15f97 switches your copy of the code to the recorded version, or commit, whose ID starts with 3d15f97. It’s the snapshot the last course used, so your code matches this lesson’s line for line. Git then prints a note that you’re in “detached HEAD” state. That’s expected, because you’re on a recorded version rather than a branch, and you won’t commit anything here.
The shop needs a current version of Node.js and Git. The examples use Node.js 26. npm run seed resets data/shop.json to the seed data, so book-1 starts at 18 on hand with no orders but the seeded order-1.
Read first, then run. The staff desk’s Save count adjustment button ends at adjustStock(), so open src/stock/adjustments.js in your editor. Here’s the whole file:
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 turns away only an adjustment that isn’t a whole number. Line 6 looks up the book’s stock row. You don’t need to open findStock(), since all it does is find the row with that bookId. Line 7, row.onHand += delta;, adds the adjustment to the count, and nothing after it compares the result with zero.
Now open the staff desk at http://127.0.0.1:3000/admin/. Under Shelf count, leave Book ID as book-1, type -20 into Adjustment, and stop before you click. Grace saw -3. Going by line 7 and the 18 copies on hand, what will your count show?
Click Save count adjustment. The line under the form says Saved book-1, and the list reads:
book-1: -2 on hand, 0 reserved
The count is -2, not Grace’s -3, because 18 minus 20 is -2. In the last course, your shop had already sold one copy of Small Maps as order-2, leaving 17, so -20 gave -3. Your steps have no order, so the number changed. The bug didn’t, because the count is below zero either way. Grace’s starting count is still one of your questions for her.
Now save -20 again without reseeding. The count drops to -22, because your first run changed the shop’s state before the second run. So each attempt needs a fresh start: stop the server, run npm run seed, and start it again. Repeat the steps that way until you’ve seen -2 three times.
Cutting a reproduction down to the steps, records, and settings that make the bug happen is called shrinking it. A shrunk reproduction tells a reader that nothing else is involved. Grace’s morning had a customer, an order, and a shelf count. Yours needs one stock row and one adjustment. Add the reproduction to map.md:
## Shelf count goes below zero (Grace's note)
- Setup: second-draft-books at 3d15f97,
npm run seed, npm start, staff desk
- Steps: Shelf count, Book ID book-1,
Adjustment -20, Save count adjustment
- Expected: refused, book-1 stays at 18 on hand
(Stock never goes negative)
- Actual: Saved book-1, book-1: -2 on hand
- Every time: 3 of 3 runs, each from a fresh seed
- Not needed: orders, customers, other books
- Ask Grace: count before, other sales that morning
Suppose a teammate skips the reseed and follows the remaining steps after an earlier adjustment of +10 and sees 8. The bug hasn’t gone. “Can’t reproduce” describes their setup, which in this case starts from a different count. They should note what they tried and change one difference at a time toward your setup.
Each run through the staff desk takes a reseed and a restart. Your map from the last course traces Save count adjustment through /api/stock/adjust to adjustStock(), so a script can call that function with the same input from the seed data, without the server.
Stop the server with Ctrl+C so your terminal is free again. Then make a checks folder in your home folder, outside the repository, so it stays put whatever happens to the repository later in this course:
In your editor, create shelf-count.mjs in that folder with this code:
import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
const file = resolve('src/stock/adjustments.js');
const adjustments = await import(pathToFileURL(file));
const text = await readFile('data/seed.json', 'utf8');
const state = JSON.parse(text);
const row = adjustments.adjustStock(state, 'book-1', -20);
console.log('Expected: refused, book-1 onHand 18');
console.log('Actual: ' + row.bookId + ' onHand ' + row.onHand);
Everything above the adjustStock() line is setup you’ll copy into every check. You don’t need to follow this part yet. It loads adjustments.js and the shop’s JSON seed data from the folder you run the check in, which is why you run it from inside second-draft-books. Because the check reads data/seed.json instead of data/shop.json, every run starts from the same 18 copies, whatever you’ve clicked on the staff desk.
The last three lines are your reproduction: one adjustment, then the expected and actual values. The .mjs extension tells Node.js that the file is an ES module, short for ECMAScript module, so import works outside the shop’s folder.
Run it from your second-draft-books folder with node ~/checks/shelf-count.mjs, or node $HOME\checks\shelf-count.mjs in Windows PowerShell. It prints two lines:
Expected: refused, book-1 onHand 18
Actual: book-1 onHand -2
The two lines disagree. That’s the bug, reproduced in one command. The -2 matches what the staff desk showed, and that match is your evidence that calling adjustStock() directly reaches the same bug as the form. Add - Check: node ~/checks/shelf-count.mjs to your map entry.
Find What Decides When It Goes Wrong
Grace says most adjustments come out fine, and she’s right. But a bug that shows up only sometimes isn’t random. Something decides it, such as the timing, the order of steps, the data, or the environment. You find the condition by changing one candidate at a time and keeping everything else fixed. Your check already does the second part, since every run starts from the same seed.
Start with the adjustment. On the adjustStock() line, change -20 to -5, save, and run the check again. Then try -18, -19, and -20. While you explore, the Expected line still describes the -20 run you saved, so ignore it and judge each Actual line against the rule: any count below zero is wrong. Here’s each run’s Actual line, after the adjustment that printed it:
-5: Actual: book-1 onHand 13
-18: Actual: book-1 onHand 0
-19: Actual: book-1 onHand -1
-20: Actual: book-1 onHand -2
Small Maps goes below zero from -19, one more than its 18 copies. Now hold the adjustment at -10 and change the book instead: 'book-2', 'book-3', and 'book-4' in place of 'book-1'. The seed data gives them 11, 8, and 15 copies on hand. Again, read only the Actual lines:
Actual: book-1 onHand 8
Actual: book-2 onHand 1
Actual: book-3 onHand -2
Actual: book-4 onHand 5
The same adjustment makes the count go negative for one of the four books. That explains Grace’s “most come out fine.” A count goes below zero only when a negative adjustment takes away more copies than are on hand, so that onHand plus the adjustment is below zero. A correction that removes fewer copies than are on hand, as most do, leaves the count at zero or above. The form doesn’t play up at random.
Put the check back to 'book-1' and -20, run it once more to see -2, and add the condition to your map entry: - Condition: goes below zero only when onHand + adjustment < 0 (tried on four books, 0 reserved in every run). If you want to test that condition against inputs you haven’t tried, press the button below and paste your entry after the question:
Check each input the reply suggests with your script before you add it to the map.
Check Your Understanding
Answer these with your map entry and your check script open:
- A teammate’s reproduction says: “Place an order for Small Maps, adjust book-1 by -20, see -3.” Which step would you cut, and why does cutting it change the number?
- Another teammate runs
-10forbook-1on a fresh seed, sees8, and wants to close Grace’s report as “can’t reproduce.” What would you tell them? - On a fresh seed, which of these adjustments to
book-4, Night Trains, leave its count below zero:-12,-15, or-16? - Why does
shelf-count.mjsreaddata/seed.jsoninstead ofdata/shop.json?
- Cut the order. The count goes below zero without it, so it isn’t needed. The order takes one copy, so without it the count drops from 18 to -2 instead of from 17 to -3. The bug is the count below zero, not the exact number.
- Their run shows the condition, not a fix.
-10takes away fewer than book-1’s 18 copies, so the count never goes below zero. They should record what they tried and rerun with a negative adjustment that takes away more than the count on hand, such as-20, from a fresh seed. - Only
-16. Night Trains starts with 15 copies, so-15leaves exactly 0 and-12leaves 3. A negative adjustment has to take away more copies than are on hand to go below zero. data/shop.jsonchanges every time you use the staff desk, so a check that read it would start from a different count each time. The seed data is fixed, so the check fails the same way every time anyone runs it.
Your next activity is the course’s first coding exercise, and it runs in your browser rather than your terminal. You change a file in the editor on the page, then click Run Tests to have the course’s own tests check it. You don’t write those tests. If you haven’t used coding exercises on RealJS before, the Contents tab below the editor explains how they work.
In Exercise: Pin Down the Shelf Count, you’ll turn Grace’s report into a check that someone else can run from a clean shop, keeping only the records and steps that matter.