# GitLab CI/CD Pipeline for Test Execution
# Generated by BMad TEA Agent - Test Architect Module
# Optimized for: Playwright/Cypress, Parallel Sharding, Burn-In Loop

stages:
  - lint
  - test
  - burn-in
  - report

variables:
  # Disable git depth for accurate change detection
  GIT_DEPTH: 0
  # Use npm ci for faster, deterministic installs
  npm_config_cache: "$CI_PROJECT_DIR/.npm"
  # Playwright browser cache
  PLAYWRIGHT_BROWSERS_PATH: "$CI_PROJECT_DIR/.cache/ms-playwright"
  # Default Node version when .nvmrc is missing
  DEFAULT_NODE_VERSION: "24"

# Caching configuration
cache:
  key:
    files:
      - package-lock.json
  paths:
    - .npm/
    - .cache/ms-playwright/
    - node_modules/

# Lint stage - Code quality checks
lint:
  stage: lint
  image: node:$DEFAULT_NODE_VERSION
  before_script:
    - |
      NODE_VERSION=$(cat .nvmrc 2>/dev/null || echo "$DEFAULT_NODE_VERSION")
      echo "Using Node $NODE_VERSION"
      npm install -g n
      n "$NODE_VERSION"
      node -v
    - npm ci
  script:
    - npm run lint
  timeout: 5 minutes

# Test stage - Parallel execution with sharding
.test-template: &test-template
  stage: test
  image: node:$DEFAULT_NODE_VERSION
  needs:
    - lint
  before_script:
    - |
      NODE_VERSION=$(cat .nvmrc 2>/dev/null || echo "$DEFAULT_NODE_VERSION")
      echo "Using Node $NODE_VERSION"
      npm install -g n
      n "$NODE_VERSION"
      node -v
    - npm ci
    - npx playwright install --with-deps chromium
  artifacts:
    when: on_failure
    paths:
      - test-results/
      - playwright-report/
    expire_in: 30 days
  timeout: 30 minutes

test:shard-1:
  <<: *test-template
  script:
    - npm run test:e2e -- --shard=1/4

test:shard-2:
  <<: *test-template
  script:
    - npm run test:e2e -- --shard=2/4

test:shard-3:
  <<: *test-template
  script:
    - npm run test:e2e -- --shard=3/4

test:shard-4:
  <<: *test-template
  script:
    - npm run test:e2e -- --shard=4/4

# Burn-in stage - Flaky test detection
burn-in:
  stage: burn-in
  image: node:$DEFAULT_NODE_VERSION
  needs:
    - test:shard-1
    - test:shard-2
    - test:shard-3
    - test:shard-4
  # Only run burn-in on merge requests to main/develop or on schedule
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
  before_script:
    - |
      NODE_VERSION=$(cat .nvmrc 2>/dev/null || echo "$DEFAULT_NODE_VERSION")
      echo "Using Node $NODE_VERSION"
      npm install -g n
      n "$NODE_VERSION"
      node -v
    - npm ci
    - npx playwright install --with-deps chromium
  script:
    - |
      echo "🔥 Starting burn-in loop - detecting flaky tests"
      for i in {1..10}; do
        echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        echo "🔥 Burn-in iteration $i/10"
        echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        npm run test:e2e || exit 1
      done
      echo "✅ Burn-in complete - no flaky tests detected"
  artifacts:
    when: on_failure
    paths:
      - test-results/
      - playwright-report/
    expire_in: 30 days
  timeout: 60 minutes

# Report stage - Aggregate results
report:
  stage: report
  image: alpine:latest
  needs:
    - test:shard-1
    - test:shard-2
    - test:shard-3
    - test:shard-4
    - burn-in
  when: always
  script:
    - |
      echo "## Test Execution Summary"
      echo ""
      echo "- Pipeline: $CI_PIPELINE_ID"
      echo "- Shards: 4"
      echo "- Branch: $CI_COMMIT_REF_NAME"
      echo ""
      echo "View detailed results in job artifacts"
