Files
orca/config/scripts/ci-closed-pr-caches.test.mjs
T
Neil d05080175b perf(ci): stop duplicating shared Linux download caches (#23578)
* perf(ci): pilot pnpm verification record caching on Linux

* test(ci): review pnpm verification record in mobile cache audit

* perf(ci): share Electron downloads and clean closed PR caches

* test(ci): retain cache ordering checks for restore-only consumers

* perf(ci): limit archive sharing rollout to primed Linux hosts
2026-09-28 01:58:46 -07:00

58 lines
2.2 KiB
JavaScript

import { readFileSync } from 'node:fs'
import { runInNewContext } from 'node:vm'
import { expect, it, vi } from 'vitest'
import { parse } from 'yaml'
const workflow = parse(readFileSync('.github/workflows/ci-closed-pr-caches.yml', 'utf8'))
const script = workflow.jobs.clean.steps[0].with.script
const ref = 'refs/pull/123/merge'
function run(caches, remove = vi.fn().mockResolvedValue(undefined)) {
const list = vi.fn()
const paginate = vi.fn().mockResolvedValue(caches)
const result = runInNewContext(`(async () => { ${script} })()`, {
context: { repo: { owner: 'owner', repo: 'repo' }, payload: { pull_request: { number: 123 } } },
github: {
paginate,
rest: { actions: { getActionsCacheList: list, deleteActionsCacheById: remove } }
},
core: { info: vi.fn() }
})
return { result, remove, paginate, list }
}
it('uses default-branch code without checking out a closed PR', () => {
expect(workflow.on).toEqual({ pull_request_target: { types: ['closed'] } })
expect(workflow.permissions).toEqual({ actions: 'write' })
expect(workflow.jobs.clean.steps).toHaveLength(1)
expect(workflow.jobs.clean.steps[0].uses).toBe('actions/github-script@v8')
})
it('lists and deletes only caches scoped to the closed merge ref', async () => {
const { result, paginate, list, remove } = run([
{ id: 12, ref },
{ id: 13, ref }
])
await result
expect(paginate).toHaveBeenCalledWith(list, { owner: 'owner', repo: 'repo', ref, per_page: 100 })
expect(remove.mock.calls).toEqual([
[{ owner: 'owner', repo: 'repo', cache_id: 12 }],
[{ owner: 'owner', repo: 'repo', cache_id: 13 }]
])
})
it('refuses a default-branch cache even if the API returns one', async () => {
const { result, remove } = run([{ id: 12, ref: 'refs/heads/main' }])
await expect(result).rejects.toThrow('Unexpected cache ref')
expect(remove).not.toHaveBeenCalled()
})
it('tolerates an eviction race but surfaces permission failures', async () => {
await expect(
run([{ id: 12, ref }], vi.fn().mockRejectedValue({ status: 404 })).result
).resolves.toBeUndefined()
await expect(
run([{ id: 12, ref }], vi.fn().mockRejectedValue({ status: 403 })).result
).rejects.toEqual({ status: 403 })
})