name: Go bindings tests CI

on:
  push:
    branches: [ "main" ]
  pull_request:
  merge_group:
    branches: [ "main" ]

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  golang-bindings:

    runs-on: ubuntu-latest

    services:
      sqld:
        image: ghcr.io/tursodatabase/libsql-server:latest
        ports:
          - 8080:8080

    steps:
    - uses: actions/checkout@v3

    # needed because we run out of disk space during tests
    - name: Free Disk Space (Ubuntu)
      uses: jlumbroso/free-disk-space@main
      with:
        # this might remove tools that are actually needed,
        # when set to "true" but frees about 6 GB
        tool-cache: true

    - uses: dtolnay/rust-toolchain@stable
    - name: Set up Go
      uses: actions/setup-go@v3
      with:
        go-version: '>=1.21'
    - name: get TCL
      run: sudo apt-get install -y tcl8.6-dev

    - name: Install Protoc
      uses: arduino/setup-protoc@v2

    - name: Set up cargo cache
      uses: actions/cache@v3
      continue-on-error: false
      with:
        path: |
          ~/.cargo/bin/
          ~/.cargo/registry/index/
          ~/.cargo/registry/cache/
          ~/.cargo/git/db/
          target/
        key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
        restore-keys: ${{ runner.os }}-cargo-

    - name: Build sql-experimental
      run: cargo b -j16 --release -p sql-experimental

    - name: go-libsql checkout
      uses: actions/checkout@v3
      with:
        repository: tursodatabase/go-libsql
        ref: "main"
        path: "go-libsql"

    - name: Copy sql-experimental library to go-libsql
      run: cp target/release/libsql_experimental.a go-libsql/lib/darwin_amd64/
           && cp target/release/libsql_experimental.a go-libsql/lib/darwin_arm64/
           && cp target/release/libsql_experimental.a go-libsql/lib/linux_amd64/
           && cp target/release/libsql_experimental.a go-libsql/lib/linux_arm64/
           && cp bindings/c/include/libsql.h go-libsql/lib/include/

    - name: Run go-libsql tests
      working-directory: go-libsql
      run: go test -v -count=1 ./...
      env:
        LIBSQL_PRIMARY_URL: "http://127.0.0.1:8080"