Upload folder using huggingface_hub
Browse files- tests/test_subscription.py +144 -13
tests/test_subscription.py
CHANGED
|
@@ -1,12 +1,20 @@
|
|
| 1 |
-
"""Test subscription manager β $1/month subscription with free trial.
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import sys
|
| 4 |
import os
|
| 5 |
import tempfile
|
| 6 |
import time
|
|
|
|
|
|
|
| 7 |
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
| 8 |
|
| 9 |
-
from splitbit_llm.subscription import
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
def test_no_subscription_blocks_access():
|
|
@@ -50,7 +58,7 @@ def test_subscribe():
|
|
| 50 |
sub = SubscriptionManager(data_dir=tmpdir)
|
| 51 |
|
| 52 |
# Subscribe
|
| 53 |
-
result = sub.subscribe()
|
| 54 |
assert result["success"]
|
| 55 |
assert result["status"] == "active"
|
| 56 |
assert result["price"] == MONTHLY_PRICE
|
|
@@ -78,12 +86,12 @@ def test_subscribe_extends():
|
|
| 78 |
sub = SubscriptionManager(data_dir=tmpdir)
|
| 79 |
|
| 80 |
# First subscription
|
| 81 |
-
result1 = sub.subscribe()
|
| 82 |
assert result1["success"]
|
| 83 |
assert result1["days_remaining"] >= 29
|
| 84 |
|
| 85 |
# Second subscription should extend
|
| 86 |
-
result2 = sub.subscribe()
|
| 87 |
assert result2["success"]
|
| 88 |
assert result2["days_remaining"] >= 59 # 30 + 30
|
| 89 |
print(f" Extended: {result2['days_remaining']} days (30 + 30)")
|
|
@@ -98,7 +106,7 @@ def test_unsubscribe():
|
|
| 98 |
"""Test cancelling subscription."""
|
| 99 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 100 |
sub = SubscriptionManager(data_dir=tmpdir)
|
| 101 |
-
sub.subscribe()
|
| 102 |
|
| 103 |
# Unsubscribe
|
| 104 |
result = sub.unsubscribe()
|
|
@@ -118,7 +126,7 @@ def test_expired_blocks_access():
|
|
| 118 |
sub = SubscriptionManager(data_dir=tmpdir)
|
| 119 |
|
| 120 |
# Subscribe
|
| 121 |
-
sub.subscribe()
|
| 122 |
|
| 123 |
# Manually expire the subscription
|
| 124 |
sub._state["expiration_date"] = time.time() - 1 # expired 1 second ago
|
|
@@ -145,7 +153,7 @@ def test_auto_renew():
|
|
| 145 |
"""Test auto-renewal when subscription expires."""
|
| 146 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 147 |
sub = SubscriptionManager(data_dir=tmpdir)
|
| 148 |
-
sub.subscribe()
|
| 149 |
|
| 150 |
months_before = sub._state["months_subscribed"]
|
| 151 |
|
|
@@ -168,7 +176,7 @@ def test_trial_persistence():
|
|
| 168 |
"""Test that subscription state persists across restarts."""
|
| 169 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 170 |
sub1 = SubscriptionManager(data_dir=tmpdir)
|
| 171 |
-
sub1.subscribe()
|
| 172 |
|
| 173 |
# Create new instance β should load state
|
| 174 |
sub2 = SubscriptionManager(data_dir=tmpdir)
|
|
@@ -193,7 +201,7 @@ def test_trial_then_subscribe():
|
|
| 193 |
print(f" Trial: {trial['days_remaining']} days")
|
| 194 |
|
| 195 |
# Subscribe while on trial
|
| 196 |
-
result = sub.subscribe()
|
| 197 |
assert result["success"]
|
| 198 |
assert result["status"] == "active"
|
| 199 |
print(f" Subscribed during trial: {result['days_remaining']} days")
|
|
@@ -210,9 +218,9 @@ def test_payment_history():
|
|
| 210 |
sub = SubscriptionManager(data_dir=tmpdir)
|
| 211 |
|
| 212 |
# Multiple payments
|
| 213 |
-
sub.subscribe(payment_reference="stripe-001")
|
| 214 |
-
sub.subscribe(payment_reference="stripe-002")
|
| 215 |
-
sub.subscribe(payment_reference="stripe-003")
|
| 216 |
|
| 217 |
stats = sub.get_stats()
|
| 218 |
assert stats["months_subscribed"] == 3
|
|
@@ -222,6 +230,117 @@ def test_payment_history():
|
|
| 222 |
print(f" History: {len(stats['payment_history'])} records")
|
| 223 |
|
| 224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
if __name__ == "__main__":
|
| 226 |
print("Running subscription manager tests...")
|
| 227 |
test_no_subscription_blocks_access()
|
|
@@ -244,4 +363,16 @@ if __name__ == "__main__":
|
|
| 244 |
print(" β test_trial_then_subscribe")
|
| 245 |
test_payment_history()
|
| 246 |
print(" β test_payment_history")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
print("\nAll subscription tests passed!")
|
|
|
|
| 1 |
+
"""Test subscription manager β $1/month subscription with free trial.
|
| 2 |
+
|
| 3 |
+
Payment integration with Soulmate OS wallet API.
|
| 4 |
+
"""
|
| 5 |
|
| 6 |
import sys
|
| 7 |
import os
|
| 8 |
import tempfile
|
| 9 |
import time
|
| 10 |
+
import json
|
| 11 |
+
from unittest.mock import patch, MagicMock
|
| 12 |
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
| 13 |
|
| 14 |
+
from splitbit_llm.subscription import (
|
| 15 |
+
SubscriptionManager, MONTHLY_PRICE, SOULMATE_API_URL,
|
| 16 |
+
FOUNDER_EMAIL, ACCEPTED_TOKENS, NETWORK,
|
| 17 |
+
)
|
| 18 |
|
| 19 |
|
| 20 |
def test_no_subscription_blocks_access():
|
|
|
|
| 58 |
sub = SubscriptionManager(data_dir=tmpdir)
|
| 59 |
|
| 60 |
# Subscribe
|
| 61 |
+
result = sub.subscribe(create_deposit=False)
|
| 62 |
assert result["success"]
|
| 63 |
assert result["status"] == "active"
|
| 64 |
assert result["price"] == MONTHLY_PRICE
|
|
|
|
| 86 |
sub = SubscriptionManager(data_dir=tmpdir)
|
| 87 |
|
| 88 |
# First subscription
|
| 89 |
+
result1 = sub.subscribe(create_deposit=False)
|
| 90 |
assert result1["success"]
|
| 91 |
assert result1["days_remaining"] >= 29
|
| 92 |
|
| 93 |
# Second subscription should extend
|
| 94 |
+
result2 = sub.subscribe(create_deposit=False)
|
| 95 |
assert result2["success"]
|
| 96 |
assert result2["days_remaining"] >= 59 # 30 + 30
|
| 97 |
print(f" Extended: {result2['days_remaining']} days (30 + 30)")
|
|
|
|
| 106 |
"""Test cancelling subscription."""
|
| 107 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 108 |
sub = SubscriptionManager(data_dir=tmpdir)
|
| 109 |
+
sub.subscribe(create_deposit=False)
|
| 110 |
|
| 111 |
# Unsubscribe
|
| 112 |
result = sub.unsubscribe()
|
|
|
|
| 126 |
sub = SubscriptionManager(data_dir=tmpdir)
|
| 127 |
|
| 128 |
# Subscribe
|
| 129 |
+
sub.subscribe(create_deposit=False)
|
| 130 |
|
| 131 |
# Manually expire the subscription
|
| 132 |
sub._state["expiration_date"] = time.time() - 1 # expired 1 second ago
|
|
|
|
| 153 |
"""Test auto-renewal when subscription expires."""
|
| 154 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 155 |
sub = SubscriptionManager(data_dir=tmpdir)
|
| 156 |
+
sub.subscribe(create_deposit=False)
|
| 157 |
|
| 158 |
months_before = sub._state["months_subscribed"]
|
| 159 |
|
|
|
|
| 176 |
"""Test that subscription state persists across restarts."""
|
| 177 |
with tempfile.TemporaryDirectory() as tmpdir:
|
| 178 |
sub1 = SubscriptionManager(data_dir=tmpdir)
|
| 179 |
+
sub1.subscribe(create_deposit=False)
|
| 180 |
|
| 181 |
# Create new instance β should load state
|
| 182 |
sub2 = SubscriptionManager(data_dir=tmpdir)
|
|
|
|
| 201 |
print(f" Trial: {trial['days_remaining']} days")
|
| 202 |
|
| 203 |
# Subscribe while on trial
|
| 204 |
+
result = sub.subscribe(create_deposit=False)
|
| 205 |
assert result["success"]
|
| 206 |
assert result["status"] == "active"
|
| 207 |
print(f" Subscribed during trial: {result['days_remaining']} days")
|
|
|
|
| 218 |
sub = SubscriptionManager(data_dir=tmpdir)
|
| 219 |
|
| 220 |
# Multiple payments
|
| 221 |
+
sub.subscribe(payment_reference="stripe-001", create_deposit=False)
|
| 222 |
+
sub.subscribe(payment_reference="stripe-002", create_deposit=False)
|
| 223 |
+
sub.subscribe(payment_reference="stripe-003", create_deposit=False)
|
| 224 |
|
| 225 |
stats = sub.get_stats()
|
| 226 |
assert stats["months_subscribed"] == 3
|
|
|
|
| 230 |
print(f" History: {len(stats['payment_history'])} records")
|
| 231 |
|
| 232 |
|
| 233 |
+
def test_payment_instructions():
|
| 234 |
+
"""Test payment instructions are returned correctly."""
|
| 235 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 236 |
+
sub = SubscriptionManager(data_dir=tmpdir)
|
| 237 |
+
# Set a fake wallet in state so we don't hit the API
|
| 238 |
+
sub._state["founder_wallet"] = "0xABC123DEF456"
|
| 239 |
+
sub._founder_wallet = "0xABC123DEF456"
|
| 240 |
+
|
| 241 |
+
instructions = sub.get_payment_instructions()
|
| 242 |
+
assert instructions["amount"] == MONTHLY_PRICE
|
| 243 |
+
assert instructions["currency"] == "USD"
|
| 244 |
+
assert instructions["period"] == "monthly"
|
| 245 |
+
assert instructions["founder_wallet"] == "0xABC123DEF456"
|
| 246 |
+
assert instructions["network"] == NETWORK
|
| 247 |
+
assert "USDT" in instructions["accepted_tokens"]
|
| 248 |
+
assert "USDC" in instructions["accepted_tokens"]
|
| 249 |
+
assert "BNB" in instructions["accepted_tokens"]
|
| 250 |
+
assert "INC" in instructions["accepted_tokens"]
|
| 251 |
+
assert "soulmate_wallet_url" in instructions
|
| 252 |
+
assert SOULMATE_API_URL in instructions["soulmate_wallet_url"]
|
| 253 |
+
print(f" Amount: ${instructions['amount']:.2f}")
|
| 254 |
+
print(f" Wallet: {instructions['founder_wallet']}")
|
| 255 |
+
print(f" Network: {instructions['network']}")
|
| 256 |
+
print(f" Tokens: {instructions['accepted_tokens']}")
|
| 257 |
+
print(f" Google Pay URL: {instructions['soulmate_wallet_url']}")
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
def test_payment_instructions_with_wallet():
|
| 261 |
+
"""Test payment instructions include wallet from state."""
|
| 262 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 263 |
+
sub = SubscriptionManager(data_dir=tmpdir)
|
| 264 |
+
sub._state["founder_wallet"] = "0xDEADBEEF1234"
|
| 265 |
+
sub._founder_wallet = "0xDEADBEEF1234"
|
| 266 |
+
|
| 267 |
+
instructions = sub.get_payment_instructions()
|
| 268 |
+
assert instructions["founder_wallet"] == "0xDEADBEEF1234"
|
| 269 |
+
assert "0xDEADBEEF1234" in instructions["instructions"]
|
| 270 |
+
print(f" Wallet in instructions: {instructions['founder_wallet']}")
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
def test_create_deposit():
|
| 274 |
+
"""Test deposit creation (mocked API)."""
|
| 275 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 276 |
+
sub = SubscriptionManager(data_dir=tmpdir)
|
| 277 |
+
sub._state["founder_wallet"] = "0xTEST123"
|
| 278 |
+
sub._founder_wallet = "0xTEST123"
|
| 279 |
+
|
| 280 |
+
# Mock the API call β deposit creation will fall back to pending
|
| 281 |
+
result = sub.create_deposit("test-user", "USDT")
|
| 282 |
+
assert "deposit_id" in result
|
| 283 |
+
assert result["founder_wallet"] == "0xTEST123"
|
| 284 |
+
assert result["token"] == "USDT"
|
| 285 |
+
assert result["amount"] == MONTHLY_PRICE
|
| 286 |
+
print(f" Deposit ID: {result['deposit_id'][:16]}...")
|
| 287 |
+
print(f" Status: {result.get('status', 'pending')}")
|
| 288 |
+
print(f" Wallet: {result['founder_wallet']}")
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
def test_verify_payment():
|
| 292 |
+
"""Test payment verification (mocked API β will return error since no real API)."""
|
| 293 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 294 |
+
sub = SubscriptionManager(data_dir=tmpdir)
|
| 295 |
+
|
| 296 |
+
# Verify a non-existent deposit β should return some status
|
| 297 |
+
result = sub.verify_payment("fake-deposit-123")
|
| 298 |
+
assert isinstance(result, dict)
|
| 299 |
+
# Without a real API, it'll be "error", "not_found", or have some other status
|
| 300 |
+
print(f" Verify result: {result.get('status', 'unknown')} β '{result.get('message', '')}'")
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
def test_subscribe_with_deposit():
|
| 304 |
+
"""Test subscribing with deposit creation."""
|
| 305 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 306 |
+
sub = SubscriptionManager(data_dir=tmpdir)
|
| 307 |
+
sub._state["founder_wallet"] = "0xWALLET456"
|
| 308 |
+
sub._founder_wallet = "0xWALLET456"
|
| 309 |
+
|
| 310 |
+
result = sub.subscribe(user_id="test-user", token="USDT")
|
| 311 |
+
assert result["success"]
|
| 312 |
+
assert result["status"] == "active"
|
| 313 |
+
assert "deposit_id" in result
|
| 314 |
+
assert result["founder_wallet"] == "0xWALLET456"
|
| 315 |
+
assert "payment" in result
|
| 316 |
+
assert result["payment"]["founder_wallet"] == "0xWALLET456"
|
| 317 |
+
print(f" Subscribed with deposit: {result['deposit_id'][:16]}...")
|
| 318 |
+
print(f" Payment instructions included: {bool(result.get('payment'))}")
|
| 319 |
+
|
| 320 |
+
# Check payment history has deposit info
|
| 321 |
+
stats = sub.get_stats()
|
| 322 |
+
last_payment = stats["payment_history"][-1]
|
| 323 |
+
assert last_payment["token"] == "USDT"
|
| 324 |
+
assert last_payment["deposit_id"] == result["deposit_id"]
|
| 325 |
+
assert "0xWALLET456" in last_payment["founder_wallet"] or last_payment["founder_wallet"].startswith("0xWALLET")
|
| 326 |
+
print(f" Payment history: token={last_payment['token']}, deposit={last_payment['deposit_id'][:16]}...")
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
def test_soulmate_config():
|
| 330 |
+
"""Test Soulmate OS config constants are set correctly."""
|
| 331 |
+
assert SOULMATE_API_URL == "https://191.44.121.29.sslip.io"
|
| 332 |
+
assert FOUNDER_EMAIL == "hawpetossjustin25@gmail.com"
|
| 333 |
+
assert "USDT" in ACCEPTED_TOKENS
|
| 334 |
+
assert "USDC" in ACCEPTED_TOKENS
|
| 335 |
+
assert "BNB" in ACCEPTED_TOKENS
|
| 336 |
+
assert "INC" in ACCEPTED_TOKENS
|
| 337 |
+
assert NETWORK == "BSC (Binance Smart Chain)"
|
| 338 |
+
print(f" API URL: {SOULMATE_API_URL}")
|
| 339 |
+
print(f" Founder: {FOUNDER_EMAIL}")
|
| 340 |
+
print(f" Tokens: {ACCEPTED_TOKENS}")
|
| 341 |
+
print(f" Network: {NETWORK}")
|
| 342 |
+
|
| 343 |
+
|
| 344 |
if __name__ == "__main__":
|
| 345 |
print("Running subscription manager tests...")
|
| 346 |
test_no_subscription_blocks_access()
|
|
|
|
| 363 |
print(" β test_trial_then_subscribe")
|
| 364 |
test_payment_history()
|
| 365 |
print(" β test_payment_history")
|
| 366 |
+
test_payment_instructions()
|
| 367 |
+
print(" β test_payment_instructions")
|
| 368 |
+
test_payment_instructions_with_wallet()
|
| 369 |
+
print(" β test_payment_instructions_with_wallet")
|
| 370 |
+
test_create_deposit()
|
| 371 |
+
print(" β test_create_deposit")
|
| 372 |
+
test_verify_payment()
|
| 373 |
+
print(" β test_verify_payment")
|
| 374 |
+
test_subscribe_with_deposit()
|
| 375 |
+
print(" β test_subscribe_with_deposit")
|
| 376 |
+
test_soulmate_config()
|
| 377 |
+
print(" β test_soulmate_config")
|
| 378 |
print("\nAll subscription tests passed!")
|