name: build-and-test-windows
on:
  push:
    branches: [main]
    tags:
      - "v[0-9]+.[0-9]+.[0-9]+*"
  merge_group:
    types: [checks_requested]
  pull_request:

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

permissions: read-all

jobs:
  windows-unittest:
    strategy:
      fail-fast: false
      matrix:
        os: [windows-2022, windows-2025, windows-11-arm]
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout Repo
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - name: Setup Go
        uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
        with:
          go-version: oldstable
          cache: false
      - name: Cache Go
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        env:
          cache-name: cache-go-modules
        with:
          path: |
            ~\go\pkg\mod
            ~\AppData\Local\go-build
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
      - name: Ensure required ports in the dynamic range are available
        run: |
          & ${{ github.workspace }}\.github\workflows\scripts\win-required-ports.ps1
      - name: Run Unit Tests
        run: make gotest

  windows-service-test:
    strategy:
      fail-fast: false
      matrix:
        os: [windows-2022, windows-2025, windows-11-arm]
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout Repo
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - name: Setup Go
        uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
        with:
          go-version: oldstable
          cache: false
      - name: Cache Go
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        env:
          cache-name: cache-go-modules
        with:
          path: |
            ~\go\pkg\mod
            ~\AppData\Local\go-build
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
      - name: Ensure required ports in the dynamic range are available
        run: |
          & ${{ github.workspace }}\.github\workflows\scripts\win-required-ports.ps1

      - name: Make otelcorecol
        run: make otelcorecol

      - name: Install otelcorecol as a service
        run: |
          New-Service -Name "otelcorecol" -StartupType "Manual" -BinaryPathName "${PWD}\bin\otelcorecol_windows_$(go env GOARCH) --config ${PWD}\examples\local\otel-config.yaml"
          eventcreate.exe /t information /id 1 /l application /d "Creating event provider for 'otelcorecol'" /so otelcorecol

      - name: Test otelcorecol service
        working-directory: ${{ github.workspace }}/otelcol
        run: |
          go test -timeout 90s -run ^TestCollectorAsService$  -v -tags=win32service

      - name: Remove otelcorecol service
        if: always()
        run: |
          Remove-Service otelcorecol
          Remove-Item HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Application\otelcorecol
