name: Test Libvirt
on:
  push:
    branches:
      - main
    paths:
      - "libvirt/**"
  pull_request:
    paths:
      - "libvirt/**"

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-latest

    defaults:
      run:
        working-directory: ./libvirt

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive

      - uses: actions/setup-python@v5
        with:
          python-version: "3.13.5"

      - name: Cache APT Packages
        uses: awalsh128/cache-apt-pkgs-action@v1.4.3
        with:
          packages: libvirt-dev
          version: 1.0

      - name: Set Node.js
        uses: actions/setup-node@v4
        with:
          node-version-file: ".nvmrc"

      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 10
          run_install: false

      - name: Get pnpm store directory
        id: pnpm-cache
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

      - uses: actions/cache@v4
        name: Setup pnpm cache
        with:
          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('libvirt/package.json') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      - name: pnpm install
        run: pnpm install --frozen-lockfile

      - name: Build
        run: pnpm run build

      - name: test
        run: pnpm run test
