name: Shared Packages CI

on:
  push:
    paths:
      - 'packages/**'
      - 'tests/contract/**'
  pull_request:
    paths:
      - 'packages/**'
      - 'tests/contract/**'

permissions:
  contents: read

jobs:
  # ---------------------------------------------------------------------------
  # EN: Unit tests for all shared TypeScript packages.
  # VI: Unit tests cho tất cả shared TypeScript packages.
  # ---------------------------------------------------------------------------
  package-unit-tests:
    name: Shared Package Unit Tests
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

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

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 8

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

      - name: Run @goodgo/types tests
        run: pnpm --filter @goodgo/types test:coverage

      - name: Run @goodgo/http-client tests
        run: pnpm --filter @goodgo/http-client test:coverage

      - name: Run @goodgo/auth-sdk tests
        run: pnpm --filter @goodgo/auth-sdk test:coverage

      - name: Run @goodgo/logger tests
        run: pnpm --filter @goodgo/logger test:coverage

      - name: Run @goodgo/tracing tests
        run: pnpm --filter @goodgo/tracing test:coverage

      - name: Upload coverage
        uses: codecov/codecov-action@v4
        with:
          files: packages/*/coverage/cobertura-coverage.xml
          flags: shared_packages
          fail_ci_if_error: false
        env:
          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

  # ---------------------------------------------------------------------------
  # EN: Pact consumer contract tests for 5 service boundaries.
  # VI: Pact consumer contract tests cho 5 ranh giới service.
  # ---------------------------------------------------------------------------
  contract-tests:
    name: Pact Contract Tests
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

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

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 8

      - name: Install contract test dependencies
        run: pnpm --filter @goodgo/contract-tests install

      - name: Run Pact consumer tests
        run: pnpm --filter @goodgo/contract-tests test

      - name: Upload Pact files as artifact
        uses: actions/upload-artifact@v4
        with:
          name: pact-contracts
          path: tests/contract/pacts/
          if-no-files-found: warn

  # ---------------------------------------------------------------------------
  # EN: Blazor WASM component unit tests (AuthStateService + PosDataService).
  # VI: Blazor WASM component unit tests (AuthStateService + PosDataService).
  # ---------------------------------------------------------------------------
  blazor-component-tests:
    name: Blazor Component Tests
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '10.0.x'

      - name: Restore component test project
        run: |
          dotnet restore apps/web-client-tpos-net/tests/WebClientTpos.ComponentTests/WebClientTpos.ComponentTests.csproj

      - name: Run component tests
        run: |
          dotnet test apps/web-client-tpos-net/tests/WebClientTpos.ComponentTests/WebClientTpos.ComponentTests.csproj \
            --configuration Release --no-restore --verbosity normal \
            --collect:"XPlat Code Coverage" \
            --settings coverage.runsettings \
            --results-directory TestResults/component

      - name: Upload coverage
        uses: codecov/codecov-action@v4
        with:
          files: TestResults/component/**/coverage.cobertura.xml
          flags: blazor_components
          fail_ci_if_error: false
        env:
          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
