Files
orca/config/scripts/ci/unit-selection/ci-unit-plan.test.mjs
T
Neil 9bd6272fdf refactor(tooling): organize build, CI, and test owners (#38130)
* refactor(tooling): organize test runtime and maintenance owners

* test(tooling): preserve relocated runtime fixture dependency edges

* refactor(tooling): preserve xterm routing owner checks

* ci(tooling): include routing owner in sparse checkout

* test(tooling): preserve copied workflow dependency closures

* fix(tooling): normalize hashed JSON assets in release parity

* fix(tooling): distinguish asset aliases from literal references

* fix(tooling): frame normalized asset reference payloads

* fix(tooling): normalize decoded JSON asset references

* refactor(tooling): organize build, CI and release owners

* fix(tooling): align moved generator and workflow path contracts

* fix(tooling): update generated plugin bundle owner banners

* test(tooling): create relocated mobile verifier fixture directories

* test(tooling): copy headless detector dependency closure

* test(tooling): align native batch policy fixtures with owner path

* fix(tooling): link moved reliability command to historical evidence

* fix(tooling): preserve nested sandbox owners and canonical guide source

* fix(tooling): target release version owners in checkout fixture
2026-11-04 21:08:19 -08:00

168 lines
5.8 KiB
JavaScript

import {
copyFileSync,
mkdtempSync,
mkdirSync,
readFileSync,
realpathSync,
rmSync,
writeFileSync
} from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { expect, it } from 'vitest'
import { runProcessSync } from '@orca/process-host'
import { FULL_SHARD_COUNT } from './ci-unit-matrix.mjs'
it('emits the full matrix from a sparse checkout without dependencies or test discovery', () => {
const root = realpathSync(mkdtempSync(join(tmpdir(), 'orca-unit-matrix-')))
try {
const script = join(root, 'matrix.mjs')
copyFileSync(new URL('./ci-unit-matrix.mjs', import.meta.url), script)
const output = join(root, 'outputs')
const run = (count) =>
runProcessSync({
program: process.execPath,
args: [script],
cwd: root,
env: {
...process.env,
ORCA_UNIT_FULL_SHARD_COUNT: count,
GITHUB_OUTPUT: output
}
})
const result = run('16')
expect(result.code, result.stderr).toBe(0)
const shards = JSON.parse(readFileSync(output, 'utf8').trim().slice('shards='.length))
expect(shards).toEqual(
Array.from({ length: 16 }, (_, index) => ({ index: index + 1, count: 16 }))
)
expect(run('2; echo unsafe').code).not.toBe(0)
expect(readFileSync(output, 'utf8').trim()).toBe(`shards=${JSON.stringify(shards)}`)
} finally {
rmSync(root, { recursive: true, force: true })
}
})
it.each([
{ withParent: true, unresolvedPackage: false },
{ withParent: false, unresolvedPackage: false },
{ withParent: true, unresolvedPackage: true }
])(
'plans from a real Git diff, retaining full coverage for incomplete package evidence: %j',
({ withParent, unresolvedPackage }) => {
const root = mkdtempSync(join(tmpdir(), 'orca-unit-plan-'))
const git = (args) => {
const result = runProcessSync({ program: 'git', args, cwd: root })
expect(result.code, result.stderr).toBe(0)
return result.stdout.trim()
}
try {
mkdirSync(join(root, 'src'))
writeFileSync(join(root, 'src/value.ts'), 'export const value = 1')
writeFileSync(
join(root, 'src/consumer.test.ts'),
unresolvedPackage ? "import '@orca/missing-package'" : "import './value'"
)
writeFileSync(join(root, 'src/unrelated.test.ts'), 'export const unrelated = true')
git(['init', '--quiet'])
git(['add', 'src'])
const commit = [
'-c',
'user.name=CI Test',
'-c',
'user.email=ci-test@example.invalid',
'-c',
'commit.gpgsign=false',
'commit',
'--quiet',
'-m',
'fixture'
]
git(commit)
if (withParent) {
writeFileSync(join(root, 'src/value.ts'), 'export const value = 2')
git(['add', 'src'])
git(commit)
}
const sourceSha = git(['rev-parse', 'HEAD'])
const result = runProcessSync({
program: process.execPath,
args: [fileURLToPath(new URL('./ci-unit-plan.mjs', import.meta.url))],
cwd: root,
env: {
...process.env,
ORCA_BACKGROUND_LAUNCH: '1',
ORCA_UNIT_SELECTION_MODE: 'selected',
GITHUB_EVENT_NAME: 'pull_request',
ORCA_PULL_REQUEST_DRAFT: 'true',
GITHUB_SHA: sourceSha,
ORCA_SHARD_SOURCE_SHA: sourceSha,
GITHUB_OUTPUT: join(root, 'outputs'),
GITHUB_STEP_SUMMARY: join(root, 'summary')
}
})
expect(result.code, result.stderr).toBe(0)
const plan = JSON.parse(readFileSync(join(root, 'ci-shards/unit-selection.json'), 'utf8'))
expect(plan.sourceSha).toBe(sourceSha)
const selected = withParent && !unresolvedPackage
expect(plan.mode).toBe(selected ? 'selected' : 'shadow')
expect(plan.executionFiles).toEqual(
selected ? ['src/consumer.test.ts'] : ['src/consumer.test.ts', 'src/unrelated.test.ts']
)
expect(plan.reason).toBe(
!withParent
? 'Error: Changed paths unavailable'
: unresolvedPackage
? 'Error: No public orca-source export for @orca/missing-package'
: 'Transitive imports plus indirect-input consumers'
)
expect(plan.shards).toHaveLength(selected ? 1 : FULL_SHARD_COUNT)
} finally {
rmSync(root, { recursive: true, force: true })
}
}
)
it.each(['5', '6', '16', 'invalid', '17'])(
'validates the requested full shard count and keeps fallback coverage: %s',
(requestedCount) => {
const root = mkdtempSync(join(tmpdir(), 'orca-unit-count-'))
try {
mkdirSync(join(root, 'src'))
writeFileSync(join(root, 'src/retained.test.ts'), 'export const retained = true')
const result = runProcessSync({
program: process.execPath,
args: [fileURLToPath(new URL('./ci-unit-plan.mjs', import.meta.url))],
cwd: root,
env: {
...process.env,
ORCA_BACKGROUND_LAUNCH: '1',
ORCA_UNIT_FULL_SHARD_COUNT: requestedCount,
GITHUB_EVENT_NAME: 'workflow_dispatch',
ORCA_SHARD_SOURCE_SHA: 'full-count-fixture'
}
})
if (['invalid', '17'].includes(requestedCount)) {
expect(result.code).not.toBe(0)
expect(result.stderr).toContain(
'ORCA_UNIT_FULL_SHARD_COUNT must be an integer from 2 to 16'
)
return
}
expect(result.code, result.stderr).toBe(0)
const plan = JSON.parse(readFileSync(join(root, 'ci-shards/unit-selection.json'), 'utf8'))
expect(plan.mode).toBe('shadow')
expect(plan.executionFiles).toEqual(['src/retained.test.ts'])
expect(plan.shards).toEqual(
Array.from({ length: Number(requestedCount) }, (_, index) => ({
index: index + 1,
count: Number(requestedCount)
}))
)
} finally {
rmSync(root, { recursive: true, force: true })
}
}
)