#!/usr/bin/env bash set -euo pipefail if [ -z "${BASE_URL:-}" ]; then if curl --silent --fail http://127.0.0.1:8085/api/health >/dev/null 2>&1; then BASE_URL="http://127.0.0.1:8085" elif curl --silent --fail http://host.containers.internal:8085/api/health >/dev/null 2>&1; then BASE_URL="http://host.containers.internal:8085" else echo "ERROR: could not determine BASE_URL. Tried 127.0.0.1 and host.containers.internal." >&2 exit 1 fi fi SESSION_ID="selected-episodes-test-$(date +%s)-$$" TMP_DIR="$(mktemp -d)" trap 'rm -rf "$TMP_DIR"' EXIT echo "== Feature test 1: add + list selected episodes ==" curl --fail --silent --show-error \ -X DELETE "${BASE_URL}/api/session/selected-episodes?session_id=${SESSION_ID}" \ -o "${TMP_DIR}/clear_before.json" cat > "${TMP_DIR}/add_payload.json" <<'JSON' { "items": [ { "id": 9784113, "season_number": 1, "episode_number": 1, "title": "Pilot", "aired": "2024-02-29", "label": "S01E01 - Pilot - 2024-02-29" }, { "id": 10347197, "season_number": 1, "episode_number": 2, "title": "A Classic New York Character", "aired": "2024-04-04", "label": "S01E02 - A Classic New York Character - 2024-04-04" } ] } JSON curl --fail --silent --show-error \ -X POST "${BASE_URL}/api/session/selected-episodes?session_id=${SESSION_ID}" \ -H "Content-Type: application/json" \ --data @"${TMP_DIR}/add_payload.json" \ -o "${TMP_DIR}/add_response.json" cat "${TMP_DIR}/add_response.json" python3 - "${TMP_DIR}/add_response.json" <<'PY' import json import sys from pathlib import Path data = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8")) assert isinstance(data, dict), "response must be an object" assert isinstance(data.get("items"), list), "items must be a list" assert len(data["items"]) == 2, "expected 2 selected episodes" assert data["items"][0]["episode"]["title"] == "Pilot", "first title mismatch" assert data["items"][1]["episode"]["title"] == "A Classic New York Character", "second title mismatch" print("add/list validation passed") PY echo echo "== Feature test 2: reorder selected episodes ==" cat > "${TMP_DIR}/reorder_payload.json" <<'JSON' { "from_index": 1, "to_index": 0 } JSON curl --fail --silent --show-error \ -X POST "${BASE_URL}/api/session/selected-episodes/reorder?session_id=${SESSION_ID}" \ -H "Content-Type: application/json" \ --data @"${TMP_DIR}/reorder_payload.json" \ -o "${TMP_DIR}/reorder_response.json" cat "${TMP_DIR}/reorder_response.json" python3 - "${TMP_DIR}/reorder_response.json" <<'PY' import json import sys from pathlib import Path data = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8")) items = data.get("items") assert isinstance(items, list), "items must be a list" assert len(items) == 2, "expected 2 selected episodes" assert items[0]["episode"]["title"] == "A Classic New York Character", "reorder did not move expected item" assert items[1]["episode"]["title"] == "Pilot", "reorder did not preserve second item" print("reorder validation passed") PY echo echo "== Feature test 3: remove + clear selected episodes ==" python3 - "${TMP_DIR}/reorder_response.json" > "${TMP_DIR}/selection_id.txt" <<'PY' import json import sys from pathlib import Path data = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8")) print(data["items"][0]["selection_id"]) PY SELECTION_ID="$(cat "${TMP_DIR}/selection_id.txt")" curl --fail --silent --show-error \ -X DELETE "${BASE_URL}/api/session/selected-episodes/${SELECTION_ID}?session_id=${SESSION_ID}" \ -o "${TMP_DIR}/remove_response.json" cat "${TMP_DIR}/remove_response.json" python3 - "${TMP_DIR}/remove_response.json" <<'PY' import json import sys from pathlib import Path data = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8")) items = data.get("items") assert isinstance(items, list), "items must be a list" assert len(items) == 1, "expected 1 selected episode after remove" print("remove validation passed") PY curl --fail --silent --show-error \ -X DELETE "${BASE_URL}/api/session/selected-episodes?session_id=${SESSION_ID}" \ -o "${TMP_DIR}/clear_response.json" cat "${TMP_DIR}/clear_response.json" python3 - "${TMP_DIR}/clear_response.json" <<'PY' import json import sys from pathlib import Path data = json.loads(Path(sys.argv[1]).read_text(encoding="utf-8")) items = data.get("items") assert isinstance(items, list), "items must be a list" assert len(items) == 0, "expected empty list after clear" print("clear validation passed") PY echo echo "All selected episodes feature tests passed."