| 1 | #!/bin/sh |
| 2 | # tests/mock_login.sh - stand-in for scorpiox-claudecode-login machine mode. |
| 3 | # |
| 4 | # Prints exactly the contract shape the real CLI prints (human progress lines |
| 5 | # first, one JSON line last), so the plugin's parsers are exercised against the |
| 6 | # production layout rather than an invented one. `--finish` writes a credential |
| 7 | # file into $CLAUDE_CREDS_PATH so a test can prove the plugin re-reads state. |
| 8 | # |
| 9 | # MOCK_FAIL=1 makes --finish fail the way Claude does for a used code. |
| 10 | |
| 11 | CREDS="${CLAUDE_CREDS_PATH:-/nonexistent}" |
| 12 | PEND="${CLAUDE_PENDING_DIR:-/nonexistent}/claudecode-default.json" |
| 13 | |
| 14 | case "$1" in |
| 15 | --status) |
| 16 | _li=false; _hr=false |
| 17 | if [ -f "$CREDS" ] && grep -q 'refreshToken' "$CREDS" 2>/dev/null; then |
| 18 | _li=true; _hr=true |
| 19 | fi |
| 20 | _exp=0 |
| 21 | if [ -f "$CREDS" ]; then |
| 22 | _exp=$(sed -n 's/.*"expiresAt"[[:space:]]*:[[:space:]]*\([0-9]*\).*/\1/p' "$CREDS" | head -1) |
| 23 | [ -z "$_exp" ] && _exp=0 |
| 24 | fi |
| 25 | printf 'scorpiox-claudecode-login 1.2.3 (SXLOGIN-MACHINE-V1)\n' |
| 26 | printf '{"ok":true,"provider":"claudecode","name":"Claude Code","flow":"paste_code",' |
| 27 | printf '"named_accounts":true,"protocol":"SXLOGIN-MACHINE-V1","account":null,' |
| 28 | printf '"logged_in":%s,"path":"%s","expires_at":%s,' |
| 29 | printf '"expired":false,"has_refresh":%s,"pending":%s,' |
| 30 | printf '"profile":"claude_code","profile_exists":false}\n' \ |
| 31 | "$_li" "$CREDS" "$_exp" "$_hr" \ |
| 32 | "$([ -f "$PEND" ] && echo true || echo false)" |
| 33 | ;; |
| 34 | --start) |
| 35 | printf 'scorpiox-claudecode-login 1.2.3\n' |
| 36 | printf 'generating PKCE challenge\n' |
| 37 | mkdir -p "$(dirname "$PEND")" 2>/dev/null |
| 38 | printf '{"code_verifier":"verifier-value","state":"st4te-value","created":1}\n' > "$PEND" |
| 39 | printf '{"ok":true,"provider":"claudecode","flow":"paste_code",' |
| 40 | printf '"auth_url":"https://claude.com/cai/oauth/authorize?code=true&client_id=test&state=st4te-value",' |
| 41 | printf '"expires_in":600,"hint":"Sign in, then paste the code shown on the callback page."}\n' |
| 42 | ;; |
| 43 | --finish) |
| 44 | if [ "$MOCK_FAIL" = "1" ]; then |
| 45 | printf '{"ok":false,"provider":"claudecode","error":"exchange","detail":"code already used"}\n' |
| 46 | exit 1 |
| 47 | fi |
| 48 | [ -z "$2" ] && { printf '{"ok":false,"error":"no_code"}\n'; exit 1; } |
| 49 | mkdir -p "$(dirname "$CREDS")" 2>/dev/null |
| 50 | _now=$(date +%s) |
| 51 | _exp=$(( (_now + 28800) * 1000 )) |
| 52 | { |
| 53 | printf '{\n "claudeAiOauth": {\n' |
| 54 | printf ' "accessToken": "sk-ant-oat01-MOCK-ACCESS-TOKEN-MUST-NOT-LEAK",\n' |
| 55 | printf ' "refreshToken": "sk-ant-ort01-MOCK-REFRESH-TOKEN-MUST-NOT-LEAK",\n' |
| 56 | printf ' "expiresAt": %s,\n' "$_exp" |
| 57 | printf ' "scopes": ["user:profile","user:inference"],\n' |
| 58 | printf ' "subscriptionType": "pro"\n }\n}\n' |
| 59 | } > "$CREDS" |
| 60 | rm -f "$PEND" |
| 61 | printf 'Login successful\n' |
| 62 | printf '{"ok":true,"provider":"claudecode","logged_in":true,"path":"%s",' |
| 63 | printf '"expires_in":28800,"profile":"claude_code","profile_created":false}\n' "$CREDS" |
| 64 | ;; |
| 65 | --cancel) |
| 66 | rm -f "$PEND" |
| 67 | printf '{"ok":true,"provider":"claudecode","cancelled":true}\n' |
| 68 | ;; |
| 69 | *) |
| 70 | printf '{"ok":false,"error":"usage"}\n' |
| 71 | exit 1 |
| 72 | ;; |
| 73 | esac |