scorpiox-bot-plugin-copilot-login
←mock_login.shbash
126 lines · 4 KBHEADBlameDownload
1#!/bin/sh
2# tests/mock_login.sh - stand-in for scorpiox-copilot-login in machine mode.
3#
4# The plugin never speaks TLS: it shells out to the node's login CLI and reads
5# only what that CLI writes. This mock reproduces the CLI's documented machine
6# contract (SXLOGIN-MACHINE-V1) against a scratch directory so the whole plugin
7# can be exercised offline, with no network and no real GitHub account.
8#
9# State lives in $COPILOT_MOCK_DIR:
10# poll_done -> the next --poll authorizes and writes credentials
11# poll_denied -> the next --poll reports access_denied
12# fail_start -> --start reports a network failure
13# no_cli -> (handled by the caller: point COPILOT_LOGIN_BIN elsewhere)
14#
15# Usage mirrors the real CLI: --status | --start | --poll | --cancel [--name N] [--force]
16
17set -u
18
19MOCK="${COPILOT_MOCK_DIR:-/tmp/copilot-mock}"
20mkdir -p "$MOCK" 2>/dev/null || true
21
22NAME=""
23MODE=""
24FORCE=0
25while [ $# -gt 0 ]; do
26 case "$1" in
27 --status|--start|--poll|--cancel) MODE="$1" ;;
28 --name) shift; NAME="$1" ;;
29 --force) FORCE=1 ;;
30 *) : ;;
31 esac
32 shift
33done
34
35[ -n "$NAME" ] || NAME="default"
36
37creds_file() {
38 if [ "$1" = "default" ]; then
39 printf '%s' "${COPILOT_CREDS_PATH:-$HOME/.copilot/.credentials.json}"
40 else
41 printf '%s' "${COPILOT_ACCOUNTS_DIR:-$HOME/.copilot/accounts}/$1.json"
42 fi
43}
44
45pending_file() {
46 printf '%s/%s.json' "${COPILOT_PENDING_DIR:-$HOME/.claude/.login-pending}" "copilot-$1"
47}
48
49write_creds() {
50 f=$(creds_file "$1")
51 d=$(dirname "$f")
52 mkdir -p "$d" 2>/dev/null || true
53 {
54 printf '{\n'
55 printf ' "github_token": "gho_MOCKTOKENMUSTNOTLEAK000000000000000",\n'
56 printf ' "token_type": "bearer",\n'
57 printf ' "scope": "%s",\n' "${COPILOT_MOCK_SCOPE:-read:user}"
58 printf ' "timestamp": "2026-01-01T00:00:00Z"\n'
59 printf '}\n'
60 } > "$f"
61}
62
63case "$MODE" in
64--status)
65 if [ -f "$(creds_file "$NAME")" ]; then logged=true; else logged=false; fi
66 if [ -f "$(pending_file "$NAME")" ]; then pending=true; else pending=false; fi
67 printf '{"ok":true,"provider":"copilot","name":"GitHub Copilot","flow":"device_code",'
68 printf '"account":"%s","logged_in":%s,"path":"%s","expires_at":0,"expired":false,'
69 printf '"has_refresh":false,"pending":%s,"profile":"copilot","profile_exists":false,'
70 printf '"named_accounts":true,"protocol":"SXLOGIN-MACHINE-V1"}\n' \
71 "$NAME" "$logged" "$(creds_file "$NAME")" "$pending"
72 exit 0
73 ;;
74--start)
75 if [ -f "$MOCK/fail_start" ]; then
76 printf 'error: device code request failed\n' >&2
77 printf '{"ok":false,"provider":"copilot","error":"network","detail":"device code request failed"}\n'
78 exit 1
79 fi
80 # A node that already holds a token refuses a second login without --force,
81 # exactly like the real CLI.
82 if [ "$FORCE" = "0" ] && [ -f "$(creds_file "$NAME")" ]; then
83 printf '{"ok":false,"provider":"copilot","error":"exists","detail":"credentials already exist; pass --force to replace"}\n'
84 exit 1
85 fi
86 p=$(pending_file "$NAME")
87 mkdir -p "$(dirname "$p")" 2>/dev/null || true
88 printf '{"device_code":"dc_mock_value","interval":5,"force":%d,"vscode":0}\n' "$FORCE" > "$p"
89 printf 'Requesting device code...\n'
90 printf '{"ok":true,"provider":"copilot","flow":"device_code",'
91 printf '"verify_url":"https://github.com/login/device","user_code":"1234-5678",'
92 printf '"interval":5,"expires_in":900,'
93 printf '"hint":"Open the link, enter the code, approve. This page checks automatically."}\n'
94 exit 0
95 ;;
96--poll)
97 p=$(pending_file "$NAME")
98 if [ ! -f "$p" ]; then
99 printf '{"ok":false,"provider":"copilot","error":"no_pending","detail":"run --start first"}\n'
100 exit 1
101 fi
102 if [ -f "$MOCK/poll_denied" ]; then
103 rm -f "$p"
104 printf '{"ok":false,"provider":"copilot","error":"denied","detail":"access_denied"}\n'
105 exit 1
106 fi
107 if [ -f "$MOCK/poll_done" ]; then
108 rm -f "$p" "$MOCK/poll_done"
109 write_creds "$NAME"
110 printf '{"ok":true,"provider":"copilot","state":"done","logged_in":true,'
111 printf '"path":"%s","profile":"copilot","profile_created":false}\n' "$(creds_file "$NAME")"
112 exit 0
113 fi
114 printf '{"ok":true,"provider":"copilot","state":"pending","interval":5}\n'
115 exit 0
116 ;;
117--cancel)
118 rm -f "$(pending_file "$NAME")"
119 printf '{"ok":true,"provider":"copilot","cancelled":true}\n'
120 exit 0
121 ;;
122*)
123 printf 'error: mock_login.sh needs --status|--start|--poll|--cancel\n' >&2
124 exit 2
125 ;;
126esac