vitest.config.ts 12 KB

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