|
#!/usr/bin/env bash |
|
# UAT setup: creates a test user and imports sample portfolio data. |
|
# Saves credentials to .uat-creds.json in the repo root. |
|
# Run once before using /uat skill. Safe to re-run (overwrites creds). |
|
|
|
set -euo pipefail |
|
|
|
API="http://localhost:3333/api/v1" |
|
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../../.." && pwd)" |
|
CREDS_FILE="$REPO_ROOT/.uat-creds.json" |
|
SAMPLE_FILE="$REPO_ROOT/test/import/ok/sample.json" |
|
|
|
echo "UAT Setup - Ghostfolio Agent" |
|
echo "==============================" |
|
|
|
# 1. Check API is running |
|
echo "[1/4] Checking API health..." |
|
if ! curl -sf "$API/health" > /dev/null 2>&1; then |
|
echo "ERROR: API not reachable at $API" |
|
echo "Start it with: npm run dev:api" |
|
echo "Or full stack: npm run dev" |
|
exit 1 |
|
fi |
|
echo " API is up." |
|
|
|
# 2. Get test user credentials |
|
echo "[2/4] Getting test user credentials..." |
|
TEST_ACCOUNTS="$REPO_ROOT/test-accounts.json" |
|
|
|
if [ -f "$TEST_ACCOUNTS" ]; then |
|
# Use first unused account from pre-created pool |
|
ACCOUNT_COUNT=$(jq length "$TEST_ACCOUNTS") |
|
# Track which account we're using via .uat-account-index |
|
INDEX_FILE="$REPO_ROOT/.uat-account-index" |
|
if [ -f "$INDEX_FILE" ]; then |
|
ACCOUNT_INDEX=$(cat "$INDEX_FILE") |
|
else |
|
ACCOUNT_INDEX=0 |
|
fi |
|
|
|
if [ "$ACCOUNT_INDEX" -ge "$ACCOUNT_COUNT" ]; then |
|
ACCOUNT_INDEX=0 |
|
echo " Wrapped around to first account." |
|
fi |
|
|
|
ACCESS_TOKEN=$(jq -r ".[$ACCOUNT_INDEX].accessToken" "$TEST_ACCOUNTS") |
|
echo " Using pre-created account $ACCOUNT_INDEX from test-accounts.json" |
|
|
|
# Get fresh JWT via login |
|
LOGIN_RESPONSE=$(curl -s -X POST "$API/auth/anonymous" \ |
|
-H "Content-Type: application/json" \ |
|
-d "{\"accessToken\": \"$ACCESS_TOKEN\"}") |
|
AUTH_TOKEN=$(echo "$LOGIN_RESPONSE" | jq -r '.authToken') |
|
|
|
if [ -z "$AUTH_TOKEN" ] || [ "$AUTH_TOKEN" = "null" ]; then |
|
echo "ERROR: Failed to login with account $ACCOUNT_INDEX" |
|
exit 1 |
|
fi |
|
|
|
# Increment index for next setup run |
|
echo $(( ACCOUNT_INDEX + 1 )) > "$INDEX_FILE" |
|
echo " Logged in. Next run will use account $(( ACCOUNT_INDEX + 1 ))." |
|
else |
|
# Fallback: create a new account |
|
echo " No test-accounts.json found, creating new user..." |
|
CREATE_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "$API/user" \ |
|
-H "Content-Type: application/json") |
|
HTTP_CODE=$(echo "$CREATE_RESPONSE" | tail -1) |
|
BODY=$(echo "$CREATE_RESPONSE" | head -n -1) |
|
|
|
if [ "$HTTP_CODE" != "201" ] && [ "$HTTP_CODE" != "200" ]; then |
|
echo "ERROR: Failed to create user (HTTP $HTTP_CODE)" |
|
echo "Response: $BODY" |
|
exit 1 |
|
fi |
|
|
|
ACCESS_TOKEN=$(echo "$BODY" | jq -r '.accessToken') |
|
AUTH_TOKEN=$(echo "$BODY" | jq -r '.authToken') |
|
|
|
if [ -z "$ACCESS_TOKEN" ] || [ "$ACCESS_TOKEN" = "null" ]; then |
|
echo "ERROR: No accessToken in response" |
|
echo "Response: $BODY" |
|
exit 1 |
|
fi |
|
|
|
echo " User created. Access token: ${ACCESS_TOKEN:0:20}..." |
|
fi |
|
|
|
# 3. Save credentials |
|
echo "[3/4] Saving credentials to .uat-creds.json..." |
|
cat > "$CREDS_FILE" <<EOF |
|
{ |
|
"accessToken": "$ACCESS_TOKEN", |
|
"authToken": "$AUTH_TOKEN" |
|
} |
|
EOF |
|
echo " Saved to: $CREDS_FILE" |
|
|
|
# 4. Import sample portfolio |
|
echo "[4/4] Importing sample portfolio..." |
|
if [ ! -f "$SAMPLE_FILE" ]; then |
|
echo " WARNING: Sample file not found at $SAMPLE_FILE - skipping import" |
|
else |
|
IMPORT_RESPONSE=$(curl -s -w "\n%{http_code}" -X POST "$API/import" \ |
|
-H "Content-Type: application/json" \ |
|
-H "Authorization: Bearer $AUTH_TOKEN" \ |
|
-d @"$SAMPLE_FILE") |
|
IMPORT_CODE=$(echo "$IMPORT_RESPONSE" | tail -1) |
|
IMPORT_BODY=$(echo "$IMPORT_RESPONSE" | head -n -1) |
|
|
|
if [ "$IMPORT_CODE" = "201" ] || [ "$IMPORT_CODE" = "200" ]; then |
|
echo " Sample portfolio imported successfully." |
|
else |
|
echo " WARNING: Import returned HTTP $IMPORT_CODE" |
|
echo " Response: $IMPORT_BODY" |
|
echo " (Non-fatal - you can still test search_symbol and get_portfolio_summary)" |
|
fi |
|
fi |
|
|
|
echo "" |
|
echo "Setup complete!" |
|
echo " Credentials: $CREDS_FILE" |
|
echo " Auth token: ${AUTH_TOKEN:0:30}..." |
|
echo "" |
|
echo "Now use /uat to test the agent:" |
|
echo " /uat What's the ticker for Tesla?" |
|
echo " /uat Show me my portfolio summary" |