#!/bin/sh
# tests/mock_refresh.sh - stand-in for scorpiox-claudecode-refreshtoken.
#
# Mirrors the production program's argument shape (--credentials-file <path>
# [--force]) and its behaviour: it rewrites the credentials file in place with a
# freshly minted access token and a new expiresAt, and prints human text (which
# may include token material) on stdout. The plugin must never forward that.
#
# MOCK_REFRESH_FAIL=1 exercises the failure path where the file is untouched.

CREDS=""
FORCE=0
while [ $# -gt 0 ]; do
  case "$1" in
    --credentials-file) shift; CREDS="${1:-}" ;;
    --force|-f) FORCE=1 ;;
  esac
  shift
done
[ -z "$CREDS" ] && CREDS="${CLAUDE_CREDS_PATH:-/nonexistent}"

if [ "$MOCK_REFRESH_FAIL" = "1" ]; then
  printf 'error: no refreshToken found in credentials\n' >&2
  exit 1
fi
if [ ! -f "$CREDS" ]; then
  printf 'error: cannot read %s\n' "$CREDS" >&2
  exit 1
fi

_now=$(date +%s)
_exp=$(( (_now + 28800) * 1000 ))
printf 'Claude OAuth Token Auto-Refresh Tool\n\n'
printf 'Found Claude config: %s\n' "$CREDS"
printf 'Refreshing OAuth token (force=%s)\n' "$FORCE"
printf '{"claudeAiOauth":{"accessToken":"sk-ant-oat01-MOCK-REFRESHED-MUST-NOT-LEAK","refreshToken":"sk-ant-ort01-MOCK-REFRESHED","expiresAt":%s,"scopes":["user:profile","user:inference"],"subscriptionType":"pro"}}\n' "$_exp" > "$CREDS"
printf 'Token refresh successful\n'
exit 0
