name: CI

on:
  push:
    branches: [main, master]
  pull_request:
    branches: [main, master]

jobs:
  test-linux:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-22.04, ubuntu-24.04, ubuntu-22.04-arm, ubuntu-24.04-arm]
        perl: ['5.28', '5.38', '5.40']
    name: Linux ${{ matrix.os }} / Perl ${{ matrix.perl }}
    steps:
      - uses: actions/checkout@v5

      - uses: shogo82148/actions-setup-perl@v1
        with:
          perl-version: ${{ matrix.perl }}

      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libgrpc-dev libgrpc++-dev libprotobuf-c-dev pkg-config

      - name: Install Perl dependencies
        run: cpanm --notest EV

      - name: Build
        run: |
          perl Makefile.PL
          make

      - name: Install etcd
        run: |
          ARCH=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')
          curl -fsSL "https://github.com/etcd-io/etcd/releases/download/v3.5.26/etcd-v3.5.26-linux-${ARCH}.tar.gz" | tar xz
          sudo mv "etcd-v3.5.26-linux-${ARCH}/etcd" /usr/local/bin/
          sudo mv "etcd-v3.5.26-linux-${ARCH}/etcdctl" /usr/local/bin/

      - name: Start etcd
        run: |
          etcd --data-dir /tmp/etcd-data &
          for i in $(seq 1 30); do
            etcdctl endpoint health 2>/dev/null && break
            sleep 1
          done
          etcdctl endpoint health

      - name: Test
        run: make test

  test-debian:
    runs-on: ubuntu-latest
    container: debian:bookworm
    name: Debian bookworm
    # etcd 3.5 in Debian-bookworm-in-container has issues with member
    # mutation operations (cluster.t fails) — known platform quirk, the
    # build/unit tests are the value here.
    continue-on-error: true
    steps:
      - uses: actions/checkout@v5

      - name: Install system dependencies
        run: |
          apt-get update
          apt-get install -y build-essential perl curl pkg-config \
            libgrpc-dev libgrpc++-dev libprotobuf-c-dev

      - name: Install cpanm and Perl dependencies
        run: |
          curl -fsSL https://cpanmin.us | perl - App::cpanminus
          cpanm --notest EV

      - name: Build
        run: |
          perl Makefile.PL
          make

      - name: Install etcd
        run: |
          ARCH=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')
          curl -fsSL "https://github.com/etcd-io/etcd/releases/download/v3.5.26/etcd-v3.5.26-linux-${ARCH}.tar.gz" | tar xz
          mv "etcd-v3.5.26-linux-${ARCH}/etcd" /usr/local/bin/
          mv "etcd-v3.5.26-linux-${ARCH}/etcdctl" /usr/local/bin/

      - name: Start etcd
        run: |
          etcd --data-dir /tmp/etcd-data &
          for i in $(seq 1 30); do
            etcdctl endpoint health 2>/dev/null && break
            sleep 1
          done
          etcdctl endpoint health

      - name: Test
        run: make test

  test-macos:
    runs-on: macos-latest
    strategy:
      fail-fast: false
      matrix:
        perl: ['5.38', '5.40']
    name: macOS / Perl ${{ matrix.perl }}
    steps:
      - uses: actions/checkout@v5

      - uses: shogo82148/actions-setup-perl@v1
        with:
          perl-version: ${{ matrix.perl }}

      - name: Install dependencies
        run: brew install grpc protobuf-c pkg-config

      - name: Install Perl dependencies
        run: cpanm --notest EV

      - name: Build
        run: |
          perl Makefile.PL
          make

      - name: Install etcd
        run: |
          ARCH=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/;s/arm64/arm64/')
          curl -fsSL "https://github.com/etcd-io/etcd/releases/download/v3.5.26/etcd-v3.5.26-darwin-${ARCH}.zip" -o etcd.zip
          unzip -q etcd.zip
          sudo mv "etcd-v3.5.26-darwin-${ARCH}/etcd" /usr/local/bin/
          sudo mv "etcd-v3.5.26-darwin-${ARCH}/etcdctl" /usr/local/bin/

      - name: Start etcd
        run: |
          etcd --data-dir /tmp/etcd-data &
          for i in $(seq 1 30); do
            etcdctl endpoint health 2>/dev/null && break
            sleep 1
          done
          etcdctl endpoint health

      - name: Test
        run: make test

  test-freebsd:
    runs-on: ubuntu-latest
    name: FreeBSD 14.2
    steps:
      - uses: actions/checkout@v5
      - uses: vmactions/freebsd-vm@v1
        with:
          release: "14.2"
          usesh: true
          prepare: |
            pkg install -y grpc protobuf-c pkgconf perl5 p5-EV gmake
            pkg install -y etcd3 || pkg install -y etcd || true
          run: |
            perl Makefile.PL
            gmake
            if command -v etcd >/dev/null 2>&1; then
              etcd --data-dir /tmp/etcd-data &
              sleep 5
              etcdctl endpoint health 2>/dev/null || true
            fi
            gmake test

  test-openbsd:
    runs-on: ubuntu-latest
    name: OpenBSD
    continue-on-error: true
    steps:
      - uses: actions/checkout@v5
      - uses: vmactions/openbsd-vm@v1
        with:
          usesh: true
          prepare: |
            pkg_add gmake grpc protobuf-c pkgconf
            pkg_add p5-EV || true
          run: |
            perl Makefile.PL
            gmake
            gmake test

  test-netbsd:
    runs-on: ubuntu-latest
    name: NetBSD
    continue-on-error: true
    steps:
      - uses: actions/checkout@v5
      - uses: vmactions/netbsd-vm@v1
        with:
          usesh: true
          prepare: |
            pkg_add pkgin
            pkgin -y install gmake grpc protobuf-c pkgconf
            pkgin -y install p5-EV || true
          run: |
            perl Makefile.PL
            gmake
            gmake test
