| 1 | #!/bin/sh |
| 2 | # tests/mock_login.sh - stand-in for scorpiox-antigravity-login machine mode. |
| 3 | # |
| 4 | # Emits exactly the contract shape the real CLI emits (one JSON line last, all |
| 5 | # human output before it), so the plugin's parsers are exercised against the |
| 6 | # production layout rather than an invented one. The exchange is simulated: |
| 7 | # --finish writes a credential file into $ANTIGRAVITY_CREDS_PATH's directory so |
| 8 | # the test can prove the plugin re-reads state after a successful login. |
| 9 | # |
| 10 | # Set MOCK_FAIL=1 to make --finish fail the way Google does when the account |
| 11 | # needs verification. |
| 12 | |
| 13 | case "$1" in |
| 14 | --status) |
| 15 | if [ -n "$MOCK_STATUS_JSON" ]; then |
| 16 | printf '%s\n' "$MOCK_STATUS_JSON" |
| 17 | else |
| 18 | # Mirror the real CLI: logged_in / has_refresh / identity all come from |
| 19 | # the credential file the exchange wrote, so a test that runs --finish |
| 20 | # and then --status sees the same thing a real node would. |
| 21 | _acct="${ANTIGRAVITY_CREDS_PATH:-/nonexistent}" |
| 22 | _li=false; _hr=false; _id="" |
| 23 | if [ -f "$_acct" ] && grep -q 'refresh_token' "$_acct" 2>/dev/null; then |
| 24 | _li=true; _hr=true |
| 25 | _id=$(sed -n 's/.*"email"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$_acct" | head -1) |
| 26 | fi |
| 27 | printf 'scorpiox-antigravity-login 1.0.0 (SXLOGIN-MACHINE-V1)\n' |
| 28 | printf '{"ok":true,"provider":"antigravity","name":"Antigravity (Google)","flow":"paste_code",' |
| 29 | printf '"logged_in":%s,"path":"%s",' "$_li" "$_acct" |
| 30 | if [ -n "$_id" ]; then printf '"identity":"%s",' "$_id"; fi |
| 31 | printf '"expires_at":0,"expired":false,"has_refresh":%s,"pending":false,' "$_hr" |
| 32 | printf '"profile":"antigravity","profile_exists":false}\n' |
| 33 | fi |
| 34 | ;; |
| 35 | --start) |
| 36 | printf 'generating PKCE challenge\n' |
| 37 | printf '{"ok":true,"provider":"antigravity","flow":"paste_code",' |
| 38 | printf '"auth_url":"https://accounts.google.com/o/oauth2/auth?client_id=test&code_challenge=abc123&code_challenge_method=S256&state=st4te",' |
| 39 | printf '"expires_in":900,"hint":"Sign in with Google and approve, then paste the code."}\n' |
| 40 | ;; |
| 41 | --finish) |
| 42 | if [ "$MOCK_FAIL" = "1" ]; then |
| 43 | printf '{"ok":false,"provider":"antigravity","error":"exchange","detail":"validation required"}\n' |
| 44 | exit 1 |
| 45 | fi |
| 46 | [ -z "$2" ] && { printf '{"ok":false,"error":"no_code"}\n'; exit 1; } |
| 47 | # simulate the CLI writing the credential + profile |
| 48 | if [ -n "$MOCK_WRITE_DIR" ]; then |
| 49 | mkdir -p "$MOCK_WRITE_DIR" "$MOCK_WRITE_DIR/scorpiox-env" |
| 50 | printf '[{"email":"pico@example.com","refresh_token":"1//mock-refresh-token-value"}]\n' \ |
| 51 | > "$MOCK_WRITE_DIR/antigravity_accounts.json" |
| 52 | printf 'pico@example.com\n' > "$MOCK_WRITE_DIR/selected_account.txt" |
| 53 | printf 'PROVIDER=google_gemini\nGOOGLE_ACCOUNT=pico@example.com\nGOOGLE_PROJECT_ID=proj-mock-42\n' \ |
| 54 | > "$MOCK_WRITE_DIR/scorpiox-env/antigravity.txt" |
| 55 | fi |
| 56 | printf 'Login Successful!\n' |
| 57 | printf '{"ok":true,"provider":"antigravity","state":"done","logged_in":true,' |
| 58 | printf '"path":"%s","identity":"pico@example.com","profile":"antigravity","profile_created":true}\n' \ |
| 59 | "${ANTIGRAVITY_CREDS_PATH:-/nonexistent}" |
| 60 | ;; |
| 61 | --cancel) |
| 62 | printf '{"ok":true,"provider":"antigravity","state":"cancelled"}\n' |
| 63 | ;; |
| 64 | *) |
| 65 | printf '{"ok":false,"error":"usage"}\n' |
| 66 | exit 1 |
| 67 | ;; |
| 68 | esac |