1
0

vitest.config.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import { spawnSync } from 'node:child_process'
  2. import { fileURLToPath } from 'node:url'
  3. import tsconfigPaths from 'vite-tsconfig-paths'
  4. import { resolvePwshPath } from './packages/shell/pwsh-local/src/resolve.ts'
  5. import { defineConfig } from 'vitest/config'
  6. import { standardDecoratorPlugin, vitestExecArgv } from './vitest.shared.ts'
  7. import { COVERAGE_EXEMPT_ENV, coverageExemptHeavySuites } from './scripts/coverage-exempt.ts'
  8. import { COVERAGE_PARTITION_MODE_ENV } from './scripts/coverage-partitions.ts'
  9. // Prints exact `path:line:col` records for every uncovered statement, branch
  10. // path, and function when a file misses the per-file 100% gate — the built-in
  11. // threshold ERRORs name only the file. Absolute path because istanbul-reports
  12. // require()s custom reporters (which is also why the reporter is CJS).
  13. const uncoveredLocationsReporter = fileURLToPath(new URL('./scripts/coverage-uncovered-locations.cjs', import.meta.url))
  14. // Resolution facade shared by every plugin instance below: tsconfig.base.json
  15. // has no include, which vite-tsconfig-paths treats as match-all, so its paths
  16. // map applies to every test file. paths must win over package exports so built
  17. // lib/ never loads a second module-singleton copy.
  18. const pathsPlugin = (): ReturnType<typeof tsconfigPaths> => tsconfigPaths({ projects: ['./tsconfig.base.json'] })
  19. const windowsUnsupportedPackages = process.platform === 'win32'
  20. ? [
  21. // Bash-requiring suites (a real POSIX shell is unavailable on Windows).
  22. // The pwsh-requiring suites (pwsh-local, tool-pwsh) deliberately stay
  23. // INCLUDED: PowerShell ships with Windows, so they run natively here.
  24. // This explicit list (not a 'packages/shell/*' glob) keeps
  25. // packages/shell/shell — the Service Definition package — running on Windows.
  26. 'packages/shell/bash-local',
  27. 'packages/shell/bash-sandbox',
  28. 'packages/shell/tool-bash',
  29. 'packages/hooks/*',
  30. 'packages/terminal/terminal-bash',
  31. 'packages/experimental/code-runtime-python',
  32. 'packages/sandbox/sandbox-local',
  33. ]
  34. : []
  35. const windowsUnsupportedTests = process.platform === 'win32'
  36. ? [
  37. ...windowsUnsupportedPackages.map(path => `${path}/tests/**/*.spec.ts`),
  38. 'packages/subprocess/subprocess/tests/**/*.spec.ts',
  39. 'packages/subprocess/subprocess-local/tests/local.spec.ts',
  40. 'packages/subprocess/subprocess-local/tests/process-inspector.spec.ts',
  41. 'packages/subprocess/subprocess-local/tests/spawn.spec.ts',
  42. 'packages/subprocess/subprocess-local/tests/terminal.spec.ts',
  43. // Oracle-diff suites: they compare the worker's POSIX path/url faces
  44. // against the host Node's own answers, which are win32 semantics on
  45. // Windows. The worker always speaks POSIX; the Linux lanes hold the diff.
  46. 'packages/experimental/webworker-runtime/tests/node/path-diff.spec.ts',
  47. 'packages/experimental/webworker-runtime/tests/node/shim-diff.spec.ts',
  48. // The subprocess ladder over the worker's child_process face: its kill
  49. // rung reaches the in-worker process table through `process.kill`,
  50. // which the ladder's win32 branch replaces with taskkill-by-real-pid —
  51. // undeliverable to a table pid. The worker host always reports 'linux',
  52. // so the Linux lanes hold the ladder.
  53. 'packages/experimental/webworker-runtime/tests/node/child-process.spec.ts',
  54. ]
  55. : []
  56. // These suites compare against or assemble the Worker's fixed Linux platform.
  57. // Host-native Windows and macOS behavior is not their oracle.
  58. const nonLinuxWebWorkerTests = process.platform === 'linux'
  59. ? []
  60. : [
  61. 'packages/experimental/webworker-runtime/tests/node/fs-watch-stream.spec.ts',
  62. 'packages/experimental/webworker-runtime/tests/node/sandbox-stack.spec.ts',
  63. ]
  64. const platformUnsupportedTests = [...windowsUnsupportedTests, ...nonLinuxWebWorkerTests]
  65. const windowsUnsupportedCoveragePackages = process.platform === 'win32'
  66. ? [...windowsUnsupportedPackages, 'packages/subprocess/*']
  67. : []
  68. // Windows-only packages: their sources execute exclusively on win32 (koffi
  69. // loads Win32 libraries), so the Linux coverage lane can never cover them.
  70. // The Windows dev/CI lane exercises them through the probe/runner suites; the
  71. // per-file 100% gate must not fail on their Linux-uncovered paths.
  72. const windowsOnlyCoverageExclusions = process.platform !== 'win32'
  73. ? [
  74. 'packages/sandbox/sandbox-windows-acl/src/**/*.ts',
  75. // The koffi-backed Win32 table (Toolhelp32/GetProcessTimes/taskkill)
  76. // executes only on win32; its decision logic is unit-pinned on every
  77. // host through the injected-internals suites.
  78. 'packages/subprocess/subprocess-local/src/windows-inspector.ts',
  79. ]
  80. : []
  81. // The confinement runner entry executes exclusively as a spawned child
  82. // process (the sandbox seam's argv-prefix wrapper): its module-level main()
  83. // would run the confinement in-process if imported, and vitest's v8 coverage
  84. // never measures child processes. Its behavior is pinned end-to-end by
  85. // tests/runner.spec.ts, which spawns the real entry through tsx.
  86. const windowsRunnerCoverageExclusions = process.platform === 'win32'
  87. ? [
  88. 'packages/sandbox/sandbox-windows-acl/src/runner.ts',
  89. // The session write lock's POSIX face (fs-ext flock plus inode
  90. // verification) executes only off-Windows: the Linux lanes hold its
  91. // per-file 100%, while the Windows branch is unit-pinned by
  92. // win32.spec's injected bindings and exercised natively by every
  93. // Windows suite through the real backend.
  94. 'packages/session/session-persistence-jsonl/src/lease.ts',
  95. ]
  96. : []
  97. // pwsh-local's run/start/lifecycle suites self-skip without a real pwsh
  98. // (executor.spec.ts hasPwsh), leaving this file
  99. // far below per-file 100% on pwsh-less hosts; the exemption keeps those hosts
  100. // green while CI runners ship pwsh and still enforce the full bar. The probe
  101. // runs the suites' own resolution (the dependency-free resolve.ts module),
  102. // so the exemption is active exactly when the suites skip — a mismatched
  103. // narrower probe could exempt the file on hosts whose suites actually run.
  104. const pwshCoverageExclusions = spawnSync(resolvePwshPath(), ['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', '$true'], { encoding: 'utf8' }).status === 0
  105. ? []
  106. : [
  107. 'packages/shell/pwsh-local/src/index.ts',
  108. 'packages/shell/pwsh-sandbox/src/**/*.ts',
  109. ]
  110. const testIncludes = [
  111. 'packages/*/*/tests/**/*.spec.{ts,tsx}',
  112. 'apps/*/tests/**/*.spec.ts',
  113. 'scripts/**/*.spec.ts',
  114. ]
  115. // The instrumented coverage gate sets this env; the exempt heavy suites then
  116. // run beside it uninstrumented (membership contract in scripts/coverage-exempt.ts).
  117. // A set-but-not-'1' value is a misconfiguration, not a silent no-op.
  118. const coverageExemptRaw = process.env[COVERAGE_EXEMPT_ENV]
  119. if (coverageExemptRaw !== undefined && coverageExemptRaw !== '' && coverageExemptRaw !== '1') {
  120. throw new Error(`vitest config: ${COVERAGE_EXEMPT_ENV} must be '1' or unset, got ${JSON.stringify(coverageExemptRaw)}.`)
  121. }
  122. const coverageExemptExcludes = coverageExemptRaw === '1'
  123. ? coverageExemptHeavySuites.map(suite => suite.exclude)
  124. : []
  125. const coveragePartitionRaw = process.env[COVERAGE_PARTITION_MODE_ENV]
  126. if (coveragePartitionRaw !== undefined && coveragePartitionRaw !== '' && coveragePartitionRaw !== '1') {
  127. throw new Error(`vitest config: ${COVERAGE_PARTITION_MODE_ENV} must be '1' or unset, got ${JSON.stringify(coveragePartitionRaw)}.`)
  128. }
  129. const coveragePartitionMode = coveragePartitionRaw === '1'
  130. // These suites exercise process-global state, process APIs, or timing-sensitive process I/O
  131. // that worker threads cannot isolate reliably under aggregate gate contention.
  132. // Keep the narrow exception in forks while the rest of the inventory avoids per-file processes.
  133. const processBoundTests = [
  134. 'packages/session/session-persistence-jsonl/tests/jsonl.spec.ts',
  135. 'packages/subagent/subagent-acp/tests/subagent-acp.spec.ts',
  136. 'packages/subprocess/subprocess-local/tests/process-exit.spec.ts',
  137. 'packages/subprocess/subprocess-local/tests/spawn.spec.ts',
  138. 'packages/context/time-context/tests/time-context.spec.ts',
  139. 'packages/llm/llm-pi-ai/tests/adapter.spec.ts',
  140. 'packages/boot/app-boot/tests/app-boot.spec.ts',
  141. 'packages/workflow/workflow-worker-thread/tests/session.spec.ts',
  142. ]
  143. export default defineConfig({
  144. plugins: [pathsPlugin(), standardDecoratorPlugin()],
  145. test: {
  146. setupFiles: ['./scripts/test-proxy-environment.ts', './scripts/test-invariants.ts'],
  147. // .tsx: client component specs (jsdom via per-file @vitest-environment pragma).
  148. include: testIncludes,
  149. exclude: platformUnsupportedTests,
  150. // One coverage invocation aggregates both projects. Every suite forks for
  151. // Node stability; process-bound suites stay separate for inventory control.
  152. projects: [
  153. {
  154. plugins: [pathsPlugin(), standardDecoratorPlugin()],
  155. test: {
  156. name: 'thread-safe',
  157. execArgv: vitestExecArgv,
  158. // Node 24 has aborted in its CJS lexer (v8::ToLocalChecked Empty
  159. // MaybeLocal in cjs_lexer::Parse) from worker threads on macOS,
  160. // Linux, and Windows. Forked workers avoid that shared thread path.
  161. pool: 'forks',
  162. setupFiles: ['./scripts/test-proxy-environment.ts', './scripts/test-invariants.ts'],
  163. include: testIncludes,
  164. exclude: [
  165. ...platformUnsupportedTests,
  166. ...processBoundTests,
  167. ...coverageExemptExcludes,
  168. ],
  169. },
  170. },
  171. {
  172. plugins: [pathsPlugin(), standardDecoratorPlugin()],
  173. test: {
  174. name: 'process-bound',
  175. execArgv: vitestExecArgv,
  176. pool: 'forks',
  177. setupFiles: ['./scripts/test-proxy-environment.ts', './scripts/test-invariants.ts'],
  178. include: processBoundTests,
  179. exclude: [
  180. ...platformUnsupportedTests,
  181. ...coverageExemptExcludes,
  182. ],
  183. },
  184. },
  185. ],
  186. coverage: {
  187. provider: 'v8',
  188. // Coverage measures OUR runtime source. Types-only files carry no
  189. // executable code; vendor/ and application/config fixtures are out of scope.
  190. // .tsx: client components are gated like everything else (jsdom lane).
  191. include: ['packages/*/*/src/**/*.{ts,tsx}'],
  192. // Types-only files have no runtime coverage. Importing self-executing bins/workers would boot
  193. // them inside the unit process, so real subprocess/Worker tests cover their thin entry glue.
  194. exclude: [
  195. 'packages/*/*/src/types.ts',
  196. 'packages/*/*/src/bin.ts',
  197. 'packages/*/*/src/worker.ts',
  198. // Dynamic Host/Client composition is covered by its focused lifecycle
  199. // tests and assembled application checks rather than per-file coverage.
  200. 'packages/self-modification/*/src/**/*.{ts,tsx}',
  201. // A killed executable lint-contract test can leave a non-product source probe behind.
  202. 'packages/*/*/src/oxlint-contract-*.ts',
  203. // Client/web UI files whose remaining branches need a browser-grade
  204. // harness the jsdom lane doesn't cover yet. TODO(gui): cover and
  205. // remove as the client test lane matures.
  206. 'packages/client/ui-trajectory/src/*',
  207. // Trajectory's compact Markdown projection retains deferred branch coverage.
  208. 'packages/client/ui-primitives/src/markdown/plain-text.ts',
  209. 'packages/client/ui-user-questions/src/client/QuestionComposer.tsx',
  210. 'packages/client/ui-primitives/src/Menu.tsx',
  211. 'packages/client/ui-primitives/src/RiskConfirmation.tsx',
  212. 'packages/client/ui-workspace/src/client/WorkspaceBrowser.tsx',
  213. 'packages/client/ui-workspace/src/client/WorkspacePicker.tsx',
  214. 'packages/client/ui-workspace/src/client/rows/WorkspaceBrowser.tsx',
  215. 'packages/client/ui-renderer/src/client/*',
  216. // Session object internals retain the runtime GUI debt exemption; the
  217. // assistant-stream reconciler, Controller entry, transport, Agent scope,
  218. // and adapters stay gated.
  219. 'packages/api/session-controller/src/client/sessions/!(assistant-stream).ts',
  220. 'packages/api/session-controller/src/client/ordered-baseline.ts',
  221. 'packages/api/session-controller/src/client/time-zone.ts',
  222. // Keep the browser conversation tree under its existing GUI debt
  223. // exemption while gating the newly stateful Host half and vocabulary.
  224. 'packages/client/ui-conversation/src/client/*',
  225. // Chat presentation and assembly retain the same GUI debt exemption;
  226. // package wiring and the new approval-detail adapter remain gated.
  227. 'packages/client/ui-chat/src/client/chat/!(ApprovalCommand).{ts,tsx}',
  228. 'packages/client/ui-chat/src/client/conversation-nodes/*',
  229. 'packages/client/ui-chat/src/client/details/*',
  230. 'packages/client/ui-chat/src/client/model/*',
  231. 'packages/client/ui-chat/src/client/contract/context-provenance.ts',
  232. 'packages/client/ui-chat/src/client/contract/snapshot.ts',
  233. 'packages/client/ui-chat/src/client/historical-images.ts',
  234. 'packages/client/ui-primitives/src/DisclosureRow.tsx',
  235. 'packages/client/ui-tool/src/*',
  236. 'packages/client/ui-slots/src/*',
  237. 'packages/client/ui-layout/src/*',
  238. 'packages/client/web/src/*',
  239. 'packages/host/webserver/src/*',
  240. // The browser-worker runtime and its image packer: the executing
  241. // composition is a real dedicated Worker driven by the web browser lane
  242. // (apps/web/tests/preview-boot.e2e.ts), which unit-process V8 coverage
  243. // cannot observe. Unit specs cover the algorithmic cores; the assembled
  244. // evidence is that boot. TODO(webworker): revisit when a browser-grade
  245. // coverage lane exists.
  246. 'packages/experimental/webworker-runtime/src/**',
  247. 'packages/experimental/webworker-packer/src/*',
  248. // Inspector execution adapters run in a Node Worker, the Host native
  249. // inspector session, or a browser realm, outside attributable parent
  250. // Vitest coverage.
  251. 'packages/experimental/inspector/src/client/**',
  252. 'packages/experimental/inspector/src/host/bridge/**',
  253. 'packages/experimental/inspector/src/host/cdp/**',
  254. 'packages/experimental/inspector/src/worker/bridge/**',
  255. 'packages/experimental/inspector/src/worker/cdp/**',
  256. 'packages/experimental/inspector/src/worker/realms/**',
  257. 'packages/experimental/inspector/src/worker/{entry,server}.ts',
  258. // Keep already-complete Inspector modules under the per-file gate and
  259. // enumerate the remaining direct-test debt instead of exempting src/**.
  260. // TODO(inspector): close these branch gaps and remove the entries.
  261. 'packages/experimental/inspector/src/host/plugin.ts',
  262. 'packages/experimental/inspector/src/shared/bridge/{control-codec,rpc}.ts',
  263. 'packages/experimental/inspector/src/shared/bridge/messages/observation.ts',
  264. 'packages/experimental/inspector/src/shared/bridge/messages/query/codec.ts',
  265. 'packages/experimental/inspector/src/shared/bridge/messages/runtime/{command-codec,console-frames,frames,value-codec}.ts',
  266. 'packages/experimental/inspector/src/shared/bridge/messages/sources/{codec,frames}.ts',
  267. 'packages/experimental/inspector/src/worker/inspection/{cordis-store,query-router,realm-store}.ts',
  268. 'packages/client/modules/src/client/system.ts',
  269. 'packages/client/hmr/src/client/index.ts',
  270. // Web config-tree boot round: the new host-side web-transport halves
  271. // whose remaining branches need real-composition/process harnesses.
  272. // TODO(gui): cover and remove with the client test lane above.
  273. 'packages/client/modules/src/index.ts',
  274. 'packages/client/modules/src/invariant.ts',
  275. 'packages/client/modules/src/client/index.ts',
  276. 'packages/client/modules/src/client/manifest.ts',
  277. 'packages/client/hmr/src/index.ts',
  278. 'packages/client/hmr/src/invariant.ts',
  279. 'packages/client/connection/src/index.ts',
  280. 'packages/client/connection/src/http-bridge.ts',
  281. // This assembly imports generated Host-for-Client code that exists
  282. // only in lib; the post-build built-bin smoke executes both entries.
  283. 'packages/api/remotes/src/index.ts',
  284. 'packages/api/remotes/src/client/index.ts',
  285. // The Team browser entry binds its source-covered mount lifecycle to
  286. // the generated Team Remote contribution, which likewise exists only in lib.
  287. 'packages/experimental/client-ui-agent-team/src/client/index.ts',
  288. // Slash/command/input round: per-file gaps deferred with the same
  289. // client-lane debt. TODO(gui): cover and remove with the lane above.
  290. 'packages/client/connection/src/client/fixture.ts',
  291. 'packages/client/ui-commands/src/index.ts',
  292. 'packages/client/ui-skill/src/index.ts',
  293. 'packages/client/ui-input-trigger/src/index.ts',
  294. 'packages/client/ui-subagent/src/index.ts',
  295. 'packages/client/ui-commands/src/client/popup.ts',
  296. 'packages/client/ui-commands/src/client/directory.ts',
  297. 'packages/client/ui-commands/src/client/service.ts',
  298. 'packages/client/ui-commands/src/client/PopupSelectView.tsx',
  299. 'packages/client/ui-model-selection/src/index.ts',
  300. 'packages/client/ui-permission-presets/src/index.ts',
  301. 'packages/client/ui-model-selection/src/client/ModelSelect.tsx',
  302. 'packages/client/ui-model-selection/src/client/directory.ts',
  303. 'packages/client/ui-model-selection/src/client/index.ts',
  304. 'packages/client/ui-model-selection/src/client/service.ts',
  305. 'packages/client/ui-input-trigger/src/client/controller.ts',
  306. 'packages/client/ui-input-trigger/src/client/service.ts',
  307. 'packages/client/ui-input-trigger/src/core/menu.ts',
  308. 'packages/client/ui-input-trigger/src/core/detect.ts',
  309. 'packages/client/ui-sidebar/src/client/index.ts',
  310. 'packages/client/ui-skill/src/client/index.ts',
  311. 'packages/client/ui-workspace/src/client/index.ts',
  312. 'packages/test-support/client-runtime/src/translate.ts',
  313. 'packages/client/ui-primitives/src/JsonTree.tsx',
  314. 'packages/client/ui-settings-models/src/client/DeepSeekOnboardingDialog.tsx',
  315. 'packages/client/ui-settings-models/src/client/welcome-store.ts',
  316. 'packages/extensions/*/src/**/*.ts',
  317. 'packages/extensions/*/src/**/*.tsx',
  318. // Typert generator: correctness is pinned by its fixture suites and
  319. // the byte-for-byte catalog reproduction test; per-file coverage
  320. // would put whole-workspace compiler analysis under v8
  321. // instrumentation — the coverage lane's longest tail.
  322. 'packages/typert/generator/src/*.ts',
  323. // Experimental webworker-runtime is outside the coverage requirement
  324. // by decision: its correctness signal is its uninstrumented suite and
  325. // the packer's end-to-end image spec.
  326. 'packages/experimental/webworker-runtime/src/**/*.ts',
  327. // Projection/command round: executor lifecycle branches and the
  328. // registry's drive tails need the same maturing lanes. TODO(gui):
  329. // cover and remove with the client test lane above.
  330. 'packages/interaction/commands/src/index.ts',
  331. 'packages/interaction/commands/src/invariant.ts',
  332. 'packages/session/session-projection/src/index.ts',
  333. ...windowsUnsupportedCoveragePackages.map(path => `${path}/src/**/*.ts`),
  334. ...windowsOnlyCoverageExclusions,
  335. ...windowsRunnerCoverageExclusions,
  336. ...pwshCoverageExclusions,
  337. ],
  338. // 100% or it doesn't merge (docs/testing.md: excessive tests are welcome).
  339. // Per-file so a well-covered big file can't subsidize a bare one.
  340. // Every v8 ignore comment must carry a reason — see the quality-gates Agent Note
  341. // (.agents/notes/implemented/process/2026-06-11-quality-gates.md).
  342. thresholds: coveragePartitionMode
  343. ? undefined
  344. : {
  345. perFile: true,
  346. statements: 100,
  347. branches: 100,
  348. functions: 100,
  349. lines: 100,
  350. },
  351. reporter: coveragePartitionMode
  352. ? []
  353. : process.env.CI
  354. ? ['text', uncoveredLocationsReporter]
  355. : ['text', 'html', uncoveredLocationsReporter],
  356. },
  357. },
  358. })