name: macOS

on:
  schedule:
    - cron: '0 0 * * 1'
  push:
    branches: [ '*' ]
  pull_request:
    branches: [ master ]
  release:
    types:
      - published
      - prereleased

env:
  BUILD_TYPE: Release
  CLICKHOUSE_USER: clickhouse_cpp_cicd
  CLICKHOUSE_PASSWORD: clickhouse_cpp_cicd

jobs:
  build:
    runs-on: macos-latest

    strategy:
      fail-fast: false
      matrix:
        build: [nossl, ssl]
        include:
        - build: nossl

        - build: ssl
          SSL_CMAKE_OPTION: >-
            -D WITH_OPENSSL=ON
            -D OPENSSL_ROOT_DIR=/usr/local/opt/openssl/
          SSL_INSTALL: openssl

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 100
          fetch-tags: true

      - name: Install dependencies
        run: brew install cmake ${{matrix.SSL_INSTALL}}

      - name: Configure CMake
        run: |
          cmake \
            -D CMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} \
            -D BUILD_TESTS=ON \
            ${{matrix.SSL_CMAKE_OPTION}} \
            -S ${{github.workspace}} \
            -B ${{github.workspace}}/build

      - name: Build
        run: |
          cmake \
            --build ${{github.workspace}}/build \
            --config ${{env.BUILD_TYPE}} \
            --target all

      - name: Start tls offoader proxy
        # that mimics non-secure clickhouse running on localhost
        # by tunneling queries to remote tls server
        # (needed because we can't start real clickhouse instance on macOS)
        run: |
              wget https://github.com/filimonov/go-tlsoffloader/releases/download/v0.1.2/go-tlsoffloader_0.1.2_Darwin_x86_64.tar.gz
              tar -xvzf go-tlsoffloader_0.1.2_Darwin_x86_64.tar.gz
              ./go-tlsoffloader -l localhost:9000 -b github.demo.trial.altinity.cloud:9440 &

      - name: Test
        working-directory: ${{github.workspace}}/build/ut
        env:
          # It is impossible to start CH server in docker on macOS due to github actions limitations,
          # so we use remote server to execute tests, some do not allow some features for anonymoust/free users:
          # - system.query_log used by 'Client/ClientCase.Query_ID' and 'Client/ClientCase.ClientName'
          # - system.opentelemetry_span_log is used by 'Client/ClientCase.TracingContext'
          GTEST_FILTER: "-Client/ClientCase.Query_ID*:Client/ClientCase.TracingContext/*:Client/ClientCase.ClientName/*"
        run: ./clickhouse-cpp-ut ${GTEST_FILTER}
