scorpiox-bot-plugin-codex-login
←mock_login.shbash
90 lines · 4 KBHEADBlameDownload
1#!/bin/sh
2# tests/mock_login.sh - stand-in for scorpiox-codex-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. `--poll` writes a credential
7# file into $CODEX_CREDS_PATH when it reports success, so a test can prove the
8# plugin re-reads state instead of trusting the CLI's word.
9#
10# MOCK_FAIL=1 makes --poll fail the way a denied device code does.
11# MOCK_NOUSER=1 makes --start return no user_code (a malformed CLI answer).
12# MOCK_EXISTS=1 makes --start report "credentials already exist".
13
14CREDS="${CODEX_CREDS_PATH:-/tmp/sx-mock-scratch}"
15# A decodable JWT whose payload says exp=1794509681; tests replace it with a
16# freshly generated one so the countdown is real.
17MOCK_DEFAULT_TOKEN="eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3OTQ1MDk2ODEsImNoYXRncHRfYWNjb3VudF9pZCI6ImFjY3QtZGVmYXVsdCJ9.DEVFAKE"
18PEND="${CODEX_PENDING_DIR:-/tmp/sx-mock-scratch}/codex-default.json"
19ACC="${CODEX_ACCOUNT:-default}"
20
21li=false; hr=false; exp=0
22if [ -f "$CREDS" ]; then
23 if grep -q 'access_token' "$CREDS" 2>/dev/null; then li=true; fi
24 if grep -q 'refresh_token' "$CREDS" 2>/dev/null; then hr=true; fi
25fi
26pend=false
27if [ -f "$PEND" ]; then pend=true; fi
28
29# The real CLI reports expires_at 0 because auth.json has no expiry field; the
30# plugin is what decodes the token. Keep that behaviour so the suite proves it.
31case "$1" in
32 --status)
33 printf 'Checking Codex credentials...\n'
34 printf '{"ok":true,"provider":"codex","name":"Codex (OpenAI)","flow":"device_code","account":"%s","logged_in":%s,"path":"%s","expires_at":0,"expired":false,"has_refresh":%s,"pending":%s,"profile":"codex","profile_exists":false,"named_accounts":true,"protocol":"SXLOGIN-MACHINE-V1"}\n' \
35 "$ACC" "$li" "$CREDS" "$hr" "$pend"
36 ;;
37 --start)
38 # --force is the only thing that makes an existing credential file a
39 # non-error, so honour it here the way the real CLI does.
40 _force=0
41 for _a in "$@"; do [ "$_a" = "--force" ] && _force=1; done
42 if [ "$MOCK_EXISTS" = "1" ] && [ "$_force" != "1" ]; then
43 printf '{"ok":false,"provider":"codex","error":"exists","detail":"credentials already exist; pass --force to replace"}\n'
44 exit 1
45 fi
46 if [ "$MOCK_NOUSER" = "1" ]; then
47 printf '{"ok":true,"provider":"codex","flow":"device_code","verify_url":"https://auth.openai.com/codex/device","interval":5,"expires_in":900}\n'
48 exit 0
49 fi
50 mkdir -p "$(dirname "$PEND")" 2>/dev/null
51 printf '{"device_auth_id":"dev-mock-1","user_code":"ABCD-1234","interval":5,"force":%s,"created":%s}\n' \
52 "${MOCK_FORCE:-0}" "$(date +%s)" > "$PEND"
53 printf 'Requesting a device code from OpenAI...\n'
54 printf '{"ok":true,"provider":"codex","flow":"device_code","verify_url":"https://auth.openai.com/codex/device","user_code":"ABCD-1234","interval":5,"expires_in":900,"hint":"Open the link, enter the code, approve. This page checks automatically."}\n'
55 ;;
56 --poll)
57 if [ "$MOCK_FAIL" = "1" ]; then
58 printf '{"ok":false,"provider":"codex","error":"denied","detail":"authorization failed or was denied; start again"}\n'
59 exit 1
60 fi
61 if [ "$MOCK_DONE" = "1" ]; then
62 cat > "$CREDS" << CREDEOF
63{
64 "OPENAI_API_KEY": null,
65 "tokens": {
66 "id_token": "${MOCK_ID_TOKEN:-$MOCK_ACCESS_TOKEN}",
67 "access_token": "${MOCK_ACCESS_TOKEN:-$MOCK_DEFAULT_TOKEN}",
68 "refresh_token": "rt-from-mock-poll",
69 "account_id": "acct-mock"
70 },
71 "last_refresh": "${MOCK_LAST_REFRESH:-2026-11-12T18:00:00.000000Z}"
72}
73CREDEOF
74 rm -f "$PEND"
75 printf '{"ok":true,"provider":"codex","state":"done","logged_in":true,"path":"%s","profile":"codex","profile_created":false}\n' "$CREDS"
76 else
77 printf 'Polling OpenAI...\n'
78 printf '{"ok":true,"provider":"codex","state":"pending","interval":5}\n'
79 fi
80 ;;
81 --cancel)
82 rm -f "$PEND"
83 printf '{"ok":true,"provider":"codex","cancelled":true}\n'
84 ;;
85 *)
86 printf 'mock: unknown flag %s\n' "$1" >&2
87 exit 2
88 ;;
89esac
90exit 0