vitest.config.ts 11 KB

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