|
| 1 | +#!/usr/bin/env bash |
| 2 | +# 03-answer-cycle.sh — Render → submit → verify scoring. |
| 3 | + |
| 4 | +set -euo pipefail |
| 5 | +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 6 | +source "$SCRIPT_DIR/lib/helpers.sh" |
| 7 | +source "$SCRIPT_DIR/lib/problems.sh" |
| 8 | + |
| 9 | +echo "=== Answer Cycle Tests ===" |
| 10 | + |
| 11 | +# ── Single answer: correct ──────────────────────────────────── |
| 12 | + |
| 13 | +# Step 1: Render as instructor to discover answer field name + correct value |
| 14 | +INST_RESP=$(render_raw "problemSource=${PROBLEM_BASIC}" "problemSeed=42" "isInstructor=1") |
| 15 | + |
| 16 | +# Extract the answer field name (e.g., AnSwEr0001) |
| 17 | +ANS_NAME=$(echo "$INST_RESP" | jq -r '.answers | keys[0]') |
| 18 | +CORRECT_VAL=$(echo "$INST_RESP" | jq -r ".answers.\"$ANS_NAME\".correct_ans") |
| 19 | + |
| 20 | +assert_ne "$ANS_NAME" "" "Discovered answer field name: $ANS_NAME" |
| 21 | +assert_ne "$CORRECT_VAL" "" "Discovered correct answer: $CORRECT_VAL" |
| 22 | + |
| 23 | +# Step 2: Render as student to get JWT tokens |
| 24 | +STU_RESP=$(render_raw "problemSource=${PROBLEM_BASIC}" "problemSeed=42") |
| 25 | +PROB_JWT=$(echo "$STU_RESP" | jq -r '.JWT.problem') |
| 26 | +SESS_JWT=$(echo "$STU_RESP" | jq -r '.JWT.session') |
| 27 | + |
| 28 | +# Step 3: Submit correct answer |
| 29 | +SUBMIT_RESP=$(curl -sf --max-time "$CURL_TIMEOUT" -X POST \ |
| 30 | + -d "problemJWT=${PROB_JWT}" \ |
| 31 | + -d "sessionJWT=${SESS_JWT}" \ |
| 32 | + -d "${ANS_NAME}=${CORRECT_VAL}" \ |
| 33 | + -d "submitAnswers=1" \ |
| 34 | + -d "answersSubmitted=1" \ |
| 35 | + -d "_format=json" \ |
| 36 | + "${BASE_URL}/render-api" 2>/dev/null) |
| 37 | + |
| 38 | +SCORE=$(echo "$SUBMIT_RESP" | jq -r '.problem_result.score') |
| 39 | +assert_eq "$SCORE" "1" "Correct answer scores 1" |
| 40 | + |
| 41 | +# Step 4: Submit wrong answer |
| 42 | +WRONG_RESP=$(curl -sf --max-time "$CURL_TIMEOUT" -X POST \ |
| 43 | + -d "problemJWT=${PROB_JWT}" \ |
| 44 | + -d "sessionJWT=${SESS_JWT}" \ |
| 45 | + -d "${ANS_NAME}=999" \ |
| 46 | + -d "submitAnswers=1" \ |
| 47 | + -d "answersSubmitted=1" \ |
| 48 | + -d "_format=json" \ |
| 49 | + "${BASE_URL}/render-api" 2>/dev/null) |
| 50 | + |
| 51 | +WRONG_SCORE=$(echo "$WRONG_RESP" | jq -r '.problem_result.score') |
| 52 | +assert_eq "$WRONG_SCORE" "0" "Wrong answer scores 0" |
| 53 | + |
| 54 | +# ── Multi-answer: partial credit ────────────────────────────── |
| 55 | + |
| 56 | +MULTI_INST=$(render_raw "problemSource=${PROBLEM_MULTI}" "problemSeed=42" "isInstructor=1") |
| 57 | + |
| 58 | +# Get both answer field names and correct values |
| 59 | +ANS1_NAME=$(echo "$MULTI_INST" | jq -r '.answers | keys[0]') |
| 60 | +ANS2_NAME=$(echo "$MULTI_INST" | jq -r '.answers | keys[1]') |
| 61 | +ANS1_CORRECT=$(echo "$MULTI_INST" | jq -r ".answers.\"$ANS1_NAME\".correct_ans") |
| 62 | +ANS2_CORRECT=$(echo "$MULTI_INST" | jq -r ".answers.\"$ANS2_NAME\".correct_ans") |
| 63 | + |
| 64 | +# Render as student |
| 65 | +MULTI_STU=$(render_raw "problemSource=${PROBLEM_MULTI}" "problemSeed=42") |
| 66 | +MULTI_PROB_JWT=$(echo "$MULTI_STU" | jq -r '.JWT.problem') |
| 67 | +MULTI_SESS_JWT=$(echo "$MULTI_STU" | jq -r '.JWT.session') |
| 68 | + |
| 69 | +# Submit 1/2 correct (first correct, second wrong) |
| 70 | +PARTIAL_RESP=$(curl -sf --max-time "$CURL_TIMEOUT" -X POST \ |
| 71 | + -d "problemJWT=${MULTI_PROB_JWT}" \ |
| 72 | + -d "sessionJWT=${MULTI_SESS_JWT}" \ |
| 73 | + -d "${ANS1_NAME}=${ANS1_CORRECT}" \ |
| 74 | + -d "${ANS2_NAME}=999" \ |
| 75 | + -d "submitAnswers=1" \ |
| 76 | + -d "answersSubmitted=1" \ |
| 77 | + -d "_format=json" \ |
| 78 | + "${BASE_URL}/render-api" 2>/dev/null) |
| 79 | + |
| 80 | +PARTIAL_SCORE=$(echo "$PARTIAL_RESP" | jq -r '.problem_result.score') |
| 81 | +assert_eq "$PARTIAL_SCORE" "0.5" "1/2 correct scores 0.5" |
| 82 | + |
| 83 | +# Submit both correct |
| 84 | +FULL_RESP=$(curl -sf --max-time "$CURL_TIMEOUT" -X POST \ |
| 85 | + -d "problemJWT=${MULTI_PROB_JWT}" \ |
| 86 | + -d "sessionJWT=${MULTI_SESS_JWT}" \ |
| 87 | + -d "${ANS1_NAME}=${ANS1_CORRECT}" \ |
| 88 | + -d "${ANS2_NAME}=${ANS2_CORRECT}" \ |
| 89 | + -d "submitAnswers=1" \ |
| 90 | + -d "answersSubmitted=1" \ |
| 91 | + -d "_format=json" \ |
| 92 | + "${BASE_URL}/render-api" 2>/dev/null) |
| 93 | + |
| 94 | +FULL_SCORE=$(echo "$FULL_RESP" | jq -r '.problem_result.score') |
| 95 | +assert_eq "$FULL_SCORE" "1" "2/2 correct scores 1" |
| 96 | + |
| 97 | +# ── Preview mode ────────────────────────────────────────────── |
| 98 | + |
| 99 | +# Preview mode renders the formatted input. The grader still runs (score reflects |
| 100 | +# correctness), but the rendered HTML shows formatted input rather than right/wrong. |
| 101 | +PREVIEW_RESP=$(curl -sf --max-time "$CURL_TIMEOUT" -X POST \ |
| 102 | + -d "problemJWT=${PROB_JWT}" \ |
| 103 | + -d "sessionJWT=${SESS_JWT}" \ |
| 104 | + -d "${ANS_NAME}=${CORRECT_VAL}" \ |
| 105 | + -d "previewAnswers=1" \ |
| 106 | + -d "answersSubmitted=1" \ |
| 107 | + -d "_format=json" \ |
| 108 | + "${BASE_URL}/render-api" 2>/dev/null) |
| 109 | + |
| 110 | +assert_json_field "$PREVIEW_RESP" '.renderedHTML' "Preview mode returns rendered HTML" |
| 111 | +assert_json_field "$PREVIEW_RESP" '.JWT.session' "Preview mode returns session JWT" |
| 112 | + |
| 113 | +summary "answer cycle tests" |
0 commit comments