vitest.config.ts 13 KB

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