ci-workflow.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import { readFileSync } from 'node:fs'
  2. import { resolve } from 'node:path'
  3. import * as yaml from 'js-yaml'
  4. import { describe, expect, it } from 'vitest'
  5. const root = resolve(import.meta.dirname, '..')
  6. const runnerPrivatePnpmDestination = '${{ runner.temp }}/setup-pnpm'
  7. describe('CI workflow', () => {
  8. it('isolates every pnpm action setup destination per runner', () => {
  9. const workflow: unknown = yaml.load(readFileSync(resolve(root, '.github/workflows/ci.yml'), 'utf8'))
  10. if (!isRecord(workflow) || !isRecord(workflow.jobs)) throw new TypeError('CI workflow must define jobs')
  11. const setups = Object.entries(workflow.jobs).flatMap(([jobName, job]) => {
  12. if (!isRecord(job) || !Array.isArray(job.steps)) return []
  13. return job.steps.flatMap((step) => {
  14. if (!isRecord(step) || typeof step.uses !== 'string' || !step.uses.startsWith('pnpm/action-setup@')) return []
  15. return [{ jobName, step }]
  16. })
  17. })
  18. expect(setups.length).toBeGreaterThan(0)
  19. for (const { jobName, step } of setups) {
  20. expect(step, `${jobName} must not share pnpm/action-setup's default destination`).toMatchObject({
  21. with: { dest: runnerPrivatePnpmDestination },
  22. })
  23. }
  24. })
  25. it('keeps a required Wine Windows job, a non-blocking native Windows job with failover, and a master-only standby', () => {
  26. const workflow = loadWorkflow('.github/workflows/ci.yml')
  27. if (!isRecord(workflow.jobs)
  28. || !isRecord(workflow.jobs.windows)
  29. || !isRecord(workflow.jobs['windows-native'])
  30. || !isRecord(workflow.jobs['wine-apt-cache'])
  31. || !isRecord(workflow.jobs['serial-windows'])
  32. || !isRecord(workflow.jobs['all-checks-passed'])) {
  33. throw new TypeError('CI workflow must define windows, windows-native, wine-apt-cache, serial-windows, and all-checks-passed jobs')
  34. }
  35. const windows = workflow.jobs.windows
  36. const windowsNative = workflow.jobs['windows-native']
  37. const wineAptCache = workflow.jobs['wine-apt-cache']
  38. const serialWindows = workflow.jobs['serial-windows']
  39. const aggregate = workflow.jobs['all-checks-passed']
  40. if (!Array.isArray(windows.steps) || !Array.isArray(aggregate.needs)) {
  41. throw new TypeError('Windows job must define steps and the aggregate must define needs')
  42. }
  43. const commandSteps = windows.steps.filter((step): step is Record<string, unknown> & { run: string } => (
  44. isRecord(step) && typeof step.run === 'string'
  45. ))
  46. // Required PR job: Wine on ubuntu-latest, runs wine-windows-gates.sh.
  47. expect(windows['runs-on']).toBe('ubuntu-latest')
  48. expect(windows.name).toBe('windows node 24 / wine blocking')
  49. expect(windows.if).toBe("github.event_name == 'pull_request'")
  50. expect(commandSteps.some(step => step.run.includes('wine-windows-gates.sh'))).toBe(true)
  51. // windows-native: non-blocking native job with failover, runs windows-complete.
  52. expect(typeof windowsNative['runs-on']).toBe('string')
  53. expect(windowsNative['runs-on']).toContain('DSH_CI_FAILOVER')
  54. expect(windowsNative['runs-on']).toContain('self-hosted')
  55. expect(windowsNative['runs-on']).toContain('dsh-win-ci')
  56. expect(windowsNative['runs-on']).toContain('dsh-windows-2025-16core')
  57. expect(windowsNative.name).toBe('windows node 24 / native complete')
  58. expect(windowsNative.if).toBe("github.event_name == 'pull_request'")
  59. const nativeCommandSteps = (windowsNative.steps as unknown[]).filter((step): step is Record<string, unknown> & { run: string } => (
  60. isRecord(step) && typeof step.run === 'string'
  61. ))
  62. expect(nativeCommandSteps.map(step => step.run)).toContain('pnpm run check:ci:windows-complete')
  63. // wine-apt-cache: master-only, seeds the Wine apt cache.
  64. expect(wineAptCache.if).toBe("github.event_name == 'push' && github.ref == 'refs/heads/master'")
  65. expect(wineAptCache['runs-on']).toBe('ubuntu-latest')
  66. // serial-windows: master-only standby, self-hosted, non-blocking.
  67. expect(serialWindows.if).toBe("github.event_name == 'push' && github.ref == 'refs/heads/master'")
  68. expect(serialWindows['runs-on']).toEqual(['self-hosted', 'dsh-win-ci', 'windows'])
  69. expect(serialWindows.name).toBe('serial / windows (self-hosted standby)')
  70. // Aggregate: Wine `windows` required, native `windows-native` excluded.
  71. expect(aggregate.needs).toContain('windows')
  72. expect(aggregate.needs).not.toContain('windows-native')
  73. expect(aggregate.needs).not.toContain('serial-windows')
  74. })
  75. it('leaves push runs uncancelled, so the self-hosted standby drills reach a verdict', () => {
  76. const workflow = loadWorkflow('.github/workflows/ci.yml')
  77. if (!isRecord(workflow.jobs) || !isRecord(workflow.concurrency)) {
  78. throw new TypeError('CI workflow must define jobs and a workflow-level concurrency block')
  79. }
  80. // Cancellation applies to the whole superseded RUN, so this has to be
  81. // decided at workflow level and gated on the event: a job-level group
  82. // cannot exempt its job from its run being cancelled. Only push is exempt —
  83. // a drill takes longer than the interval between master merges. The negated
  84. // form is load-bearing: `== 'pull_request'` would also stop cancelling
  85. // workflow_dispatch, and a re-dispatched runner benchmark holds up to 12
  86. // larger runners for 15 minutes in this same group on master.
  87. expect(workflow.concurrency['cancel-in-progress']).toBe("${{ github.event_name != 'push' }}")
  88. // Neither drill may re-introduce a job-level group: it would not help, and
  89. // it would imply the run-scoped cancellation had been solved locally.
  90. for (const name of ['serial-linux-selfhosted', 'serial-windows']) {
  91. const job = workflow.jobs[name]
  92. if (!isRecord(job)) throw new TypeError(`${name} must be defined`)
  93. expect(job.concurrency).toBeUndefined()
  94. // Both stay master-push-only; that is what makes the push carve-out safe.
  95. expect(job.if).toBe("github.event_name == 'push' && github.ref == 'refs/heads/master'")
  96. }
  97. // What bounds the cost of never cancelling a push run: a master push may
  98. // only carry the cache seeder and the two drills. Any job reachable on push
  99. // would start accumulating uncancelled runs, so the set is pinned here.
  100. //
  101. // Classification is an exact allowlist of the conditions in use, not a
  102. // substring match: `github.event_name != 'pull_request'` mentions
  103. // `pull_request` yet IS push-reachable, so matching on the event name alone
  104. // would silently misclassify it as gated.
  105. const NOT_PUSH_REACHABLE = new Set([
  106. "github.event_name == 'pull_request'",
  107. "always() && github.event_name == 'pull_request'",
  108. "github.event_name == 'workflow_dispatch' && inputs.suite == 'larger-runner-benchmark'",
  109. "github.event_name == 'workflow_dispatch' && inputs.suite == 'consolidated-runner-benchmark'",
  110. ])
  111. const pushReachable = Object.entries(workflow.jobs)
  112. .filter(([, job]) => {
  113. if (!isRecord(job)) return false
  114. if (job.if === undefined) return true // unconditional: runs on every event
  115. if (job.if === false) return false // `if: false` parses as a boolean
  116. if (typeof job.if !== 'string') return true // unrecognized shape: surface it
  117. return !NOT_PUSH_REACHABLE.has(job.if.trim())
  118. })
  119. .map(([name]) => name)
  120. .sort()
  121. expect(pushReachable).toEqual(['serial-linux-selfhosted', 'serial-windows', 'wine-apt-cache'])
  122. // Why workflow_dispatch must keep cancelling: each benchmark fans out to a
  123. // dozen larger runners at once, in this same group on master. If it stopped
  124. // cancelling, a re-dispatch would queue ahead of a drill instead of
  125. // replacing the stale measurement.
  126. for (const name of ['larger-runner-benchmark', 'consolidated-runner-benchmark']) {
  127. const job = workflow.jobs[name]
  128. if (!isRecord(job) || !isRecord(job.strategy)) {
  129. throw new TypeError(`${name} must define a matrix strategy`)
  130. }
  131. expect(job.strategy['max-parallel']).toBe(12)
  132. expect(job['timeout-minutes']).toBe(15)
  133. }
  134. })
  135. it('keeps supported LSP source under native Windows coverage', () => {
  136. const config = readFileSync(resolve(root, 'vitest.config.ts'), 'utf8')
  137. expect(config).not.toContain('packages/lsp/lsp-local/src/connection.ts')
  138. expect(config).not.toContain('packages/lsp/lsp-local/src/index.ts')
  139. expect(config).not.toContain('packages/lsp/lsp-local/src/instance.ts')
  140. })
  141. it('keeps every Vitest project process-isolated on native Windows', () => {
  142. const config = readFileSync(resolve(root, 'vitest.config.ts'), 'utf8')
  143. expect(config).not.toContain("pool: process.platform === 'win32' ? 'threads' : 'forks'")
  144. expect(config.match(/pool: 'forks'/g)).toHaveLength(2)
  145. })
  146. })
  147. describe('E2B e2e workflow', () => {
  148. it('is manual-only and fails loud before running the focused live suite', () => {
  149. const workflow = loadWorkflow('.github/workflows/e2b-e2e.yml')
  150. expect(workflow.on).toEqual({ workflow_dispatch: null })
  151. if (!isRecord(workflow.jobs) || !isRecord(workflow.jobs.e2b) || !Array.isArray(workflow.jobs.e2b.steps)) {
  152. throw new TypeError('E2B e2e workflow must define the e2b job steps')
  153. }
  154. const steps = workflow.jobs.e2b.steps.filter(isRecord)
  155. const preflight = steps.find(step => step.name === 'Preflight (require E2B API key)')
  156. const e2b = steps.find(step => step.name === 'E2B tests (live sandbox)')
  157. expect(preflight).toMatchObject({
  158. env: { E2B_API_KEY: '${{ secrets.E2B_API_KEY_EXTERNAL }}' },
  159. })
  160. expect(preflight?.run).toContain('E2B_API_KEY_EXTERNAL repository secret')
  161. expect(e2b).toMatchObject({
  162. env: {
  163. E2B_API_KEY: '${{ secrets.E2B_API_KEY_EXTERNAL }}',
  164. DSH_E2E_MAX_WORKERS: '1',
  165. DSH_EXAMPLE_MODE: 'lib',
  166. },
  167. })
  168. expect(e2b?.run).toContain('packages/e2b/e2b/tests/composition.e2e.ts')
  169. })
  170. })
  171. describe('Issue lifecycle workflow', () => {
  172. it('uses explicit review handoff events without rerunning when a draft becomes ready', () => {
  173. const lifecycle = loadWorkflow('.github/workflows/issue-lifecycle.yml')
  174. const lifecyclePullRequest = workflowEvent(lifecycle, 'pull_request')
  175. const lifecycleReview = workflowEvent(lifecycle, 'pull_request_review')
  176. const lifecycleJob = workflowJob(lifecycle, 'lifecycle')
  177. const policy = loadWorkflow('.github/workflows/issue-policy.yml')
  178. const policyPullRequest = workflowEvent(policy, 'pull_request')
  179. expect(lifecyclePullRequest.types).not.toContain('ready_for_review')
  180. expect(lifecyclePullRequest.types).toContain('review_requested')
  181. expect(lifecycleReview.types).toEqual(['submitted'])
  182. expect(lifecycleJob.if).toBe(
  183. "${{ github.event_name != 'pull_request_review' || (github.event.action == 'submitted' && github.event.review.state == 'changes_requested') }}",
  184. )
  185. expect(policyPullRequest.types).toContain('ready_for_review')
  186. })
  187. })
  188. describe('Git hooks', () => {
  189. it('leaves frozen Agent Note sidecars to the archive verifier', () => {
  190. const lefthook = loadWorkflow('lefthook.yml')
  191. for (const hookName of ['pre-commit', 'pre-merge-commit']) {
  192. const hook = lefthook[hookName]
  193. if (!isRecord(hook) || !Array.isArray(hook.jobs)) {
  194. throw new TypeError(`lefthook must define ${hookName} jobs`)
  195. }
  196. const pairing: unknown = hook.jobs.find(
  197. (job: unknown) => isRecord(job) && job.name === 'translation pairing (staged records)',
  198. )
  199. expect(pairing).toMatchObject({ exclude: ['.agents/notes/archived/**'] })
  200. }
  201. })
  202. })
  203. function loadWorkflow(path: string): Record<string, unknown> {
  204. const workflow: unknown = yaml.load(readFileSync(resolve(root, path), 'utf8'))
  205. if (!isRecord(workflow)) throw new TypeError(`${path} must define a workflow`)
  206. return workflow
  207. }
  208. function workflowEvent(workflow: Record<string, unknown>, event: string): Record<string, unknown> {
  209. if (!isRecord(workflow.on) || !isRecord(workflow.on[event])) {
  210. throw new TypeError(`workflow must define the ${event} event`)
  211. }
  212. return workflow.on[event]
  213. }
  214. function workflowJob(workflow: Record<string, unknown>, job: string): Record<string, unknown> {
  215. if (!isRecord(workflow.jobs) || !isRecord(workflow.jobs[job])) {
  216. throw new TypeError(`workflow must define the ${job} job`)
  217. }
  218. return workflow.jobs[job]
  219. }
  220. function isRecord(value: unknown): value is Record<string, unknown> {
  221. return typeof value === 'object' && value !== null && !Array.isArray(value)
  222. }