run-gates.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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-contracts-ready',
  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('keeps the public repository link policy in the documentation gate', () => {
  74. const ids = withPnpmEntrypoint(() => gatesForMode('doc-sync').map(subject => subject.id))
  75. expect(ids).toContain('public-repository-links')
  76. })
  77. it.each(['ci-primary', 'ci-static', 'check-all'] as const)(
  78. 'keeps the DSH package license policy in %s',
  79. (mode) => {
  80. const ids = withPnpmEntrypoint(() => gatesForMode(mode).map(subject => subject.id))
  81. expect(ids).toContain('dsh-package-licenses')
  82. },
  83. )
  84. it.each(['ci-primary', 'ci-static', 'check-all'] as const)(
  85. 'keeps the client dependency policy in %s',
  86. (mode) => {
  87. const ids = withPnpmEntrypoint(() => gatesForMode(mode).map(subject => subject.id))
  88. expect(ids).toContain('client-packages')
  89. },
  90. )
  91. it('keeps native Windows coverage blocking while portability inventory remains observational', () => {
  92. const gates = withPnpmEntrypoint(() => gatesForMode('ci-windows-complete'))
  93. const byId = new Map(gates.map(subject => [subject.id, subject]))
  94. expect(byId.get('coverage')?.allowFailure).not.toBe(true)
  95. expect(byId.get('coverage-exempt-heavy')?.allowFailure).not.toBe(true)
  96. expect(byId.get('duplication')?.allowFailure).toBe(true)
  97. })
  98. it('applies one configured test and polling timeout to both coverage gates', () => {
  99. const gates = withEnv('DSH_COVERAGE_TEST_TIMEOUT_MS', '15000', () =>
  100. withPnpmEntrypoint(() => gatesForMode('ci-windows-complete')))
  101. for (const id of ['coverage', 'coverage-exempt-heavy']) {
  102. expect(gates.find(subject => subject.id === id)?.args).toEqual(expect.arrayContaining([
  103. '--testTimeout=15000',
  104. '--expect.poll.timeout=15000',
  105. ]))
  106. }
  107. })
  108. it('keeps Vitest timeout defaults when the coverage override is absent', () => {
  109. const gates = withEnv('DSH_COVERAGE_TEST_TIMEOUT_MS', undefined, () =>
  110. withPnpmEntrypoint(() => gatesForMode('ci-windows-complete')))
  111. for (const id of ['coverage', 'coverage-exempt-heavy']) {
  112. expect(gates.find(subject => subject.id === id)?.args).not.toEqual(expect.arrayContaining([
  113. expect.stringMatching(/^--(?:testTimeout|expect\.poll\.timeout)=/),
  114. ]))
  115. }
  116. })
  117. it('rejects an invalid coverage timeout before starting a gate', () => {
  118. expect(() => withEnv('DSH_COVERAGE_TEST_TIMEOUT_MS', '0', () =>
  119. withPnpmEntrypoint(() => gatesForMode('ci-windows-complete'))))
  120. .toThrow('DSH_COVERAGE_TEST_TIMEOUT_MS must be a positive integer')
  121. })
  122. it.each([
  123. ['empty', [], /gate graph has no gates/],
  124. ['duplicate ids', [gate('same'), gate('same')], /duplicate gate id "same"/],
  125. ['unknown dependencies', [gate('subject', { needs: ['missing'] })], /depends on unknown gate "missing"/],
  126. ['cycles', [gate('first', { needs: ['second'] }), gate('second', { needs: ['first'] })], /dependency cycle: first -> second -> first/],
  127. ] as const)('rejects %s before starting a child', async (_label, invalid, message) => {
  128. const execute = vi.fn(async (subject: Gate) => resultFor(subject))
  129. await expect(runGates([...invalid], 1, execute)).rejects.toThrow(message)
  130. expect(execute).not.toHaveBeenCalled()
  131. })
  132. it('rejects an invalid worker count before starting a child', async () => {
  133. const execute = vi.fn(async (subject: Gate) => resultFor(subject))
  134. await expect(runGates([gate('subject')], 0, execute)).rejects.toThrow('max concurrency must be a positive integer')
  135. expect(execute).not.toHaveBeenCalled()
  136. })
  137. it('skips dependents after their prerequisite fails', async () => {
  138. const dependent = gate('dependent', { needs: ['root'] })
  139. const root = gate('root')
  140. const execute = vi.fn(async (subject: Gate) => resultFor(subject, 'failed'))
  141. const results = await runGates([dependent, root], 1, execute)
  142. expect(execute).toHaveBeenCalledOnce()
  143. expect(execute).toHaveBeenCalledWith(root)
  144. expect(results[0]).toMatchObject({ gate: dependent, status: 'skipped', error: 'dependency failed or skipped: root' })
  145. })
  146. })
  147. describe('Oxlint gate', () => {
  148. it('uses the package script when no worker bound is configured', () => {
  149. const subject = withEnv('DSH_OXLINT_THREADS', undefined, () =>
  150. withPnpmEntrypoint(() => gatesForMode('ci-lint-contracts-ready')[0]))
  151. expect(subject).toMatchObject({
  152. id: 'lint',
  153. displayCommand: 'pnpm run lint:contracts-ready',
  154. command: process.execPath,
  155. args: ['/private/pnpm.cjs', 'run', 'lint:contracts-ready'],
  156. })
  157. })
  158. it('surfaces the configured worker bound on the shared package script', () => {
  159. const subject = withEnv('DSH_OXLINT_THREADS', '4', () =>
  160. withPnpmEntrypoint(() => gatesForMode('ci-lint-contracts-ready')[0]))
  161. expect(subject).toMatchObject({
  162. id: 'lint',
  163. displayCommand: 'DSH_OXLINT_THREADS=4 pnpm run lint:contracts-ready',
  164. command: process.execPath,
  165. args: ['/private/pnpm.cjs', 'run', 'lint:contracts-ready'],
  166. })
  167. })
  168. })
  169. describe('Typert contract preparation', () => {
  170. it('prepares primary source consumers once before they run', () => {
  171. const subject = withEnv('DSH_OXLINT_THREADS', undefined, () =>
  172. withPnpmEntrypoint(() => gatesForMode('ci-primary')))
  173. expect(subject.find(item => item.id === 'typert-contracts')).toMatchObject({
  174. displayCommand: 'pnpm run build:lib:host',
  175. args: ['/private/pnpm.cjs', 'run', 'build:lib:host'],
  176. })
  177. for (const [id, script] of [
  178. ['typecheck', 'typecheck:contracts-ready'],
  179. ['lint', 'lint:contracts-ready'],
  180. ['doc-typecheck', 'doc-typecheck:contracts-ready'],
  181. ] as const) {
  182. expect(subject.find(item => item.id === id)).toMatchObject({
  183. displayCommand: `pnpm run ${script}`,
  184. args: ['/private/pnpm.cjs', 'run', script],
  185. needs: ['typert-contracts'],
  186. })
  187. }
  188. expect(subject.find(item => item.id === 'build')?.needs).toEqual([
  189. 'typecheck',
  190. 'lint',
  191. 'doc-typecheck',
  192. ])
  193. })
  194. it('reuses contracts from the validated consumer build', () => {
  195. const subject = withPnpmEntrypoint(() => gatesForMode('ci-consumers'))
  196. expect(subject.find(item => item.id === 'lint-and-duplication')).toMatchObject({
  197. displayCommand: 'pnpm run check:ci:lint:contracts-ready',
  198. args: ['/private/pnpm.cjs', 'run', 'check:ci:lint:contracts-ready'],
  199. })
  200. expect(subject.find(item => item.id === 'doc-typecheck')).toMatchObject({
  201. displayCommand: 'pnpm run doc-typecheck:contracts-ready',
  202. args: ['/private/pnpm.cjs', 'run', 'doc-typecheck:contracts-ready'],
  203. })
  204. })
  205. it('keeps standalone doc sync responsible for preparation', () => {
  206. const docTypecheck = withPnpmEntrypoint(() =>
  207. gatesForMode('doc-sync').find(item => item.id === 'doc-typecheck'))
  208. expect(docTypecheck?.displayCommand).toBe('pnpm run doc-typecheck')
  209. })
  210. })
  211. describe('Node compatibility graph', () => {
  212. it('runs the jsdom environment smoke on every advertised Node line', () => {
  213. const subject = withPnpmEntrypoint(() => gatesForMode('node-compat'))
  214. expect(subject.find(item => item.id === 'vitest-jsdom-smoke')).toMatchObject({
  215. label: 'Vitest jsdom smoke',
  216. args: [
  217. '/private/pnpm.cjs',
  218. 'exec',
  219. 'vitest',
  220. 'run',
  221. 'scripts/vitest-environment.compat.spec.ts',
  222. ],
  223. })
  224. })
  225. })
  226. describe('Node 24 lane ownership', () => {
  227. it('keeps the static lane source-only', () => {
  228. const subject = withPnpmEntrypoint(() => gatesForMode('ci-static'))
  229. expect(subject.map(item => item.id)).not.toContain('build')
  230. expect(subject.map(item => item.id)).not.toContain('doc-typecheck')
  231. })
  232. it('owns the build and orders its artifact consumers', () => {
  233. const subject = withPnpmEntrypoint(() => gatesForMode('ci-consumers'))
  234. expect(defaultConcurrency('ci-consumers', subject.length, 4)).toEqual({
  235. workers: 10,
  236. source: 'ci-consumers gate count',
  237. })
  238. expect(subject.map(item => item.id)).toEqual([
  239. 'build',
  240. 'node-compat',
  241. 'publint',
  242. 'built-package-invariants',
  243. 'lint-and-duplication',
  244. 'snapshot',
  245. 'web-snapshot',
  246. 'doc-typecheck',
  247. 'node-next-types',
  248. 'built-bin-smoke',
  249. ])
  250. expect(subject.find(item => item.id === 'publint')?.needs).toEqual(['build'])
  251. expect(subject.find(item => item.id === 'built-package-invariants')?.needs).toEqual(['publint'])
  252. expect(subject.find(item => item.id === 'lint-and-duplication')?.needs).toEqual(['built-package-invariants'])
  253. for (const id of [
  254. 'snapshot',
  255. 'web-snapshot',
  256. 'doc-typecheck',
  257. 'node-next-types',
  258. 'built-bin-smoke',
  259. ]) {
  260. expect(subject.find(item => item.id === id)?.needs).toEqual(['built-package-invariants'])
  261. }
  262. expect(subject.find(item => item.id === 'snapshot')?.env).toEqual({ DSH_EXAMPLE_MODE: 'lib' })
  263. expect(subject.find(item => item.id === 'doc-typecheck')?.env).toEqual({
  264. DSH_DOC_TYPECHECK_USE_BUILD_OUTPUT: '1',
  265. })
  266. expect(subject.find(item => item.id === 'built-bin-smoke')?.args).toEqual(
  267. expect.arrayContaining([
  268. 'packages/subagent/subagent-codex/tests/loader-composition.e2e.ts',
  269. 'packages/subagent/subagent-claude-code/tests/loader-composition.e2e.ts',
  270. ]),
  271. )
  272. expect(subject.find(item => item.id === 'web-snapshot')).toMatchObject({
  273. displayCommand: 'DSH_SNAPSHOT=replay pnpm run test:web:built',
  274. env: { DSH_SNAPSHOT: 'replay' },
  275. })
  276. })
  277. })
  278. describe('Linux primary graph', () => {
  279. it('adds the same compare-only web gate after built client artifacts', () => {
  280. const subject = withPnpmEntrypoint(() => gatesForMode('ci-linux-primary'))
  281. const web = subject.find(item => item.id === 'web-snapshot')
  282. expect(web).toMatchObject({
  283. displayCommand: 'DSH_SNAPSHOT=replay pnpm run test:web:built',
  284. env: { DSH_SNAPSHOT: 'replay' },
  285. needs: ['built-package-invariants'],
  286. })
  287. })
  288. })
  289. describe('gate process outcomes', () => {
  290. it.skipIf(process.platform === 'win32')('reports signal termination independently from exit status', async () => {
  291. const result = await runGate(gate('terminated', {
  292. args: ['-e', "process.kill(process.pid, 'SIGTERM')"],
  293. }))
  294. expect(result.status).toBe('failed')
  295. expect(result.exitCode).toBeNull()
  296. expect(result.signalCode).toBe('SIGTERM')
  297. expect(formatGateResultReason(result)).toBe('signal SIGTERM')
  298. })
  299. })