ci:quick: browser suites only for nearby imports (full ci runs them always)

This commit is contained in:
CRTkafa 2026-10-12 05:08:23 +08:00
parent 5e1357116d
commit 44db357b73
3 changed files with 26 additions and 17 deletions

View file

@ -52,6 +52,12 @@ describe('tools/ci-plan: related tests (ci:quick)', () => {
expect(GLOBAL.test('bunfig.toml')).toBe(true); expect(GLOBAL.test('bunfig.toml')).toBe(true);
expect(relatedTests([['docs/nobody', 'reads.md'].join('-')], all)).toEqual([]); expect(relatedTests([['docs/nobody', 'reads.md'].join('-')], all)).toEqual([]);
}); });
test('a browser suite runs for its own module and its near imports, not for every shared core module', () => {
expect(relatedTests(['cli/lib/upsert.ts'], all, undefined, HEAVY)).not.toContain('tests/console-fill.test.ts');
expect(relatedTests(['cli/lib/upsert.ts'], all)).toContain('tests/console-fill.test.ts');
expect(relatedTests(['cli/console/fill.ts'], all, undefined, HEAVY)).toContain('tests/console-fill.test.ts');
expect(relatedTests(['tests/capture-web.test.ts'], all, undefined, HEAVY)).toEqual(['tests/capture-web.test.ts']);
});
test('Rust steps only for the Tauri crate or the sidecar build, or with CI=full / unknown diff', () => { test('Rust steps only for the Tauri crate or the sidecar build, or with CI=full / unknown diff', () => {
expect(rustNeeded(['cli/index.ts'], {})).toBe(false); expect(rustNeeded(['cli/index.ts'], {})).toBe(false);
expect(rustNeeded(['apps/desktop/src-tauri/src/main.rs'], {})).toBe(true); expect(rustNeeded(['apps/desktop/src-tauri/src/main.rs'], {})).toBe(true);

View file

@ -123,30 +123,30 @@ export function depsOf(file: string, root = ROOT, src = readFileSync(join(root,
/** /**
* Tests related to the changed files: a changed test itself, every test that imports a changed file (transitively), * Tests related to the changed files: a changed test itself, every test that imports a changed file (transitively),
* and every test that names a changed non-code file (fixture, page, skill) by its file name or its fixture folder. * and every test that names a changed non-code file (fixture, page, skill) by its file name or its fixture folder.
* A change to a GLOBAL file returns every test. * A change to a GLOBAL file returns every test. Files in `near` (the browser suites, by default none) count as
* related only through imports at most NEAR_DEPTH steps away: nearly every module reaches cli/lib/config.ts and the
* store clients, and ci:quick should not start a browser for every change there (the full ci runs them).
*/ */
export function relatedTests(changed: string[], tests: string[], root = ROOT): string[] { export const NEAR_DEPTH = 2;
export function relatedTests(changed: string[], tests: string[], root = ROOT, near: string[] = []): string[] {
if (changed.some(f => GLOBAL.test(f))) return tests; if (changed.some(f => GLOBAL.test(f))) return tests;
const changedSet = new Set(changed); const changedSet = new Set(changed);
const memo = new Map<string, Set<string>>();
const direct = new Map<string, string[]>(); const direct = new Map<string, string[]>();
const depsCached = (f: string) => { const depsCached = (f: string) => {
let d = direct.get(f); let d = direct.get(f);
if (!d) { try { d = depsOf(f, root); } catch { d = []; } direct.set(f, d); } if (!d) { try { d = depsOf(f, root); } catch { d = []; } direct.set(f, d); }
return d; return d;
}; };
const closure = (f: string): Set<string> => { /** Every file a test reaches, with its import distance. */
const hit = memo.get(f); const closure = (f: string): Map<string, number> => {
if (hit) return hit; const depth = new Map<string, number>([[f, 0]]);
const seen = new Set<string>([f]); const queue = [f];
memo.set(f, seen); while (queue.length) {
const stack = [f]; const cur = queue.shift()!;
while (stack.length) {
const cur = stack.pop()!;
if (!/\.(ts|tsx)$/.test(cur)) continue; if (!/\.(ts|tsx)$/.test(cur)) continue;
for (const d of depsCached(cur)) if (!seen.has(d)) { seen.add(d); stack.push(d); } for (const d of depsCached(cur)) if (!depth.has(d)) { depth.set(d, depth.get(cur)! + 1); queue.push(d); }
} }
return seen; return depth;
}; };
const names = changed.filter(f => !/\.(ts|tsx)$/.test(f) || f.includes('/fixtures/')).flatMap(f => { const names = changed.filter(f => !/\.(ts|tsx)$/.test(f) || f.includes('/fixtures/')).flatMap(f => {
const base = f.split('/').pop()!; const base = f.split('/').pop()!;
@ -155,10 +155,11 @@ export function relatedTests(changed: string[], tests: string[], root = ROOT): s
}); });
return tests.filter(t => { return tests.filter(t => {
if (changedSet.has(t)) return true; if (changedSet.has(t)) return true;
const deps = closure(t); const all = closure(t);
if (changed.some(c => deps.has(c))) return true; const max = near.includes(t) ? NEAR_DEPTH : Infinity;
if (changed.some(c => all.has(c) && all.get(c)! <= max)) return true;
if (!names.length) return false; if (!names.length) return false;
const srcs = [...deps].filter(d => d.startsWith('tests/') || d.startsWith('apps/desktop/tests/')).map(d => { const srcs = [...all.keys()].filter(d => d.startsWith('tests/') || d.startsWith('apps/desktop/tests/')).map(d => {
try { return readFileSync(join(root, d), 'utf8'); } catch { return ''; } try { return readFileSync(join(root, d), 'utf8'); } catch { return ''; }
}).join('\n'); }).join('\n');
return names.some(n => srcs.includes(n)); return names.some(n => srcs.includes(n));

View file

@ -46,8 +46,10 @@ function addTests(files: string[]) {
} }
if (quick) { if (quick) {
const related = changed === null ? tests : relatedTests(changed, tests); const related = changed === null ? tests : relatedTests(changed, tests, ROOT, HEAVY);
const far = changed === null ? [] : relatedTests(changed, tests).filter(t => !related.includes(t));
console.log(`ci:quick: ${changed === null ? 'no origin/main to diff against: every test' : `${changed.length} changed file(s), ${related.length} related test file(s)`}`); console.log(`ci:quick: ${changed === null ? 'no origin/main to diff against: every test' : `${changed.length} changed file(s), ${related.length} related test file(s)`}`);
if (far.length) console.log(`ci:quick: left out ${far.length} browser suite(s) reached only through shared modules (bun run ci runs them): ${far.join(' ')}`);
add({ name: 'adapters', cmd: ['bun', 'tools/gen-adapters.ts', '--check'] }); add({ name: 'adapters', cmd: ['bun', 'tools/gen-adapters.ts', '--check'] });
tsc('typecheck', '.'); tsc('typecheck', '.');
if (touches(/^apps\/desktop\//)) tsc('desktop typecheck', 'apps/desktop'); if (touches(/^apps\/desktop\//)) tsc('desktop typecheck', 'apps/desktop');