name: Language server tests

on:
  schedule:
  # * is a special character in YAML so you have to quote this string
    - cron: '0 8 * * 6'
  workflow_dispatch:
  workflow_call:
    inputs:
      all-platforms:
        required: false
        default: true
        type: boolean

jobs:
  test-lsp:
    strategy:
      matrix:
        platform: ${{ (inputs.all-platforms && fromJSON('["ubuntu-24.04", "macos-latest", "windows-latest"]')) || fromJSON('["ubuntu-24.04"]') }}
    runs-on: ${{ matrix.platform }}
    steps:
      # Uninstall operations are needed because the language server is able to use multiple
      # different compilers for syntax checking. We test that it correctly detects which tools are
      # present and responds appropriately.
      - name: Check out lingua-franca repository
        uses: actions/checkout@v3
        with:
          repository: lf-lang/lingua-franca
          submodules: true
          ref: ${{ inputs.compiler-ref }}
          fetch-depth: 0
      - name: Prepare build environment
        uses: ./.github/actions/prepare-build-env
      - name: Uninstall packages Ubuntu
        run: sudo apt-get remove clang-*
        if: ${{ runner.os == 'Linux' }}
      - name: Uninstall packages Windows
        shell: pwsh
        run: |
          try { $exes=Get-Command "g++" -All; $exes | Remove-Item; } catch { "There is no g++ present in pwsh PATH." }
          try { $exes=Get-Command "clang++" -All; $exes | Remove-Item; } catch { "There is no clang++ present in pwsh PATH." }
        if: ${{ runner.os == 'Windows' }}
      - name: Setup Node.js environment
        uses: actions/setup-node@v3
        with:
          node-version: 18
      - name: Install pnpm
        run: npm i -g pnpm
      - name: Cache .pnpm-store
        uses: actions/cache@v4
        with:
          path: ~/.pnpm-store
          key: ${{ runner.os }}-node${{ matrix.node-version }}-${{ hashFiles('core/src/main/resources/lib/ts/package.json') }}
      - name: Setup Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable
          components: clippy
          rustflags: "" # don't use -D warnings
      - name: Install Dependencies Ubuntu
        run: |
          sudo apt-get install libprotobuf-dev protobuf-compiler libprotobuf-c-dev protobuf-c-compiler
        if: ${{ runner.os == 'Linux' }}
      - name: Install Dependencies OS X
        run: |
          brew install protobuf
          brew install protobuf-c
        if: ${{ runner.os == 'macOS' }}
      - name: Setup Python
        uses: actions/setup-python@v4
        with:
          python-version: "3.12"
      - name: Run language server Python tests without PyLint
        run: ./gradlew core:integrationTest --tests org.lflang.tests.lsp.LspTests.pythonValidationTestSyntaxOnly core:integrationTestCodeCoverageReport
      - name: Install pylint
        run: python3 -m pip install pylint
        if: ${{ runner.os != 'macOS' }}
      - name: Install pylint macOS
        run: brew install pylint
        if: ${{ runner.os == 'macOS' }}
      - name: Run language server tests
        run: ./gradlew core:integrationTest --tests org.lflang.tests.lsp.LspTests.*ValidationTest core:integrationTestCodeCoverageReport
      - name: Report to CodeCov
        uses: ./.github/actions/report-code-coverage
        with:
          files: core/build/reports/jacoco/integrationTestCodeCoverageReport/integrationTestCodeCoverageReport.xml
        if: ${{ github.repository == 'lf-lang/lingua-franca' }}
