Skip to Content
Meubles Morinière

Engineering choicesTechnical case studies8 min read

Tests that read the code in place of the reviewer

A server action with no guard, a write that forgets to expire its cache, a state overwritten by a concurrent action. An ordinary test doesn’t see any of these mistakes. So architecture rules became tests that read the source code and block the push.

The problem

Some of the project’s rules can’t be checked by calling a function.

The first one is server actions. A "use server" file exports functions, and Next.js makes them callable through an HTTP request. The test reminds whoever forgets it: “Every export of a "use server" file is a POST endpoint” (apps/erp/test/admin-guard.regression.test.ts). So an action written without its guard doesn’t check the session of whoever calls it.

The second one is the cache. An action that changes what the storefront displays has to expire the pages concerned. If it forgets, nothing breaks: “the page just stays old” (8100972291).

The third one is states. An order only moves to “paid” if it was still pending at the moment of the write (Data). A write that sets the state without that condition works on every try. It goes wrong the day a redelivered webhook crosses an action made in the store.

These mistakes pass the compiler and the tests of the nominal case. They used to be found in review. Except that with AI the code moves fast, and review alone no longer catches them (My contribution and AI).

The constraints

  • The check has to run without me, at every push, and finish quickly. So it can’t need a database or the network.
  • Whoever breaks the rule is often a session that doesn’t know why the rule exists. So the failure message has to say what to do.
  • There are legitimate exceptions. Signing out acts without refusing anyone, and an action that changes nothing visible has no cache to expire.
  • Several rules link two files: a cache tag read here and expired there, a database constraint and its refusal test, a scheduled task and its route.

The alternatives

  • Reviewing. That’s what these tests replaced. The commit of August 31, 2026 says so: “Three scans replace the review” (8100972291).
  • Writing custom ESLint rules. ESLint already guards the boundaries between layers (ADR 0006). But it judges one file at a time, and several of these rules compare two. It’s a question a reader would ask. No record shows it was weighed.
  • Going through the type system, so the mistake can’t be written. The project does that when it can. For example, the guide of the morning e-mail is typed on the keys of its sections, so a section with no guide doesn’t compile (MORNING_GUIDES). But a type says nothing about an action someone wrote without going through it.
  • Analysing the syntax tree rather than the text. It’s more exact, but it takes longer to write. Only one of these tests does it (apps/erp/test/deferred-block-boundaries.regression.test.ts). The others read the text.

The decision

When a rule matters and you can’t prove it by running the code, it becomes a test. That test reads the repository’s source code and fails on every departure. It carries its exceptions with their reason, it checks that it did read something, and its message says what to do.

The implementation

An empty scan fails

A test that walks through files and finds none turns green. So the list of files comes from git, and it refuses to serve below a floor. A prefix that no longer matches any file fails the same way (repoFilesUnder). On top of that, most scans start with a case that counts what they found.

repoFilesapps/erp/test/support/repo-files.ts · lines 9–36
export function repoFiles(): readonly string[] {
	const out = execFileSync(
		"git",
		["ls-files", "--cached", "--others", "--exclude-standard", "-z"],
		{
			cwd: ROOT,
			encoding: "utf8",
			maxBuffer: 64 * 1024 * 1024,
		},
	);

	const listed = out
		.split("\0")
		.filter((path) => path !== "")
		.sort();

	if (listed.length < NOT_VACUOUS_FLOOR) {
		throw new Error(
			`\`git ls-files\` n'a rendu que ${listed.length} fichier(s), sous le plancher ` +
				`de ${NOT_VACUOUS_FLOOR}. Les scans de doctrine qui lisent cette liste seraient ` +
				"tous VERTS sans avoir rien lu. Causes usuelles : un clone partiel " +
				"(`--filter`, `sparse-checkout`), un `cwd` hors du dépôt, ou `git` absent " +
				"du PATH. Le plancher ne se baisse pas : c'est l'énumération qu'il faut réparer.",
		);
	}

	return listed.filter((path) => existsSync(resolve(ROOT, path)));
}

