name: Windows Selftest

on:
  pull_request:
    types:
      - opened
      - reopened
      - synchronize
    paths:
      - 'meteor'
      - 'meteor.bat'
      - 'tools/**'
      - 'packages/babel-compiler/**'
      - 'packages/dynamic-import/**'
      - 'packages/meteor/**'
      - 'packages/meteor-tool/**'
      - '.github/workflows/windows-selftest.yml'

  push:
    branches:
      - devel

env:
  METEOR_PRETTY_OUTPUT: 0
  SELF_TEST_TOOL_NODE_FLAGS: ' '
  TOOL_NODE_FLAGS: --expose-gc
  TIMEOUT_SCALE_FACTOR: 20
  METEOR_HEADLESS: true
  SELF_TEST_EXCLUDE: '^NULL-LEAVE-THIS-HERE-NULL$'
  METEOR_MODERN: true

jobs:
  test:
    runs-on: windows-2019-meteor
    concurrency:
      group: ${{ github.head_ref }}-meteor-selftest-windows
      cancel-in-progress: true

    steps:
      - name: Cleanup
        shell: powershell
        run: Remove-Item -Recurse -Force ${{ github.workspace }}\*

      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 22.x

      - name: Cache dependencies
        id: meteor-cache
        uses: actions/cache@v4
        with:
          path: |
            dev_bundle/
            .babel-cache/
            .meteor/
            ~/.npm
            node_modules/
            packages/**/.npm
          key: ${{ runner.os }}-meteor-${{ hashFiles('meteor', 'meteor.bat') }}
          restore-keys: |
            ${{ runner.os }}-meteor-

      - name: Reset submodules (force refetch)
        shell: pwsh
        run: |
          git submodule deinit -f --all
          if (Test-Path ".git/modules") { Remove-Item -Recurse -Force ".git/modules" }

      - name: Install dependencies
        shell: pwsh
        run: |
          $env:PATH = "C:\Program Files\7-Zip;$env:PATH"
          .\scripts\windows\ci\install.ps1

      # Run ONLY when the cache was NOT restored
      - name: Prepare Meteor (cache miss)
        if: steps.meteor-cache.outputs.cache-hit != 'true'
        shell: pwsh
        run: |
          $env:PATH = "C:\Program Files\7-Zip;$env:PATH"
          .\meteor.bat --get-ready

      - name: Run tests
        shell: pwsh
        run: |
          $env:PATH = "C:\Program Files\7-Zip;$env:PATH"
          .\scripts\windows\ci\test.ps1
