diff --git a/tests/ci-plan.test.ts b/tests/ci-plan.test.ts index 770817d7..f34396db 100644 --- a/tests/ci-plan.test.ts +++ b/tests/ci-plan.test.ts @@ -52,6 +52,12 @@ describe('tools/ci-plan: related tests (ci:quick)', () => { expect(GLOBAL.test('bunfig.toml')).toBe(true); 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', () => { expect(rustNeeded(['cli/index.ts'], {})).toBe(false); expect(rustNeeded(['apps/desktop/src-tauri/src/main.rs'], {})).toBe(true); diff --git a/tools/ci-plan.ts b/tools/ci-plan.ts index bc5a0c68..b1b9b8b1 100644 --- a/tools/ci-plan.ts +++ b/tools/ci-plan.ts @@ -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), * 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; const changedSet = new Set(changed); - const memo = new Map>(); const direct = new Map(); const depsCached = (f: string) => { let d = direct.get(f); if (!d) { try { d = depsOf(f, root); } catch { d = []; } direct.set(f, d); } return d; }; - const closure = (f: string): Set => { - const hit = memo.get(f); - if (hit) return hit; - const seen = new Set([f]); - memo.set(f, seen); - const stack = [f]; - while (stack.length) { - const cur = stack.pop()!; + /** Every file a test reaches, with its import distance. */ + const closure = (f: string): Map => { + const depth = new Map([[f, 0]]); + const queue = [f]; + while (queue.length) { + const cur = queue.shift()!; 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 base = f.split('/').pop()!; @@ -155,10 +155,11 @@ export function relatedTests(changed: string[], tests: string[], root = ROOT): s }); return tests.filter(t => { if (changedSet.has(t)) return true; - const deps = closure(t); - if (changed.some(c => deps.has(c))) return true; + const all = closure(t); + 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; - 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 ''; } }).join('\n'); return names.some(n => srcs.includes(n)); diff --git a/tools/ci.ts b/tools/ci.ts index bf29d665..74d6c6fe 100644 --- a/tools/ci.ts +++ b/tools/ci.ts @@ -46,8 +46,10 @@ function addTests(files: string[]) { } 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)`}`); + 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'] }); tsc('typecheck', '.'); if (touches(/^apps\/desktop\//)) tsc('desktop typecheck', 'apps/desktop');