# Artillery.js Load Testing Configuration for SavePal API
config:
  target: 'http://localhost:4100'
  phases:
    # Warm-up phase
    - duration: 60
      arrivalRate: 5
      name: "Warm-up"
    
    # Gradual load increase
    - duration: 300
      arrivalRate: 10
      rampTo: 50
      name: "Gradual load increase"
    
    # Sustained load
    - duration: 600
      arrivalRate: 50
      name: "Sustained load"
    
    # Peak load
    - duration: 180
      arrivalRate: 100
      name: "Peak load"
    
    # Cool down
    - duration: 120
      arrivalRate: 10
      name: "Cool down"

  defaults:
    headers:
      Content-Type: 'application/json'
  
  variables:
    testEmails:
      - 'loadtest1@savepal.ai'
      - 'loadtest2@savepal.ai'
      - 'loadtest3@savepal.ai'
      - 'loadtest4@savepal.ai'
      - 'loadtest5@savepal.ai'
    
    searchQueries:
      - 'black shirt'
      - 'running shoes'
      - 'laptop'
      - 'dress'
      - 'headphones'
      - 'watch'
      - 'jeans'
      - 'smartphone'

  processor: "./test/performance/load-test-processor.js"

scenarios:
  # Authentication Flow
  - name: "User Registration and Login"
    weight: 20
    flow:
      - post:
          url: "/api/v1/auth/register"
          json:
            email: "{{ $randomString() }}@savepal.ai"
            password: "TestPassword123!"
            profile:
              firstName: "Load"
              lastName: "Test"
          capture:
            - json: "$.data.tokens.accessToken"
              as: "accessToken"
            - json: "$.data.user.id"
              as: "userId"
          expect:
            - statusCode: 201
            - hasProperty: "data.tokens.accessToken"

  # Product Search and Discovery
  - name: "Product Search Flow"
    weight: 40
    flow:
      - get:
          url: "/api/v1/products/search"
          qs:
            query: "{{ searchQueries[$randomNumber(0, 7)] }}"
            page: "{{ $randomNumber(1, 5) }}"
            limit: "{{ $randomNumber(10, 50) }}"
          expect:
            - statusCode: 200
            - hasProperty: "data.products"
          capture:
            - json: "$.data.products[0].id"
              as: "productId"
      
      - get:
          url: "/api/v1/products/{{ productId }}"
          expect:
            - statusCode: 200
            - hasProperty: "data.id"
      
      - get:
          url: "/api/v1/products/{{ productId }}/similar"
          qs:
            limit: 5
          expect:
            - statusCode: 200

  # Authenticated User Actions
  - name: "Authenticated User Flow"
    weight: 30
    flow:
      # Login first
      - post:
          url: "/api/v1/auth/login"
          json:
            email: "{{ testEmails[$randomNumber(0, 4)] }}"
            password: "TestPassword123!"
          capture:
            - json: "$.data.tokens.accessToken"
              as: "accessToken"
            - json: "$.data.user.id"
              as: "userId"
          expect:
            - statusCode: 200
      
      # Get user profile
      - get:
          url: "/api/v1/auth/me"
          headers:
            Authorization: "Bearer {{ accessToken }}"
          expect:
            - statusCode: 200
      
      # Search products
      - get:
          url: "/api/v1/products/search"
          qs:
            query: "{{ searchQueries[$randomNumber(0, 7)] }}"
            page: 1
            limit: 20
          headers:
            Authorization: "Bearer {{ accessToken }}"
          capture:
            - json: "$.data.products[0].id"
              as: "productId"
          expect:
            - statusCode: 200
      
      # Get recommendations
      - get:
          url: "/api/v1/products/recommendations/{{ userId }}"
          qs:
            limit: 10
          headers:
            Authorization: "Bearer {{ accessToken }}"
          expect:
            - statusCode: 200
      
      # Track interaction
      - post:
          url: "/api/v1/products/{{ productId }}/track"
          headers:
            Authorization: "Bearer {{ accessToken }}"
          json:
            actionType: "view"
            sessionId: "load-test-{{ $randomString() }}"
          expect:
            - statusCode: 200

  # Coupon and Savings Flow
  - name: "Coupons and Savings"
    weight: 15
    flow:
      - get:
          url: "/api/v1/coupons"
          qs:
            page: 1
            limit: 20
            retailer: "namshi"
          expect:
            - statusCode: 200
            - hasProperty: "data.coupons"
      
      - get:
          url: "/api/v1/coupons/deals/all"
          qs:
            category: "fashion"
            limit: 10
          expect:
            - statusCode: 200

  # Social Features
  - name: "Social Features"
    weight: 10
    flow:
      # Login first
      - post:
          url: "/api/v1/auth/login"
          json:
            email: "{{ testEmails[$randomNumber(0, 4)] }}"
            password: "TestPassword123!"
          capture:
            - json: "$.data.tokens.accessToken"
              as: "accessToken"
          expect:
            - statusCode: 200
      
      # Get social dashboard
      - get:
          url: "/api/v1/social/dashboard"
          headers:
            Authorization: "Bearer {{ accessToken }}"
          expect:
            - statusCode: 200
      
      # Get social feed
      - get:
          url: "/api/v1/social/feed"
          qs:
            limit: 20
          headers:
            Authorization: "Bearer {{ accessToken }}"
          expect:
            - statusCode: 200
      
      # Get trending activities
      - get:
          url: "/api/v1/social/trending"
          qs:
            limit: 10
          expect:
            - statusCode: 200

  # AI and Sustainability Features
  - name: "AI and Sustainability"
    weight: 10
    flow:
      # Login first
      - post:
          url: "/api/v1/auth/login"
          json:
            email: "{{ testEmails[$randomNumber(0, 4)] }}"
            password: "TestPassword123!"
          capture:
            - json: "$.data.tokens.accessToken"
              as: "accessToken"
          expect:
            - statusCode: 200
      
      # Get sustainability dashboard
      - get:
          url: "/api/v1/sustainability/dashboard"
          headers:
            Authorization: "Bearer {{ accessToken }}"
          expect:
            - statusCode: 200
      
      # Get sustainable brands
      - get:
          url: "/api/v1/sustainability/brands/sustainable"
          qs:
            category: "fashion"
            minScore: 70
            limit: 10
          expect:
            - statusCode: 200
      
      # AI chat (if available)
      - post:
          url: "/api/v1/ai/chat"
          headers:
            Authorization: "Bearer {{ accessToken }}"
          json:
            messages:
              - role: "user"
                content: "Find me a black shirt under 200 AED"
            model: "gpt-3.5-turbo"
          expect:
            - statusCode: 200

# Performance thresholds
expect:
  # Response time thresholds
  - expression: "response.time < 500"
    as: "Response time under 500ms"
  
  - expression: "response.time < 1000"
    as: "Response time under 1s"
  
  # Success rate threshold
  - expression: "response.statusCode < 400"
    as: "Successful responses"

# Metrics to track
metrics:
  - name: "response_time_p95"
    unit: "ms"
  - name: "response_time_p99"
    unit: "ms"
  - name: "errors_rate"
    unit: "percent"
  - name: "requests_per_second"
    unit: "rps"
