name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Cache Cargo registry and build
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-

      - name: Cache test data
        id: cache-test-data
        uses: actions/cache@v4
        with:
          path: data
          key: test-data-v2

      - name: Download test data
        if: steps.cache-test-data.outputs.cache-hit != 'true'
        run: |
          mkdir -p data/skyview_10deg_test_images data/tess_test_images data/tess_same_ccd
          BASE=https://storage.googleapis.com/tetra3rs-testvecs
          curl --fail -o data/hip2.dat "$BASE/hip2.dat"
          for f in orion_region_10deg pleiades_region_10deg cygnus_region_10deg \
                   andromeda_m31_10deg north_pole_10deg sagittarius_10deg \
                   cassiopeia_10deg scorpius_antares_10deg south_pole_10deg \
                   virgo_cluster_10deg; do
            curl --fail -o "data/skyview_10deg_test_images/${f}.fits" "$BASE/skyview_10deg_test_images/${f}.fits"
          done
          for f in moderate_density_field sparse_field_north_ecliptic dense_galactic_plane; do
            curl --fail -o "data/tess_test_images/${f}.fits" "$BASE/tess_test_images/${f}.fits"
          done
          for s in 01 02 03 04 05 06 14 15 16 17; do
            curl --fail -o "data/tess_same_ccd/sector${s}_cam1_ccd1.fits" "$BASE/tess_same_ccd/sector${s}_cam1_ccd1.fits"
          done

      - name: Build
        run: cargo build --verbose

      - name: Run unit tests
        run: cargo test --verbose

      - name: Run SkyView integration test
        run: cargo test --test skyview_solve_test --features image -- --nocapture

      - name: Run TESS integration test
        run: cargo test --test tess_solve_test --features image -- --nocapture
