File size: 3,629 Bytes
1c1abed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""Offline boundary checks for upload integrity, retries, and publication scope."""
import importlib.util
import json
from pathlib import Path
from types import SimpleNamespace
import sys

import pytest

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
spec = importlib.util.spec_from_file_location("axiomesh_hf_upload", ROOT / "upload_hf.py")
uploader = importlib.util.module_from_spec(spec)
spec.loader.exec_module(uploader)
from validate_release import check_manifest, REQUIRED


class ServerFailure(Exception):
    def __init__(self, code):
        self.response = SimpleNamespace(status_code=code)


def test_changed_file_is_rejected_before_any_network_call(tmp_path):
    files = {name: "0" * 64 for name in REQUIRED}
    (tmp_path / "MANIFEST.json").write_text(json.dumps({"algorithm": "sha256", "files": files}))
    for name in REQUIRED:
        path = tmp_path / name
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_text("changed")
    with pytest.raises(ValueError, match="hash mismatch"):
        check_manifest(tmp_path)


def test_transient_retry_is_bounded_and_succeeds():
    calls, delays = [], []
    def operation():
        calls.append(1)
        if len(calls) < 3:
            raise ServerFailure(503)
        return "committed"
    assert uploader.retry(operation, 3, "test", delays.append) == "committed"
    assert len(calls) == 3 and delays == [1, 2]


def test_authentication_error_is_not_retried():
    calls = []
    def operation():
        calls.append(1)
        raise ServerFailure(403)
    with pytest.raises(ServerFailure):
        uploader.retry(operation, 3, "test", lambda _: pytest.fail("must not wait"))
    assert len(calls) == 1


def test_repeated_server_error_stops_at_attempt_limit():
    calls = []
    def operation():
        calls.append(1)
        raise ServerFailure(500)
    with pytest.raises(ServerFailure):
        uploader.retry(operation, 2, "test", lambda _: None)
    assert len(calls) == 2


def test_upload_uses_only_explicit_paths_and_never_deletes():
    class FakeAPI:
        def __init__(self): self.operations = []
        def create_repo(self, **kwargs): self.operations.append(("create", kwargs))
        def upload_folder(self, **kwargs):
            self.operations.append(("upload", kwargs))
            return SimpleNamespace(oid="offline-test-commit")
    api = FakeAPI()
    args = SimpleNamespace(repo_type="dataset", private=False, max_attempts=1, commit_message="test")
    result = uploader.upload(api, "example/FNE-AXIOMESH", args, ["README.md", "MANIFEST.json"])
    assert result.oid == "offline-test-commit"
    request = api.operations[1][1]
    assert api.operations[0][1]["repo_type"] == "dataset"
    assert request["repo_type"] == "dataset"
    assert request["allow_patterns"] == ["README.md", "MANIFEST.json"]
    assert "delete_patterns" not in request and "delete" not in request


def test_dry_run_needs_neither_client_nor_account(monkeypatch, capsys):
    monkeypatch.setattr(uploader, "upload_paths", lambda: ["README.md"])
    monkeypatch.setattr(uploader, "upload", lambda *args: pytest.fail("network action in dry-run"))
    assert uploader.main(["--dry-run", "--repo-id", "example/FNE-AXIOMESH"]) == 0
    assert "No network calls" in capsys.readouterr().out


def test_repository_type_defaults_to_dataset():
    assert uploader.parse_args([]).repo_type == "dataset"


@pytest.mark.parametrize("invalid", ["name", "../name", "a/../b", "a/b--c", "a/b.git"])
def test_invalid_repository_id_is_rejected(invalid):
    with pytest.raises(ValueError):
        uploader.repo_id(invalid)