vitest.snapshot.config.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { availableParallelism } from 'node:os'
  2. import tsconfigPaths from 'vite-tsconfig-paths'
  3. import { defineConfig } from 'vitest/config'
  4. import { vitestExecArgv } from './vitest.shared.ts'
  5. const DEFAULT_SNAPSHOT_MAX_CONCURRENCY = 5
  6. function positiveIntFromEnv(name: string, fallback: number): number {
  7. const raw = process.env[name]
  8. if (raw === undefined || raw === '') return fallback
  9. const value = Number(raw)
  10. if (!Number.isInteger(value) || value < 1) {
  11. throw new Error(`${name} must be a positive integer, got ${JSON.stringify(raw)}`)
  12. }
  13. return value
  14. }
  15. const snapshotMaxConcurrency = positiveIntFromEnv(
  16. 'DSH_SNAPSHOT_MAX_CONCURRENCY',
  17. Math.min(DEFAULT_SNAPSHOT_MAX_CONCURRENCY, availableParallelism()),
  18. )
  19. // Replay is the keyless default: boot real subprocess paths from recorded model responses and diff
  20. // assembled requests, normalized protocol or transcript output, and persisted-log expected outputs.
  21. // `record` calls the real API and updates fixtures and expected outputs; `refresh` replays committed scripts
  22. // and updates current expected outputs. Replay/refresh never load `.env`; only record reads a key from the
  23. // environment or root `.env`.
  24. if (process.env.DSH_SNAPSHOT === 'record') {
  25. try {
  26. process.loadEnvFile(new URL('.env', import.meta.url).pathname)
  27. } catch (error) {
  28. // ENOENT (no .env) is fine — the key may already be in the environment.
  29. // Surface any other failure rather than silently recording with wrong env.
  30. if ((error as NodeJS.ErrnoException | null)?.code !== 'ENOENT') throw error
  31. }
  32. }
  33. export default defineConfig({
  34. // Same resolution note as vitest.config.ts: bare workspace names resolve
  35. // through the tsconfig.base.json paths facade; the native option cannot do
  36. // this (the root tsconfig is a solution file with no paths).
  37. plugins: [tsconfigPaths({ projects: ['./tsconfig.base.json'] })],
  38. test: {
  39. execArgv: vitestExecArgv,
  40. setupFiles: ['./scripts/test-invariants.ts'],
  41. include: [
  42. 'scripts/**/*.snapshot.ts',
  43. // The assembled Web snapshot executes generated client bundles; source
  44. // mode remains the zero-build path, while lib mode requires a prior build.
  45. ...(process.env.DSH_EXAMPLE_MODE === 'lib' ? ['apps/web/tests/**/*.snapshot.ts'] : []),
  46. 'examples/*/tests/**/*.snapshot.ts',
  47. // The shipped TUI's terminal-journey scenarios moved here with its config.
  48. 'apps/cli/tests/**/*.snapshot.ts',
  49. 'packages/sdk/*/tests/**/*.snapshot.ts',
  50. 'packages/ui/tui/tests/**/*.snapshot.ts',
  51. ],
  52. // Each test boots a subprocess; give it room and keep the worker file singular. Replay tests
  53. // opt into bounded in-file concurrency, while record/refresh stay serial because they write
  54. // fixtures. The environment knob restores serial replay with value 1 on constrained machines.
  55. testTimeout: 120_000,
  56. hookTimeout: 30_000,
  57. fileParallelism: false,
  58. maxConcurrency: snapshotMaxConcurrency,
  59. },
  60. })