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

permissions:
  contents: read

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

    runs-on: ubuntu-latest

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

      - name: Setup Node environment
        uses: ./.github/workflows/actions/setup-node-environment

      - name: Set SEGMENT_KEY
        env:
          SEGMENT_KEY: ${{ secrets.SEGMENT_KEY_PROD }}
        run: |
          echo "SEGMENT_KEY=${SEGMENT_KEY}" >> $GITHUB_ENV
        shell: bash

      - name: Validate SEGMENT_KEY
        run: |
          if [ -z "${SEGMENT_KEY}" ]; then
            echo "SEGMENT_KEY is not set or is empty"
            exit 1
          fi
        shell: bash

      - name: Run Checks
        run: pnpm run check
        shell: bash

  build-vsix:
    name: Build VSIX
    needs: build-and-check
    runs-on: ubuntu-latest

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

      - name: Setup Node environment
        uses: ./.github/workflows/actions/setup-node-environment

      - name: Set SEGMENT_KEY
        env:
          SEGMENT_KEY: ${{ secrets.SEGMENT_KEY_PROD }}
        run: |
          echo "SEGMENT_KEY=${SEGMENT_KEY}" >> $GITHUB_ENV
        shell: bash

      - name: Build .vsix
        env:
          NODE_OPTIONS: "--require ./scripts/no-npm-list-fail.js --max_old_space_size=4096"
        run: |
          pnpm exec vsce package --githubBranch main
        shell: bash

      - name: Check .vsix filesize
        run: pnpm run check-vsix-size
        shell: bash

      - name: Upload .vsix
        uses: actions/upload-artifact@v4
        with:
          name: vsix
          path: '*.vsix'

  test:
    name: Run Tests
    needs: build-and-check

    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: Setup Node environment
        uses: ./.github/workflows/actions/setup-node-environment

      - name: Run Tests
        env:
          NODE_OPTIONS: "--max_old_space_size=4096"
          MDB_IS_TEST: "true"
        run: |
          pnpm run test
        shell: bash

  test-install:
    name: Run Install Test
    needs: build-vsix

    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: Setup Node environment
        uses: ./.github/workflows/actions/setup-node-environment

      - name: Download .vsix
        uses: actions/download-artifact@v4
        with:
          name: vsix

      - name: Run VSIX Install Test
        env:
          RUNNER_OS: ${{ runner.os }}
        run: pnpm run test-install
        shell: bash
