vitest.config.ts 14 KB

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