Written exceptions, which have to melt away

The guard scan reads every action file. It requires each export to start with the guard that refuses. The exceptions are in two lists. PUBLIC_ACTIONS names the actions open without a session, and today it’s empty. GUARDED_WITHOUT_REFUSAL names the two sign-out actions.

These lists are checked in both directions. If an exception is declared and its file has disappeared, the test fails. If an action declared public now calls the guard, it fails too, and it asks you to remove its line: “an allowlist that does not melt is lying”.

test “chaque export de %s s'ouvre sur la garde qui refuse, ou le fichier est PUBLIC”apps/erp/test/admin-guard.regression.test.ts · lines 80–86
it.each(actionFiles)(
	"chaque export de %s s'ouvre sur la garde qui refuse, ou le fichier est PUBLIC",
	(relativePath) => {
		const source = readFileSync(resolve(repoRoot, relativePath), "utf8");
		expect(actionFaults(relativePath, source, regimeOf(relativePath))).toEqual([]);
	},
);

A worked example: state transitions

The transition scan starts by reading the Prisma schema. It looks for the models that have a column whose type ends in Status. So a new model with a state enters the check without anyone touching the test.

Then it lists every update, updateMany and upsert on these models, in the ERP’s modules and shared code, and it applies four rules. A state column set in data has to be expected in the where of an updateMany. A plain update is only allowed for the files of BARE_UPDATE_ALLOWED, each with its written reason, and it sets no state column. An upsert is only allowed through a second list, empty today (UPSERT_ALLOWED). And a raw SQL write on these models is rejected, because the scan couldn’t read the condition in it.

test “%s — une colonne d'état posée dans data est attendue dans le where d'un updateMany”apps/erp/test/state-transitions.regression.test.ts · lines 179–223
it.each(sites.map((site) => [`${site.path}:${site.line}`, site] as const))(
	"%s — une colonne d'état posée dans data est attendue dans le where d'un updateMany",
	(_label, site) => {
		if (site.method === "upsert") return;
		const data = propertyLiteral(site.call, "data");
		const columns = stateColumns[site.model] ?? [];
		const transitions = columns.filter(
			(column) => data !== undefined && mentionsColumn(data, column),
		);
		if (data === undefined && site.method === "update" && BARE_UPDATE_ALLOWED.has(site.path))
			return;
		if (data === undefined) {
			const outsideWhere = site.call.replace(propertyLiteral(site.call, "where") ?? "", "");
			expect(
				columns.some((column) => mentionsColumn(outsideWhere, column)),
				`${site.path}:${site.line} construit son \`data\` hors littéral : écrire \`data: { … }\` ` +
					"sur place pour que la transition se lise",
			).toBe(false);
			return;
		}
		if (transitions.length === 0) return;

		expect(
			site.method,
			`${site.path}:${site.line} écrit ${transitions.join(", ")} par un \`update\` sec : un ` +
				"écrivain concurrent (webhook relivré, opérateur) se fait écraser. Passer par " +
				"`updateMany` avec l'état attendu dans le `where`, et juger `count`.",
		).toBe("updateMany");

		const where = propertyLiteral(site.call, "where");
		expect(
			where,
			`${site.path}:${site.line} écrit ${transitions.join(", ")} sans \`where\` littéral : ` +
				"la condition de transition doit se lire sur place",
		).toBeDefined();
		for (const column of transitions) {
			expect(
				mentionsColumn(where ?? "", column),
				`${site.path}:${site.line} pose \`${column}\` sans l'attendre dans le \`where\` : c'est ` +
					"un lire-puis-écrire — la transition n'est pas conditionnelle, et deux " +
					"écrivains concurrents la rejouent l'un sur l'autre",
			).toBe(true);
		}
	},
);

This scan also imposes a way of writing. It reads the text of the call, so the data object has to be written in place. If the call names a state column and data isn’t an object written at that spot (a condition, a composed object), the test rejects it. It asks for it to be written in place “so that the transition can be read”.

