name: Tests

on:
  push:
    tags:
      - "*"
    branches:
      - main
      - dev
  pull_request:
  merge_group:
    types: [checks_requested]

permissions:
  contents: read

# Cancel ongoing workflow runs if a new one is started
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  Unit:
    runs-on: ${{ matrix.os }}
    env:
      GOPRIVATE: github.com/luxfi/*
      GONOSUMDB: github.com/luxfi/*
      GOWORK: off
      GOEXPERIMENT: runtimesecret
    strategy:
      fail-fast: false
      matrix:
        # GH-hosted arm64 macos forbidden; CI runs amd64 only. Release builds
        # cover darwin/arm64 via cross-compile in build-macos-release.yml.
        os: [macos-13, ubuntu-22.04, ubuntu-24.04, windows-2022]
    steps:
      - uses: actions/checkout@v4
      - uses: ./.github/actions/setup-go-for-project
      - name: Configure Git for private modules
        shell: bash
        run: git config --global url."https://${{ github.token }}@github.com/".insteadOf "https://github.com/"
      - name: Set timeout on Windows
        shell: bash
        if: matrix.os == 'windows-2022'
        run: echo "TIMEOUT=240s" >> "$GITHUB_ENV"
      - name: build_test
        shell: bash
        run: ./scripts/build_test.sh
        env:
          TIMEOUT: ${{ env.TIMEOUT }}
          CGO_ENABLED: '0'
  Fuzz:
    runs-on: lux-build-amd64
    env:
      GOPRIVATE: github.com/luxfi/*
      GONOSUMDB: github.com/luxfi/*
      GOWORK: off
      GOEXPERIMENT: runtimesecret
    steps:
      - uses: actions/checkout@v4
      - uses: ./.github/actions/setup-go-for-project
      - name: Configure Git for private modules
        shell: bash
        run: git config --global url."https://${{ github.token }}@github.com/".insteadOf "https://github.com/"
      - name: fuzz_test
        shell: bash
        run: ./scripts/build_fuzz.sh 20 # Run each fuzz test 20 seconds
        env:
          CGO_ENABLED: '0'
  # NOTE: E2E tests disabled - require tmpnet infrastructure
  # e2e_pre_etna, e2e_post_etna, e2e_existing_network, Upgrade
  # These will be re-enabled once tmpnet is properly configured
  Lint:
    runs-on: lux-build-amd64
    env:
      GOPRIVATE: github.com/luxfi/*
      GONOSUMDB: github.com/luxfi/*
      GOWORK: off
      GOEXPERIMENT: runtimesecret
    steps:
      - uses: actions/checkout@v4
      - uses: ./.github/actions/setup-go-for-project
      - name: Configure Git for private modules
        shell: bash
        run: git config --global url."https://${{ github.token }}@github.com/".insteadOf "https://github.com/"
      - name: Run static analysis tests
        shell: bash
        run: scripts/lint.sh
        env:
          CGO_ENABLED: '0'
      - name: Run shellcheck
        shell: bash
        run: scripts/shellcheck.sh
      - name: Run actionlint
        shell: bash
        run: scripts/actionlint.sh
  check_mockgen:
    name: Up-to-date mocks
    runs-on: lux-build-amd64
    continue-on-error: true
    env:
      GOPRIVATE: github.com/luxfi/*
      GONOSUMDB: github.com/luxfi/*
      GOWORK: off
    steps:
      - uses: actions/checkout@v4
      - uses: ./.github/actions/setup-go-for-project
      - name: Configure Git for private modules
        shell: bash
        run: git config --global url."https://${{ github.token }}@github.com/".insteadOf "https://github.com/"
      - shell: bash
        run: scripts/mock.gen.sh
        env:
          CGO_ENABLED: '0'
      - shell: bash
        run: .github/workflows/check-clean-branch.sh
  go_mod_tidy:
    name: Up-to-date go.mod and go.sum
    runs-on: lux-build-amd64
    env:
      GOPRIVATE: github.com/luxfi/*
      GONOSUMDB: github.com/luxfi/*
      GOWORK: off
    steps:
      - uses: actions/checkout@v4
      - uses: ./.github/actions/setup-go-for-project
      - name: Configure Git for private modules
        shell: bash
        run: git config --global url."https://${{ github.token }}@github.com/".insteadOf "https://github.com/"
      - shell: bash
        run: go mod tidy
      - shell: bash
        run: .github/workflows/check-clean-branch.sh
  test_build_image:
    name: Image build
    runs-on: lux-build-amd64
    steps:
      - uses: actions/checkout@v4
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Build Docker image (test only)
        uses: docker/build-push-action@v5
        with:
          context: .
          push: false
          platforms: linux/amd64
          cache-from: type=gha
          cache-to: type=gha,mode=max
