# Per-VU auth tokens with automatic refresh — Artillery's `generating-vu-tokens`
# + `refresh-auth-token` in one place.
#
# Each VU mints its own token the first time it needs one (scripts/auth.js,
# beforeRequest hook), attaches it to every request, and silently re-mints it
# just before it expires. A custom `token_refreshes` counter tracks how often
# that happens. Swap the synthetic mint() for a real call to your auth server.
#
#   loadr run examples/36-auth-tokens.yaml
name: auth-tokens
description: Per-VU token minting + transparent refresh before expiry

defaults:
  http:
    base_url: https://api.example.com

js:
  file: scripts/auth.js
  timeout: 5s

metrics:
  token_refreshes: { kind: counter }

scenarios:
  authed_traffic:
    executor: constant-vus
    vus: 10
    duration: 1m
    flow:
      - request:
          name: profile
          url: /me
          checks: [ { type: status, equals: 200 } ]
      - think_time: { type: uniform, min: 200ms, max: 800ms }
      - request:
          name: orders
          url: /orders
          checks: [ { type: status, equals: 200 } ]

thresholds:
  checks: [ "rate>0.99" ]
