# Harness CI Pipeline for Test Execution
# Generated by BMad TEA Agent - Test Architect Module
# Optimized for: Parallel Sharding, Burn-In Loop
# Stack: {test_stack_type} | Framework: {test_framework}
#
# Variables to customize per project:
#   INSTALL_CMD       - dependency install command (e.g., npm ci, pnpm install --frozen-lockfile)
#   TEST_CMD          - main test command (e.g., npm run test:e2e, npm test, npx vitest)
#   LINT_CMD          - lint command (e.g., npm run lint)
#   BROWSER_INSTALL   - browser install command (frontend/fullstack only; omit for backend)
#   PLAYWRIGHT_IMAGE  - keep in sync with your @playwright/test version (example: mcr.microsoft.com/playwright:v1.58.2-noble)

pipeline:
  name: Test Pipeline
  identifier: test_pipeline
  projectIdentifier: default
  orgIdentifier: default
  stages:
    # Lint stage - Code quality checks
    - stage:
        name: Lint
        identifier: lint
        type: CI
        spec:
          cloneCodebase: true
          infrastructure:
            type: KubernetesDirect
            spec:
              connectorRef: account.harnessImage
              namespace: default
          execution:
            steps:
              - step:
                  type: Run
                  name: Install dependencies
                  identifier: install
                  spec:
                    connectorRef: account.harnessImage
                    image: node:24
                    shell: Sh
                    command: npm ci # Replace with INSTALL_CMD

              - step:
                  type: Run
                  name: Run linter
                  identifier: lint
                  spec:
                    connectorRef: account.harnessImage
                    image: node:24
                    shell: Sh
                    command: npm run lint # Replace with LINT_CMD

    # Test stage - Parallel execution with sharding
    - stage:
        name: Test
        identifier: test
        type: CI
        spec:
          cloneCodebase: true
          infrastructure:
            type: KubernetesDirect
            spec:
              connectorRef: account.harnessImage
              namespace: default
          execution:
            steps:
              - step:
                  type: Run
                  name: Install dependencies
                  identifier: install
                  spec:
                    connectorRef: account.harnessImage
                    image: node:24
                    shell: Sh
                    command: npm ci # Replace with INSTALL_CMD

              # Frontend/Fullstack only — remove this step for backend-only stacks
              - step:
                  type: Run
                  name: Install browsers
                  identifier: browsers
                  spec:
                    connectorRef: account.harnessImage
                    image: mcr.microsoft.com/playwright:v1.58.2-noble # Replace with PLAYWRIGHT_IMAGE
                    shell: Sh
                    command: npx playwright install --with-deps chromium # Replace with BROWSER_INSTALL

              - parallel:
                  - step:
                      type: Run
                      name: Test Shard 1
                      identifier: shard_1
                      spec:
                        connectorRef: account.harnessImage
                        image: mcr.microsoft.com/playwright:v1.58.2-noble # Replace with PLAYWRIGHT_IMAGE
                        shell: Sh
                        command: npm run test:e2e -- --shard=1/4 # Replace with TEST_CMD + shard args
                  - step:
                      type: Run
                      name: Test Shard 2
                      identifier: shard_2
                      spec:
                        connectorRef: account.harnessImage
                        image: mcr.microsoft.com/playwright:v1.58.2-noble # Replace with PLAYWRIGHT_IMAGE
                        shell: Sh
                        command: npm run test:e2e -- --shard=2/4 # Replace with TEST_CMD + shard args
                  - step:
                      type: Run
                      name: Test Shard 3
                      identifier: shard_3
                      spec:
                        connectorRef: account.harnessImage
                        image: mcr.microsoft.com/playwright:v1.58.2-noble # Replace with PLAYWRIGHT_IMAGE
                        shell: Sh
                        command: npm run test:e2e -- --shard=3/4 # Replace with TEST_CMD + shard args
                  - step:
                      type: Run
                      name: Test Shard 4
                      identifier: shard_4
                      spec:
                        connectorRef: account.harnessImage
                        image: mcr.microsoft.com/playwright:v1.58.2-noble # Replace with PLAYWRIGHT_IMAGE
                        shell: Sh
                        command: npm run test:e2e -- --shard=4/4 # Replace with TEST_CMD + shard args

    # Burn-in stage - Flaky test detection
    # Note: Burn-in targets UI flakiness. For backend-only stacks, remove this stage entirely.
    - stage:
        name: Burn-In
        identifier: burn_in
        type: CI
        when:
          condition: <+pipeline.triggerType> == "WEBHOOK" || <+pipeline.triggerType> == "SCHEDULER"
        spec:
          cloneCodebase: true
          infrastructure:
            type: KubernetesDirect
            spec:
              connectorRef: account.harnessImage
              namespace: default
          execution:
            steps:
              - step:
                  type: Run
                  name: Install and burn-in
                  identifier: burn_in_loop
                  spec:
                    connectorRef: account.harnessImage
                    image: mcr.microsoft.com/playwright:v1.58.2-noble # Replace with PLAYWRIGHT_IMAGE
                    shell: Sh
                    command: |
                      npm ci
                      npx playwright install --with-deps chromium
                      echo "Starting burn-in loop - detecting flaky tests"
                      for i in $(seq 1 10); do
                        echo "Burn-in iteration $i/10"
                        npm run test:e2e || exit 1
                      done
                      echo "Burn-in complete - no flaky tests detected"
                    # Replace npm ci with INSTALL_CMD, npm run test:e2e with TEST_CMD
