File size: 1,944 Bytes
507acad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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.storage import gen_full_url, _get_json_from_repo, HF_REPO_ID, SHORTENER_JSON_FILE

def test_download_settings_by_short_id_handles_both(capsys):
    # Use a fixed short id for testing
    short_id = "hDjsB_dl1"

    # 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"
        return

    # 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("Downloaded settings.json:", settings)
    # Optionally, add more assertions about the settings structure
    assert "uid" in settings
    assert "word_list" in settings
    assert "score" in settings