name: Go Test

on:
  push:
  pull_request:

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Set up Go
        uses: actions/setup-go@v6
        with:
          go-version-file: go.mod
          cache: true
      - name: Install gotestsum
        run: go install gotest.tools/gotestsum@latest

      - name: Setup test environment
        run: |
          go build -tags encoder_setup -o encoder-setup ./cmd/encoder-setup
          ./encoder-setup

      - name: Run tests
        run: |
          mkdir -p test-results
          gotestsum --junitfile test-results/junit.xml --format testname -- -race -coverprofile=coverage.txt -covermode=atomic ./...

      - name: Upload test result artifact
        if: ${{ !cancelled() }}
        uses: actions/upload-artifact@v7
        with:
          name: test-results
          path: |
            test-results/junit.xml
            test-results/coverage.txt
          retention-days: 7
      - name: Upload test results to Codecov
        if: ${{ !cancelled() }}
        uses: codecov/test-results-action@v1
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: test-results/junit.xml
      - name: Upload coverage reports to Codecov
        if: ${{ !cancelled() }}
        uses: codecov/codecov-action@v6
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
