name: Core Tests

on:
  workflow_dispatch: # manual trigger only, auto-run disabled for now
  # push:
  #   branches: ["main"]
  # pull_request:
  #   branches: ["main"]

env:
  CARGO_TERM_COLOR: always
  CARGO_INCREMENTAL: 0
  CARGO_NET_RETRY: 10
  RUSTUP_MAX_RETRIES: 10

jobs:
  test:
    strategy:
      fail-fast: false
      matrix:
        settings:
          - host: [self-hosted, macOS, ARM64]
            target: aarch64-apple-darwin
            os: macos
          - host: ubuntu-22.04
            target: blacksmith-4vcpu-ubuntu-2404
            os: linux
          - host: [self-hosted, Windows, X64]
            target: x86_64-pc-windows-msvc
            os: windows
    name: Test Core - ${{ matrix.settings.os }}
    runs-on: ${{ matrix.settings.host }}
    if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository

    steps:
      - name: Maximize build space
        if: ${{ matrix.settings.os == 'linux' }}
        uses: easimon/maximize-build-space@master
        with:
          swap-size-mb: 3072
          root-reserve-mb: 6144
          remove-dotnet: 'true'
          remove-codeql: 'true'
          remove-haskell: 'true'
          remove-docker-images: 'true'

      - name: Symlink target to C:\
        if: ${{ matrix.settings.os == 'windows' }}
        shell: cmd
        run: |
          if exist target rmdir /S /Q target
          if not exist C:\spacedrive_target mkdir C:\spacedrive_target
          mklink /J target C:\spacedrive_target

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

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.settings.target }}

      - name: Setup System and Rust
        uses: ./.github/actions/setup-system
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          target: ${{ matrix.settings.target }}

      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: ~/.cargo/registry
          key: ${{ matrix.settings.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}

      - name: Cache cargo index
        uses: actions/cache@v4
        with:
          path: ~/.cargo/git
          key: ${{ matrix.settings.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}

      - name: Cache cargo build
        uses: actions/cache@v4
        with:
          path: target
          key: ${{ matrix.settings.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}

      - name: Setup native dependencies
        run: cargo run -p xtask -- setup

      - name: Run all tests
        run: cargo run -p xtask -- test-core --verbose
