name: 'test'
on:
  pull_request:
    paths: ['**.go', 'go.mod', '.github/workflows/*']
  push:
    branches: ['main', 'master']

jobs:
  fuzz:
    runs-on: 'ubuntu-latest'
    steps:
      - uses: 'actions/checkout@v6'
      - uses: 'actions/setup-go@v6'
        with: {go-version: 'stable'}
      - shell: 'bash'
        run: |
          set -eu
          f=($(grep -Eo '^func Fuzz[a-zA-Z0-9_]+' fuzz_test.go | cut -d' ' -f2))
          t=$(( 300 / ${#f[@]} ))s
          for ff in ${f[@]}; do
            echo go test -test.run=DontRunTests -fuzztime=$t -fuzz=$ff
            go test -test.run=DontRunTests -fuzztime=$t -fuzz=$ff
          done

  ubuntu:
    runs-on: 'ubuntu-latest'
    strategy:
      fail-fast: false
      matrix:
        pg: ['14', '15', '16', '17', '18']
        go: ['1.21', '1.25']
    steps:
    - uses: 'actions/checkout@v6'
    - uses: 'actions/setup-go@v6'
      with:
        go-version: ${{ matrix.go }}
    - name: 'Start PostgreSQL'
      run: |
        docker compose up pg${{ matrix.pg }} -d --wait || {
          docker compose logs
          exit 1
        }
        echo '127.0.0.1 postgres postgres-invalid' | sudo tee -a /etc/hosts
    - name: 'Run tests'
      run: |
        echo 'PQTEST_BINARY_PARAMETERS=no  go test -race ./...'
        PQTEST_BINARY_PARAMETERS=no  go test -race ./...

        echo 'PQTEST_BINARY_PARAMETERS=yes go test -race ./...'
        PQTEST_BINARY_PARAMETERS=yes go test -race ./...

  pgbouncer:
    runs-on: 'ubuntu-latest'
    steps:
    - uses: 'actions/checkout@v6'
    - uses: 'actions/setup-go@v6'
      with: {go-version: '1.25'}
    - name: 'Start PostgreSQL'
      run: |
        docker compose up pg18 -d --wait || {
          docker compose logs
          exit 1
        }
        docker compose up pgbouncer -d --wait || {
          docker compose logs
          exit 1
        }
        echo '127.0.0.1 postgres postgres-invalid' | sudo tee -a /etc/hosts
    - name: 'Run tests'
      run: |
        echo 'PGPORT=6432 PQTEST_BINARY_PARAMETERS=no  go test -race ./...'
        PGPORT=6432 PQTEST_BINARY_PARAMETERS=no  go test -race ./...

        echo PGPORT=6432 'PQTEST_BINARY_PARAMETERS=yes go test -race ./...'
        PGPORT=6432 PQTEST_BINARY_PARAMETERS=yes go test -race ./...

  # Disabled for now as it's kind of finecky and flaky.
  #pgpool:
  #  runs-on: 'ubuntu-latest'
  #  steps:
  #  - uses: 'actions/checkout@v6'
  #  - uses: 'actions/setup-go@v6'
  #    with: {go-version: '1.25'}
  #  - name: 'Start PostgreSQL'
  #    run: |
  #      docker compose up pg18 -d --wait || {
  #        docker compose logs
  #        exit 1
  #      }
  #      docker compose up pgpool -d --wait || {
  #        docker compose logs
  #        exit 1
  #      }
  #      echo '127.0.0.1 postgres postgres-invalid' | sudo tee -a /etc/hosts
  #  - name: 'Run tests'
  #    run: |
  #      echo 'PGPORT=6432 PQTEST_BINARY_PARAMETERS=no  go test -race ./...'
  #      PGPORT=7432 PQTEST_BINARY_PARAMETERS=no  go test -race ./...

  #      echo PGPORT=6432 'PQTEST_BINARY_PARAMETERS=yes go test -race ./...'
  #      PGPORT=7432 PQTEST_BINARY_PARAMETERS=yes go test -race ./...

  # TODO: disabled for now as it's very slow and flaky.
  #macos:
  #  runs-on: 'macos-15-intel'
  #  strategy:
  #    fail-fast: false
  #    matrix:
  #      pg: ['18']
  #      go: ['1.18', '1.25']
  #  steps:
  #  - uses: 'actions/checkout@v6'
  #  - uses: 'douglascamata/setup-docker-macos-action@v1'
  #  - uses: 'actions/setup-go@v6'
  #    with:
  #      go-version: ${{ matrix.go }}
  #  - name: 'Start PostgreSQL'
  #    run: |
  #      docker compose up pg${{ matrix.pg }} -d --wait || {
  #        docker compose logs
  #        exit 1
  #      }
  #      echo '127.0.0.1 postgres postgres-invalid' | sudo tee -a /etc/hosts
  #  - name: 'Run tests'
  #    run: |
  #      echo 'PQTEST_BINARY_PARAMETERS=no  go test -race ./...'
  #      PQTEST_BINARY_PARAMETERS=no  go test -race ./...
  #
  #      echo 'PQTEST_BINARY_PARAMETERS=yes go test -race ./...'
  #      PQTEST_BINARY_PARAMETERS=yes go test -race ./...

  # TODO: can't get this to work; always fails with:
  # dial tcp [::1]:5432: connectex: No connection could be made because the target machine actively refused it.
  #
  # Which is a Windows firewall thing. I can't get it to work.
  #windows:
  #  runs-on: 'windows-latest'
  #  strategy:
  #    fail-fast: false
  #    matrix:
  #      #go: ['1.18', '1.25']
  #      pg: ['18']
  #      go: ['1.25']
  #  steps:
  #  - uses: 'actions/checkout@v6'
  #  - uses: 'Vampire/setup-wsl@v6'
  #  # ubuntu because Debian doesn't work: https://github.com/Vampire/setup-wsl/issues/76
  #    with: {distribution: 'Ubuntu-24.04', additional-packages: 'docker.io docker-compose-v2'}
  #  - name: 'Start PostgreSQL'
  #    shell: 'wsl-bash {0}'
  #    run: |
  #      docker compose up pg${{ matrix.pg }} -d --wait || {
  #        docker compose logs
  #        exit 1
  #      }
  #      echo '127.0.0.1 postgres postgres-invalid' | sudo tee -a /etc/hosts

  #  - uses: 'actions/setup-go@v6'
  #    with:
  #      go-version: ${{ matrix.go }}
  #  - name: 'Run tests'
  #    shell: 'bash'
  #    run: |
  #      echo 'PQTEST_BINARY_PARAMETERS=no  go test -race ./...'
  #      PQTEST_BINARY_PARAMETERS=no  go test -race ./...

  #      echo 'PQTEST_BINARY_PARAMETERS=yes go test -race ./...'
  #      PQTEST_BINARY_PARAMETERS=yes go test -race ./...
