1- import { spawnSync } from "node:child_process"
1+ import { execFile , spawnSync } from "node:child_process"
2+ import { mkdtempSync , readFileSync , readdirSync , rmSync } from "node:fs"
3+ import { tmpdir } from "node:os"
4+ import { relative , resolve } from "node:path"
25import process from "node:process"
6+ import { promisify } from "node:util"
37
48const pnpm = process . platform === "win32" ? process . env . npm_execpath : "pnpm"
59if ( ! pnpm ) throw new Error ( "pnpm executable path is unavailable" )
610const command = process . platform === "win32" ? process . execPath : pnpm
711const args = process . platform === "win32" ? [ pnpm ] : [ ]
812const result = spawnSync (
913 command ,
10- [ ...args , "turbo" , "run" , "test:coverage:unit" , "test:dist" , "--filter=zoo-code" , "--dry=json" ] ,
14+ [
15+ ...args ,
16+ "turbo" ,
17+ "run" ,
18+ "test:coverage:api" ,
19+ "test:coverage:core" ,
20+ "test:coverage:services" ,
21+ "test:coverage:misc" ,
22+ "test:coverage:tree-sitter" ,
23+ "test:dist" ,
24+ "--filter=zoo-code" ,
25+ "--dry=json" ,
26+ ] ,
1127 { encoding : "utf8" } ,
1228)
1329if ( result . status !== 0 ) {
@@ -16,14 +32,135 @@ if (result.status !== 0) {
1632}
1733
1834const graph = JSON . parse ( result . stdout )
19- const coverageTask = graph . tasks . find ( ( { taskId } ) => taskId === "zoo-code#test:coverage:unit" )
35+ const ownershipLanes = [ "api" , "core" , "services" , "misc" ]
36+ const ownershipTasks = ownershipLanes . map ( ( lane ) =>
37+ graph . tasks . find ( ( { taskId } ) => taskId === `zoo-code#test:coverage:${ lane } ` ) ,
38+ )
39+ const treeSitterTask = graph . tasks . find ( ( { taskId } ) => taskId === "zoo-code#test:coverage:tree-sitter" )
2040const distTask = graph . tasks . find ( ( { taskId } ) => taskId === "zoo-code#test:dist" )
21- if ( ! coverageTask ) throw new Error ( "Unit coverage task missing" )
41+ if ( ownershipTasks . some ( ( task ) => ! task ) || ! treeSitterTask ) throw new Error ( "Extension coverage lane missing" )
2242if ( graph . tasks . some ( ( { taskId } ) => taskId === "zoo-code#prepare:tree-sitter-wasms" ) )
2343 throw new Error ( "Removed WASM preparation task remains in the graph" )
24- if ( coverageTask . dependencies . includes ( "zoo-code#bundle" ) ) throw new Error ( "Unit coverage must not depend on bundle" )
25- if ( ! coverageTask . dependencies . includes ( "@roo-code/types#build" ) )
26- throw new Error ( "Unit coverage must depend on the types build" )
27- if ( ! Object . hasOwn ( coverageTask . inputs , "package.json" ) ) throw new Error ( "Unit coverage must hash package.json" )
28- if ( ! coverageTask . hashOfExternalDependencies ) throw new Error ( "Unit coverage must hash external dependencies" )
44+ for ( const task of [ ...ownershipTasks , treeSitterTask ] ) {
45+ if ( task . dependencies . includes ( "zoo-code#bundle" ) ) throw new Error ( "Coverage lanes must not depend on bundle" )
46+ if ( ! Object . hasOwn ( task . inputs , "package.json" ) ) throw new Error ( "Coverage lanes must hash package.json" )
47+ if ( ! task . hashOfExternalDependencies ) throw new Error ( "Coverage lanes must hash external dependencies" )
48+ }
49+ for ( const task of ownershipTasks ) {
50+ for ( const dependency of [
51+ "@roo-code/cloud" ,
52+ "@roo-code/core" ,
53+ "@roo-code/ipc" ,
54+ "@roo-code/telemetry" ,
55+ "@roo-code/types" ,
56+ ] )
57+ if ( ! task . dependencies . includes ( `${ dependency } #build` ) )
58+ throw new Error ( `Ownership coverage lanes must depend on ${ dependency } #build` )
59+ }
60+ if ( ! treeSitterTask . dependencies . includes ( "@roo-code/types#build" ) )
61+ throw new Error ( "Tree-sitter coverage must depend on @roo-code/types#build" )
62+ for ( const task of ownershipTasks ) {
63+ if ( ! Object . hasOwn ( task . inputs , "services/tree-sitter/index.ts" ) )
64+ throw new Error ( "Ownership coverage lanes must hash shared tree-sitter production sources" )
65+ }
66+ const representativeTests = {
67+ api : "api/providers/__tests__/anthropic.spec.ts" ,
68+ core : "core/task/__tests__/Task.spec.ts" ,
69+ services : "services/mcp/__tests__/McpHub.spec.ts" ,
70+ misc : "utils/__tests__/path.spec.ts" ,
71+ }
72+ for ( const [ index , task ] of ownershipTasks . entries ( ) ) {
73+ for ( const [ lane , testPath ] of Object . entries ( representativeTests ) ) {
74+ const ownsInput = Object . hasOwn ( task . inputs , testPath )
75+ if ( ownsInput !== ( lane === ownershipLanes [ index ] ) )
76+ throw new Error ( `${ ownershipLanes [ index ] } coverage has incorrect ownership for ${ testPath } ` )
77+ }
78+ if ( Object . hasOwn ( task . inputs , "services/tree-sitter/__tests__/wasm.spec.ts" ) )
79+ throw new Error ( "Ownership coverage lanes must not hash tree-sitter-owned tests" )
80+ }
81+ if ( ! Object . hasOwn ( treeSitterTask . inputs , "services/tree-sitter/index.ts" ) )
82+ throw new Error ( "Tree-sitter coverage must hash tree-sitter sources" )
83+ if ( Object . hasOwn ( treeSitterTask . inputs , "core/task/Task.ts" ) )
84+ throw new Error ( "Tree-sitter coverage must not hash unrelated core sources" )
2985if ( ! distTask ?. dependencies . includes ( "zoo-code#bundle" ) ) throw new Error ( "Dist smoke test must depend on bundle" )
86+
87+ const root = resolve ( import . meta. dirname , ".." )
88+ const testPattern = / \. (?: t e s t | s p e c ) \. (?: [ c m ] ? [ j t ] s x ? ) $ /
89+ const ignoredDirectories = new Set ( [ "coverage" , "dist" , "node_modules" ] )
90+ const testFiles = [ ]
91+ const collectTests = ( directory ) => {
92+ for ( const entry of readdirSync ( directory , { withFileTypes : true } ) ) {
93+ if ( entry . isDirectory ( ) ) {
94+ if ( ! ignoredDirectories . has ( entry . name ) ) collectTests ( resolve ( directory , entry . name ) )
95+ } else if ( testPattern . test ( entry . name ) ) {
96+ testFiles . push ( relative ( root , resolve ( directory , entry . name ) ) . replaceAll ( "\\" , "/" ) )
97+ }
98+ }
99+ }
100+ collectTests ( root )
101+
102+ const laneForTest = ( file ) => {
103+ if ( file === "__tests__/dist_assets.spec.ts" ) return "dist"
104+ if ( file . startsWith ( "services/tree-sitter/" ) ) return "tree-sitter"
105+ for ( const lane of [ "api" , "core" , "services" ] ) if ( file . startsWith ( `${ lane } /` ) ) return lane
106+ if (
107+ [ "__tests__" , "activate" , "extension" , "i18n" , "integrations" , "scripts" , "shared" , "test-utils" , "utils" ] . some (
108+ ( directory ) => file . startsWith ( `${ directory } /` ) ,
109+ )
110+ )
111+ return "misc"
112+ throw new Error ( `Unit test is not assigned to a coverage lane: ${ file } ` )
113+ }
114+ const laneCounts = Object . groupBy ( testFiles , laneForTest )
115+ if ( Object . values ( laneCounts ) . flat ( ) . length !== testFiles . length )
116+ throw new Error ( "Coverage lanes do not form an exact test partition" )
117+
118+ const collectionDirectory = mkdtempSync ( resolve ( tmpdir ( ) , "zoo-code-coverage-contract-" ) )
119+ try {
120+ const execFileAsync = promisify ( execFile )
121+ const collect = async ( config ) => {
122+ const output = resolve ( collectionDirectory , `${ config } .json` )
123+ await execFileAsync (
124+ command ,
125+ [
126+ ...args ,
127+ "exec" ,
128+ "vitest" ,
129+ "list" ,
130+ "--staticParse" ,
131+ "--config" ,
132+ `vitest.${ config } .config.ts` ,
133+ `--json=${ output } ` ,
134+ ] ,
135+ { maxBuffer : 10 * 1024 * 1024 } ,
136+ )
137+ return new Set ( JSON . parse ( readFileSync ( output , "utf8" ) ) . map ( ( { file, name } ) => `${ file } \0${ name } ` ) )
138+ }
139+ const collections = [ "unit" , ...ownershipLanes , "tree-sitter" ]
140+ const [ unitTests , ...resolvedLanes ] = await Promise . all ( collections . map ( collect ) )
141+ const laneTests = new Set ( )
142+ for ( const tests of resolvedLanes ) {
143+ for ( const test of tests ) {
144+ if ( laneTests . has ( test ) ) throw new Error ( `Test belongs to multiple coverage lanes: ${ test } ` )
145+ laneTests . add ( test )
146+ }
147+ }
148+ if ( unitTests . size !== laneTests . size || [ ...unitTests ] . some ( ( test ) => ! laneTests . has ( test ) ) )
149+ throw new Error ( "Coverage lane test collection differs from monolithic unit coverage" )
150+ } finally {
151+ rmSync ( collectionDirectory , { recursive : true , force : true } )
152+ }
153+
154+ const coverageSources = new Map ( )
155+ for ( const lane of [ ...ownershipLanes , "tree-sitter" ] ) {
156+ let source
157+ for ( const line of readFileSync ( resolve ( root , "coverage" , lane , "lcov.info" ) , "utf8" ) . split ( / \r ? \n / ) ) {
158+ if ( line . startsWith ( "SF:" ) ) source = line . slice ( 3 )
159+ if ( line . startsWith ( "LF:" ) ) coverageSources . set ( source , Number ( line . slice ( 3 ) ) )
160+ }
161+ }
162+ const instrumentedLines = [ ...coverageSources . values ( ) ] . reduce ( ( sum , lines ) => sum + lines , 0 )
163+ if ( coverageSources . size !== 469 || instrumentedLines !== 30_229 )
164+ throw new Error (
165+ `Coverage source population changed: ${ coverageSources . size } records and ${ instrumentedLines } lines; verify equivalence and update the baseline deliberately` ,
166+ )
0 commit comments