name: Go Test Multi-Platform

on:
  push:
    branches:
      - main
      - master
  pull_request:
    branches:
      - main
      - master

permissions:
  contents: read

jobs:
  test:
    name: Test (${{ matrix.os }}, ${{ matrix.goarch }})
    strategy:
      matrix:
        include:
          # AMD64 testing on Linux
          - os: ubuntu-latest
            goarch: amd64
          # ARM64 testing on Linux
          - os: ubuntu-latest
            goarch: arm64

    runs-on: ${{ matrix.os }}

    steps:
      - name: Checkout Source
        uses: https://git.quad4.io/actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

      - name: Set up Go 1.25
        uses: https://git.quad4.io/actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
        with:
          go-version: '1.25'

      - name: Setup Task
        uses: https://git.quad4.io/actions/setup-task@0ab1b2a65bc55236a3bc64cde78f80e20e8885c2 # v1
        with:
          version: '3.46.3'

      - name: Cache Go modules
        uses: https://git.quad4.io/actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0
        with:
          path: |
            ~/go/pkg/mod
            ~/.cache/go-build
          key: ${{ runner.os }}-go-${{ matrix.goarch }}-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-${{ matrix.goarch }}-

      - name: Set up Node.js
        if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'amd64'
        uses: https://git.quad4.io/actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6
        with:
          node-version: '22'

      - name: Run tests
        run: task test

      - name: Run tests with race detector (Linux AMD64 only)
        if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'amd64'
        run: task test-race

      - name: Run WebAssembly tests (Linux AMD64 only)
        if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'amd64'
        run: |
          chmod +x misc/wasm/go_js_wasm_exec
          task test-wasm

      - name: Test build (ensure compilation works)
        run: |
          echo "Testing build for current platform (${{ matrix.os }}, ${{ matrix.goarch }})..."
          task build

      - name: Test WebAssembly build (Linux AMD64 only)
        if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'amd64'
        run: task build-wasm

      - name: Test binary execution
        run: |
          echo "Testing binary execution on (${{ matrix.os }}, ${{ matrix.goarch }})..."
          timeout 5s ./bin/reticulum-go || echo "Binary started successfully (timeout expected)"

      - name: Test cross-compilation (AMD64 runners only)
        if: matrix.goarch == 'amd64'
        run: |
          echo "Testing ARM64 cross-compilation from AMD64..."
          GOOS=linux GOARCH=arm64 task build
        env:
          GOOS: linux
          GOARCH: arm64

      - name: Test ARMv6 cross-compilation (AMD64 runners only)
        if: matrix.goarch == 'amd64'
        run: |
          echo "Testing ARMv6 cross-compilation from AMD64..."
          GOOS=linux GOARCH=arm GOARM=6 task build
        env:
          GOOS: linux
          GOARCH: arm
          GOARM: 6
