name: CI – Swift Tests (macOS)

on:
  workflow_dispatch:
  push:
    branches: [master]
    paths:
      - "Migration/**"
      - "MockServer/**"
      - "Demonstrator/App/**"
      - ".github/workflows/ci.yml"
  pull_request:
    paths:
      - "Migration/**"
      - "MockServer/**"
      - "Demonstrator/App/**"
      - ".github/workflows/ci.yml"

jobs:
  test:
    name: Swift Tests (macOS)
    runs-on: macos-15

    env:
      FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

    defaults:
      run:
        working-directory: Migration

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Select Xcode 16.3
        run: sudo xcode-select -s /Applications/Xcode_16.3.app/Contents/Developer

      - name: Show Swift version
        run: swift --version

      - name: Install SwiftLint
        run: brew install swiftlint

      - name: Run SwiftLint
        run: swiftlint lint --reporter github-actions-logging

      - name: Build
        run: swift build 2>&1 | tee build.log; exit ${PIPESTATUS[0]}

      - name: Run tests
        run: swift test 2>&1 | tee test.log; exit ${PIPESTATUS[0]}

      - name: Start MockServer
        run: |
          cd "$GITHUB_WORKSPACE/MockServer"
          npm install --silent
          node server.js &
          echo "MOCK_PID=$!" >> "$GITHUB_ENV"
          sleep 2
          curl -sf http://localhost:3000/auth/login \
            -X POST -H 'Content-Type: application/json' \
            -d '{"username":"ci","password":"ci"}' > /dev/null
          echo "MockServer running on :3000"

      - name: Run integration tests
        env:
          MOCKSERVER_INTEGRATION: "1"
        run: swift test --filter MockServerIntegration 2>&1 | tee integration.log; exit ${PIPESTATUS[0]}

      - name: Stop MockServer
        if: always()
        run: kill $MOCK_PID 2>/dev/null || true

  demonstrator:
    name: Demonstrator Build + Test (macOS)
    runs-on: macos-15

    env:
      FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

    defaults:
      run:
        working-directory: Demonstrator/App

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Select Xcode 16.3
        run: sudo xcode-select -s /Applications/Xcode_16.3.app/Contents/Developer

      - name: Show Swift version
        run: swift --version

      - name: Build
        run: swift build 2>&1 | tee build.log; exit ${PIPESTATUS[0]}

      - name: Run tests
        run: swift test 2>&1 | tee test.log; exit ${PIPESTATUS[0]}
