Files
bitchat/.github/workflows/swift-tests.yml
T

418 lines
18 KiB
YAML

name: Build & Test
on:
push:
branches:
- main
paths-ignore:
- "**.md"
- "docs/**"
- "plans/**"
- "LICENSE"
pull_request:
paths-ignore:
- "**.md"
- "docs/**"
- "plans/**"
- "LICENSE"
permissions:
contents: read
concurrency:
group: swift-tests-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
test:
name: Run Swift Tests
runs-on: macos-latest
# A hung test must fail fast, not hold a runner for GitHub's 360-minute
# default (observed: intermittent app-suite hangs starving the queue).
# The long steps carry tighter individual bounds (5-minute test watchdog,
# 6-minute benchmark step, 10-minute floor gate that may re-run the
# benchmarks up to twice on a noisy runner); this is the backstop.
timeout-minutes: 25
steps:
- name: Checkout code
uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5
with:
persist-credentials: false
# Use the Xcode-bundled Swift toolchain: it always matches the SDK on
# the runner image. A standalone swift.org toolchain (setup-swift) broke
# whenever the image's Xcode moved ahead of it ("this SDK is not
# supported by the compiler").
- name: Note toolchain version (cache key)
id: swift-version
run: echo "version=$(swift --version 2>/dev/null | head -1 | shasum | cut -c1-12)" >> "$GITHUB_OUTPUT"
# Cache coverage (.build) and uninstrumented benchmark (.build-perf)
# separately so the serial benchmark step never overwrites and poisons
# the coverage build. Only pushes to main write new cache entries, keeping
# repository cache usage well below GitHub's 10 GB eviction limit while
# PRs still get warm incremental builds from main's latest artifacts.
- name: Cache coverage build artifacts (main)
if: github.event_name == 'push'
uses: actions/cache@caa296126883cff596d87d8935842f9db880ef25 # v5
with:
path: .build
key: ${{ runner.os }}-${{ steps.swift-version.outputs.version }}-app-cov-${{ hashFiles('Package.resolved', 'Package.swift') }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-${{ steps.swift-version.outputs.version }}-app-cov-${{ hashFiles('Package.resolved', 'Package.swift') }}-
${{ runner.os }}-${{ steps.swift-version.outputs.version }}-app-cov-
- name: Restore coverage build artifacts (PR)
if: github.event_name != 'push'
uses: actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25 # v5
with:
path: .build
key: ${{ runner.os }}-${{ steps.swift-version.outputs.version }}-app-cov-${{ hashFiles('Package.resolved', 'Package.swift') }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-${{ steps.swift-version.outputs.version }}-app-cov-${{ hashFiles('Package.resolved', 'Package.swift') }}-
${{ runner.os }}-${{ steps.swift-version.outputs.version }}-app-cov-
- name: Cache benchmark build artifacts (main)
if: github.event_name == 'push'
uses: actions/cache@caa296126883cff596d87d8935842f9db880ef25 # v5
with:
path: .build-perf
key: ${{ runner.os }}-${{ steps.swift-version.outputs.version }}-app-perf-${{ hashFiles('Package.resolved', 'Package.swift') }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-${{ steps.swift-version.outputs.version }}-app-perf-${{ hashFiles('Package.resolved', 'Package.swift') }}-
${{ runner.os }}-${{ steps.swift-version.outputs.version }}-app-perf-
- name: Restore benchmark build artifacts (PR)
if: github.event_name != 'push'
uses: actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25 # v5
with:
path: .build-perf
key: ${{ runner.os }}-${{ steps.swift-version.outputs.version }}-app-perf-${{ hashFiles('Package.resolved', 'Package.swift') }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-${{ steps.swift-version.outputs.version }}-app-perf-${{ hashFiles('Package.resolved', 'Package.swift') }}-
${{ runner.os }}-${{ steps.swift-version.outputs.version }}-app-perf-
- name: Run local package tests (BitLogger & BitFoundation)
run: |
set -euo pipefail
swift test --parallel --quiet --package-path localPackages/BitLogger
swift test --parallel --quiet --package-path localPackages/BitFoundation
- name: Build app tests
# Built separately so the hang watchdog below times only test
# execution: a cold-cache coverage build on a slow runner can
# legitimately take several minutes.
run: swift build --build-tests --enable-code-coverage --package-path .
- name: Run app tests
# Perf benchmarks are excluded here and run in their own serial step
# below: measuring while parallel test processes contend for cores
# produces noisy numbers, and the XCTest measure machinery has hung
# intermittently under parallel workers on loaded runners. Excluded
# via --skip (not just the env guard): every app run since the
# baselines landed timed out at the 15-minute job limit with the
# baseline tests dispatched into the parallel phase.
#
# The watchdog samples any still-running test processes after 5
# minutes (the suite passes in seconds when healthy; the build is
# done by this step) and kills the run, so a hang fails fast with
# stacks in the log instead of a silent timeout.
env:
BITCHAT_SKIP_PERF_BASELINES: "1"
run: |
swift test --skip-build --parallel --quiet --enable-code-coverage \
--skip PerformanceBaselineTests \
--package-path . &
test_pid=$!
(
sleep 300
if kill -0 "$test_pid" 2>/dev/null; then
echo "::group::Tests still running after 5 minutes — sampling before kill"
for pid in $(pgrep -if 'swiftpm-testing|xctest|PackageTests' || true); do
echo "--- sample of pid $pid ---"
sample "$pid" 5 2>/dev/null || true
done
echo "::endgroup::"
pkill -KILL -P "$test_pid" 2>/dev/null || true
kill -KILL "$test_pid" 2>/dev/null || true
fi
) &
watchdog_pid=$!
wait "$test_pid" && status=0 || status=$?
pkill -TERM -P "$watchdog_pid" 2>/dev/null || true
kill "$watchdog_pid" 2>/dev/null || true
exit "$status"
# Informational only: there is deliberately no percentage threshold, but
# a broken/missing report is a CI configuration error and must be visible.
- name: Coverage summary
run: |
BIN_PATH=$(swift build --show-bin-path --package-path .)
PROF="$BIN_PATH/codecov/default.profdata"
XCTEST=$(find "$BIN_PATH" -maxdepth 1 -name '*.xctest' | head -1)
BINARY="$XCTEST/Contents/MacOS/$(basename "$XCTEST" .xctest)"
if [ ! -f "$PROF" ] || [ ! -f "$BINARY" ]; then
echo "::error::Coverage profile or test binary is missing"
exit 1
fi
xcrun llvm-cov report "$BINARY" -instr-profile "$PROF" \
-ignore-filename-regex='(Tests|\.build|checkouts|Mocks|_PreviewHelpers)'
# Benchmarks run serially on an otherwise idle runner using a separate
# uninstrumented build directory (.build-perf) so they neither measure
# coverage instrumentation overhead nor clobber the cached .build tree.
- name: Run performance benchmarks (serial)
timeout-minutes: 6
env:
BITCHAT_PERF_LOG: ${{ github.workspace }}/perf-output.log
run: swift test --build-path .build-perf --quiet --filter PerformanceBaselineTests
# Order-of-magnitude performance regression gate. Floors are deliberately
# generous (see bitchatTests/Performance/perf-floors.json) so this
# catches algorithmic regressions, never runner variance. If a metric
# still lands below floor (a saturated runner can dip one), the script
# re-runs the benchmarks — appending to the same log and keeping each
# benchmark's best value per metric — so noise clears on retry while a
# real regression fails every attempt. Floors are never lowered by this.
- name: Performance floor gate
timeout-minutes: 10
env:
BITCHAT_PERF_REMEASURE_CMD: "swift test --build-path .build-perf --skip-build --quiet --filter PerformanceBaselineTests"
run: ./scripts/check-perf-floors.sh perf-output.log
# SPM tests do not link the shipping app targets. Build the three Release
# targets in parallel to keep wall-clock time around ~3-4 minutes. Pull
# requests build native arm64 for simulator/macOS for fast feedback; pushes
# to main validate full universal (arm64 + x86_64) slices. iOS device always
# links against the shipping ios-arm64 vendored Arti slice.
ios-build:
name: Build Release (${{ matrix.name }})
runs-on: macos-latest
timeout-minutes: 15
strategy:
fail-fast: false
matrix:
include:
- name: iOS device
target: ios-device
- name: iOS simulator
target: ios-simulator
- name: macOS
target: macos
steps:
- name: Checkout code
uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5
with:
persist-credentials: false
- name: Check clean recipe safety
if: matrix.target == 'ios-device'
run: bash scripts/check-just-clean-safety.sh
- name: Run workflow and script unit tests
if: matrix.target == 'ios-device'
run: python3 -m unittest discover -s scripts/tests -v
- name: Build iOS (device, no signing)
if: matrix.target == 'ios-device'
# Validate linking against the shipping ios-arm64 vendored Arti slice.
run: |
set -o pipefail
xcodebuild -project bitchat.xcodeproj \
-scheme "bitchat (iOS)" \
-configuration Release \
-sdk iphoneos \
-destination 'generic/platform=iOS' \
ARCHS='arm64' \
ONLY_ACTIVE_ARCH=NO \
CODE_SIGNING_ALLOWED=NO \
-onlyUsePackageVersionsFromResolvedFile \
build
- name: Build iOS (simulator, no signing)
if: matrix.target == 'ios-simulator'
env:
BUILD_UNIVERSAL: ${{ github.event_name == 'push' && '1' || '0' }}
run: |
set -o pipefail
if [ "$BUILD_UNIVERSAL" = "1" ]; then
archs="arm64 x86_64"
only_active="NO"
else
archs="arm64"
only_active="YES"
fi
xcodebuild -project bitchat.xcodeproj \
-scheme "bitchat (iOS)" \
-configuration Release \
-sdk iphonesimulator \
-destination 'generic/platform=iOS Simulator' \
ARCHS="$archs" \
ONLY_ACTIVE_ARCH="$only_active" \
CODE_SIGNING_ALLOWED=NO \
-onlyUsePackageVersionsFromResolvedFile \
build
- name: Build macOS (no signing)
if: matrix.target == 'macos'
env:
BUILD_UNIVERSAL: ${{ github.event_name == 'push' && '1' || '0' }}
run: |
set -o pipefail
if [ "$BUILD_UNIVERSAL" = "1" ]; then
archs="arm64 x86_64"
only_active="NO"
else
archs="arm64"
only_active="YES"
fi
xcodebuild -project bitchat.xcodeproj \
-scheme "bitchat (macOS)" \
-configuration Release \
-destination 'generic/platform=macOS' \
ARCHS="$archs" \
ONLY_ACTIVE_ARCH="$only_active" \
CODE_SIGNING_ALLOWED=NO \
-onlyUsePackageVersionsFromResolvedFile \
build
# The SwiftPM suite runs on macOS and already executes all cross-platform tests
# in parallel. Select an available iPhone simulator via simctl (skipping the
# slow xcodebuild -showdestinations fallback), disable unused iOS coverage, and
# let xcodebuild compile with all runner cores before booting the simulator
# (pre-booting during compilation starved the 3-vCPU runner and doubled build
# time). Pull requests run the 18 iOS-conditional / iOS-framework suites for
# fast feedback; pushes to main run the full simulator suite (minus
# PerformanceBaselineTests, which is already gated in Run Swift Tests).
ios-tests:
name: Run iOS simulator tests
runs-on: macos-latest
timeout-minutes: 20
steps:
- name: Checkout code
uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5
with:
persist-credentials: false
- name: Select available iPhone simulator
id: destination
run: |
set -uo pipefail
# Query simctl directly first (xcodebuild -showdestinations takes
# ~45s resolving packages and still falls back to simctl on images
# with placeholder destinations).
destination_id=$(xcrun simctl list devices available --json | jq -r '
[.devices | to_entries[]
| select(.key | contains("iOS"))
| .value[]
| select(.isAvailable and (.name | startswith("iPhone")))]
| first.udid // empty')
if [ -n "$destination_id" ]; then
echo "Selected existing simctl device: $destination_id"
else
echo "No available iPhone simulator device; creating one"
create_spec=$(xcrun simctl list runtimes --json | jq -r '
[.runtimes[] | select(.platform == "iOS" and .isAvailable)]
| sort_by(.version | split(".") | map(tonumber))
| last // empty
| .identifier as $runtime
| ([(.supportedDeviceTypes // [])[]
| select(.productFamily == "iPhone"
or (.name // "" | startswith("iPhone")))]
| first.identifier // empty) as $devicetype
| "\($devicetype) \($runtime)"')
read -r devicetype runtime <<< "$create_spec" || true
if [ -z "${devicetype:-}" ] || [ -z "${runtime:-}" ]; then
echo "::error::No iPhone simulator destination found and none creatable (no installed iOS runtime with an iPhone device type)"
exit 1
fi
destination_id=$(xcrun simctl create ci-iphone "$devicetype" "$runtime") || {
echo "::error::simctl create failed for $devicetype on $runtime"
exit 1
}
echo "Created simulator $destination_id ($devicetype, $runtime)"
fi
echo "Using iPhone simulator destination id: $destination_id"
echo "id=$destination_id" >> "$GITHUB_OUTPUT"
- name: Run iOS tests
env:
DESTINATION_ID: ${{ steps.destination.outputs.id }}
RUN_FULL_IOS_SUITE: ${{ github.event_name == 'push' && '1' || '0' }}
TEST_RUNNER_BITCHAT_SKIP_PERF_BASELINES: "1"
run: |
set -o pipefail
test_filters=()
if [ "$RUN_FULL_IOS_SUITE" = "1" ]; then
test_filters+=(
-skip-testing:bitchatTests_iOS/PerformanceBaselineTests
)
else
# iOS-conditional (#if os(iOS) / UIKit) and iOS framework
# (AVFoundation, CoreLocation, UserNotifications, CoreBluetooth,
# Share Extension) suites. Cross-platform tests already ran in
# parallel in Run Swift Tests; pushes to main run the full suite.
for suite in \
ComposerActionFrameTests \
ViewSmokeTests \
ImageUtilsTests \
ChatMediaPreparationTests \
ChatMediaTransferCoordinatorContextTests \
ChatViewModelMediaTransferTests \
MediaRetentionTests \
AudioSessionCoordinatorTests \
VoiceRecorderTests \
VoiceCaptureSessionTests \
VoiceNotePlaybackControllerTests \
PTTAudioTests \
PTTBurstPlayerTests \
LocationStateManagerTests \
NotificationServiceTests \
SharedContentHandoffTests \
BLEServiceCoreTests \
BLEServiceTests; do
test_filters+=("-only-testing:bitchatTests_iOS/$suite")
done
fi
xcodebuild -project bitchat.xcodeproj \
-scheme "bitchat (iOS)" \
-sdk iphonesimulator \
-destination "platform=iOS Simulator,id=$DESTINATION_ID" \
-enableCodeCoverage NO \
-parallel-testing-enabled NO \
CODE_SIGNING_ALLOWED=NO \
-onlyUsePackageVersionsFromResolvedFile \
"${test_filters[@]}" \
test
# Blocking since the backlog reached zero (all violations fixed; rules the
# codebase deliberately breaks are disabled in .swiftlint.yml). --strict
# promotes warnings to errors so new violations fail the job. Runs in a
# pinned container (no Xcode plugin, no pbxproj changes) so it can never
# break the documented xcodebuild path.
lint:
name: SwiftLint
runs-on: ubuntu-latest
timeout-minutes: 15
# This job runs a third-party container image, so give it the least
# privilege we can: a read-only token, and no credentials left in the
# checkout for the container to find.
permissions:
contents: read
container:
# Tag for readability, digest for immutability (tags can be repointed).
# Bump both together, deliberately — never a floating tag.
image: ghcr.io/realm/swiftlint:0.65.0@sha256:a482729f4b58741875af1566f23397f3f6db300372756fc31606d0a4527fab9e
steps:
- uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5
with:
persist-credentials: false
- name: Run SwiftLint
run: swiftlint lint --strict --reporter github-actions-logging