name: Build

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Release tag'
        type: string
        required: false
  push:
    branches: [main, dev]
  pull_request:
    branches: [main, dev]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
  cancel-in-progress: true

jobs:
  linux:
    runs-on: ubuntu-24.04
    strategy:
      fail-fast: false
      matrix:
        arch: [amd64, arm64, "386", arm, loong64, mipsle, mips64le, riscv64]
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: 'recursive'
      - uses: actions/setup-go@v5
        with:
          go-version: ^1.25
      - name: Get naiveproxy commit
        id: naive
        run: echo "commit=$(git -C naiveproxy rev-parse HEAD)" >> $GITHUB_OUTPUT
      - name: Get Chromium version
        id: chromium
        run: echo "version=$(cat naiveproxy/CHROMIUM_VERSION)" >> $GITHUB_OUTPUT
      - name: Regenerate Debian keyring
        run: |
          rm -f ./naiveproxy/src/build/linux/sysroot_scripts/keyring.gpg
          GPG_TTY=/dev/null ./naiveproxy/src/build/linux/sysroot_scripts/generate_keyring.sh
      - name: Cache toolchain
        uses: actions/cache@v4
        with:
          path: |
            naiveproxy/src/third_party/llvm-build
            naiveproxy/src/gn/out
            naiveproxy/src/chrome/build/pgo_profiles
            naiveproxy/src/out/sysroot-build
          key: toolchain-linux-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}-${{ hashFiles('naiveproxy/src/get-clang.sh', 'naiveproxy/src/get-sysroot.sh', 'naiveproxy/src/get-openwrt.sh', 'naiveproxy/src/DEPS', 'naiveproxy/src/tools/clang/scripts/update.py', 'naiveproxy/src/build/linux/sysroot_scripts/sysroot_creator.py', 'naiveproxy/src/build/linux/sysroot_scripts/reversion_glibc.py', 'naiveproxy/src/build/linux/sysroot_scripts/generated_package_lists/**', 'cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}
      - name: Cache build artifacts
        id: build-cache
        uses: actions/cache@v4
        with:
          path: naiveproxy/src/out
          key: naive-build-${{ steps.naive.outputs.commit }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-linux-${{ matrix.arch }}
      - name: Cache ccache files
        if: steps.build-cache.outputs.cache-hit != 'true'
        uses: actions/cache@v4
        with:
          path: ~/.cache/ccache
          key: ccache-linux-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}
          restore-keys: ccache-linux-${{ matrix.arch }}-
      - name: Install packages
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: |
          sudo apt update || true
          sudo apt install -y ninja-build ccache
      - name: Reset ccache stats
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: ccache -z
      - name: Build
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: go run ./cmd/build-naive --target=linux/${{ matrix.arch }} build
      - name: Show ccache stats
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: ccache -s
      - name: Package
        run: |
          go run ./cmd/build-naive --target=linux/${{ matrix.arch }} package --local
          go run ./cmd/build-naive --target=linux/${{ matrix.arch }} package
      - name: Download clang and sysroot
        run: go run ./cmd/build-naive --target=linux/${{ matrix.arch }} download-toolchain
      - name: Install QEMU for cross-platform testing
        run: |
          sudo apt update || true
          sudo apt install -y qemu-user-binfmt
      - name: Build test binary
        run: |
          eval $(go run ./cmd/build-naive --target=linux/${{ matrix.arch }} env --export)
          GO_TEST_LDFLAGS=
          if [ "${{ matrix.arch }}" = "mipsle" ] || [ "${{ matrix.arch }}" = "mips64le" ]; then
            GO_TEST_LDFLAGS=-ldflags=-w
          fi
          CGO_ENABLED=1 GOOS=linux GOARCH=${{ matrix.arch }} go -C test test -c $GO_TEST_LDFLAGS -o ../cronet.test .
      - name: Run test binary
        run: |
          eval $(go run ./cmd/build-naive --target=linux/${{ matrix.arch }} env --export)
          if [ "${{ matrix.arch }}" != "amd64" ] && [ "${{ matrix.arch }}" != "386" ]; then
            export QEMU_LD_PREFIX
          fi
          ./cronet.test -test.v -test.run=TestEngineVersion
      - name: Build purego test binary
        if: matrix.arch == 'amd64' || matrix.arch == 'arm64' || matrix.arch == 'loong64'
        run: |
          cp lib/linux_${{ matrix.arch }}/libcronet.so .
          CGO_ENABLED=0 GOOS=linux GOARCH=${{ matrix.arch }} go -C test test -tags with_purego -c -o ../cronet.purego.test .
      - name: Run purego test binary
        if: matrix.arch == 'amd64' || matrix.arch == 'arm64' || matrix.arch == 'loong64'
        run: |
          if [ "${{ matrix.arch }}" != "amd64" ]; then
            eval $(go run ./cmd/build-naive --target=linux/${{ matrix.arch }} env --export)
            export QEMU_LD_PREFIX
          fi
          export LD_LIBRARY_PATH=$PWD
          ./cronet.purego.test -test.v -test.run=TestEngineVersion
      - uses: actions/upload-artifact@v4
        with:
          name: cronet-linux-${{ matrix.arch }}
          path: |
            lib/
            include/
            include_cgo.go
            lib_*_cgo.go

  darwin:
    runs-on: macos-15
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: darwin/amd64
            name: darwin-amd64
          - target: darwin/arm64
            name: darwin-arm64
          # iOS
          - target: ios/arm64
            name: ios-arm64
          - target: ios/arm64/simulator
            name: ios-arm64-simulator
          - target: ios/amd64
            name: ios-amd64-simulator
          # tvOS
          - target: tvos/arm64
            name: tvos-arm64
          - target: tvos/arm64/simulator
            name: tvos-arm64-simulator
          - target: tvos/amd64
            name: tvos-amd64-simulator
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: 'recursive'
      - uses: actions/setup-go@v5
        with:
          go-version: ^1.22
      - name: Get naiveproxy commit
        id: naive
        run: echo "commit=$(git -C naiveproxy rev-parse HEAD)" >> $GITHUB_OUTPUT
      - name: Get Chromium version
        id: chromium
        run: echo "version=$(cat naiveproxy/CHROMIUM_VERSION)" >> $GITHUB_OUTPUT
      - name: Cache build artifacts
        id: build-cache
        uses: actions/cache@v4
        with:
          path: naiveproxy/src/out
          key: naive-build-${{ steps.naive.outputs.commit }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-${{ matrix.name }}
      - name: Cache ccache files
        if: steps.build-cache.outputs.cache-hit != 'true'
        uses: actions/cache@v4
        with:
          path: ~/Library/Caches/ccache
          key: ccache-darwin-${{ matrix.name }}-${{ steps.chromium.outputs.version }}
          restore-keys: ccache-darwin-${{ matrix.name }}-
      - name: Install tools
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: brew install ninja ccache
      - name: Reset ccache stats
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: ccache -z
      - name: Build
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: go run ./cmd/build-naive --target=${{ matrix.target }} build
      - name: Show ccache stats
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: ccache -s
      - name: Package
        run: |
          go run ./cmd/build-naive --target=${{ matrix.target }} package --local
          go run ./cmd/build-naive --target=${{ matrix.target }} package
      - name: Build and run test binary
        if: matrix.target == 'darwin/arm64'
        run: |
          CGO_ENABLED=1 go -C test test -c -o ../cronet.test .
          ./cronet.test -test.v -test.run=TestEngineVersion
      - uses: actions/upload-artifact@v4
        with:
          name: cronet-${{ matrix.name }}
          path: |
            lib/
            include/
            include_cgo.go
            lib_*_cgo.go

  windows:
    runs-on: windows-2022
    env:
      SCCACHE_ARM64_VERSION: 0.12.0
    strategy:
      fail-fast: false
      matrix:
        arch: [amd64, arm64]
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: 'recursive'
      - uses: actions/setup-go@v5
        with:
          go-version: ^1.22
      - name: Get naiveproxy commit
        id: naive
        shell: bash
        run: echo "commit=$(git -C naiveproxy rev-parse HEAD)" >> $GITHUB_OUTPUT
      - name: Get Chromium version
        id: chromium
        shell: bash
        run: echo "version=$(cat naiveproxy/CHROMIUM_VERSION)" >> $GITHUB_OUTPUT
      - name: Cache build artifacts
        id: build-cache
        uses: actions/cache@v4
        with:
          path: naiveproxy/src/out
          key: naive-build-${{ steps.naive.outputs.commit }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-windows-${{ matrix.arch }}
      - name: Install tools
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: |
          choco install ninja -y
      - name: Install sccache
        if: steps.build-cache.outputs.cache-hit != 'true'
        shell: pwsh
        run: |
          if ("${{ matrix.arch }}" -eq "arm64") {
            choco install sccache --version=${{ env.SCCACHE_ARM64_VERSION }} -y
          } else {
            choco install sccache -y
          }
      - name: Get sccache version
        if: steps.build-cache.outputs.cache-hit != 'true'
        id: sccache
        shell: pwsh
        run: |
          $rawVersion = sccache --version
          if ($LASTEXITCODE -ne 0) {
            throw "failed to get sccache version"
          }
          $version = ($rawVersion | Select-Object -First 1) -replace '^sccache\s+', ''
          if ([string]::IsNullOrWhiteSpace($version)) {
            throw "invalid sccache version output: $rawVersion"
          }
          "version=$version" >> $env:GITHUB_OUTPUT
      - name: Cache sccache files
        if: steps.build-cache.outputs.cache-hit != 'true'
        uses: actions/cache@v4
        with:
          path: ~/AppData/Local/Mozilla/sccache
          key: sccache-windows-${{ matrix.arch }}-${{ steps.sccache.outputs.version }}-${{ steps.chromium.outputs.version }}
          restore-keys: sccache-windows-${{ matrix.arch }}-${{ steps.sccache.outputs.version }}-
      - name: Reset sccache stats
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: sccache -z
      - name: Build
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: go run ./cmd/build-naive --target=windows/${{ matrix.arch }} build
      - name: Show sccache stats
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: sccache -s
      - name: Package
        run: go run ./cmd/build-naive --target=windows/${{ matrix.arch }} package
      - name: Build and run test binary
        if: matrix.arch != 'arm64'
        shell: bash
        run: |
          # Copy DLL to current directory for fallback loading
          cp lib/windows_${{ matrix.arch }}/libcronet.dll .
          CGO_ENABLED=0 \
          GOOS=windows \
          GOARCH=${{ matrix.arch }} \
          go -C test test -tags with_purego -c -o ../cronet.test.exe .
          ./cronet.test.exe -test.v -test.run=TestEngineVersion
      - uses: actions/upload-artifact@v4
        with:
          name: cronet-windows-${{ matrix.arch }}
          path: |
            lib/
            include/
            include_cgo.go

  android:
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: arm64
            cc: aarch64-linux-android23-clang
          - arch: amd64
            cc: x86_64-linux-android23-clang
          - arch: arm
            cc: armv7a-linux-androideabi23-clang
          - arch: "386"
            cc: i686-linux-android23-clang
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: 'recursive'
      - uses: actions/setup-go@v5
        with:
          go-version: ^1.22
      - name: Get naiveproxy commit
        id: naive
        run: echo "commit=$(git -C naiveproxy rev-parse HEAD)" >> $GITHUB_OUTPUT
      - name: Get Chromium version
        id: chromium
        run: echo "version=$(cat naiveproxy/CHROMIUM_VERSION)" >> $GITHUB_OUTPUT
      - name: Cache build artifacts
        id: build-cache
        uses: actions/cache@v4
        with:
          path: naiveproxy/src/out
          key: naive-build-${{ steps.naive.outputs.commit }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-android-${{ matrix.arch }}
      - name: Cache ccache files
        if: steps.build-cache.outputs.cache-hit != 'true'
        uses: actions/cache@v4
        with:
          path: ~/.cache/ccache
          key: ccache-android-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}
          restore-keys: ccache-android-${{ matrix.arch }}-
      - name: Install packages
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: |
          sudo apt update || true
          sudo apt install -y ninja-build ccache
      - name: Reset ccache stats
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: ccache -z
      - name: Build
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: go run ./cmd/build-naive --target=android/${{ matrix.arch }} build
      - name: Show ccache stats
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: ccache -s
      - name: Package
        run: |
          go run ./cmd/build-naive --target=android/${{ matrix.arch }} package --local
          go run ./cmd/build-naive --target=android/${{ matrix.arch }} package
      - name: Setup Android NDK
        uses: nttld/setup-ndk@v1
        id: setup-ndk
        with:
          ndk-version: r28
      - name: Build test binary
        run: |
          CC=${{ steps.setup-ndk.outputs.ndk-path }}/toolchains/llvm/prebuilt/linux-x86_64/bin/${{ matrix.cc }} \
          CGO_ENABLED=1 GOOS=android GOARCH=${{ matrix.arch }} go -C test test -c -o ../cronet.test .
      - uses: actions/upload-artifact@v4
        with:
          name: cronet-android-${{ matrix.arch }}
          path: |
            lib/
            include/
            include_cgo.go
            lib_*_cgo.go

  linux-musl:
    runs-on: ubuntu-24.04
    strategy:
      fail-fast: false
      matrix:
        arch: [amd64, arm64, "386", arm, loong64, mipsle, riscv64]
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: 'recursive'
      - uses: actions/setup-go@v5
        with:
          go-version: ^1.22
      - name: Get naiveproxy commit
        id: naive
        run: echo "commit=$(git -C naiveproxy rev-parse HEAD)" >> $GITHUB_OUTPUT
      - name: Get Chromium version
        id: chromium
        run: echo "version=$(cat naiveproxy/CHROMIUM_VERSION)" >> $GITHUB_OUTPUT
      - name: Cache toolchain
        uses: actions/cache@v4
        with:
          path: |
            naiveproxy/src/third_party/llvm-build
            naiveproxy/src/gn/out
            naiveproxy/src/chrome/build/pgo_profiles
            naiveproxy/src/out/sysroot-build
          key: toolchain-linux-musl-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}-${{ hashFiles('naiveproxy/src/get-clang.sh', 'naiveproxy/src/get-sysroot.sh', 'naiveproxy/src/get-openwrt.sh', 'naiveproxy/src/DEPS', 'naiveproxy/src/tools/clang/scripts/update.py', 'naiveproxy/src/build/linux/sysroot_scripts/sysroot_creator.py', 'naiveproxy/src/build/linux/sysroot_scripts/reversion_glibc.py', 'naiveproxy/src/build/linux/sysroot_scripts/generated_package_lists/**', 'cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}
      - name: Cache build artifacts
        id: build-cache
        uses: actions/cache@v4
        with:
          path: naiveproxy/src/out
          key: naive-build-${{ steps.naive.outputs.commit }}-${{ hashFiles('cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}-linux-musl-${{ matrix.arch }}
      - name: Cache ccache files
        if: steps.build-cache.outputs.cache-hit != 'true'
        uses: actions/cache@v4
        with:
          path: ~/.cache/ccache
          key: ccache-linux-musl-${{ matrix.arch }}-${{ steps.chromium.outputs.version }}
          restore-keys: ccache-linux-musl-${{ matrix.arch }}-
      - name: Install packages
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: |
          sudo apt update || true
          sudo apt install -y ninja-build zstd ccache
      - name: Reset ccache stats
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: ccache -z
      - name: Build
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: go run ./cmd/build-naive --target=linux/${{ matrix.arch }} --libc=musl build
      - name: Show ccache stats
        if: steps.build-cache.outputs.cache-hit != 'true'
        run: ccache -s
      - name: Package
        run: |
          go run ./cmd/build-naive --target=linux/${{ matrix.arch }} --libc=musl package --local
          go run ./cmd/build-naive --target=linux/${{ matrix.arch }} --libc=musl package
      - name: Download toolchain
        run: go run ./cmd/build-naive --target=linux/${{ matrix.arch }} --libc=musl download-toolchain
      - name: Install QEMU for cross-platform testing
        run: |
          sudo apt update || true
          sudo apt install -y qemu-user-binfmt
      - name: Build test binary
        run: |
          eval $(go run ./cmd/build-naive --target=linux/${{ matrix.arch }} --libc=musl env --export)
          [[ "${{ matrix.arch }}" == "mipsle" ]] && export GOMIPS=softfloat || true
          GO_TEST_LDFLAGS=
          [[ "${{ matrix.arch }}" == "mipsle" ]] && GO_TEST_LDFLAGS=-ldflags=-w || true
          CGO_ENABLED=1 GOOS=linux GOARCH=${{ matrix.arch }} go -C test test -c $GO_TEST_LDFLAGS -tags with_musl -o ../cronet.test .
      - name: Run test binary
        run: |
          # Static binary - can run directly with QEMU binfmt or explicit QEMU for cross-arch
          case "${{ matrix.arch }}" in
            amd64)   QEMU=() ;;
            arm64)   QEMU=(qemu-aarch64) ;;
            386)     QEMU=(qemu-i386) ;;
            arm)     QEMU=(qemu-arm) ;;
            loong64) QEMU=(qemu-loongarch64) ;;
            mipsle)  QEMU=(qemu-mipsel) ;;
            riscv64) QEMU=(qemu-riscv64) ;;
          esac
          timeout 120 "${QEMU[@]}" ./cronet.test -test.v -test.run=TestEngineVersion -test.timeout=90s
      - uses: actions/upload-artifact@v4
        with:
          name: cronet-linux-musl-${{ matrix.arch }}
          path: |
            lib/
            include/
            include_cgo.go
            lib_*_cgo.go

  integration-test:
    if: github.event_name != 'workflow_dispatch'
    needs: [linux, darwin, windows]
    strategy:
      fail-fast: false
      matrix:
        include:
          # Linux
          - name: linux-amd64-cgo
            os: ubuntu-22.04
            artifact: cronet-linux-amd64
            target: linux/amd64
            mode: cgo
          - name: linux-amd64-purego
            os: ubuntu-22.04
            artifact: cronet-linux-amd64
            target: linux/amd64
            mode: purego
          - name: linux-arm64-purego
            os: ubuntu-22.04-arm
            artifact: cronet-linux-arm64
            target: linux/arm64
            mode: purego
          # macOS ARM64
          - name: darwin-arm64-macos15
            os: macos-15
            artifact: cronet-darwin-arm64
            target: darwin/arm64
            mode: cgo
          - name: darwin-arm64-macos26
            os: macos-26
            artifact: cronet-darwin-arm64
            target: darwin/arm64
            mode: cgo
          # macOS x64 (Intel)
          - name: darwin-amd64-macos15
            os: macos-15-intel
            artifact: cronet-darwin-amd64
            target: darwin/amd64
            mode: cgo
          # Windows x64
          - name: windows-amd64-2022
            os: windows-2022
            artifact: cronet-windows-amd64
            target: windows/amd64
            mode: purego
          - name: windows-amd64-2025
            os: windows-2025
            artifact: cronet-windows-amd64
            target: windows/amd64
            mode: purego
          # Windows ARM64
          - name: windows-arm64
            os: windows-11-arm
            artifact: cronet-windows-arm64
            target: windows/arm64
            mode: purego
    runs-on: ${{ matrix.os }}
    timeout-minutes: 40
    env:
      CRONET_TEST_ARTIFACT_DIR: ${{ github.workspace }}/test-artifacts/${{ matrix.name }}
      CRONET_TEST_CAPTURE_NETLOG: "1"
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: 'recursive'

      - uses: actions/setup-go@v5
        with:
          go-version: ^1.24

      - name: Download artifact
        uses: actions/download-artifact@v4
        with:
          name: ${{ matrix.artifact }}
          path: .

      - name: Get Chromium version
        id: chromium
        shell: bash
        run: echo "version=$(cat naiveproxy/CHROMIUM_VERSION)" >> $GITHUB_OUTPUT

      # Linux: restore toolchain cache (needed for env command)
      - name: Restore toolchain cache (Linux)
        if: runner.os == 'Linux'
        uses: actions/cache/restore@v4
        with:
          path: |
            naiveproxy/src/third_party/llvm-build
            naiveproxy/src/gn/out
            naiveproxy/src/chrome/build/pgo_profiles
            naiveproxy/src/out/sysroot-build
          key: toolchain-linux-${{ contains(matrix.target, 'arm64') && 'arm64' || 'amd64' }}-${{ steps.chromium.outputs.version }}-${{ hashFiles('naiveproxy/src/get-clang.sh', 'naiveproxy/src/get-sysroot.sh', 'naiveproxy/src/get-openwrt.sh', 'naiveproxy/src/DEPS', 'naiveproxy/src/tools/clang/scripts/update.py', 'naiveproxy/src/build/linux/sysroot_scripts/sysroot_creator.py', 'naiveproxy/src/build/linux/sysroot_scripts/reversion_glibc.py', 'naiveproxy/src/build/linux/sysroot_scripts/generated_package_lists/**', 'cmd/build-naive/cmd_build.go', 'cmd/build-naive/cmd.go') }}

      - name: Download toolchain (Linux)
        if: runner.os == 'Linux'
        run: go run ./cmd/build-naive --target=${{ matrix.target }} download-toolchain

      # Install iperf3
      - name: Install iperf3 (Linux)
        if: runner.os == 'Linux'
        run: |
          sudo apt update || true
          sudo apt install -y iperf3

      - name: Install iperf3 (macOS)
        if: runner.os == 'macOS'
        run: brew install iperf3

      - name: Install iperf3 (Windows)
        if: runner.os == 'Windows'
        run: choco install iperf3

      # CGO mode tests
      - name: Run integration tests (CGO)
        if: matrix.mode == 'cgo'
        timeout-minutes: 20
        run: |
          mkdir -p "$CRONET_TEST_ARTIFACT_DIR"
          eval $(go run ./cmd/build-naive --target=${{ matrix.target }} env --export)
          cd test
          LOG_PATH="$CRONET_TEST_ARTIFACT_DIR/go-test.log"
          if ! CGO_ENABLED=1 go test -v -timeout=12m >"$LOG_PATH" 2>&1; then
            tail -n 400 "$LOG_PATH" || true
            exit 1
          fi

      # purego mode tests
      - name: Run integration tests (purego - Linux)
        if: matrix.mode == 'purego' && runner.os == 'Linux'
        timeout-minutes: 20
        working-directory: test
        run: |
          mkdir -p "$CRONET_TEST_ARTIFACT_DIR"
          export LD_LIBRARY_PATH=$PWD/../lib/linux_${{ contains(matrix.target, 'arm64') && 'arm64' || 'amd64' }}
          LOG_PATH="$CRONET_TEST_ARTIFACT_DIR/go-test.log"
          if ! CGO_ENABLED=0 go test -tags with_purego -v -timeout=12m >"$LOG_PATH" 2>&1; then
            tail -n 400 "$LOG_PATH" || true
            exit 1
          fi

      - name: Run integration tests (purego - Windows)
        if: matrix.mode == 'purego' && runner.os == 'Windows'
        timeout-minutes: 20
        working-directory: test
        shell: bash
        run: |
          # Determine library directory based on target architecture
          case "${{ matrix.target }}" in
            */arm64) LIB_DIR="windows_arm64" ;;
            */amd64) LIB_DIR="windows_amd64" ;;
          esac
          export PATH="$PWD/../lib/${LIB_DIR}:$PATH"
          mkdir -p "$CRONET_TEST_ARTIFACT_DIR"
          LOG_PATH="$CRONET_TEST_ARTIFACT_DIR/go-test.log"
          if ! go test -tags with_purego -v -timeout=12m >"$LOG_PATH" 2>&1; then
            tail -n 400 "$LOG_PATH" || true
            exit 1
          fi

      - name: Stress QUIC socket handoff (Windows Server 2022)
        if: matrix.name == 'windows-amd64-2022'
        timeout-minutes: 10
        working-directory: test
        shell: bash
        run: |
          export PATH="$PWD/../lib/windows_amd64:$PATH"
          go test -tags with_purego -run '^TestNaiveQUICLargeTransfer$' -count=20 -timeout=8m

      - name: Upload integration logs (test failure)
        if: always() && (failure() || cancelled())
        uses: actions/upload-artifact@v4
        with:
          name: integration-logs-${{ matrix.name }}
          path: test-artifacts/${{ matrix.name }}/
          if-no-files-found: warn

  publish:
    if: github.event_name == 'push' && !contains(github.event.head_commit.message, '[skip publish]')
    needs: [linux, linux-musl, darwin, windows, android, integration-test]
    runs-on: ubuntu-22.04
    env:
      TARGET_BRANCH: ${{ github.ref_name == 'main' && 'go' || 'go_dev' }}
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-go@v5
        with:
          go-version: ^1.22

      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts/

      - name: Merge artifacts
        run: |
          for dir in artifacts/cronet-*/; do
            cp -r "$dir"/* .
          done
          ls -laR lib/ include/ include_cgo.go

      - name: Configure git
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

      - name: Publish
        run: go run ./cmd/build-naive publish --branch ${{ env.TARGET_BRANCH }}

  release:
    needs: [linux, windows]
    if: github.event_name == 'workflow_dispatch'
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - name: Download Linux artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: cronet-linux-*
          path: artifacts/
          merge-multiple: false

      - name: Download Windows artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: cronet-windows-*
          path: artifacts/
          merge-multiple: false

      - name: Get Chromium version
        id: chromium
        run: |
          VERSION=$(grep -h 'const Version' artifacts/cronet-linux-amd64/lib/linux_amd64/libcronet_cgo.go | sed 's/.*"\(.*\)".*/\1/')
          echo "version=$VERSION" >> $GITHUB_OUTPUT

      - name: Prepare release assets
        run: |
          mkdir -p release

          # Linux glibc shared libraries
          for dir in artifacts/cronet-linux-*/lib/linux_*; do
            if [[ "$dir" != *_musl ]]; then
              ARCH=$(basename "$dir" | sed 's/linux_//')
              if [ -f "$dir/libcronet.so" ]; then
                cp "$dir/libcronet.so" "release/libcronet-linux-${ARCH}.so"
              fi
            fi
          done

          # Windows DLLs
          for dir in artifacts/cronet-windows-*/lib/windows_*; do
            ARCH=$(basename "$dir" | sed 's/windows_//')
            if [ -f "$dir/libcronet.dll" ]; then
              cp "$dir/libcronet.dll" "release/libcronet-windows-${ARCH}.dll"
            fi
          done

          ls -la release/

      - name: Create or update release draft
        if: github.event.inputs.version != ''
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          VERSION="${{ github.event.inputs.version }}"
          CHROMIUM_VERSION="${{ steps.chromium.outputs.version }}"

          # Check if release exists
          if gh release view "$VERSION" &>/dev/null; then
            echo "Release $VERSION exists, uploading assets..."
          else
            echo "Creating release draft $VERSION..."
            gh release create "$VERSION" \
              --draft \
              --title "$VERSION" \
              --notes "Cronet Go binding release based on Chromium $CHROMIUM_VERSION"
          fi

          # Upload assets (Linux .so and Windows .dll)
          gh release upload "$VERSION" release/* --clobber
