ci-workflow.spec.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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('exempts push from cancellation, so one master merge does not cancel the running drill', () => {
  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. The
  87. // expression is evaluated against the NEWLY TRIGGERED run, so a dispatch on
  88. // master still cancels a mid-flight drill; the runbook records that bound.
  89. expect(workflow.concurrency['cancel-in-progress']).toBe("${{ github.event_name != 'push' }}")
  90. // Neither drill may carry a job-level group: it would not exempt the job
  91. // from run-scoped cancellation.
  92. for (const name of ['serial-linux-selfhosted', 'serial-windows']) {
  93. const job = workflow.jobs[name]
  94. if (!isRecord(job)) throw new TypeError(`${name} must be defined`)
  95. expect(job.concurrency).toBeUndefined()
  96. // Both stay master-push-only; that is what makes the push carve-out safe.
  97. expect(job.if).toBe("github.event_name == 'push' && github.ref == 'refs/heads/master'")
  98. }
  99. // What bounds the cost of exempting push: a master push may only carry the
  100. // cache seeder and the two drills. Any job reachable on push would start
  101. // accumulating uncancelled runs, so the set is pinned here.
  102. //
  103. // Classification is an exact allowlist of the conditions in use, not a
  104. // substring match: `github.event_name != 'pull_request'` mentions
  105. // `pull_request` yet IS push-reachable, so matching on the event name alone
  106. // would silently misclassify it as gated.
  107. const NOT_PUSH_REACHABLE = new Set([
  108. "github.event_name == 'pull_request'",
  109. "always() && github.event_name == 'pull_request'",
  110. "github.event_name == 'workflow_dispatch' && inputs.suite == 'larger-runner-benchmark'",
  111. "github.event_name == 'workflow_dispatch' && inputs.suite == 'consolidated-runner-benchmark'",
  112. ])
  113. const pushReachable = Object.entries(workflow.jobs)
  114. .filter(([, job]) => {
  115. if (!isRecord(job)) return false
  116. if (job.if === undefined) return true // unconditional: runs on every event
  117. if (job.if === false) return false // `if: false` parses as a boolean
  118. if (typeof job.if !== 'string') return true // unrecognized shape: surface it
  119. return !NOT_PUSH_REACHABLE.has(job.if.trim())
  120. })
  121. .map(([name]) => name)
  122. .sort()
  123. expect(pushReachable).toEqual(['serial-linux-selfhosted', 'serial-windows', 'wine-apt-cache'])
  124. // Why workflow_dispatch must keep cancelling: each benchmark fans out to a
  125. // dozen larger runners at once, in this same group on master. If it stopped
  126. // cancelling, a re-dispatch would queue ahead of a drill instead of
  127. // replacing the stale measurement.
  128. for (const name of ['larger-runner-benchmark', 'consolidated-runner-benchmark']) {
  129. const job = workflow.jobs[name]
  130. if (!isRecord(job) || !isRecord(job.strategy)) {
  131. throw new TypeError(`${name} must define a matrix strategy`)
  132. }
  133. expect(job.strategy['max-parallel']).toBe(12)
  134. expect(job['timeout-minutes']).toBe(15)
  135. }
  136. })
  137. it('keeps supported LSP source under native Windows coverage', () => {
  138. const config = readFileSync(resolve(root, 'vitest.config.ts'), 'utf8')
  139. expect(config).not.toContain('packages/lsp/lsp-stdio/src/connection.ts')
  140. expect(config).not.toContain('packages/lsp/lsp-stdio/src/index.ts')
  141. expect(config).not.toContain('packages/lsp/lsp-stdio/src/instance.ts')
  142. })
  143. it('requires one release-shaped Python runtime target on every pull request', () => {
  144. const workflow = loadWorkflow('.github/workflows/ci.yml')
  145. const pythonRuntime = workflowJob(workflow, 'python-runtime')
  146. const aggregate = workflowJob(workflow, 'all-checks-passed')
  147. if (!Array.isArray(aggregate.needs)) {
  148. throw new TypeError('CI aggregate must define required job dependencies')
  149. }
  150. expect(pythonRuntime).toMatchObject({
  151. if: "github.event_name == 'pull_request'",
  152. name: 'python runtime / release-shaped Linux x64',
  153. uses: './.github/workflows/build-exe-for-python-sdk.yml',
  154. with: {
  155. targets: 'node24-linux-x64',
  156. ci: true,
  157. },
  158. })
  159. expect(aggregate.needs).toContain('python-runtime')
  160. })
  161. it('keeps every Vitest project process-isolated on native Windows', () => {
  162. const config = readFileSync(resolve(root, 'vitest.config.ts'), 'utf8')
  163. expect(config).not.toContain("pool: process.platform === 'win32' ? 'threads' : 'forks'")
  164. expect(config.match(/pool: 'forks'/g)).toHaveLength(2)
  165. })
  166. })
  167. describe('E2B e2e workflow', () => {
  168. it('is manual-only and fails loud before running the focused live suite', () => {
  169. const workflow = loadWorkflow('.github/workflows/e2b-e2e.yml')
  170. expect(workflow.on).toEqual({ workflow_dispatch: null })
  171. if (!isRecord(workflow.jobs) || !isRecord(workflow.jobs.e2b) || !Array.isArray(workflow.jobs.e2b.steps)) {
  172. throw new TypeError('E2B e2e workflow must define the e2b job steps')
  173. }
  174. const steps = workflow.jobs.e2b.steps.filter(isRecord)
  175. const preflight = steps.find(step => step.name === 'Preflight (require E2B API key)')
  176. const e2b = steps.find(step => step.name === 'E2B tests (live sandbox)')
  177. expect(preflight).toMatchObject({
  178. env: { E2B_API_KEY: '${{ secrets.E2B_API_KEY_EXTERNAL }}' },
  179. })
  180. expect(preflight?.run).toContain('E2B_API_KEY_EXTERNAL repository secret')
  181. expect(e2b).toMatchObject({
  182. env: {
  183. E2B_API_KEY: '${{ secrets.E2B_API_KEY_EXTERNAL }}',
  184. DSH_E2E_MAX_WORKERS: '1',
  185. DSH_EXAMPLE_MODE: 'lib',
  186. },
  187. })
  188. expect(e2b?.run).toContain('packages/e2b/e2b/tests/composition.e2e.ts')
  189. })
  190. })
  191. describe('Python release workflows', () => {
  192. it('keeps complete wheel validation separate from protected public publication', () => {
  193. const workflow = loadWorkflow('.github/workflows/python-release.yml')
  194. const dispatch = workflowEvent(workflow, 'workflow_dispatch')
  195. const pullRequest = workflowEvent(workflow, 'pull_request')
  196. const build = workflowJob(workflow, 'build')
  197. const pythonCompat = workflowJob(workflow, 'python-compat')
  198. const validate = workflowJob(workflow, 'validate')
  199. const publishRuntime = workflowJob(workflow, 'publish-runtime')
  200. const publishSdk = workflowJob(workflow, 'publish-sdk')
  201. if (!isRecord(dispatch.inputs)
  202. || !isRecord(dispatch.inputs.publish)
  203. || !Array.isArray(pythonCompat.steps)
  204. || !Array.isArray(validate.steps)
  205. || !Array.isArray(publishRuntime.steps)
  206. || !Array.isArray(publishSdk.steps)) {
  207. throw new TypeError('Python release workflow must define publish input and release steps')
  208. }
  209. expect(dispatch.inputs.publish).toMatchObject({ type: 'boolean', default: false })
  210. expect(pullRequest).toEqual({ types: ['labeled'] })
  211. expect(build).toMatchObject({
  212. if: "github.event_name == 'workflow_dispatch' || github.event.label.name == 'python-release-dry-run'",
  213. uses: './.github/workflows/build-exe-for-python-sdk.yml',
  214. with: {
  215. targets: 'node24-linux-x64,node24-linux-arm64,node24-macos-arm64',
  216. release: true,
  217. },
  218. })
  219. expect(pythonCompat.strategy).toMatchObject({ matrix: { python: ['3.10', '3.14'] } })
  220. expect(JSON.stringify(pythonCompat.steps)).toContain('deepseek-harness-sdk==${{ steps.compatibility-version.outputs.version }}')
  221. const validateSteps = JSON.stringify(validate.steps)
  222. const authorize = validate.steps.filter(isRecord).find(step => step.name === 'Authorize publication request')
  223. if (!isRecord(authorize) || typeof authorize.run !== 'string') {
  224. throw new TypeError('Python release validation must authorize publication requests')
  225. }
  226. expect(validateSteps).toContain('PUBLIC_PYPI_RELEASE_ENABLED')
  227. expect(authorize).toMatchObject({
  228. env: {
  229. PYPI_PUBLISHER_REPOSITORY: '${{ vars.PYPI_PUBLISHER_REPOSITORY }}',
  230. REPOSITORY: '${{ github.repository }}',
  231. },
  232. })
  233. expect(authorize.run).toContain('[ "$REPOSITORY" = "$PYPI_PUBLISHER_REPOSITORY" ]')
  234. expect(validateSteps).toContain('100000000')
  235. expect(publishRuntime).toMatchObject({
  236. if: "github.event_name == 'workflow_dispatch' && inputs.publish",
  237. needs: 'validate',
  238. environment: 'pypi-runtime',
  239. permissions: { contents: 'read', 'id-token': 'write' },
  240. })
  241. expect(publishSdk).toMatchObject({
  242. if: "github.event_name == 'workflow_dispatch' && inputs.publish",
  243. needs: ['validate', 'publish-runtime'],
  244. environment: 'pypi',
  245. permissions: { contents: 'read', 'id-token': 'write' },
  246. })
  247. const runtimeSteps = publishRuntime.steps.filter(isRecord)
  248. const sdkSteps = publishSdk.steps.filter(isRecord)
  249. const runtimePublish = runtimeSteps.find(step => step.name === 'Publish runtime wheels')
  250. const sdkPublish = sdkSteps.find(step => step.name === 'Publish SDK wheel')
  251. const runtimeHashes = runtimeSteps.find(step => step.name === 'Verify release artifact hashes')
  252. const sdkHashes = sdkSteps.find(step => step.name === 'Verify release artifact hashes')
  253. expect([...runtimeSteps, ...sdkSteps].some(
  254. step => typeof step.uses === 'string' && step.uses.startsWith('actions/checkout@'),
  255. )).toBe(false)
  256. expect([...runtimeSteps, ...sdkSteps].filter(
  257. step => step.uses === 'pypa/gh-action-pypi-publish@release/v1',
  258. )).toHaveLength(2)
  259. expect(runtimePublish).toMatchObject({
  260. with: { 'packages-dir': 'dist/runtime/', attestations: false },
  261. })
  262. expect(sdkPublish).toMatchObject({
  263. with: { 'packages-dir': 'dist/sdk/', attestations: false },
  264. })
  265. expect(runtimeHashes).toMatchObject({ run: 'cd dist && sha256sum -c SHA256SUMS' })
  266. expect(sdkHashes).toMatchObject({ run: 'cd dist && sha256sum -c SHA256SUMS' })
  267. })
  268. it('exposes the native wheel builder to the release caller with normalized versions', () => {
  269. const workflow = loadWorkflow('.github/workflows/build-exe-for-python-sdk.yml')
  270. const call = workflowEvent(workflow, 'workflow_call')
  271. const plan = workflowJob(workflow, 'plan')
  272. const build = workflowJob(workflow, 'build')
  273. if (!isRecord(call.inputs) || !Array.isArray(plan.steps) || !Array.isArray(build.steps)) {
  274. throw new TypeError('Python wheel builder must define workflow_call inputs and plan steps')
  275. }
  276. const buildSteps: unknown[] = build.steps
  277. const manylinuxAddon = buildSteps.find(step => isRecord(step) && step.name === 'Rebuild Linux node-pty against manylinux 2.28')
  278. const macosCheck = buildSteps.find(step => isRecord(step) && step.name === 'Check macOS deployment target')
  279. const manylinuxSmoke = buildSteps.find(step => isRecord(step) && step.name === 'Run wheel in a manylinux 2.28 container')
  280. expect(call.inputs).toHaveProperty('targets')
  281. expect(call.inputs).toMatchObject({
  282. ci: { type: 'boolean', default: false },
  283. release: { type: 'boolean', default: false },
  284. })
  285. expect(workflow.concurrency).toMatchObject({
  286. group: 'build-single-exe-${{ github.workflow }}-${{ github.ref }}',
  287. })
  288. expect(plan.if).toContain('inputs.ci')
  289. expect(plan.if).toContain('inputs.release')
  290. expect(JSON.stringify(plan.steps)).toContain('pep440_version')
  291. expect(JSON.stringify(workflow)).toContain('macosx_14_0_arm64')
  292. expect(manylinuxAddon).toMatchObject({ if: "runner.os == 'Linux'" })
  293. expect(JSON.stringify(manylinuxAddon)).toContain('manylinux_2_28_x86_64')
  294. expect(JSON.stringify(manylinuxAddon)).toContain('manylinux_2_28_aarch64')
  295. expect(JSON.stringify(manylinuxAddon)).toContain('$HOME/setup-pnpm:$HOME/setup-pnpm:ro')
  296. expect(JSON.stringify(manylinuxAddon)).toContain('node-pty-glibc-versions.txt')
  297. expect(JSON.stringify(manylinuxAddon)).toContain('le 2.28')
  298. expect(macosCheck).toMatchObject({ if: "runner.os == 'macOS'" })
  299. expect(JSON.stringify(macosCheck)).toContain('scripts/check-macos-deployment-target.py')
  300. expect(JSON.stringify(macosCheck)).toContain('$EXE-spawn-helper')
  301. expect(manylinuxSmoke).toMatchObject({ if: "runner.os == 'Linux'" })
  302. expect(JSON.stringify(manylinuxSmoke)).toContain('-e DSH_TELEMETRY_DISABLED')
  303. })
  304. it('uses the shared macOS deployment-target check in GitLab', () => {
  305. const workflow = loadWorkflow('.gitlab-ci.yml')
  306. const runtimeWheel = workflow['.runtime-wheel']
  307. if (!isRecord(runtimeWheel) || !Array.isArray(runtimeWheel.script)) {
  308. throw new TypeError('GitLab CI must define the runtime wheel script')
  309. }
  310. const runtimeScript: unknown[] = runtimeWheel.script
  311. const macosCheck = runtimeScript.find(
  312. step => typeof step === 'string' && step.includes('PLATFORM" = macos-arm64'),
  313. )
  314. if (typeof macosCheck !== 'string') {
  315. throw new TypeError('GitLab CI must check the macOS deployment target')
  316. }
  317. expect(macosCheck).toContain('scripts/check-macos-deployment-target.py')
  318. expect(macosCheck).toContain('"$EXE" "$EXE-spawn-helper"')
  319. })
  320. })
  321. describe('Issue lifecycle workflow', () => {
  322. it('uses explicit review handoff events without rerunning when a draft becomes ready', () => {
  323. const lifecycle = loadWorkflow('.github/workflows/issue-lifecycle.yml')
  324. const lifecyclePullRequest = workflowEvent(lifecycle, 'pull_request')
  325. const lifecycleReview = workflowEvent(lifecycle, 'pull_request_review')
  326. const lifecycleJob = workflowJob(lifecycle, 'lifecycle')
  327. const policy = loadWorkflow('.github/workflows/issue-policy.yml')
  328. const policyPullRequest = workflowEvent(policy, 'pull_request')
  329. expect(lifecyclePullRequest.types).not.toContain('ready_for_review')
  330. expect(lifecyclePullRequest.types).toContain('review_requested')
  331. expect(lifecycleReview.types).toEqual(['submitted'])
  332. expect(lifecycleJob.if).toBe(
  333. "${{ github.event_name != 'pull_request_review' || (github.event.action == 'submitted' && github.event.review.state == 'changes_requested') }}",
  334. )
  335. expect(policyPullRequest.types).toContain('ready_for_review')
  336. })
  337. })
  338. describe('Git hooks', () => {
  339. it('leaves frozen Agent Note sidecars to the archive verifier', () => {
  340. const lefthook = loadWorkflow('lefthook.yml')
  341. for (const hookName of ['pre-commit', 'pre-merge-commit']) {
  342. const hook = lefthook[hookName]
  343. if (!isRecord(hook) || !Array.isArray(hook.jobs)) {
  344. throw new TypeError(`lefthook must define ${hookName} jobs`)
  345. }
  346. const pairing: unknown = hook.jobs.find(
  347. (job: unknown) => isRecord(job) && job.name === 'translation pairing (staged records)',
  348. )
  349. expect(pairing).toMatchObject({ exclude: ['.agents/notes/archived/**'] })
  350. }
  351. })
  352. })
  353. function loadWorkflow(path: string): Record<string, unknown> {
  354. const workflow: unknown = yaml.load(readFileSync(resolve(root, path), 'utf8'))
  355. if (!isRecord(workflow)) throw new TypeError(`${path} must define a workflow`)
  356. return workflow
  357. }
  358. function workflowEvent(workflow: Record<string, unknown>, event: string): Record<string, unknown> {
  359. if (!isRecord(workflow.on) || !isRecord(workflow.on[event])) {
  360. throw new TypeError(`workflow must define the ${event} event`)
  361. }
  362. return workflow.on[event]
  363. }
  364. function workflowJob(workflow: Record<string, unknown>, job: string): Record<string, unknown> {
  365. if (!isRecord(workflow.jobs) || !isRecord(workflow.jobs[job])) {
  366. throw new TypeError(`workflow must define the ${job} job`)
  367. }
  368. return workflow.jobs[job]
  369. }
  370. function isRecord(value: unknown): value is Record<string, unknown> {
  371. return typeof value === 'object' && value !== null && !Array.isArray(value)
  372. }