Spaces:
Running
Running
| # file: tests/test_download_game_settings.py | |
| import os | |
| import sys | |
| import pytest | |
| # Ensure the modules path is available | |
| sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) | |
| from battlewords.modules.constants import HF_API_TOKEN # <-- Import the token | |
| from battlewords.modules.storage import gen_full_url, _get_json_from_repo, HF_REPO_ID, SHORTENER_JSON_FILE | |
| # Ensure the token is set for Hugging Face Hub | |
| if HF_API_TOKEN: | |
| os.environ["HF_API_TOKEN"] = HF_API_TOKEN | |
| def test_download_settings_by_short_id_handles_both(capsys): | |
| # Use a fixed short id for testing | |
| short_id = "hDjsB_dl" | |
| # Step 1: Resolve short ID to full URL | |
| status, full_url = gen_full_url( | |
| short_url=short_id, | |
| repo_id=HF_REPO_ID, | |
| json_file=SHORTENER_JSON_FILE | |
| ) | |
| # Failure branch: provide a helpful message and assert expected failure shape | |
| if status != "success_retrieved_full" or not full_url: | |
| print( | |
| f"Could not resolve short id '{short_id}'. " | |
| f"Status: {status}. " | |
| f"Check repo '{HF_REPO_ID}' and mapping file '{SHORTENER_JSON_FILE}'." | |
| ) | |
| captured = capsys.readouterr() | |
| assert "Could not resolve short id" in captured.out | |
| # Ensure failure shape is consistent | |
| assert not full_url, "full_url should be empty/None on failure" | |
| print("settings.json was not found or could not be resolved.") | |
| return | |
| else: | |
| print(f"Resolved short id '{short_id}' to full URL: {full_url}") | |
| # Success branch | |
| assert status == "success_retrieved_full", f"Failed to resolve short ID: {status}" | |
| assert full_url, "No full URL returned" | |
| # Step 2: Extract file path from full URL | |
| url_parts = full_url.split("/resolve/main/") | |
| assert len(url_parts) == 2, f"Invalid full URL format: {full_url}" | |
| file_path = url_parts[1] | |
| # Step 3: Download and parse settings.json | |
| settings = _get_json_from_repo(HF_REPO_ID, file_path, repo_type="dataset") | |
| assert settings, "Failed to download or parse settings.json" | |
| print("\nDownloaded settings.json contents:", settings) | |
| # Optionally, add more assertions about the settings structure | |
| assert "challenge_id" in settings | |
| assert "wordlist_source" in settings | |
| assert "users" in settings | |
| if __name__ == "__main__": | |
| pytest.main(["-s", __file__]) |