name: Go test darwin

on:
  pull_request:
    branches: [main]
  workflow_dispatch:

env:
  SENZING_TOOLS_DATABASE_URL: sqlite3://na:na@nowhere/tmp/sqlite/G2C.db

permissions: {}

jobs:
  go-test-darwin:
    name: "Go test with Senzing: ${{ matrix.senzingsdk-version }}; OS: ${{ matrix.os }}; Go: ${{ matrix.go }}"
    permissions:
      contents: read
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        go: ["1.24"]
        os: [macos-latest]
        senzingsdk-version: [production-v4, staging-v4]

    steps:
      - name: Checkout repository
        uses: actions/checkout@v5
        with:
          persist-credentials: false

      - name: Setup go ${{ matrix.go }}
        uses: actions/setup-go@v6
        with:
          go-version: ${{ matrix.go }}

      - name: Set up gotestfmt
        uses: gotesttools/gotestfmt-action@v2
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Install Senzing SDK
        uses: senzing-factory/github-action-install-senzing-sdk@v3
        with:
          senzingsdk-version: ${{ matrix.senzingsdk-version }}

      - name: Set environment variables
        run: |
          SENZING_PATH="${HOME}/senzing"
          {
            echo "SENZING_PATH=${SENZING_PATH}"
            echo "LD_LIBRARY_PATH=${SENZING_PATH}/er/lib:${SENZING_PATH}/er/lib/macos"
            echo "DYLD_LIBRARY_PATH=${SENZING_PATH}/er/lib:${SENZING_PATH}/er/lib/macos"
            echo  CGO_CFLAGS="-g -I${SENZING_PATH}/er/sdk/c"
            echo  CGO_LDFLAGS="-L${SENZING_PATH}/er/lib -lSz -Wl,-no_warn_duplicate_libraries"
          } >> "$GITHUB_ENV"

      - name: Copy test database files
        run: |
          mkdir -p /tmp/sqlite
          cp testdata/sqlite/G2C.db /tmp/sqlite/

      - name: Run go test
        run: |
          go test -exec "${GITHUB_WORKSPACE}/bin/macos_exec_dyld.sh" -json -v -p 1 -coverprofile="./cover-${{ matrix.senzingsdk-version }}.out" -covermode=atomic -coverpkg=./... ./...  2>&1 | tee "/tmp/gotest-${{ matrix.senzingsdk-version }}.log" | gotestfmt

      - name: Store coverage file
        uses: actions/upload-artifact@v4
        with:
          name: "cover-${{ matrix.senzingsdk-version }}.out"
          path: "./cover-${{ matrix.senzingsdk-version }}.out"

      - name: Upload test log
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: "test-log-${{ matrix.senzingsdk-version }}"
          path: "/tmp/gotest-${{ matrix.senzingsdk-version }}.log"
          if-no-files-found: error

  coverage:
    name: Coverage
    needs: go-test-darwin
    permissions:
      contents: read
    uses: senzing-factory/build-resources/.github/workflows/go-coverage.yaml@v3
    with:
      coverage-config: ./.github/coverage/testcoverage.yaml
      profile: "cover-production-v4.out,cover-staging-v4.out"
