Files
ersatztv/web/vite-plugins/trackedSourceFiles.test.ts
T
timothy eedf56b8ad fix(819): close the review's blocker and the blind spot it measured
Three defects found by cold review of 9e5c288db, all reproduced here before
fixing and re-measured after.

BLOCKER — the Docker image build failed on the previous commit.
`docker/Dockerfile`'s web-build stage copies only `web/` and `design-system/`,
so it has no `.git`, and `node:22-bookworm-slim` ships no git binary. Its
`RUN` line ran `npm test -- --run`, which loads the virtual module, which shells
out to git and throws by design. Measured in an exact gitless replica of the
stage: `Test Files 1 failed | 117 passed`, against `117 passed` on the parent.
`build` is gated `needs: [test, migrations, scan]` and the `test` job — which
carries no `if:`, so it runs on PR, main and tag alike — already runs the
identical suite on a real checkout, so the Dockerfile's run was duplicated work
and is dropped rather than propped up with a `.git` copy and a git install. The
previous commit message asserted this build was safe on the strength of having
verified `vite build` alone; the test step on the same line was never measured.

HIGH — a tracked file could still leave the population silently, and one way was
a REGRESSION. `import.meta.glob` matches no dotfile or dot-directory, and on
`core.ignorecase=true` a disk spelling that diverges from the index spelling
(`caseprobe.tsx` vs `CaseProbe.tsx`) is tracked, present, and invisible to the
intersection — measured blind here, and measured CAUGHT by the parent commit.
The stated residual named only the deleted-file case, so the prose claimed a
completeness the code did not have. Fixed by asserting the other direction: the
plugin now reports which tracked paths are absent from disk, and the guard
requires every tracked, on-disk, in-scope path to have reached the scanner. A
mid-edit deletion stays tolerated — measured, still green — because that is
transient, while a hole is now named and red. This is the rule
`scripts/tests/tracked_files.py` already states for the Python derivations:
existence is ASSERTED, never filtered.

MEDIUM — the clause proof was fixture-only. Every clause test drove
`listSourceFilesFrom` with injected inputs, so `... && tracked.size < 10` was
live over the 1-2 entry fixtures and dead over the real 260-path index: the fix
fully regressed, suite green. Measured. Added a liveness test that drives the
REAL glob and REAL index with one synthetic key. That mutation now reddens it,
and disarming the clause outright reddens two named tests.

Also: the plugin refuses instead of guessing when `configResolved` never ran
(a wrong root was a silent wrong answer), its doc comment states the `src/`-only
pathspec so a future caller cannot inherit a truncated population, and git's
stderr is piped so the framed error is the only message.

Docs: `docs/spa-conventions.md` §1 gains the `web/vite-plugins/` layout entry;
`docs/guard-inventory.md`'s hand-written TS/C# list gains the fourth file the
review noted it had already gone stale against, and the row's Notes no longer
overstate the population as "`git ls-files`" when it is the walk intersected
with the index.

refs #819
2026-08-29 05:12:23 +02:00

