name: Base Tests

on:
  workflow_call:

jobs:
  test:
    name: Test (${{ matrix.os }})
    runs-on: ${{ matrix.os }}-latest
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu, macos]
    env:
      MISE_PROFILE: cicd
      # Reduce GC frequency (default 100) to speed up builds/tests at cost of higher memory
      GOGC: "400"

    steps:
      - name: Checkout code
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
        with:
          fetch-depth: 1

      - name: Use mise to install dependencies
        uses: jdx/mise-action@c1ecc8f748cd28cdeabf76dab3cccde4ce692fe4 # v4.0.0
        with:
          version: 2026.1.9
          experimental: true
        env:
          # Adding token here to reduce the likelihood of hitting rate limit issues.
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - id: go-cache-paths
        run: |
          echo "go-build=$(go env GOCACHE)" >> "$GITHUB_OUTPUT"
          echo "go-mod=$(go env GOMODCACHE)" >> "$GITHUB_OUTPUT"
        shell: bash

      - name: Go Build Cache
        uses: actions/cache@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5
        with:
          path: ${{ steps.go-cache-paths.outputs.go-build }}
          key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.sum') }}-${{ matrix.os }}-amd64
          restore-keys: |
            ${{ runner.os }}-go-build-

      - name: Go Mod Cache
        uses: actions/cache@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5
        with:
          path: ${{ steps.go-cache-paths.outputs.go-mod }}
          key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}-${{ matrix.os }}-amd64
          restore-keys: |
            ${{ runner.os }}-go-mod-

      - name: Terragrunt Provider Cache
        uses: actions/cache@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5
        with:
          path: ${{ runner.os == 'Linux' && '~/.cache/terragrunt' || '~/Library/Caches/terragrunt' }}
          key: ${{ runner.os }}-terragrunt-provider-cache-${{ hashFiles('**/.terraform.lock.hcl') }}
          restore-keys: |
            ${{ runner.os }}-terragrunt-provider-cache-

      - name: Run Tests
        id: run-tests
        run: |
          set -o pipefail
          go test -v -coverprofile=coverage.out -covermode=atomic ./... -timeout 45m | tee >(go-junit-report -set-exit-code > result.xml)
        shell: bash
        env:
          # Adding token here to reduce the likelihood of hitting rate limit issues.
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          # Use shorter temp path on macOS to avoid path length issues with Git worktrees.
          # Default /var/folders/.../T/ paths are too long for Git's path resolution.
          TMPDIR: ${{ matrix.os == 'macos' && '/tmp' || '' }}

      - name: Generate Coverage Summary
        if: always() && matrix.os == 'ubuntu'
        run: ./.github/scripts/coverage/generate-summary.sh
        shell: bash

      - name: Upload coverage.out
        if: always() && matrix.os == 'ubuntu'
        uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
        with:
          name: coverage.out
          path: coverage.out
          retention-days: 30

      - name: Upload coverage-summary.json
        if: always() && matrix.os == 'ubuntu'
        uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
        with:
          name: coverage-summary.json
          path: coverage-summary.json
          retention-days: 30

      - name: Upload coverage-summary.html
        if: always() && matrix.os == 'ubuntu'
        uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
        with:
          name: coverage-summary.html
          path: coverage-summary.html
          retention-days: 30

      - name: Upload Report (${{ matrix.os }})
        uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
        with:
          name: test-report-${{ matrix.os }}
          path: result.xml

      - name: Display Test Results (${{ matrix.os }})
        uses: mikepenz/action-junit-report@49b2ca06f62aa7ef83ae6769a2179271e160d8e4 # v6
        if: always()
        with:
          report_paths: result.xml
          detailed_summary: 'true'
          include_time_in_summary: 'true'
          group_suite: 'true'
