run-gates.spec.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { describe, expect, it, vi } from 'vitest'
  2. import {
  3. defaultConcurrency,
  4. formatGateResultReason,
  5. gatesForMode,
  6. runGate,
  7. runGates,
  8. type Gate,
  9. type GateResult,
  10. } from './run-gates.ts'
  11. function gate(id: string, options: Partial<Gate> = {}): Gate {
  12. return {
  13. id,
  14. label: id,
  15. displayCommand: `run ${id}`,
  16. command: process.execPath,
  17. args: ['-e', ''],
  18. ...options,
  19. }
  20. }
  21. function resultFor(subject: Gate, status: GateResult['status'] = 'passed'): GateResult {
  22. return {
  23. gate: subject,
  24. status,
  25. durationMs: 10,
  26. output: [],
  27. exitCode: status === 'passed' ? 0 : 1,
  28. signalCode: null,
  29. }
  30. }
  31. function withPnpmEntrypoint<T>(action: () => T): T {
  32. const previous = process.env.npm_execpath
  33. process.env.npm_execpath = '/private/pnpm.cjs'
  34. try {
  35. return action()
  36. } finally {
  37. if (previous === undefined) Reflect.deleteProperty(process.env, 'npm_execpath')
  38. else process.env.npm_execpath = previous
  39. }
  40. }
  41. function withEnv<T>(name: string, value: string | undefined, action: () => T): T {
  42. const previous = process.env[name]
  43. if (value === undefined) Reflect.deleteProperty(process.env, name)
  44. else process.env[name] = value
  45. try {
  46. return action()
  47. } finally {
  48. if (previous === undefined) Reflect.deleteProperty(process.env, name)
  49. else process.env[name] = previous
  50. }
  51. }
  52. describe('gate graph validation', () => {
  53. it.each([
  54. 'ci-primary',
  55. 'ci-linux-primary',
  56. 'ci-static',
  57. 'ci-lint',
  58. 'ci-coverage',
  59. 'ci-snapshot',
  60. 'ci-artifacts',
  61. 'ci-consumers',
  62. 'ci-windows-blocking',
  63. 'ci-windows-complete',
  64. 'ci-windows-observational',
  65. 'node-compat',
  66. 'check-all',
  67. 'doc-sync',
  68. ] as const)('constructs and executes preflight for a valid non-empty %s graph', async (mode) => {
  69. const subject = withPnpmEntrypoint(() => gatesForMode(mode))
  70. const execute = vi.fn(async (item: Gate) => resultFor(item))
  71. await expect(runGates(subject, subject.length, execute)).resolves.toHaveLength(subject.length)
  72. })
  73. it.each([
  74. ['empty', [], /gate graph has no gates/],
  75. ['duplicate ids', [gate('same'), gate('same')], /duplicate gate id "same"/],
  76. ['unknown dependencies', [gate('subject', { needs: ['missing'] })], /depends on unknown gate "missing"/],
  77. ['cycles', [gate('first', { needs: ['second'] }), gate('second', { needs: ['first'] })], /dependency cycle: first -> second -> first/],
  78. ] as const)('rejects %s before starting a child', async (_label, invalid, message) => {
  79. const execute = vi.fn(async (subject: Gate) => resultFor(subject))
  80. await expect(runGates([...invalid], 1, execute)).rejects.toThrow(message)
  81. expect(execute).not.toHaveBeenCalled()
  82. })
  83. it('rejects an invalid worker count before starting a child', async () => {
  84. const execute = vi.fn(async (subject: Gate) => resultFor(subject))
  85. await expect(runGates([gate('subject')], 0, execute)).rejects.toThrow('max concurrency must be a positive integer')
  86. expect(execute).not.toHaveBeenCalled()
  87. })
  88. it('skips dependents after their prerequisite fails', async () => {
  89. const dependent = gate('dependent', { needs: ['root'] })
  90. const root = gate('root')
  91. const execute = vi.fn(async (subject: Gate) => resultFor(subject, 'failed'))
  92. const results = await runGates([dependent, root], 1, execute)
  93. expect(execute).toHaveBeenCalledOnce()
  94. expect(execute).toHaveBeenCalledWith(root)
  95. expect(results[0]).toMatchObject({ gate: dependent, status: 'skipped', error: 'dependency failed or skipped: root' })
  96. })
  97. })
  98. describe('Oxlint gate', () => {
  99. it('uses the package script when no worker bound is configured', () => {
  100. const subject = withEnv('DSH_OXLINT_THREADS', undefined, () =>
  101. withPnpmEntrypoint(() => gatesForMode('ci-lint')[0]))
  102. expect(subject).toMatchObject({
  103. id: 'lint',
  104. displayCommand: 'pnpm run lint',
  105. command: process.execPath,
  106. args: ['/private/pnpm.cjs', 'run', 'lint'],
  107. })
  108. })
  109. it('surfaces the configured worker bound on the shared package script', () => {
  110. const subject = withEnv('DSH_OXLINT_THREADS', '4', () =>
  111. withPnpmEntrypoint(() => gatesForMode('ci-lint')[0]))
  112. expect(subject).toMatchObject({
  113. id: 'lint',
  114. displayCommand: 'DSH_OXLINT_THREADS=4 pnpm run lint',
  115. command: process.execPath,
  116. args: ['/private/pnpm.cjs', 'run', 'lint'],
  117. })
  118. })
  119. })
  120. describe('Node compatibility graph', () => {
  121. it('runs the jsdom environment smoke on every advertised Node line', () => {
  122. const subject = withPnpmEntrypoint(() => gatesForMode('node-compat'))
  123. expect(subject.find(item => item.id === 'vitest-jsdom-smoke')).toMatchObject({
  124. label: 'Vitest jsdom smoke',
  125. args: [
  126. '/private/pnpm.cjs',
  127. 'exec',
  128. 'vitest',
  129. 'run',
  130. 'scripts/vitest-environment.compat.spec.ts',
  131. ],
  132. })
  133. })
  134. })
  135. describe('Node 24 lane ownership', () => {
  136. it('keeps the static lane source-only', () => {
  137. const subject = withPnpmEntrypoint(() => gatesForMode('ci-static'))
  138. expect(subject.map(item => item.id)).not.toContain('build')
  139. expect(subject.map(item => item.id)).not.toContain('doc-typecheck')
  140. })
  141. it('owns the build and orders its artifact consumers', () => {
  142. const subject = withPnpmEntrypoint(() => gatesForMode('ci-consumers'))
  143. expect(defaultConcurrency('ci-consumers', subject.length, 4)).toEqual({
  144. workers: 10,
  145. source: 'ci-consumers gate count',
  146. })
  147. expect(subject.map(item => item.id)).toEqual([
  148. 'build',
  149. 'node-compat',
  150. 'publint',
  151. 'built-package-invariants',
  152. 'lint-and-duplication',
  153. 'snapshot',
  154. 'web-snapshot',
  155. 'doc-typecheck',
  156. 'node-next-types',
  157. 'built-bin-smoke',
  158. ])
  159. expect(subject.find(item => item.id === 'publint')?.needs).toEqual(['build'])
  160. expect(subject.find(item => item.id === 'built-package-invariants')?.needs).toEqual(['publint'])
  161. expect(subject.find(item => item.id === 'lint-and-duplication')?.needs).toEqual(['built-package-invariants'])
  162. for (const id of ['snapshot', 'web-snapshot', 'doc-typecheck', 'node-next-types', 'built-bin-smoke']) {
  163. expect(subject.find(item => item.id === id)?.needs).toEqual(['built-package-invariants'])
  164. }
  165. expect(subject.find(item => item.id === 'snapshot')?.env).toEqual({ DSH_EXAMPLE_MODE: 'lib' })
  166. expect(subject.find(item => item.id === 'doc-typecheck')?.env).toEqual({
  167. DSH_DOC_TYPECHECK_USE_BUILD_OUTPUT: '1',
  168. })
  169. expect(subject.find(item => item.id === 'web-snapshot')).toMatchObject({
  170. displayCommand: 'DSH_SNAPSHOT=replay pnpm run test:web:built',
  171. env: { DSH_SNAPSHOT: 'replay' },
  172. })
  173. })
  174. })
  175. describe('Linux primary graph', () => {
  176. it('adds the same compare-only web gate after built client artifacts', () => {
  177. const subject = withPnpmEntrypoint(() => gatesForMode('ci-linux-primary'))
  178. const web = subject.find(item => item.id === 'web-snapshot')
  179. expect(web).toMatchObject({
  180. displayCommand: 'DSH_SNAPSHOT=replay pnpm run test:web:built',
  181. env: { DSH_SNAPSHOT: 'replay' },
  182. needs: ['built-package-invariants'],
  183. })
  184. })
  185. })
  186. describe('gate process outcomes', () => {
  187. it.skipIf(process.platform === 'win32')('reports signal termination independently from exit status', async () => {
  188. const result = await runGate(gate('terminated', {
  189. args: ['-e', "process.kill(process.pid, 'SIGTERM')"],
  190. }))
  191. expect(result.status).toBe('failed')
  192. expect(result.exitCode).toBeNull()
  193. expect(result.signalCode).toBe('SIGTERM')
  194. expect(formatGateResultReason(result)).toBe('signal SIGTERM')
  195. })
  196. })