Pairing in both directions

Several scans compare two lists and reject what’s missing on either side.

  • Every ERP action is in one of two lists: those that expire a cache, and those that expire none, with their reason. The scan checks that the two lists cover every action in the repository, because one invalidation too many costs as much as one forgotten (apps/erp/test/cache-invalidation.regression.test.ts). A second scan checks that every tag that’s read is expired by at least one write (apps/erp/test/silent-failure-harness/cache-tag-pairing.test.ts).
  • Every CHECK constraint in the migrations has its refusal case in the suite run on Postgres (Quality).
  • Every anomaly reading is wired into the morning check, and the check only wires readings that exist (A morning check that says what it could not read).
  • Every query of the storefront has its right in the list of the Postgres role (The storefront only has the rights it needs).

Checking the check

A badly written pattern finds nothing and lets everything through. So the scans try their patterns on examples: a compliant case, and the mistakes they have to see.

test “le motif voit l'absence de l'ordre (garde-fou du garde-fou)”apps/erp/test/variant-lock-order.regression.test.ts · lines 54–58
it("le motif voit l'absence de l'ordre (garde-fou du garde-fou)", () => {
	expect(ORDERED_ITEMS.test(`items: { orderBy: { variantId: "asc" }, select: {} }`)).toBe(true);
	expect(ORDERED_ITEMS.test(`items: { select: { variantId: true } }`)).toBe(false);
	expect(ORDERED_ITEMS.test(`items: { orderBy: { variantId: "desc" } }`)).toBe(false);
});

When a scan runs into a form it can’t read, it fails. The guard scan rejects an export whose form it doesn’t recognise. It asks you to write it differently, or to teach the scan that form. The transition scan throws an error on a call whose end it can’t find.

At every push

The pre-push hook runs all these tests for the ERP and then for the storefront, in the worktree of the pushed commit (vitest run test/ regression, .husky/pre-push). They don’t need a database or the network (Git checks).

The results

regression tests, most of which read the source code and reject any departure from a rule
92
apps/*/test/*.regression.test.ts
Measured in the repository at each build of this documentation.

This figure is the number of *.regression.test.ts files. They don’t all read the source code. On November 16, 2026, out of the 92 files of the two applications, 72 read files from the repository. The other 20 execute the code to pin down a fixed bug, for example an invoice rendered as a PDF whose text is read back. To count them, I searched each file for the functions that read the repository (readFileSync, readRepoFile, repoFiles, serverActionFiles).

The guard scan dates from August 30, 2026 (7203bc1fe8), and the cache scans from the following day (8100972291).

The latest failure is from November 16, 2026. A new action, which orders the home page showcase, did expire the catalogue. But it wasn’t in the scan’s list, so the test failed. The fix is one line (8386b0ad1e). The example also shows what the rule costs: you have to enter every new action by hand in one of the two lists.

An instruction written for the sessions can become one of these tests. “Every deferred block has its ReadFailureBoundary” is a loading rule I asked for on October 22, 2026 in AGENTS.md. And deferred-block-boundaries.regression.test.ts checks that every RevealSuspense does sit under its own.

The limits

  • These tests read text with patterns. So the code has to be written in the form they can read, and a new way of writing something makes them fail when there’s no mistake. The other way round, a pattern that’s too narrow lets through a mistake nobody had thought of, because checking the check only tries the cases that were foreseen.
  • The transition scan has a known hole. If the data of an updateMany is built entirely in a variable, higher up in the service, the call names no state column. So it passes without being judged.
  • A scan proves that a call is there. It doesn’t prove that the guard refuses correctly, or that the right tag is expired for the right reason. You still need behaviour tests alongside.
  • The lock-order scan only watches three named writes, the checkout, the batch count and the in-store sale. A new write that locks variants escapes it (Data).
  • The lists are maintained by hand. And these tests are code to reread too when a rule changes (An ERP too heavy to maintain).
  • They’re only useful if the gate runs them. git push --no-verify skips them (What is not covered).

On this page