# This workflow will test if running the e2e tests works.
name: E2E Tests

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  admin-tests:
    timeout-minutes: 60
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

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

      - name: Install dependencies
        run: dotnet workload install wasm-tools

      - name: Build
        working-directory: apps/server
        run: dotnet build

      - name: Start dev database
        run: ./install.sh configure-dev-db start

      - name: Ensure browsers are installed
        working-directory: apps/server
        run: pwsh Tests/AliasVault.E2ETests/bin/Debug/net9.0/playwright.ps1 install --with-deps

      - name: Run AdminTests with retry
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 60
          max_attempts: 3
          command: cd apps/server && dotnet test Tests/AliasVault.E2ETests --no-build --verbosity normal --filter "Category=AdminTests"

      - name: Upload Test Results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: admin-test-results
          path: TestResults-Admin.xml

  client-tests:
    timeout-minutes: 60
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        shard: [1, 2, 3, 4, 5]
    steps:
      - uses: actions/checkout@v4

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

      - name: Install dependencies
        run: dotnet workload install wasm-tools

      - name: Build
        working-directory: apps/server
        run: dotnet build

      - name: Start dev database
        run: ./install.sh configure-dev-db start

      - name: Ensure browsers are installed
        working-directory: apps/server
        run: pwsh Tests/AliasVault.E2ETests/bin/Debug/net9.0/playwright.ps1 install --with-deps

      - name: Run ClientTests with retry (Shard ${{ matrix.shard }})
        uses: nick-fields/retry@v3
        with:
          timeout_minutes: 60
          max_attempts: 3
          command: cd apps/server && dotnet test Tests/AliasVault.E2ETests --no-build --verbosity normal --filter "FullyQualifiedName~.E2ETests.Tests.Client.Shard${{ matrix.shard }}."
