scorpiox-bot-plugin-opencode-login
←mock_login.shbash
114 lines · 4 KBHEADBlameDownload
1#!/bin/sh
2# tests/mock_login.sh - stand-in for scorpiox-opencode-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 device exchange is
7# simulated: --poll writes a credential file into $OPENCODE_CREDS_PATH and
8# removes the pending marker when MOCK_APPROVE=1, which is what the real CLI
9# does after the user approves in the browser.
10#
11# MOCK_START_FAIL=1 --start answers the "credentials already exist" error
12# MOCK_APPROVE=1 --poll completes the flow
13# MOCK_DENY=1 --poll answers a terminal denial
14# MOCK_NO_PENDING=1 --poll answers no_pending even if a marker exists
15
16CREDS="${OPENCODE_CREDS_PATH:-/nonexistent-auth.json}"
17PEND="${OPENCODE_PENDING_PATH:-/nonexistent-pending.json}"
18
19# pyget <file> <key> -> the value, or empty string when absent/unreadable
20pyget() {
21 python3 - "$1" "$2" <<'PY'
22import json, sys
23try:
24 with open(sys.argv[1]) as f:
25 d = json.load(f)
26except Exception:
27 d = {}
28v = d.get(sys.argv[2]) if isinstance(d, dict) else None
29if v is None:
30 print("")
31elif isinstance(v, bool):
32 print("true" if v else "false")
33else:
34 print(v)
35PY
36}
37
38jstr() { # jstr <value> -> "value" or null
39 if [ -z "$1" ]; then printf 'null'; else printf '"%s"' "$1"; fi
40}
41
42case "$1" in
43 --status)
44 _li=false; _hr=false; _exp=0; _org=""; _pend=false
45 if [ -f "$CREDS" ]; then
46 _tok=$(pyget "$CREDS" access_token)
47 [ -n "$_tok" ] && _li=true
48 _rt=$(pyget "$CREDS" refresh_token)
49 [ -n "$_rt" ] && _hr=true
50 _exp=$(pyget "$CREDS" expires_at)
51 [ -z "$_exp" ] && _exp=0
52 _org=$(pyget "$CREDS" org_id)
53 fi
54 [ -f "$PEND" ] && _pend=true
55 printf 'scorpiox-opencode-login 1.0.0 (SXLOGIN-MACHINE-V1)\n'
56 printf '{"ok":true,"provider":"opencode","name":"OpenCode","flow":"device_code",'
57 printf '"account":%s,"logged_in":%s,"path":"%s",' "$(jstr "$_org")" "$_li" "$CREDS"
58 printf '"expires_at":%s,"expired":false,"has_refresh":%s,"pending":%s,' "$_exp" "$_hr" "$_pend"
59 printf '"profile":"superopencode","profile_exists":false,"named_accounts":false,'
60 printf '"protocol":"SXLOGIN-MACHINE-V1"}\n'
61 ;;
62 --start)
63 if [ "$MOCK_START_FAIL" = "1" ]; then
64 printf '{"ok":false,"provider":"opencode","error":"exists","detail":"credentials already exist; pass --force to replace"}\n'
65 exit 0
66 fi
67 if [ "$MOCK_START_NETFAIL" = "1" ]; then
68 printf '{"ok":false,"provider":"opencode","error":"network","detail":"device code request failed"}\n'
69 exit 0
70 fi
71 mkdir -p "$(dirname "$PEND")" 2>/dev/null || :
72 printf '{"device_code":"oc-mock-device-code-must-not-leak","interval":5,"force":0,"created":%s}\n' \
73 "$(date +%s)" > "$PEND"
74 printf '{"ok":true,"provider":"opencode","flow":"device_code",'
75 printf '"verify_url":"https://opencode.ai/console/device?user_code=ABCD-EFGH&client_id=opencode-cli",'
76 printf '"user_code":"ABCD-EFGH","expires_in":900}\n'
77 ;;
78 --poll)
79 if [ "$MOCK_APPROVE" = "1" ]; then
80 mkdir -p "$(dirname "$CREDS")" 2>/dev/null || :
81 # one redirection for the whole block: in POSIX sh a redirect attaches
82 # to a single command, so three separate printfs would write two lines
83 # to stdout and only the last one to the credential file.
84 {
85 printf '{"access_token":"oc-mock-access-token-must-not-leak",'
86 printf '"refresh_token":"oc-mock-refresh-token-must-not-leak","token_type":"Bearer",'
87 printf '"expires_in":2591999,"expires_at":%s,"org_id":"org-mock-42"}\n' \
88 "$(( $(date +%s) + 2591999 ))"
89 } > "$CREDS"
90 rm -f "$PEND"
91 printf 'Logged in successfully!\n'
92 printf '{"ok":true,"provider":"opencode","state":"done","logged_in":true,'
93 printf '"path":"%s","profile":"superopencode","profile_created":false}\n' "$CREDS"
94 exit 0
95 fi
96 if [ "$MOCK_DENY" = "1" ]; then
97 rm -f "$PEND"
98 printf '{"ok":false,"provider":"opencode","error":"denied","detail":"access_denied"}\n'
99 exit 0
100 fi
101 if [ ! -f "$PEND" ] || [ "$MOCK_NO_PENDING" = "1" ]; then
102 printf '{"ok":false,"provider":"opencode","error":"no_pending","detail":"run --start first"}\n'
103 exit 0
104 fi
105 printf '{"ok":true,"provider":"opencode","state":"pending","interval":5}\n'
106 ;;
107 --cancel)
108 rm -f "$PEND"
109 printf '{"ok":true,"provider":"opencode","cancelled":true}\n'
110 ;;
111 *)
112 printf '{"ok":false,"provider":"opencode","error":"usage","detail":"unknown flag"}\n'
113 ;;
114esac