run-gates.spec.ts 10 KB

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