#!/bin/bash
# Run all package tests, then build the app for the iOS simulator.
# Usage: Scripts/test.sh [--tests-only]
set -euo pipefail
cd "$(dirname "$0")/.."

FAIL=0
for pkg in MusicModel AudioCore ScoreLayout; do
    echo "=== swift test: $pkg ==="
    # SwiftPM's build.db sporadically reports "disk I/O error"; a clean retry fixes it.
    (cd "Packages/$pkg" && swift build --build-tests --quiet && swift test --quiet) \
        || (cd "Packages/$pkg" && rm -rf .build && swift build --build-tests --quiet && swift test --quiet) \
        || FAIL=1
done

if [ $FAIL -ne 0 ]; then
    echo "❌ Package tests failed"
    exit 1
fi
echo "✅ All package tests passed"

if [ "${1:-}" == "--tests-only" ]; then
    exit 0
fi

echo "=== xcodegen ==="
xcodegen generate --quiet

echo "=== build app for simulator ==="
xcodebuild -project Acapella.xcodeproj -scheme Acapella \
    -destination 'generic/platform=iOS Simulator' \
    -quiet build

echo "✅ App builds"
