vitest.config.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/bash/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. // Prints exact `path:line:col` records for every uncovered statement, branch
  9. // path, and function when a file misses the per-file 100% gate — the built-in
  10. // threshold ERRORs name only the file. Absolute path because istanbul-reports
  11. // require()s custom reporters (which is also why the reporter is CJS).
  12. const uncoveredLocationsReporter = fileURLToPath(new URL('./scripts/coverage-uncovered-locations.cjs', import.meta.url))
  13. // Resolution facade shared by every plugin instance below: tsconfig.base.json
  14. // has no include, which vite-tsconfig-paths treats as match-all, so its paths
  15. // map applies to every test file. paths must win over package exports so built
  16. // lib/ never loads a second module-singleton copy.
  17. const pathsPlugin = (): ReturnType<typeof tsconfigPaths> => tsconfigPaths({ projects: ['./tsconfig.base.json'] })
  18. const windowsUnsupportedPackages = process.platform === 'win32'
  19. ? [
  20. // Bash-requiring suites (a real POSIX shell is unavailable on Windows).
  21. // The pwsh-requiring suites (pwsh-local, tool-pwsh) deliberately stay
  22. // INCLUDED: PowerShell ships with Windows, so they run natively here.
  23. // This explicit list (not a 'packages/bash/*' glob) keeps
  24. // packages/bash/bash — the Service Definition package — running on Windows.
  25. 'packages/bash/bash-local',
  26. 'packages/bash/bash-sandbox',
  27. 'packages/bash/tool-bash',
  28. 'packages/hooks/*',
  29. 'packages/subprocess/*',
  30. 'packages/pty/pty-local',
  31. 'packages/sandbox/sandbox-local',
  32. ]
  33. : []
  34. // Windows-only packages: their sources execute exclusively on win32 (koffi
  35. // loads Win32 libraries), so the Linux coverage lane can never cover them.
  36. // The Windows dev/CI lane exercises them through the probe/runner suites; the
  37. // per-file 100% gate must not fail on their Linux-uncovered paths.
  38. const windowsOnlyCoverageExclusions = process.platform !== 'win32'
  39. ? [
  40. 'packages/sandbox/sandbox-windows-acl/src/**/*.ts',
  41. ]
  42. : []
  43. // The confinement runner entry executes exclusively as a spawned child
  44. // process (the sandbox seam's argv-prefix wrapper): its module-level main()
  45. // would run the confinement in-process if imported, and vitest's v8 coverage
  46. // never measures child processes. Its behavior is pinned end-to-end by
  47. // tests/runner.spec.ts, which spawns the real entry through tsx.
  48. const windowsRunnerCoverageExclusions = process.platform === 'win32'
  49. ? ['packages/sandbox/sandbox-windows-acl/src/runner.ts']
  50. : []
  51. // pwsh-local's run/start/lifecycle suites self-skip without a real pwsh
  52. // (executor.spec.ts hasPwsh), leaving this file
  53. // far below per-file 100% on pwsh-less hosts; the exemption keeps those hosts
  54. // green while CI runners ship pwsh and still enforce the full bar. The probe
  55. // runs the suites' own resolution (the dependency-free resolve.ts module),
  56. // so the exemption is active exactly when the suites skip — a mismatched
  57. // narrower probe could exempt the file on hosts whose suites actually run.
  58. const pwshCoverageExclusions = spawnSync(resolvePwshPath(), ['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', '$true'], { encoding: 'utf8' }).status === 0
  59. ? []
  60. : [
  61. 'packages/bash/pwsh-local/src/index.ts',
  62. 'packages/bash/pwsh-sandbox/src/**/*.ts',
  63. ]
  64. const testIncludes = [
  65. 'packages/*/*/tests/**/*.spec.{ts,tsx}',
  66. 'apps/*/tests/**/*.spec.ts',
  67. 'examples/*/tests/**/*.spec.ts',
  68. 'scripts/**/*.spec.ts',
  69. ]
  70. // The instrumented coverage gate sets this env; the exempt heavy suites then
  71. // run beside it uninstrumented (membership contract in scripts/coverage-exempt.ts).
  72. // A set-but-not-'1' value is a misconfiguration, not a silent no-op.
  73. const coverageExemptRaw = process.env[COVERAGE_EXEMPT_ENV]
  74. if (coverageExemptRaw !== undefined && coverageExemptRaw !== '' && coverageExemptRaw !== '1') {
  75. throw new Error(`vitest config: ${COVERAGE_EXEMPT_ENV} must be '1' or unset, got ${JSON.stringify(coverageExemptRaw)}.`)
  76. }
  77. const coverageExemptExcludes = coverageExemptRaw === '1'
  78. ? coverageExemptHeavySuites.map(suite => suite.exclude)
  79. : []
  80. // These suites exercise process-global state, process APIs, or timing-sensitive process I/O
  81. // that worker threads cannot isolate reliably under aggregate gate contention.
  82. // Keep the narrow exception in forks while the rest of the inventory avoids per-file processes.
  83. const processBoundTests = [
  84. 'packages/session/session-persistence-jsonl/tests/jsonl.spec.ts',
  85. 'packages/subagent/subagent-acp/tests/subagent-acp.spec.ts',
  86. 'packages/subprocess/subprocess-local/tests/spawn.spec.ts',
  87. 'packages/context/time-context/tests/time-context.spec.ts',
  88. 'packages/llm/llm-pi-ai/tests/adapter.spec.ts',
  89. 'packages/boot/app-boot/tests/app-boot.spec.ts',
  90. 'packages/workflow/workflow-workerthread/tests/session.spec.ts',
  91. ]
  92. export default defineConfig({
  93. plugins: [pathsPlugin(), standardDecoratorPlugin()],
  94. test: {
  95. setupFiles: ['./scripts/test-invariants.ts'],
  96. // .tsx: client component specs (jsdom via per-file @vitest-environment pragma).
  97. include: testIncludes,
  98. exclude: windowsUnsupportedPackages.map(path => `${path}/tests/**/*.spec.ts`),
  99. // One coverage invocation aggregates both projects. Every suite forks for
  100. // Node stability; process-bound suites stay separate for inventory control.
  101. projects: [
  102. {
  103. plugins: [pathsPlugin(), standardDecoratorPlugin()],
  104. test: {
  105. name: 'thread-safe',
  106. execArgv: vitestExecArgv,
  107. // Node 24 has aborted in its CJS lexer (v8::ToLocalChecked Empty
  108. // MaybeLocal in cjs_lexer::Parse) from worker threads on macOS,
  109. // Linux, and Windows. Forked workers avoid that shared thread path.
  110. pool: 'forks',
  111. setupFiles: ['./scripts/test-invariants.ts'],
  112. include: testIncludes,
  113. exclude: [
  114. ...windowsUnsupportedPackages.map(path => `${path}/tests/**/*.spec.ts`),
  115. ...processBoundTests,
  116. ...coverageExemptExcludes,
  117. ],
  118. },
  119. },
  120. {
  121. plugins: [pathsPlugin(), standardDecoratorPlugin()],
  122. test: {
  123. name: 'process-bound',
  124. execArgv: vitestExecArgv,
  125. pool: 'forks',
  126. setupFiles: ['./scripts/test-invariants.ts'],
  127. include: processBoundTests,
  128. exclude: [
  129. ...windowsUnsupportedPackages.map(path => `${path}/tests/**/*.spec.ts`),
  130. ...coverageExemptExcludes,
  131. ],
  132. },
  133. },
  134. ],
  135. coverage: {
  136. provider: 'v8',
  137. // Coverage measures OUR runtime source. Types-only files carry no
  138. // executable code; vendor/ and examples/ are out of scope (examples are
  139. // exercised by the demo smoke test instead).
  140. // .tsx: client components are gated like everything else (jsdom lane).
  141. include: ['packages/*/*/src/**/*.{ts,tsx}'],
  142. // Types-only files have no runtime coverage. Importing self-executing bins/workers would boot
  143. // them inside the unit process, so real subprocess/Worker tests cover their thin entry glue.
  144. exclude: [
  145. 'packages/*/*/src/types.ts',
  146. 'packages/*/*/src/bin.ts',
  147. 'packages/*/*/src/worker.ts',
  148. // A killed executable lint-contract test can leave a non-product source probe behind.
  149. 'packages/*/*/src/oxlint-contract-*.ts',
  150. // Client/web UI files whose remaining branches need a browser-grade
  151. // harness the jsdom lane doesn't cover yet. TODO(gui): cover and
  152. // remove as the client test lane matures.
  153. 'packages/client/ui-trajectory/src/*',
  154. // Trajectory's compact Markdown projection retains deferred branch coverage.
  155. 'packages/client/ui-primitives/src/markdown/plain-text.ts',
  156. 'packages/client/ui-question/src/client/QuestionComposer.tsx',
  157. 'packages/client/ui-primitives/src/Menu.tsx',
  158. 'packages/client/ui-primitives/src/RiskConfirmation.tsx',
  159. 'packages/client/ui-workspace/src/client/WorkspaceBrowser.tsx',
  160. 'packages/client/ui-workspace/src/client/WorkspacePicker.tsx',
  161. 'packages/client/web-react/src/*',
  162. // This isolated settings-scope lifecycle has complete unit coverage;
  163. // keep it out of the broader client-runtime GUI debt exemption.
  164. 'packages/client/runtime/src/**/!(settings-scope).ts',
  165. // Keep the browser conversation tree under its existing GUI debt
  166. // exemption while gating the newly stateful Host half and vocabulary.
  167. 'packages/client/ui-conversation/src/client/*',
  168. 'packages/client/ui-conversation/src/invariant.ts',
  169. 'packages/client/ui-primitives/src/DisclosureRow.tsx',
  170. 'packages/client/ui-tool/src/*',
  171. 'packages/client/ui-slots/src/*',
  172. 'packages/client/ui-layout/src/*',
  173. 'packages/client/web/src/*',
  174. 'packages/host/webserver/src/*',
  175. 'packages/client/modules/src/client/system.ts',
  176. 'packages/client/hmr/src/client/index.ts',
  177. // Web config-tree boot round: the new host-side web-transport halves
  178. // whose remaining branches need real-composition/process harnesses.
  179. // TODO(gui): cover and remove with the client test lane above.
  180. 'packages/client/modules/src/index.ts',
  181. 'packages/client/modules/src/invariant.ts',
  182. 'packages/client/modules/src/client/index.ts',
  183. 'packages/client/modules/src/client/manifest.ts',
  184. 'packages/client/hmr/src/index.ts',
  185. 'packages/client/hmr/src/invariant.ts',
  186. 'packages/client/connection/src/index.ts',
  187. 'packages/client/connection/src/http-bridge.ts',
  188. // This assembly imports generated Host-for-Client code that exists
  189. // only in lib; the post-build built-bin smoke executes both entries.
  190. 'packages/api/remotes/src/index.ts',
  191. 'packages/api/remotes/src/client/index.ts',
  192. // Slash/command/input round: per-file gaps deferred with the same
  193. // client-lane debt. TODO(gui): cover and remove with the lane above.
  194. 'packages/client/connection/src/client/fixture.ts',
  195. 'packages/client/ui-command/src/index.ts',
  196. 'packages/client/ui-skill/src/index.ts',
  197. 'packages/client/ui-slash/src/index.ts',
  198. 'packages/client/ui-subagent/src/index.ts',
  199. 'packages/client/ui-command/src/client/popup.ts',
  200. 'packages/client/ui-command/src/client/directory.ts',
  201. 'packages/client/ui-command/src/client/service.ts',
  202. 'packages/client/ui-command/src/client/PopupSelectView.tsx',
  203. 'packages/client/ui-model/src/index.ts',
  204. 'packages/client/ui-permission/src/index.ts',
  205. 'packages/client/ui-model/src/client/ModelSelect.tsx',
  206. 'packages/client/ui-model/src/client/directory.ts',
  207. 'packages/client/ui-model/src/client/index.ts',
  208. 'packages/client/ui-model/src/client/service.ts',
  209. 'packages/client/ui-slash/src/client/controller.ts',
  210. 'packages/client/ui-slash/src/client/service.ts',
  211. 'packages/client/ui-slash/src/core/menu.ts',
  212. 'packages/client/ui-slash/src/core/detect.ts',
  213. 'packages/client/ui-sidebar/src/client/index.ts',
  214. 'packages/client/ui-skill/src/client/index.ts',
  215. 'packages/client/ui-workspace/src/client/index.ts',
  216. 'packages/client/test-runtime/src/translate.ts',
  217. 'packages/client/ui-primitives/src/JsonTree.tsx',
  218. // Typert generator: correctness is pinned by its fixture suites and
  219. // the byte-for-byte catalog reproduction test; per-file coverage
  220. // would put whole-workspace compiler analysis under v8
  221. // instrumentation — the coverage lane's longest tail.
  222. 'packages/typert/generator/src/*.ts',
  223. 'packages/host/apiproxy/src/index.ts',
  224. 'packages/host/apiproxy/src/invariant.ts',
  225. 'packages/host/apiproxy/src/api-proxy.ts',
  226. // Projection/command round: executor lifecycle branches and the
  227. // registry's drive tails need the same maturing lanes. TODO(gui):
  228. // cover and remove with the client test lane above.
  229. 'packages/interaction/commands/src/index.ts',
  230. 'packages/interaction/commands/src/invariant.ts',
  231. 'packages/session/session-projection/src/index.ts',
  232. ...windowsUnsupportedPackages.map(path => `${path}/src/**/*.ts`),
  233. ...windowsOnlyCoverageExclusions,
  234. ...windowsRunnerCoverageExclusions,
  235. ...pwshCoverageExclusions,
  236. ],
  237. // 100% or it doesn't merge (docs/testing.md: excessive tests are welcome).
  238. // Per-file so a well-covered big file can't subsidize a bare one.
  239. // Every v8 ignore comment must carry a reason — see the quality-gates Agent Note
  240. // (.agents/notes/implemented/process/2026-06-11-quality-gates.md).
  241. thresholds: {
  242. perFile: true,
  243. statements: 100,
  244. branches: 100,
  245. functions: 100,
  246. lines: 100,
  247. },
  248. reporter: process.env.CI
  249. ? ['text', uncoveredLocationsReporter]
  250. : ['text', 'html', uncoveredLocationsReporter],
  251. },
  252. },
  253. })