name: Test and Build
on:
  push:
    branches:
      - main
    tags:
      - "v*.*.*"
  pull_request:
  workflow_dispatch:
  schedule:
    - cron: "0 0 * * *"

permissions:
  contents: read

jobs:
  test-and-build:
    name: Test and Build
    if: github.event_name != 'pull_request' || (github.event.pull_request.user.login != 'dependabot[bot]' && github.event.pull_request.head.repo.full_name == github.repository)

    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
      fail-fast: false

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

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Get VS Code Node.js version
        id: get-vscode-node-version
        shell: bash
        run: |
          # Fetch Node.js version from VS Code's .nvmrc, default to 22.20.0 if unavailable
          NODE_VERSION=$(curl -fsSL https://raw.githubusercontent.com/microsoft/vscode/main/.nvmrc | tr -d '[:space:]')
          if [ -z "$NODE_VERSION" ]; then
            NODE_VERSION="22.21.1"
          fi
          echo "Using Node.js version for VS Code extension tests: $NODE_VERSION"
          echo "vscode-node-version=$NODE_VERSION" >> $GITHUB_OUTPUT

      - name: Setup pnpm
        uses: pnpm/action-setup@v4

      - name: Setup Node.js Environment
        uses: actions/setup-node@v4
        with:
          node-version: ${{ steps.get-vscode-node-version.outputs.vscode-node-version }}
          cache: pnpm

      - name: Run tests and build
        uses: ./.github/workflows/actions/test-and-build
        with:
          SEGMENT_KEY: ${{ secrets.SEGMENT_KEY_PROD }}
          ARTIFACTORY_HOST: ${{ secrets.ARTIFACTORY_HOST }}
          ARTIFACTORY_PASSWORD: ${{ secrets.ARTIFACTORY_PASSWORD }}
          ARTIFACTORY_USERNAME: ${{ secrets.ARTIFACTORY_USERNAME }}
          GARASIGN_PASSWORD: ${{ secrets.GARASIGN_PASSWORD }}
          GARASIGN_USERNAME: ${{ secrets.GARASIGN_USERNAME }}
          SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
          JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }}
          MDB_IS_TEST: "true"
