File size: 8,513 Bytes
14e9cd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75bcdb3
 
14e9cd5
 
75bcdb3
 
 
 
 
 
 
 
 
 
14e9cd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python3
"""
Test script to verify the latest Trackio Space deployment using HF Hub API
"""

import os
import sys
import tempfile
import shutil
from pathlib import Path

# Add project root to path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

def test_hf_hub_import():
    """Test that huggingface_hub can be imported"""
    print("πŸ” Testing huggingface_hub import...")
    
    try:
        from huggingface_hub import HfApi, create_repo
        print("βœ… huggingface_hub imported successfully")
        return True
    except ImportError as e:
        print(f"❌ Failed to import huggingface_hub: {e}")
        print("πŸ’‘ Install with: pip install huggingface_hub>=0.19.0")
        return False

def test_deployment_script_import():
    """Test that the deployment script can be imported"""
    print("\nπŸ” Testing deployment script import...")
    
    try:
        sys.path.insert(0, str(project_root / "scripts" / "trackio_tonic"))
        from deploy_trackio_space import TrackioSpaceDeployer
        
        # Test class instantiation
        deployer = TrackioSpaceDeployer("test-space", "test-user", "test-token")
        print("βœ… TrackioSpaceDeployer class imported successfully")
        
        # Test API availability
        if hasattr(deployer, 'api'):
            print("βœ… HF API initialized")
        else:
            print("⚠️  HF API not available (fallback to CLI)")
        
        return True
        
    except Exception as e:
        print(f"❌ Error testing deployment script: {e}")
        return False

def test_api_methods():
    """Test that the deployment script has the new API methods"""
    print("\nπŸ” Testing API methods...")
    
    try:
        sys.path.insert(0, str(project_root / "scripts" / "trackio_tonic"))
        from deploy_trackio_space import TrackioSpaceDeployer
        
        deployer = TrackioSpaceDeployer("test-space", "test-user", "test-token")
        
        # Test required methods exist
        required_methods = [
            "create_space",
            "_create_space_cli",
            "prepare_space_files", 
            "upload_files_to_space",
            "test_space",
            "deploy"
        ]
        
        for method in required_methods:
            if hasattr(deployer, method):
                print(f"βœ… Method exists: {method}")
            else:
                print(f"❌ Missing method: {method}")
                return False
        
        return True
        
    except Exception as e:
        print(f"❌ Error testing API methods: {e}")
        return False

def test_create_repo_api():
    """Test the create_repo API function"""
    print("\nπŸ” Testing create_repo API...")
    
    try:
        from huggingface_hub import create_repo
        
        # Test that the function exists and has the right parameters
        import inspect
        sig = inspect.signature(create_repo)
        
        # Check for required parameters
        required_params = ['repo_id', 'token']
        optional_params = ['repo_type', 'space_sdk', 'space_hardware']
        
        param_names = list(sig.parameters.keys())
        
        for param in required_params:
            if param in param_names:
                print(f"βœ… Required parameter: {param}")
            else:
                print(f"❌ Missing required parameter: {param}")
                return False
        
        for param in optional_params:
            if param in param_names:
                print(f"βœ… Optional parameter: {param}")
            else:
                print(f"⚠️  Optional parameter not found: {param}")
        
        print("βœ… create_repo API signature looks correct")
        return True
        
    except Exception as e:
        print(f"❌ Error testing create_repo API: {e}")
        return False

def test_space_creation_logic():
    """Test the space creation logic"""
    print("\nπŸ” Testing space creation logic...")
    
    try:
        sys.path.insert(0, str(project_root / "scripts" / "trackio_tonic"))
        from deploy_trackio_space import TrackioSpaceDeployer
        
        # Test with mock data
        deployer = TrackioSpaceDeployer("test-space", "test-user", "test-token")
        
        # Test that the space URL is correctly formatted
        expected_url = "https://huggingface.co/spaces/test-user/test-space"
        if deployer.space_url == expected_url:
            print("βœ… Space URL formatted correctly")
        else:
            print(f"❌ Space URL incorrect: {deployer.space_url}")
            return False
        
        # Test that the repo_id is correctly formatted
        repo_id = f"{deployer.username}/{deployer.space_name}"
        expected_repo_id = "test-user/test-space"
        if repo_id == expected_repo_id:
            print("βœ… Repo ID formatted correctly")
        else:
            print(f"❌ Repo ID incorrect: {repo_id}")
            return False
        
        return True
        
    except Exception as e:
        print(f"❌ Error testing space creation logic: {e}")
        return False

def test_template_files():
    """Test that all required template files exist"""
    print("\nπŸ” Testing template files...")
    
    spaces_dir = project_root / "templates" / "spaces"
    demo_types = ["demo_smol", "demo_gpt", "trackio"]
    required_files = ["app.py", "requirements.txt", "README.md"]
    
    for demo_type in demo_types:
        demo_dir = spaces_dir / demo_type
        print(f"Checking {demo_type} templates...")
        for file_name in required_files:
            file_path = demo_dir / file_name
            if file_path.exists():
                print(f"βœ… {demo_type}/{file_name} exists")
            else:
                print(f"❌ {demo_type}/{file_name} missing")
                return False
    
    return True

def test_temp_directory_handling():
    """Test temporary directory handling"""
    print("\nπŸ” Testing temporary directory handling...")
    
    try:
        import tempfile
        
        # Test temp directory creation
        temp_dir = tempfile.mkdtemp()
        print(f"βœ… Created temp directory: {temp_dir}")
        
        # Test file copying
        templates_dir = project_root / "templates" / "spaces"
        test_file = templates_dir / "app.py"
        
        if test_file.exists():
            dest_file = Path(temp_dir) / "app.py"
            shutil.copy2(test_file, dest_file)
            print("βœ… File copying works")
        else:
            print("❌ Source file not found")
            return False
        
        # Clean up
        shutil.rmtree(temp_dir)
        print("βœ… Cleanup successful")
        
        return True
        
    except Exception as e:
        print(f"❌ Error testing temp directory handling: {e}")
        return False

def main():
    """Run all deployment tests"""
    print("πŸš€ Testing Latest Trackio Space Deployment")
    print("=" * 55)
    
    tests = [
        test_hf_hub_import,
        test_deployment_script_import,
        test_api_methods,
        test_create_repo_api,
        test_space_creation_logic,
        test_template_files,
        test_temp_directory_handling
    ]
    
    passed = 0
    total = len(tests)
    
    for test in tests:
        try:
            if test():
                passed += 1
        except Exception as e:
            print(f"❌ Test {test.__name__} crashed: {e}")
    
    print(f"\nπŸ“Š Test Results: {passed}/{total} tests passed")
    
    if passed == total:
        print("βœ… All deployment tests passed! The latest deployment should work correctly.")
        print("\n🎯 Next steps:")
        print("1. Install latest huggingface_hub: pip install huggingface_hub>=0.19.0")
        print("2. Run the deployment script: python scripts/trackio_tonic/deploy_trackio_space.py")
        print("3. Provide your HF username, space name, and token")
        print("4. Wait for the Space to build (2-5 minutes)")
        print("5. Test the Space URL")
        return True
    else:
        print("❌ Some deployment tests failed. Please check the errors above.")
        print("\nπŸ’‘ Troubleshooting:")
        print("1. Install huggingface_hub: pip install huggingface_hub>=0.19.0")
        print("2. Check that all template files exist")
        print("3. Verify the deployment script structure")
        return False

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)