Munchable scans food labels, and last autumn it grew a recipe library. 38 recipes, visible to anybody at munchable.app/recipes and shipped inside the phone app as well.
The interesting thing about it as a piece of engineering is where it lives. It is not in a CMS, not in markdown, and not in a database. It is packages/content, a TypeScript package with no build step, and every recipe is an object literal:
{ slug: 'ginger-chicken-rice-bowl', title: 'Ginger chicken rice bowl', summary: 'Chicken and vegetables cooked gently with fresh ginger, over plain white rice.', serves: 2, minutes: 25, mealTypes: ['lunch', 'dinner'], ingredients: [ { qty: 2, unit: 'item', name: 'chicken breast', namePlural: 'chicken breasts', prep: 'sliced', grams: 230, tags: [...] }, { qty: 150, unit: 'g', name: 'white rice', grams: 150, tags: [...] }, ... ], steps: [...], } 908 lines of that. And a 349 line test file next to it that produces 462 tests and runs in 192 ms.
Writing content as code only pays off if the tests are the reason you did it. Here is what they actually check, and why each one is there.
One test per recipe, not one test per rule
The whole file is built on a four line helper:
/** Every recipe is tested individually, so a failure names the recipe that caused it. */ function forEachRecipe(name: string, check: (recipe: Recipe) => void): void { for (const recipe of RECIPES) test(`${name}: ${recipe.slug}`, () => check(recipe)); } Twelve rules written this way, times 38 recipes, plus six library-wide tests, is where 462 comes from. The alternative (one test that loops inside itself) gives you ✖ every ingredient has a nutrition entry and a stack trace. This gives you ✖ every ingredient has a nutrition entry: tuna-potato-and-green-bean-salad, which is the entire debugging session.
That matters more than it sounds, because of what this file is for. Today the library is hand authored. The package comment says what happens next:
* Generated recipes will land in a sibling `recipes-ai.ts` and be concatenated * here: the model proposes recipes, and `__tests__/recipes.test.ts` decides * which of them are allowed to exist. So this is not a regression suite. It is an admissions policy, written before the applicants arrive.
The checks that catch a human
My favourite is the density check, because it catches a class of error no type system can:
forEachRecipe('written quantities match their weights', (r) => { const ML: Record<string, number> = { tbsp: 15, tsp: 5, ml: 1 }; const MIN_DENSITY = 0.15; const MAX_DENSITY = 1.6; for (const i of r.ingredients) { const ml = ML[i.unit]; if (ml === undefined) continue; const density = i.grams / (i.qty * ml); assert.ok( density >= MIN_DENSITY && density <= MAX_DENSITY, `"${i.name}": ${i.qty} ${i.unit} listed as ${i.grams} g is ${density.toFixed(2)} g/ml`, ); } }); Every ingredient line carries both a written quantity (what you read on the page: "1 tbsp olive oil") and a weight in grams (what everything downstream computes with). The grams are the truth and the written quantity is decoration, which means nothing whatsoever catches an author typing "1 tbsp" next to 200 g. Nothing except this.
Of the 229 ingredient lines, 111 are in spoons or millilitres, so 111 of them get this check. The band is deliberately wide: oil is about 0.9 g/ml, and two tablespoons of chopped chives are about 8 g, which is 0.27. Narrow the band and you start failing honest recipes. What survives the width is the error that actually happens: a misplaced decimal point, or a unit typed as though it were grams. A spoonful recorded at 200 g lands at 13 g/ml and is caught on the spot.
Two smaller ones in the same spirit. The 80 lines written in grams have to agree with themselves (qty must equal grams, because "150 g" and grams: 140 is a typo with two plausible fixes). And the 38 lines measured in items must carry a plural form, because the renderer has to write "2 chicken breasts" and there is no rule for English plurals that survives contact with food.
Water goes in the steps
forEachRecipe('cooking water is not an ingredient line', (r) => { for (const i of r.ingredients) { assert.ok(!isWater(i), 'water belongs in the steps, not the ingredients'); } }); A litre of water for boiling rice is not an ingredient, it is a cooking instruction, and the difference is not philosophical. Put it on an ingredient line and it inflates the weight of the whole batch, which shrinks every other ingredient's share of it. The list would still read correctly to a human and would mean something different to everything that reads it as data.
So water lives in the steps ("cook it in plenty of salted water until tender, about 12 minutes"), and a test makes sure nobody helpfully adds it back.
Scaling is a property test
Every recipe has a serving stepper. Cook for two, cook for six. The rule is that scaling must not change what the food is:
forEachRecipe('scaling does not change what the engine sees', (r) => { const base = toProduct(r); for (const serves of [1, r.serves + 2, r.serves * 3]) { const scaled = toProduct(scaleTo(r, serves)); assert.equal(scaled.serving?.sizeG?.toFixed(6), base.serving?.sizeG?.toFixed(6)); assert.deepEqual(scaled.ingredientsTags, base.ingredientsTags); } }); Everything derived from a recipe is either a ratio or a per-serving figure, so cooking for four is the same food as cooking for two. Three scale factors per recipe, compared to six decimal places. If somebody ever writes a scaling routine that rounds in the wrong place, a reader would see a recipe change character when they pressed a plus button, and that is the sort of bug you cannot talk your way out of.
The floors, which are about empty screens
The app shows a reader only the recipes that suit them. That design choice has a direct consequence for the data: a condition with thin coverage does not produce a short list, it produces nothing at all.
const MINIMUM_PER_CONDITION = 5; const MINIMUM_PER_PAIR = 3; So there is a floor per condition, a floor for every one of the 28 possible pairs of conditions, and one more test that insists there is something to cook even for somebody who has all eight at once. Pairs are where a library quietly runs out: two conditions pulling in different directions can each have fifteen recipes and share two.
There is a second pass over the same floors that I like a great deal and will describe without the numbers. Some of the lines the library is measured against are placeholders, honest ones, written down as "this needs a dietitian to finalise". A library that clears a placeholder by one gram is worthless, because the day the placeholder moves, every recipe still passes its own tests and most of a condition's library silently disappears. So the floors are checked a second time against a deliberately tightened version of each line, and the lines are read from the engine's own exported constants rather than retyped in the test. Moving a number re-runs the check instead of invalidating it quietly.
The words, which are the real liability
Munchable is a wellness app and not a medical device, and recipe copy is the easiest place in the whole product for that line to get crossed, whether by a tired author or a helpful model:
const BANNED = [ /\bcures?\b/i, /\bheals?\b/i, /\bsoothes?\b/i, /\brelieves?\b/i, /\bsymptoms?\b/i, /\banti[- ]inflammatory\b/i, /\bdetox\w*\b/i, ... ]; forEachRecipe('copy makes no health-outcome claims', (r) => { const copy = [r.title, r.summary, ...r.steps]; ... }); Titles, summaries and all 160 steps go through it. "A soothing soup for flare ups" is a sentence a person writes without noticing, and it is a claim about treating a disease. A regex is a crude instrument for prose, and for this particular job it is the right one: the vocabulary of accidental medical claims is small and well known.
A second house-style test in the same loop rejects em dashes and emoji, which is a rule about Munchable's voice rather than about liability, and is exactly the kind of thing that is easier to assert than to remember.
Two renderers, one package
The payoff for keeping all of this as data is that the same 38 objects render twice.
The public pages are fully static (export const dynamic = 'error', nothing to revalidate, since the library ships in the repo) and carry Recipe structured data, so a search engine can read the ingredients and the timings. Try the ginger chicken rice bowl, the herby baked cod or the buckwheat pancakes.
In the phone app the same package is bundled and filtered on the device, which is why that part of the app works with no signal and why the list is personal to the reader without a server knowing anything about them.
Two exported strings keep the two surfaces honest with each other. One is the line at the foot of every recipe, "Checked against this recipe as written. Swap an ingredient or change the portion and the check changes with it." The other sits on the 27 ingredient lines marked packaged: "Brands differ. Scan the one in your cupboard to check what it actually contains." Both are constants in the package, not strings typed into two codebases, because the moment they disagree one of them is wrong.
What I would reuse
Not the recipes. The shape: content as a package, one test per item rather than one test per rule, numeric sanity checks on any field a human types twice, coverage floors expressed as the thing the user would experience (an empty screen) rather than as a percentage, and a copy test for the sentences your domain does not allow you to write.
462 tests in 192 ms is cheap enough to run on every save, which is the only reason any of it gets enforced.
![[Phân tích kỹ thuật] Anthropic ra mắt Code Mode cho Claude Code: Bước ngoặt xâu chuỗi Tool Call bằng JavaScript](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuurfrhh192toa94intza.webp)