- docs/dth/experiment-platform/web-design.md: T1 架构设计
- src/frontend/src/main.tsx + App.tsx: 入口 + 路由
- src/frontend/src/api/{auth,operators,runs,client}.ts: 4 文件
- src/frontend/src/components/{AuthContext,ProtectedRoute,OperatorCard,RunResult,Radar}.tsx: 5 组件(Radar SVG 自绘 139 行)
- src/frontend/src/views/{Login,Dashboard,OperatorList,Run}.tsx: 4 页
- src/frontend/src/styles.css: 159 行(< 200 预算,无 UI 库)
- src/frontend/vite.config.ts: proxy /api → :30000
- tests/e2e/dth_frontend.test.sh: 14 case (5 API + 4 routes + 2 static + 3 list)
- openspec/changes/chg_0004_experiment_platform_frontend/{5 工件}
- package-lock.json: +react/react-dom/react-router-dom/axios + vite/typescript
Co-Authored-By: Claude Opus 4.8 <[email protected]>
123 lines
5.9 KiB
Bash
Executable File
123 lines
5.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
||
# dth_frontend.test.sh — e2e verification for chg_0004 (experiment_platform_web).
|
||
# Covers C1–C6:
|
||
# C1 login success returns token → S1, S2
|
||
# C2 wrong password returns 401 → S3
|
||
# C3 list operators returns ≥ 3 with schema → S4
|
||
# C4 run hello-mpc returns UUID → S5
|
||
# C5 GET /runs/:id returns real output_json → S6
|
||
# C6 static route check in App.tsx → S4, S5, S6
|
||
#
|
||
# C1–C5 talk to the live NestJS backend on :30000 (chg_0002).
|
||
# C6 reads the source tree — pure static check, no runtime needed.
|
||
#
|
||
# Usage:
|
||
# bash tests/e2e/dth_frontend.test.sh
|
||
# DTH_BASE=http://127.0.0.1:30000 bash tests/e2e/dth_frontend.test.sh
|
||
#
|
||
# Pre-req: backend is running and admin seed is loaded (run.sh dev).
|
||
|
||
set -uo pipefail
|
||
|
||
BASE="${DTH_BASE:-http://127.0.0.1:${PORT:-30000}}"
|
||
LOGIN_URL="$BASE/api/ssw/dth/auth/login"
|
||
OPS_URL="$BASE/api/ssw/dth/experiment/operators"
|
||
RUNS_URL="$BASE/api/ssw/dth/experiment/runs"
|
||
|
||
pass=0; fail=0; fails=()
|
||
|
||
json_get() {
|
||
local expr="$1"; local body="$2"
|
||
python3 -c "import json,sys; d=json.loads(sys.argv[1]); print(d${expr})" "$body" 2>/dev/null
|
||
}
|
||
|
||
assert() {
|
||
local id="$1"; local cond="$2"; local detail="$3"
|
||
if eval "$cond"; then
|
||
pass=$((pass+1))
|
||
printf " \033[32m✓\033[0m %s\n" "$id"
|
||
else
|
||
fail=$((fail+1))
|
||
fails+=("$id: $detail")
|
||
printf " \033[31m✗\033[0m %s — %s\n" "$id" "$detail"
|
||
fi
|
||
}
|
||
|
||
# ----- pre: login admin -----
|
||
ADMIN_BODY="$(curl -s -X POST "$LOGIN_URL" -H 'Content-Type: application/json' -d '{"username":"admin","password":"123456"}')"
|
||
ADMIN_TOKEN="$(json_get "['data']['token']" "$ADMIN_BODY")"
|
||
|
||
assert "C1_login_success_returns_token" "[ -n '$ADMIN_TOKEN' ]" \
|
||
"admin token empty; login body=$ADMIN_BODY"
|
||
|
||
# ----- C2: wrong password → 401 envelope -----
|
||
WRONG_BODY="$(curl -s -X POST "$LOGIN_URL" -H 'Content-Type: application/json' -d '{"username":"admin","password":"wrong"}')"
|
||
WRONG_CODE="$(json_get "['code']" "$WRONG_BODY")"
|
||
WRONG_MSG="$(json_get "['msg']" "$WRONG_BODY")"
|
||
assert "C2_wrong_password_returns_401" "[ '$WRONG_CODE' = '401' ]" \
|
||
"got code=$WRONG_CODE, msg=$WRONG_MSG"
|
||
|
||
# ----- C3: list operators ≥ 3 with schema -----
|
||
LIST_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$OPS_URL")"
|
||
LIST_LEN="$(python3 -c "import json,sys; d=json.loads(sys.argv[1]); print(len(d['data']))" "$LIST_BODY" 2>/dev/null)"
|
||
HAS_SCHEMA="$(python3 -c "import json,sys; d=json.loads(sys.argv[1]); print(all('schema' in o for o in d['data']))" "$LIST_BODY" 2>/dev/null)"
|
||
HAS_FIELDS="$(python3 -c "import json,sys; d=json.loads(sys.argv[1]); print(all(set(['code','name','version','schema']).issubset(o) for o in d['data']))" "$LIST_BODY" 2>/dev/null)"
|
||
assert "C3_list_operators_returns_three" "[ '${LIST_LEN:-0}' -ge 3 ]" \
|
||
"got len=$LIST_LEN"
|
||
assert "C3_list_operators_has_schema" "[ '$HAS_SCHEMA' = 'True' ]" \
|
||
"missing schema field"
|
||
assert "C3_list_operators_has_all_fields" "[ '$HAS_FIELDS' = 'True' ]" \
|
||
"missing code/name/version/schema"
|
||
|
||
# ----- C4: run hello-mpc returns UUID -----
|
||
RUN_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
|
||
-d '{"operator":"hello-mpc","input":{"x":42}}' "$RUNS_URL")"
|
||
RUN_ID="$(json_get "['data']['run_id']" "$RUN_BODY")"
|
||
RUN_STATUS="$(json_get "['data']['status']" "$RUN_BODY")"
|
||
IS_UUID="$(python3 -c "import re,sys; print(bool(re.match(r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$', sys.argv[1])))" "$RUN_ID" 2>/dev/null)"
|
||
assert "C4_run_returns_uuid" "[ '$IS_UUID' = 'True' ]" \
|
||
"run_id=$RUN_ID (status=$RUN_STATUS) is not UUID"
|
||
|
||
# ----- C5: GET /runs/:id returns real output_json -----
|
||
# hello-mpc operator shape (src/backend/src/dth/experiment/operators/hello-mpc.ts):
|
||
# { ok: true, echo: <input>, mpc: {...}, computed_at: ... }
|
||
# No `hello_mpc` wrapper — the operator's return value IS the output_json.
|
||
GET_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$RUN_ID")"
|
||
OK_FLAG="$(python3 -c "import json,sys; d=json.loads(sys.argv[1]); print(d['data']['output_json']['ok'])" "$GET_BODY" 2>/dev/null)"
|
||
ECHO_X="$(python3 -c "import json,sys; d=json.loads(sys.argv[1]); print(d['data']['output_json']['echo']['x'])" "$GET_BODY" 2>/dev/null)"
|
||
assert "C5_run_output_ok_true" "[ '$OK_FLAG' = 'True' ]" \
|
||
"output_json.ok=$OK_FLAG, body=$GET_BODY"
|
||
assert "C5_run_output_echo_x_42" "[ '$ECHO_X' = '42' ]" \
|
||
"output_json.echo.x=$ECHO_X"
|
||
|
||
# ----- C6: static route check in App.tsx -----
|
||
APP_TSX="src/frontend/src/App.tsx"
|
||
if [ -f "$APP_TSX" ]; then
|
||
HAS_LOGIN=$(grep -c '"/login"' "$APP_TSX" || true)
|
||
HAS_DASH=$(grep -c '"/dashboard"' "$APP_TSX" || true)
|
||
HAS_OPS=$(grep -c '"/operators"' "$APP_TSX" || true)
|
||
HAS_RUN=$(grep -c '"/operators/:code/run"' "$APP_TSX" || true)
|
||
assert "C6_route_login_in_app" "[ '$HAS_LOGIN' -ge 1 ]" "missing /login in App.tsx"
|
||
assert "C6_route_dashboard_in_app" "[ '$HAS_DASH' -ge 1 ]" "missing /dashboard in App.tsx"
|
||
assert "C6_route_operators_in_app" "[ '$HAS_OPS' -ge 1 ]" "missing /operators in App.tsx"
|
||
assert "C6_route_run_in_app" "[ '$HAS_RUN' -ge 1 ]" "missing /operators/:code/run in App.tsx"
|
||
else
|
||
assert "C6_app_tsx_exists" "false" "$APP_TSX not found"
|
||
fi
|
||
|
||
# ----- C6 (continuation): radar SVG + auth context present -----
|
||
# Radar uses a single template-literal data-testid 'radar-{operatorCode}'.
|
||
HAS_RADAR=$(grep -c 'radar-\${operatorCode}' src/frontend/src/components/Radar.tsx 2>/dev/null || true)
|
||
HAS_AUTH_CTX=$(grep -c 'AuthProvider' src/frontend/src/main.tsx 2>/dev/null || true)
|
||
assert "C6_radar_component_has_data_testid" "[ '${HAS_RADAR:-0}' -ge 1 ]" "Radar.tsx missing radar-{operatorCode} data-testid"
|
||
assert "C6_main_wraps_authprovider" "[ '${HAS_AUTH_CTX:-0}' -ge 1 ]" "main.tsx missing AuthProvider"
|
||
|
||
# ----- summary -----
|
||
echo
|
||
printf "passed: %d, failed: %d\n" "$pass" "$fail"
|
||
if [ "$fail" -gt 0 ]; then
|
||
printf "FAILURES:\n"
|
||
for f in "${fails[@]}"; do printf " - %s\n" "$f"; done
|
||
exit 1
|
||
fi
|
||
exit 0 |