| 1 | #!/bin/sh |
| 2 | # tests/mock_refresh.sh - stand-in for scorpiox-copilot-refreshtoken. |
| 3 | # |
| 4 | # The real CLI derives the short-lived Copilot JWT from the stored github_token |
| 5 | # and caches it in ~/.copilot/.copilot-token-cache.json, then prints the JWT on |
| 6 | # stdout. The plugin must discard that stdout entirely and prove the refresh by |
| 7 | # re-reading the cache file, so this mock prints a deliberately leaky payload to |
| 8 | # make a regression visible in the tests. |
| 9 | # |
| 10 | # --credentials-file <path> [--force] |
| 11 | # |
| 12 | # COPILOT_MOCK_FAIL=1 makes the refresh fail without touching the cache. |
| 13 | |
| 14 | set -u |
| 15 | |
| 16 | CREDS="" |
| 17 | while [ $# -gt 0 ]; do |
| 18 | case "$1" in |
| 19 | --credentials-file) shift; CREDS="$1" ;; |
| 20 | *) : ;; |
| 21 | esac |
| 22 | shift |
| 23 | done |
| 24 | |
| 25 | [ -n "$CREDS" ] || CREDS="${COPILOT_CREDS_PATH:-$HOME/.copilot/.credentials.json}" |
| 26 | |
| 27 | if [ ! -f "$CREDS" ]; then |
| 28 | printf 'error: cannot read credentials file: %s\n' "$CREDS" >&2 |
| 29 | exit 1 |
| 30 | fi |
| 31 | |
| 32 | if [ "${COPILOT_MOCK_FAIL:-0}" = "1" ]; then |
| 33 | printf 'error: token exchange failed\n' >&2 |
| 34 | exit 1 |
| 35 | fi |
| 36 | |
| 37 | case "$CREDS" in |
| 38 | */.copilot/.credentials.json) D=$(dirname "$CREDS") ;; |
| 39 | *) D=$(dirname "$CREDS") ;; |
| 40 | esac |
| 41 | CACHE="$D/.copilot-token-cache.json" |
| 42 | |
| 43 | EXP=$(( $(date +%s) + 1800 )) |
| 44 | { |
| 45 | printf '{"access_token": "MOCK-JWT-MUST-NOT-LEAK-0000", "expires_at": %s}\n' "$EXP" |
| 46 | } > "$CACHE" |
| 47 | |
| 48 | # Exactly what the real CLI prints: token material on stdout. The plugin reads |
| 49 | # this and throws it away. |
| 50 | printf 'Token exchange successful\n' |
| 51 | printf '{"access_token": "MOCK-JWT-MUST-NOT-LEAK-0000", "expires_at": %s}\n' "$EXP" |
| 52 | exit 0 |