scorpiox-bot-plugin-copilot-login
←mock_refresh.shbash
52 lines · 1 KBHEADBlameDownload
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
14set -u
15
16CREDS=""
17while [ $# -gt 0 ]; do
18 case "$1" in
19 --credentials-file) shift; CREDS="$1" ;;
20 *) : ;;
21 esac
22 shift
23done
24
25[ -n "$CREDS" ] || CREDS="${COPILOT_CREDS_PATH:-$HOME/.copilot/.credentials.json}"
26
27if [ ! -f "$CREDS" ]; then
28 printf 'error: cannot read credentials file: %s\n' "$CREDS" >&2
29 exit 1
30fi
31
32if [ "${COPILOT_MOCK_FAIL:-0}" = "1" ]; then
33 printf 'error: token exchange failed\n' >&2
34 exit 1
35fi
36
37case "$CREDS" in
38 */.copilot/.credentials.json) D=$(dirname "$CREDS") ;;
39 *) D=$(dirname "$CREDS") ;;
40esac
41CACHE="$D/.copilot-token-cache.json"
42
43EXP=$(( $(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.
50printf 'Token exchange successful\n'
51printf '{"access_token": "MOCK-JWT-MUST-NOT-LEAK-0000", "expires_at": %s}\n' "$EXP"
52exit 0