Files
civitai__civitai/scripts/__tests__/test-cache-e2e.test.ts
Justin Maier 75312960da fix(test-cache): stop three id shapes from making a test permanently uncacheable
A happy-dom test goes through the web transform, so a node builtin it imports
resolves to `__vite-browser-external:crypto` — an id no path can name. The key
fingerprinted it as a file, got `missing`, and the reporter read that as a module
deleted during the run. All 44 happy-dom test files in the repo were recorded on
no run and re-ran on every one. Vite virtual ids (a `\0` prefix, a plugin scheme)
fail the same way, so the rule is written for the shape, not the one spelling.

`src/hooks/useDateLocale.ts` imports `dayjs/locale/${tag}.js`, which the
computed-import rule treats as opaque — but whatever it computes resolves under
node_modules, which the lockfile already covers. Narrowed to a template whose
literal head is a bare specifier, excluding `@civitai/*` (a workspace symlink
into packages/), any head carrying a `:` (`node:${mod}` can be child_process)
and `./` or `~/`. That one file made another 44 test files uncacheable.

`toRel` also THREW on a POSIX `file://` id on Windows, where fileURLToPath raises
ERR_INVALID_FILE_URL_PATH. That is red on main today, in this file's own POSIX
invariant test, on every Windows run.

The e2e gains a happy-dom fixture so the control is end to end rather than a
predicate assertion: reverting the first fix fails it with
`expected { recorded: 1, notRecorded: { "imported module gone": 1 } } to deeply
equal { recorded: 2, notRecorded: {} }`.

Verified: full unit suite 1927 passed | 3 skipped (1930), exit 0, 0 false skips;
typecheck 0 errors; test:lint-rules 47 files, 660 tests.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-19 23:02:38 -06:00

59 lines
2.3 KiB
TypeScript

import { spawnSync } from 'child_process';
import { mkdtempSync, readFileSync } from 'fs';
import { tmpdir } from 'os';
import { join, resolve } from 'path';
import { describe, expect, it } from 'vitest';
/**
* The one test that runs the cache for real: vitest, the real sequencer, reporter and fs tracker,
* over a one-file fixture — twice. Unit tests with fake vitest objects stayed green through two
* regressions that made the cache record NOTHING on the real repo, because the fakes modelled
* neither the snapshot probe every file makes nor a setup closure mentioning 'cluster'. This is
* what reddens when the cache is inert.
*/
const repo = resolve(__dirname, '../..');
const vitestBin = join(repo, 'node_modules/vitest/vitest.mjs');
const config = 'scripts/test-cache/__e2e__/vitest.e2e.config.mts';
const reporter = join(repo, 'scripts/test-cache/reporter.mjs');
function runOnce(cacheDir: string) {
const r = spawnSync(
process.execPath,
[vitestBin, 'run', '--config', config, '--reporter=default', `--reporter=${reporter}`],
{
cwd: repo,
encoding: 'utf8',
// Bounded: a wedged child fails this test with a timeout message, never hangs the runner.
timeout: 120_000,
env: {
...process.env,
CI: '',
CIVITAI_TEST_CACHE: 'on',
CIVITAI_TEST_CACHE_DIR: cacheDir,
CIVITAI_TEST_CACHE_SAMPLE: '0',
},
}
);
const ledger = readFileSync(join(cacheDir, 'ledger.jsonl'), 'utf8').trim().split('\n');
return { status: r.status, last: JSON.parse(ledger[ledger.length - 1]) };
}
describe('the cache, run for real', () => {
it('records a passing file, then skips it when nothing changed', () => {
const cacheDir = mkdtempSync(join(tmpdir(), 'test-cache-e2e-'));
const cold = runOnce(cacheDir);
expect(cold.status).toBe(0);
// Both fixtures, one of which is a happy-dom file: that one resolves a node builtin to a vite
// virtual id, and a key that treats such an id as a path records nothing for it.
expect({ recorded: cold.last.recorded, notRecorded: cold.last.notRecorded }).toEqual({
recorded: 2,
notRecorded: {},
});
const warm = runOnce(cacheDir);
expect(warm.status).toBe(0);
expect({ ran: warm.last.ran, skipped: warm.last.skipped }).toEqual({ ran: 0, skipped: 2 });
}, 300_000);
});