name: 'test'
on:
  pull_request:
    paths-ignore:
      - '**.md'
    tags-ignore:
      - '**'
  push:
    branches:
      - '**'
    paths-ignore:
      - '**.md'
    tags-ignore:
      - '**'
  workflow_dispatch:

jobs:
  tests:
    name: 🧪 Test on ${{ matrix.os }}, .NET=${{ matrix.use-dotnet }}, include-templates=${{ matrix.include-templates }}
    if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
    runs-on: ${{ matrix.os }}
    strategy:
      # Don't cancel other OS runners if one fails.
      fail-fast: false
      matrix:
        # Put the operating systems you want to run on here.
        os: [ubuntu-latest, macos-latest, windows-latest]
        use-dotnet: [false, true]
        include-templates: [false, true]
    env:
      DOTNET_CLI_TELEMETRY_OPTOUT: true
      DOTNET_NOLOGO: true
    defaults:
      run:
        # Use bash shells on all platforms.
        shell: bash
    steps:
      - uses: actions/checkout@v4
        name: 🧾 Checkout

      - uses: actions/setup-dotnet@v4
        if: ${{ matrix.use-dotnet }}
        name: 💽 Setup .NET SDK
        with:
          dotnet-version: '6.0.x'

      - uses: ./
        name: 🤖 Setup Godot
        with:
          # Version must include major, minor, and patch, and be >= 4.0.0
          # Pre-release label is optional.
          version: 4.0.0
          use-dotnet: ${{ matrix.use-dotnet }}
          include-templates: ${{ matrix.include-templates }}

      - name: 🔬 Verify Dotnet
        if: ${{ matrix.use-dotnet }}
        run: |
          dotnet --version

      - name: 🔬 Verify Godot
        run: |
          godot --version

  test-global-json:
    name: 🌐 Test with version from global.json
    if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        name: 🧾 Checkout

      - uses: ./
        name: 🤖 Setup Godot
        with:
          version: global.json # Use the version specified in global.json
          use-dotnet: false
      
      - name: 🔬 Verify Godot
        run: |
          godot --version

  check-dist:
    if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set Node.js 20.x
        uses: actions/setup-node@v4
        with:
          node-version: 20.x

      - name: Install dependencies
        run: npm ci

      - name: Rebuild the dist/ directory
        run: |
          npm run build
          npm run package

      - name: Compare the expected and actual dist/ directories
        run: |
          if [ "$(git diff --ignore-all-space --ignore-space-at-eol dist/ | wc -l)" -gt "0" ]; then
            echo "Detected uncommitted changes after build.  See status below:"
            git diff
            exit 1
          fi
        id: diff

      # If index.js was different than expected, upload the expected version as an artifact
      - uses: actions/upload-artifact@v3
        if: ${{ failure() && steps.diff.conclusion == 'failure' }}
        with:
          name: dist
          path: dist/
