vitest.config.ts 18 KB

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