#!/usr/bin/env python3 """ Simple test to verify token works directly """ import os import sys from pathlib import Path # Add the scripts directory to the path sys.path.append(str(Path(__file__).parent.parent / "scripts")) def test_token_direct(): """Test token validation directly""" print("šŸ” Testing Token Directly") print("=" * 50) # Test token from user test_token = "xxxx" print(f"Testing token directly: {'*' * 10}...{test_token[-4:]}") # Clear any existing environment variables if 'HF_TOKEN' in os.environ: del os.environ['HF_TOKEN'] if 'HUGGING_FACE_HUB_TOKEN' in os.environ: del os.environ['HUGGING_FACE_HUB_TOKEN'] # Import the validation function try: from validate_hf_token import validate_hf_token print("āœ… Token validation module imported successfully") except ImportError as e: print(f"āŒ Failed to import token validation module: {e}") return False # Test token validation try: success, username, error = validate_hf_token(test_token) if success: print(f"āœ… Token validation successful!") print(f"āœ… Username: {username}") return True else: print(f"āŒ Token validation failed: {error}") return False except Exception as e: print(f"āŒ Token validation error: {e}") return False def test_username_extraction_direct(): """Test username extraction directly""" print("\nšŸ” Testing Username Extraction Directly") print("=" * 50) # Test token from user test_token = "xxx" print(f"Testing username extraction directly: {'*' * 10}...{test_token[-4:]}") # Clear any existing environment variables if 'HF_TOKEN' in os.environ: del os.environ['HF_TOKEN'] if 'HUGGING_FACE_HUB_TOKEN' in os.environ: del os.environ['HUGGING_FACE_HUB_TOKEN'] # Import the username extraction function try: sys.path.append(str(Path(__file__).parent.parent / "scripts" / "dataset_tonic")) from setup_hf_dataset import get_username_from_token print("āœ… Username extraction module imported successfully") except ImportError as e: print(f"āŒ Failed to import username extraction module: {e}") return False # Test username extraction try: username = get_username_from_token(test_token) if username: print(f"āœ… Username extraction successful: {username}") return True else: print(f"āŒ Username extraction failed") return False except Exception as e: print(f"āŒ Username extraction error: {e}") return False def main(): """Run all direct token tests""" print("šŸš€ Direct Token Testing") print("=" * 50) tests = [ test_token_direct, test_username_extraction_direct ] all_passed = True for test in tests: try: if not test(): all_passed = False except Exception as e: print(f"āŒ Test failed with error: {e}") all_passed = False print("\n" + "=" * 50) if all_passed: print("šŸŽ‰ ALL DIRECT TOKEN TESTS PASSED!") print("āœ… Token validation: Working") print("āœ… Username extraction: Working") print("\nThe token works correctly when used directly!") else: print("āŒ SOME DIRECT TOKEN TESTS FAILED!") print("Please check the failed tests above.") return all_passed if __name__ == "__main__": success = main() sys.exit(0 if success else 1)