Ten properties of our profit calculator held across 20,000 random nights each, and the eleventh breaks at 3.5e306
fast-check has been in our package.json for months. Nothing imports it. This week I pointed it at one module to see whether it earns its place, and the answer turned out to be more interesting than "yes" or "no". The mo
fast-check has been in our package.json for months. Nothing imports it. This week I pointed it at one module to see whether it earns its place, and the answer turned out to be more interesting than "yes" or "no".
The module is the arithmetic behind a free tool on our site: the quiz night profit calculator. You put in teams, entry fee, bar spend, margin, prize and host costs, and it gives you profit per night, profit over a year of weekly quizzes, and the number of teams at which the night stops losing money. It is 114 lines of pure functions, it has 8 hand-written example tests, and it makes no network requests, so it is a clean target.
I wrote the properties as throwaway tests, ran them, and deleted the file. Nothing below changed the repository.
Why properties instead of more examples
An example test says "ten teams of four at two pounds gives this number". It is good at proving you understood the case you thought of. A property says something that must be true of every input, and the generator goes looking for the input you did not think of.
The calculator suits this because its rules are about relationships. More teams cannot make the night worse. Doubling every price must double the profit. The break-even answer must be the first team count that stops losing, not just some team count that does.
The eleven properties
Each one ran against 20,000 generated inputs with a fixed seed, so the numbers are repeatable. The generator produced realistic ranges first: 0 to 60 teams, 1 to 8 players per team, money from 0 to 500, percentages from 0 to 100.
| Property | Result |
|---|---|
| Profit over a year is exactly 52 times profit per night | holds |
| Entry kept plus prize paid out equals entry taken | holds |
| Adding a team never lowers the night's profit | holds |
| Break-even teams is the first count that is not losing (and null only if 60 teams still loses) | holds |
| Doubling every money input doubles the profit | holds |
| If regulars cover the whole room, extra bar profit is exactly zero | holds |
| One more regular never raises profit | holds |
| An empty room earns nothing from entry or bar | holds |
| Negative inputs behave exactly like zero (or clamped to 0 to 100 for percentages) | holds |
| Money inputs up to a million and 1,000 teams give finite output | holds |
| Any double at all, including huge ones, gives finite output | fails |
Ten of eleven held. That is the answer the module's author hoped for: the bar-profit rule (the six regulars who would have been in anyway are not the quiz's doing) and the clamping both survive 20,000 random nights each.
One thing the properties told me that the example tests did not: profit is monotone in the number of teams. The code finds break-even by looping over 60 integers and its comment says that is because the line has a kink where the regulars run out. The kink is real, but the function is still non-decreasing across it, so a binary search would have been correct too. The loop is a fine choice. I mention it because a property that holds is also a licence to simplify later, and now that licence is written down.
The eleventh
I gave the last property no ranges at all: any double the generator can produce. It failed after five tests. fast-check shrank the failure automatically to this:
{ teams: 0, playersPerTeam: 0, entryFee: 0, entryToPrizePercent: 0,
spendPerPlayer: 0, marginPercent: 0, regularsAnyway: 0,
barTabPrize: 3.457102182427531e+306, hostFeePerNight: 0, softwarePerMonth: 0 }
That is a bar tab prize of about 3.5 times ten to the 306th power and every other field zero. The per-night figure is finite. The yearly one is 52 times larger, and the largest number a double can hold is about 1.8 times ten to the 308th, so it overflows to negative infinity.
I checked the boundary by hand:
| Bar tab prize | Profit per year |
|---|---|
| 1e300 | about -5.2e301 |
| 3e306 | -1.56e308 |
| 3.5e306 | -Infinity |
Does it matter in a browser?
It cannot matter to a person with a real bar. But it can be reached, so I checked it on the live page rather than assume the input box stops it.
The inputs are type="number", and the max attribute is only a hint to the spinner; a typed value is not refused. I opened the calculator, typed 1e307 into "Bar tab or other prize (£)", and read the results panel:
-
Per night: a 411-character figure beginning
-£4,000,000,... -
Over a year of weekly quizzes:
-£∞ -
Teams to break even:
Never
None of that crashes. The page renders, it says a quiz with a prize worth ten to the 307th pounds loses money, and it is right. The ∞ is the formatter doing its best with infinity.
You can reproduce it in under a minute. Open the calculator, click into the bar tab field, type 1e307, and look at the second figure. If you see -£∞, you have found the same overflow I did.
I wrote about bad input on this tool once before, from the other direction: I typed absurd numbers until something looked wrong. That found the symptom. The property run found the cause from one line of generator, with a minimal counterexample I did not have to guess. That is the real difference, and it is the argument for keeping a property test around for pure modules.
What I would and would not do about it
I would not add a guard for this. Nobody runs a pub quiz with a prize that costs more than the observable universe, and a clamp on every field adds code that exists to hide a number nobody will type. If the page ever needs to be defensible against it, the one-line fix is to refuse non-finite results in the component and show "check your numbers" instead of ∞.
What I would do is keep the ten properties that held. They are cheap, they run in a few seconds, and each one is a sentence about the product that a future edit could quietly break. "Adding a team never lowers the night's profit" is the sort of claim that stops being true the day someone adds a venue-capacity cap.
What I would copy
- Write realistic generators first and wild ones second. Realistic ranges tell you the logic is right. Wild ranges tell you where the types end. They are different questions and deserve different tests.
- Fix the seed. A property test that finds a bug once and never again is a flaky test. With a seed the failure is a fact.
- A dependency nobody imports is a free experiment. I did not have to install anything. The tool was already in the lockfile.
- Do not treat the uniform generator as a survey. Across 20,000 uniformly random inputs, 71 per cent never broke even within 60 teams. That is a statement about my generator, which draws a host fee up to £500 as often as £5, not about pubs.
There is a free tier that needs no card if you want to try running your own quiz night, but the calculator needs no account at all, and nothing you type leaves the browser.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.