scorpiox-bot-plugin-opencode-login
←mock_token.shbash
50 lines · 1 KBHEADBlameDownload
1#!/bin/sh
2# tests/mock_token.sh - stand-in for scorpiox-opencode-fetchtoken.
3#
4# The real CLI reads ~/.config/opencode/auth.json and prints
5# {"access_token":"...","org_id":"...","expires_at":1234567890}
6# The plugin only ever looks at access_token presence and expires_at, so this
7# mock mirrors that shape and reads the same credential file the login mock
8# writes, which makes a "refresh after login" test behave like a real node.
9#
10# MOCK_TOKEN_FAIL=1 exit non-zero with an error on stderr (no token)
11
12CREDS="${OPENCODE_CREDS_PATH:-/nonexistent-auth.json}"
13
14case "$1" in
15 --help|-h)
16 printf 'usage: scorpiox-opencode-fetchtoken [-local|-config|-tcp|-remote]\n'
17 ;;
18 -local|""|-config)
19 if [ "$MOCK_TOKEN_FAIL" = "1" ]; then
20 printf 'error: auth file not found: %s\n' "$CREDS" >&2
21 exit 1
22 fi
23 if [ ! -f "$CREDS" ]; then
24 printf 'error: auth file not found: %s\n' "$CREDS" >&2
25 exit 1
26 fi
27 python3 - "$CREDS" <<'PY'
28import json, sys, time
29try:
30 with open(sys.argv[1]) as f:
31 d = json.load(f)
32except Exception:
33 d = {}
34tok = d.get("access_token") or ""
35exp = d.get("expires_at") or 0
36if not exp and d.get("expires_in"):
37 exp = int(time.time()) + int(d["expires_in"])
38if not tok:
39 sys.stderr.write("error: no access_token in auth file\n")
40 sys.exit(1)
41print(json.dumps({"access_token": tok,
42 "org_id": d.get("org_id") or "",
43 "expires_at": int(exp)}))
44PY
45 ;;
46 *)
47 printf 'error: unknown mode %s\n' "$1" >&2
48 exit 2
49 ;;
50esac