What Should AI-Generated Code Have to Prove?
Exploring property-based testing with fast-check in Leavely.
I recently shared Addy Osmani's article on how the Claude Code team thinks about verification with the coding agents working on Leavely.
One idea stood out: when coding agents can implement changes so quickly, giving them a reliable way to verify their work becomes increasingly important.
I touched on this in an earlier article about why testing matters when AI agents write the code. One of the concerns I explored was how easily the same mistaken assumption can end up in both an implementation and its tests.
This time, I wanted to explore verification from a different angle by introducing property-based testing with fast-check into Leavely.
The result was 19 property tests covering four areas: working-day calculations, leave-year boundaries, overlapping requests, and leave entitlements.
But the interesting part wasn't adding more tests. It was discovering how much a passing test can leave unanswered.
From testing examples to testing properties
Most conventional tests start with a specific scenario and an expected result.
Take a working-day calculation. An employee requests leave from January 1 to January 5, 2024. They work Monday through Friday, and Wednesday is included in the company's holiday list.
The expected result is four working days.
expect(
calculateWorkingDays(
"2024-01-01",
"2024-01-05",
workingDays,
["2024-01-03"]
)
).toBe(4)
There's nothing wrong with this approach. It's readable, deterministic, and verifies a concrete business requirement.
But the function accepts far more combinations than this example covers.
What happens if the employee works weekends? What if the holiday list contains duplicates? What if a request spans several months, crosses a year boundary, or starts in the afternoon?
More examples can cover more scenarios, but someone still has to decide which combinations to write.
Property-based testing approaches the same problem from a different angle.
Instead of asking whether a particular input produces a particular result, it asks whether a rule remains true across many different inputs.
For working-day calculations, one such rule is:
Splitting a full-day leave request into two adjacent ranges shouldn't change its total working-day count.
That relationship should hold regardless of the start date, the length of either part, the working schedule, or the holidays involved.
Here's a simplified version of the property test:
import { fc, test } from "@fast-check/vitest"
import { expect } from "vitest"
const day = (offset: number) =>
new Date(Date.UTC(2000, 0, 1 + offset))
.toISOString()
.slice(0, 10)
const length = fc.integer({ min: 1, max: 14 })
test.prop({
start: fc.integer({ min: 0, max: 15_000 }),
left: length,
right: length,
})(
"splitting a range preserves its count",
({ start, left, right }) => {
const split = start + left
const end = split + right - 1
const count = (a: number, b: number) =>
calculateWorkingDays(day(a), day(b))
expect(
count(start, split - 1) + count(split, end)
).toBe(count(start, end))
}
)
This version uses the default working schedule and no holidays. The actual test in Leavely also generates different working schedules and holiday lists.
The important part is the assertion.
There isn't a hardcoded expected number of working days. Instead, the test checks a relationship between three calculations that should always agree.
fast-check generates different inputs and attempts to find a combination that breaks that relationship.
If it finds one, it can also shrink the failing input, reducing a complex case to a smaller counterexample that's easier to reproduce and understand.
That makes property-based testing particularly useful for functions with large input spaces and rules that can be expressed independently of a specific example.
When passing tests didn't mean everything was correct
The first interesting finding came from leave-request overlap checks.
Leavely supports both full-day and half-day bookings. Two requests can cover the same calendar date without necessarily conflicting. Someone might book the morning and then book the afternoon separately.
The initial property tests for overlapping requests passed.
But reviewing the inputs those tests considered valid revealed an inconsistency elsewhere in the booking flow.
A multi-day leave request could be shortened to a single day while retaining incompatible internal start and end periods. The form displayed "Full Day", but the underlying state could still contain a combination such as FULL/MORNING.
That created a disagreement between different parts of the application.
The calendar treated the request as a full day, while the working-day calculation charged only half a day.
The property tests didn't directly fail on this bug.
Instead, examining their assumptions led to three concrete example-based tests that reproduced the incorrect behavior.
The fix normalized the form state and enforced consistent rules in the relevant validation schemas. A database check at the time found no affected stored requests.
But verification didn't stop when the tests passed.
After the fix, the booking flow was also checked in a running browser. A multi-day request starting in the afternoon was shortened to a single day. The form switched to "Full Day", and both period values prepared for submission were full. The request was intercepted before reaching the server.
This was an important check because the original bug was exactly that disagreement: what the interface displayed wasn't necessarily what the application was about to submit.
That distinction matters.
It would be tempting to describe this as a bug discovered by property-based testing. More precisely, the process of designing and reviewing those tests exposed an assumption that hadn't been examined closely enough.
The generated tests helped investigate the domain. Conventional regression tests captured the specific failure. The browser check confirmed how the corrected behavior appeared in the actual user flow.
Each provided a different kind of evidence.
When four tests agreed on the wrong answer
A second finding was even more revealing.
Leavely lets companies configure when their leave year begins. Some use the calendar year, others use a fiscal year, and some choose a custom start date.
The initial leave-year properties checked several reasonable expectations:
- The returned leave year contains the requested date.
- Each leave year lasts 365 or 366 days.
- The first and last dates belong to the same leave year.
- The next leave year begins immediately after the previous one.
All of those properties passed.
During review, the implementation was deliberately replaced with a version that always returned January-to-December leave years, regardless of the company's policy.
All four properties still passed.
The modified implementation was internally consistent. Its dates lined up, its years had valid lengths, and the functions agreed with each other.
It just ignored the company's actual configuration.
A fifth property was added to check the returned start date directly against the policy's month and day.
That test failed against the deliberately incorrect implementation.
The new assertion also covers a less obvious case: a company whose leave year starts on February 29. In Leavely, that policy falls back to February 28 in non-leap years.
This wasn't a newly discovered production regression. It was a weakness in the verification itself, exposed by intentionally introducing an incorrect implementation.
And it demonstrates something important:
Tests can agree with each other and still be wrong.
When multiple checks rely on the same assumption, they can reinforce an incorrect result rather than challenge it.
An independent expectation matters, especially when the purpose of the test is to establish correctness rather than internal consistency.
The generators need testing too
Another lesson came from the test inputs themselves.
After the half-day booking rules were fixed, one of the working-day properties started failing.
The issue wasn't an incorrect working-day calculation. The generator was still producing a combination that the updated application rules no longer allowed: a multi-day request starting in the morning.
fast-check reduced the failing case through shrinking, making the disagreement easier to inspect.
The generator was then constrained to valid period combinations.
That solved the immediate problem, but introduced an important trade-off.
The property tests now depend on a shared helper to decide which inputs are valid. If that helper is wrong, the generator might silently exclude exactly the cases worth testing.
That helper has separate example-based tests, but the broader point remains:
The quality of a property test depends on the properties and the inputs it generates.
Randomness alone doesn't make a test useful. The generated values need to exercise the meaningful boundaries of the system without silently avoiding difficult cases.
Property-based testing doesn't replace conventional tests
Consider the earlier range-splitting property:
expect(firstPart + secondPart).toBe(joined)
A function that always returns zero would pass it.
Zero plus zero is still zero.
So although the property verifies consistency, it doesn't establish that the calculation is correct.
That's why the working-day suite also includes a property checking the expected result for a single day: one if the date is a scheduled working day and not a holiday, zero otherwise.
Conventional example-based tests still verify specific expected outputs.
The same distinction appears in leave entitlements.
Property tests check that entitlements remain within allowed bounds, that proration behaves consistently, and that starting employment later doesn't increase the allowance under a nondecreasing seniority policy.
But a conventional test still verifies an exact entitlement of 19 days for a particular midyear seniority change.
Each type of test answers a different question.
Example-based tests verify known expectations. Property-based tests explore whether broader rules survive varied inputs. End-to-end tests check whether the pieces work together through actual user journeys.
None provides complete assurance on its own.
Verification is more than adding tests
In Leavely's current setup, fast-check runs 100 successful generated cases per property by default.
Across 19 properties, that's 1,900 generated executions in a passing run, although some inputs may repeat. More intensive runs can explore additional cases.
It's useful coverage, but the number is not especially meaningful without understanding what's being checked.
A thousand generated inputs testing a weak property may tell less than one carefully chosen example.
And a passing property can still miss a bug if its generator never produces the problematic input.
How these tests fit into the development workflow matters too.
At the time, Leavely's AGENTS.md required a test plan in the Linear issue before writing tests, with each test layer tied to the behavior it was supposed to verify. The plan had to be agreed before implementation and included in the PR's Evidence section.
For these four pure functions, unit testing was the appropriate layer.
An agent could run an individual property-test file locally using:
bun run test --run <path>
The existing Vitest CI job ran the complete suite with:
bun run test --run
But one of the most useful instructions added to docs/testing.md was about what happens when a property fails.
Rather than running the test again with fresh random inputs and hoping for a pass, the agent should replay the same counterexample using the seed and path reported by fast-check.
The next step is to understand why the failure occurred. Was it an implementation defect, an incorrectly defined property, or an invalid generated input?
When a real defect is fixed, the counterexample should be preserved as a deterministic regression test.
That way, a failure discovered through generated inputs becomes a permanent check against the same behavior breaking again.
I think there's an important difference between giving an agent a command to run tests and giving it a process for interpreting test failures.
The first lets it use a tool. The second helps it act on the evidence that tool produces.
For me, that's the most useful connection back to the original Anthropic article.
Coding agents make it easier to implement changes, generate tests, and explore more scenarios. But those capabilities don't remove the need to decide what correct behavior actually means.
What must remain true when the inputs change?
Which states should the application allow?
Does the test have an independent expectation, or does it simply reproduce the implementation's assumptions?
And when everything passes, what remains untested?
Introducing property-based testing turned out to be worthwhile, not because 19 new tests prove Leavely is correct, but because the exercise exposed assumptions about its business rules and even about the tests themselves.
A green test suite is useful. Understanding what that green result actually means is even more valuable.