137 lines
6.2 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
TRACKED_SOURCE_FILES_ID,
type FileExists,
type GitRunner,
resolveTrackedSourceFiles,
trackedSourceFilesPlugin
} from './trackedSourceFiles';
/**
* The population this plugin supplies is what `web/src/api/pageSizeCallSites.guard.test.ts` asserts
* completeness over (#819), so the plugin is verification code and carries its own proof rather
* than inheriting the guard's — `testing.verification-code-needs-its-own-proof`.
*/
const RESOLVED_ID = `\0${TRACKED_SOURCE_FILES_ID}`;
const allPresent: FileExists = () => true;
function runnerReturning(output: string): { run: GitRunner; calls: (readonly string[])[] } {
const calls: (readonly string[])[] = [];
return {
calls,
run: (args) => {
calls.push(args);
return output;
}
};
}
/** Drives `load()` through the plugin's own `configResolved`, as Vite does. */
function loadedBy(plugin: ReturnType<typeof trackedSourceFilesPlugin>, root = '/repo/web'): string | undefined {
const configResolved = plugin.configResolved as (config: { root: string }) => void;
configResolved.call(plugin, { root });
return (plugin.load as (id: string) => string | undefined).call(plugin, RESOLVED_ID);
}
describe('resolveTrackedSourceFiles (#819)', () => {
it("splits git's NUL-separated output into paths, dropping the trailing empty field", () => {
const { run } = runnerReturning('src/a.ts\0src/b.tsx\0');
expect(resolveTrackedSourceFiles('/repo/web', run, allPresent).tracked).toEqual(['src/a.ts', 'src/b.tsx']);
});
it('asks git for NUL-separated output, scoped to src/, in the given root', () => {
// Pins `-z` specifically. Without it git applies `core.quotePath` to a non-ASCII name and
// returns a C-quoted string that no longer equals the path Vite reports, which would drop that
// file from the population silently — the guard would then under-enumerate while looking green.
const { run, calls } = runnerReturning('src/a.ts\0');
resolveTrackedSourceFiles('/repo/web', run, allPresent);
expect(calls).toEqual([['-C', '/repo/web', 'ls-files', '-z', '--', 'src']]);
});
it('keeps a path containing spaces intact rather than splitting on whitespace', () => {
const { run } = runnerReturning('src/screens/My Screen.tsx\0src/b.ts\0');
expect(resolveTrackedSourceFiles('/repo/web', run, allPresent).tracked).toEqual([
'src/screens/My Screen.tsx',
'src/b.ts'
]);
});
it('REPORTS tracked paths with no file on disk rather than dropping them', () => {
// The consumer needs a mid-edit deletion (transient, tolerated) told apart from a tracked file
// the walk cannot see (a real hole). Collapsing them is how a population silently shrinks.
const { run } = runnerReturning('src/present.ts\0src/deleted.ts\0');
const seen: string[] = [];
const exists: FileExists = (path) => {
seen.push(path);
return !path.endsWith('deleted.ts');
};
const result = resolveTrackedSourceFiles('/repo/web', run, exists);
expect(result.tracked).toEqual(['src/present.ts', 'src/deleted.ts']);
expect(result.absentFromDisk).toEqual(['src/deleted.ts']);
// Existence is checked against the ROOT-joined path, not the repo-relative one.
expect(seen).toEqual(['/repo/web/src/present.ts', '/repo/web/src/deleted.ts']);
});
it('THROWS when git fails, instead of degrading to an unfiltered walk', () => {
// The failure this rejects is the fail-open one: answering "could not tell" with the very
// population the index was brought in to replace is a permanent no-op that reports green.
const boom: GitRunner = () => {
throw new Error('not a git repository');
};
expect(() => resolveTrackedSourceFiles('/repo/web', boom, allPresent)).toThrow(/could not read the git index/);
});
it('THROWS when git reports zero tracked files (anti-vacuity at the source)', () => {
const { run } = runnerReturning('');
expect(() => resolveTrackedSourceFiles('/repo/web', run, allPresent)).toThrow(/ZERO tracked files/);
});
});
describe('trackedSourceFilesPlugin (#819)', () => {
it('resolves only its own virtual id', () => {
const plugin = trackedSourceFilesPlugin(runnerReturning('src/a.ts\0').run, allPresent);
const resolveId = plugin.resolveId as (id: string) => string | undefined;
expect(resolveId.call(plugin, TRACKED_SOURCE_FILES_ID)).toBe(RESOLVED_ID);
expect(resolveId.call(plugin, 'react')).toBeUndefined();
expect(resolveId.call(plugin, './pageSizeScan')).toBeUndefined();
});
it('emits the tracked list and the absent set as a default-exported module', () => {
const plugin = trackedSourceFilesPlugin(runnerReturning('src/a.ts\0src/b.tsx\0').run, allPresent);
expect(loadedBy(plugin)).toBe('export default {"tracked":["src/a.ts","src/b.tsx"],"absentFromDisk":[]};\n');
});
it('REFUSES rather than guessing when configResolved never ran', () => {
// A wrong root is a silent wrong answer. Seeding it with `process.cwd()` would happen to work
// in this repo's layout and quietly derive the population from the wrong tree elsewhere.
const { run, calls } = runnerReturning('src/a.ts\0');
const plugin = trackedSourceFilesPlugin(run, allPresent);
const load = plugin.load as (id: string) => string | undefined;
expect(() => load.call(plugin, RESOLVED_ID)).toThrow(/configResolved` hook never ran/);
expect(calls).toHaveLength(0);
});
it('does NOT shell out to git unless the virtual module is actually loaded', () => {
// Constructing the plugin, or loading anything else, must not run git — otherwise registering
// it in vite.config.ts would break every build in a context without a repository.
const { run, calls } = runnerReturning('src/a.ts\0');
const plugin = trackedSourceFilesPlugin(run, allPresent);
const configResolved = plugin.configResolved as (config: { root: string }) => void;
const load = plugin.load as (id: string) => string | undefined;
configResolved.call(plugin, { root: '/repo/web' });
expect(calls).toHaveLength(0);
expect(load.call(plugin, '\0some-other-virtual-module')).toBeUndefined();
expect(calls).toHaveLength(0);
load.call(plugin, RESOLVED_ID);
expect(calls).toHaveLength(1);
});
});