|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { expect, test } from 'vitest'; |
| 4 | +import { parse } from 'yaml'; |
| 5 | +import { selectAppleBridgeProof, selectIosXctests } from '../apple-ci-impact.ts'; |
| 6 | +import { selectChecks } from '../check-affected/model.ts'; |
| 7 | + |
| 8 | +const repoRoot = path.resolve(import.meta.dirname, '../..'); |
| 9 | + |
| 10 | +function cacheInputs(action: string): string[] { |
| 11 | + const doc = parse(action) as { runs?: { steps?: Array<{ id?: string; run?: string }> } }; |
| 12 | + const sourceHash = doc.runs?.steps?.find((step) => step.id === 'source-hash')?.run ?? ''; |
| 13 | + const expressions = [...sourceHash.matchAll(/hashFiles\(([\s\S]*?)\)/g)]; |
| 14 | + expect(expressions.length).toBeGreaterThan(0); |
| 15 | + return expressions.flatMap((expression) => |
| 16 | + [...expression[1]!.matchAll(/'([^']+)'/g)].map((match) => match[1]!), |
| 17 | + ); |
| 18 | +} |
| 19 | + |
| 20 | +test('every runner build-cache input triggers the PR XCTest lane', () => { |
| 21 | + const action = fs.readFileSync( |
| 22 | + path.join(repoRoot, '.github/actions/setup-apple-runner-build/action.yml'), |
| 23 | + 'utf8', |
| 24 | + ); |
| 25 | + expect(cacheInputs(action)).toContain('packages/platform-apple/src/runner/**'); |
| 26 | + expect(cacheInputs(action)).toContain('!packages/platform-apple/src/runner/__tests__/**'); |
| 27 | + const uncovered = (text: string) => |
| 28 | + cacheInputs(text) |
| 29 | + .filter((input) => !input.startsWith('!')) |
| 30 | + .filter((input) => { |
| 31 | + const path = input.replace(/\*\*?$/, 'probe.ts'); |
| 32 | + const plan = selectChecks({ changedFiles: [path] }); |
| 33 | + return !plan.failOpen && !plan.checks.includes('swift-runner-ios'); |
| 34 | + }); |
| 35 | + expect(uncovered(action)).toEqual([]); |
| 36 | + expect( |
| 37 | + uncovered(action.replace('apple/runner/**', 'packages/platform-apple/src/snapshot-source/**')), |
| 38 | + ).toEqual(['packages/platform-apple/src/snapshot-source/**']); |
| 39 | + expect( |
| 40 | + uncovered( |
| 41 | + action.replace( |
| 42 | + 'hashFiles(', |
| 43 | + "hashFiles('packages/platform-apple/src/foldable/**',\n ", |
| 44 | + ), |
| 45 | + ), |
| 46 | + ).toEqual(['packages/platform-apple/src/foldable/**']); |
| 47 | +}); |
| 48 | + |
| 49 | +test('the PR workflow applies the impact decision to the XCTest step', () => { |
| 50 | + const workflow = fs.readFileSync(path.join(repoRoot, '.github/workflows/ios.yml'), 'utf8'); |
| 51 | + expect(workflow).toContain('node --experimental-strip-types scripts/apple-ci-impact.ts xctest'); |
| 52 | + expect(workflow).toMatch( |
| 53 | + /- name: Run targeted iOS runner XCTest regressions\n\s+id: ios-xctest\n\s+if: steps\.xctest-impact\.outputs\.run != 'false'/, |
| 54 | + ); |
| 55 | + expect(workflow).toContain('if [ "$SELECTED" = \'true\' ] && [ "$OUTCOME" = \'skipped\' ]'); |
| 56 | +}); |
| 57 | + |
| 58 | +test('macOS clean-install proof follows live UI replay', () => { |
| 59 | + const workflow = fs.readFileSync(path.join(repoRoot, '.github/workflows/macos.yml'), 'utf8'); |
| 60 | + const replay = workflow.indexOf('- name: Run macOS integration test'); |
| 61 | + const proof = workflow.indexOf( |
| 62 | + '- name: Verify clean-installed Simulator snapshot bridge preparation', |
| 63 | + ); |
| 64 | + expect(replay).toBeGreaterThan(-1); |
| 65 | + expect(proof).toBeGreaterThan(replay); |
| 66 | + expect(workflow).toContain('node --experimental-strip-types scripts/apple-ci-impact.ts bridge'); |
| 67 | + expect(workflow).toContain("steps.bridge-impact.outputs.run != 'false'"); |
| 68 | +}); |
| 69 | + |
| 70 | +test('native runner and golden-table changes run XCTest; TypeScript runtime changes use live E2E', () => { |
| 71 | + for (const file of [ |
| 72 | + 'apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests.swift', |
| 73 | + 'apple/snapshot-presentation/Sources/Presenter.swift', |
| 74 | + 'packages/platform-apple/src/runner/runner-icon.ts', |
| 75 | + 'contracts/fixtures/scroll-gesture.json', |
| 76 | + '.github/workflows/ios.yml', |
| 77 | + 'scripts/apple-ci-impact.ts', |
| 78 | + ]) { |
| 79 | + expect(selectIosXctests('pull_request', [file]).run, file).toBe(true); |
| 80 | + } |
| 81 | + for (const file of [ |
| 82 | + 'packages/platform-apple/src/snapshot-source/cache.ts', |
| 83 | + 'packages/platform-apple/src/runner/__tests__/runner-icon.test.ts', |
| 84 | + 'apple/fold-helper/fold-helper.c', |
| 85 | + 'test/integration/ios-simulator-e2e/live-runner.ts', |
| 86 | + ]) { |
| 87 | + expect(selectIosXctests('pull_request', [file]), file).toMatchObject({ run: false }); |
| 88 | + } |
| 89 | +}); |
| 90 | + |
| 91 | +test('pushes and uncertain diffs keep the full XCTest selection', () => { |
| 92 | + expect(selectIosXctests('push', ['src/index.ts']).run).toBe(true); |
| 93 | + expect(selectIosXctests('pull_request', null).run).toBe(true); |
| 94 | + expect(selectIosXctests('pull_request', []).run).toBe(true); |
| 95 | + expect(selectIosXctests('pull_request', ['src/index.ts', 'package.json']).run).toBe(true); |
| 96 | +}); |
| 97 | + |
| 98 | +test('bridge proof runs for its owning sources and uncertain tooling changes', () => { |
| 99 | + for (const file of [ |
| 100 | + 'apple/snapshot-bridge/Bridge.c', |
| 101 | + 'apple/fold-helper/Helper.c', |
| 102 | + 'apple/new-native-module/Source.m', |
| 103 | + 'packages/platform-apple/src/snapshot-source/native-runtime.ts', |
| 104 | + 'packages/platform-apple/src/foldable/fold-helper-cache.ts', |
| 105 | + 'packages/platform-apple/src/new-module/runtime.ts', |
| 106 | + 'scripts/check-package.ts', |
| 107 | + '.github/workflows/macos.yml', |
| 108 | + ]) { |
| 109 | + expect(selectAppleBridgeProof([file]).run, file).toBe(true); |
| 110 | + } |
| 111 | + expect(selectAppleBridgeProof(['src/index.ts']).run).toBe(false); |
| 112 | + expect(selectAppleBridgeProof(null).run).toBe(true); |
| 113 | + expect(selectAppleBridgeProof([]).run).toBe(true); |
| 114 | +}); |
0 commit comments