Expectations
Say what must always be true. When it doesn't hold, it's a problem with a test target, even though nothing crashed.
Most wrong results never throw. A total comes out negative, an order ships with no items, a discount is bigger than the price, and the app carries on. An expectation says what must always be true, so Controxity sees it when it isn't.
controxity.expect("total-not-negative", total >= 0, { input: { cart } });controxity.expect("total-not-negative", total >= 0, input={"cart": cart})When the condition holds, the app carries on and it's counted. When it doesn't:
- Nothing is thrown: the app goes on as it would have.
- It's a problem, like an error: titled "total-not-negative" didn't hold, with where it was checked, how often, on which places and versions, and the shape of the input when you pass one.
- The problem says how rare it is: it broke 3 of the 12,400 times it was checked in the last 7 days. Under it, a small chart shows the breaks each day for the last 7 days (hover a day for its checks), so once a fix ships you can watch the breaks go to zero while the checks go on.
- The fixer makes it hold where the wrong value comes from, with a test that asserts it. It never fixes one by removing or loosening the check.
Every expectation also shows on the product's page, under Expectations, broken or not: how often it held and broke these 7 days on production and on staging, a bar for each day's breaks, when it was last checked, and a link to its problem if it broke. One not checked for two days or more is marked still in the code?: it may be dead code, or its name may be misspelt.
It returns the condition, so it can guard too:
if (!controxity.expect("has-items", cart.items?.length > 0, { input: { cart } })) return emptyCart();Names#
A name is a short code: letters, digits, and - _ . : /. Each name is its own problem, wherever it's checked. Name what must be true, not what went wrong: total-not-negative, paid-orders-have-a-receipt, stock.never-below-zero.
Good ones to write#
- Money and counts: totals, balances, stock, quantities that can't go negative or past a limit.
- What every result must have: an order with a customer, a receipt for a payment, a reply with an id.
- Sums that must agree: the lines add up to the total, the refunds don't exceed the charge.
- States that can't happen: shipped but not paid, cancelled but still billed.
Your coding agent is good at writing these: ask it for the expectations a function should keep, then let Controxity watch them in production.
What's sent#
When it breaks: the name, where it was checked (file, line, function), the count, and the shape of input. With each send, about once a minute: each name with how many times it held and broke. Never the values involved. Counting needs Node SDK 0.14.0 or Python SDK 0.11.0; older ones send only the breaks. See What never leaves your server.