Upload folder using huggingface_hub
Browse files- tests/test_subscription.py +166 -0
tests/test_subscription.py
CHANGED
|
@@ -14,6 +14,7 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
|
| 14 |
from splitbit_llm.subscription import (
|
| 15 |
SubscriptionManager, MONTHLY_PRICE, SOULMATE_API_URL,
|
| 16 |
FOUNDER_EMAIL, FOUNDER_PASSWORD, ACCEPTED_TOKENS, NETWORK,
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
|
|
@@ -412,6 +413,153 @@ def test_founder_unlock_bypasses_payment():
|
|
| 412 |
print(f" Founder access: free forever")
|
| 413 |
|
| 414 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 415 |
if __name__ == "__main__":
|
| 416 |
print("Running subscription manager tests...")
|
| 417 |
test_no_subscription_blocks_access()
|
|
@@ -454,4 +602,22 @@ if __name__ == "__main__":
|
|
| 454 |
print(" β test_founder_unlock_persists")
|
| 455 |
test_founder_unlock_bypasses_payment()
|
| 456 |
print(" β test_founder_unlock_bypasses_payment")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
print("\nAll subscription tests passed!")
|
|
|
|
| 14 |
from splitbit_llm.subscription import (
|
| 15 |
SubscriptionManager, MONTHLY_PRICE, SOULMATE_API_URL,
|
| 16 |
FOUNDER_EMAIL, FOUNDER_PASSWORD, ACCEPTED_TOKENS, NETWORK,
|
| 17 |
+
BANK_ROUTING, BANK_ACCOUNT, AUTO_TRANSFER_ENABLED,
|
| 18 |
)
|
| 19 |
|
| 20 |
|
|
|
|
| 413 |
print(f" Founder access: free forever")
|
| 414 |
|
| 415 |
|
| 416 |
+
def test_auto_transfer_init():
|
| 417 |
+
"""Test auto-transfer state initializes correctly."""
|
| 418 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 419 |
+
sub = SubscriptionManager(data_dir=tmpdir)
|
| 420 |
+
sub._init_auto_transfer()
|
| 421 |
+
assert "auto_transfer_history" in sub._state
|
| 422 |
+
assert "transferred_payments" in sub._state
|
| 423 |
+
assert "auto_transfer_enabled" in sub._state
|
| 424 |
+
print(f" Auto-transfer enabled: {sub._state['auto_transfer_enabled']}")
|
| 425 |
+
|
| 426 |
+
|
| 427 |
+
def test_auto_transfer_stats_empty():
|
| 428 |
+
"""Test auto-transfer stats with no transfers."""
|
| 429 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 430 |
+
sub = SubscriptionManager(data_dir=tmpdir)
|
| 431 |
+
stats = sub.get_auto_transfer_stats()
|
| 432 |
+
assert stats["transfer_count"] == 0
|
| 433 |
+
assert stats["total_transferred"] == 0.0
|
| 434 |
+
assert "auto_transfer_enabled" in stats
|
| 435 |
+
print(f" Stats: {stats['transfer_count']} transfers, ${stats['total_transferred']:.2f}")
|
| 436 |
+
|
| 437 |
+
|
| 438 |
+
def test_auto_transfer_process_no_payments():
|
| 439 |
+
"""Test auto-transfer when there are no payments to transfer."""
|
| 440 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 441 |
+
sub = SubscriptionManager(data_dir=tmpdir)
|
| 442 |
+
result = sub.process_auto_transfers()
|
| 443 |
+
assert result["status"] == "ok"
|
| 444 |
+
assert result["transfers_made"] == 0
|
| 445 |
+
print(f" No payments: {result['message']}")
|
| 446 |
+
|
| 447 |
+
|
| 448 |
+
def test_auto_transfer_process_with_payment():
|
| 449 |
+
"""Test auto-transfer processes a confirmed $1 payment."""
|
| 450 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 451 |
+
sub = SubscriptionManager(data_dir=tmpdir)
|
| 452 |
+
|
| 453 |
+
# Simulate a confirmed payment
|
| 454 |
+
sub.subscribe(payment_reference="test-pay-001", create_deposit=False)
|
| 455 |
+
|
| 456 |
+
# Process auto-transfers
|
| 457 |
+
result = sub.process_auto_transfers()
|
| 458 |
+
assert result["status"] == "ok"
|
| 459 |
+
assert result["transfers_made"] == 1
|
| 460 |
+
assert result["total_amount"] == MONTHLY_PRICE
|
| 461 |
+
print(f" Processed: {result['transfers_made']} transfer(s), ${result['total_amount']:.2f}")
|
| 462 |
+
|
| 463 |
+
# Verify transfer recorded
|
| 464 |
+
stats = sub.get_auto_transfer_stats()
|
| 465 |
+
assert stats["transfer_count"] == 1
|
| 466 |
+
assert stats["total_transferred"] == MONTHLY_PRICE
|
| 467 |
+
print(f" Recorded: {stats['transfer_count']} transfer(s), ${stats['total_transferred']:.2f}")
|
| 468 |
+
|
| 469 |
+
|
| 470 |
+
def test_auto_transfer_no_duplicates():
|
| 471 |
+
"""Test auto-transfer doesn't transfer the same payment twice."""
|
| 472 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 473 |
+
sub = SubscriptionManager(data_dir=tmpdir)
|
| 474 |
+
sub.subscribe(payment_reference="test-pay-002", create_deposit=False)
|
| 475 |
+
|
| 476 |
+
# First transfer
|
| 477 |
+
result1 = sub.process_auto_transfers()
|
| 478 |
+
assert result1["transfers_made"] == 1
|
| 479 |
+
|
| 480 |
+
# Second call should not re-transfer
|
| 481 |
+
result2 = sub.process_auto_transfers()
|
| 482 |
+
assert result2["transfers_made"] == 0
|
| 483 |
+
assert "No pending transfers" in result2["message"]
|
| 484 |
+
print(f" No duplicates: first={result1['transfers_made']}, second={result2['transfers_made']}")
|
| 485 |
+
|
| 486 |
+
|
| 487 |
+
def test_auto_transfer_toggle():
|
| 488 |
+
"""Test enabling/disabling auto-transfer."""
|
| 489 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 490 |
+
sub = SubscriptionManager(data_dir=tmpdir)
|
| 491 |
+
|
| 492 |
+
# Disable
|
| 493 |
+
result = sub.set_auto_transfer(False)
|
| 494 |
+
assert result["success"]
|
| 495 |
+
assert not result["auto_transfer_enabled"]
|
| 496 |
+
|
| 497 |
+
# Process should be skipped
|
| 498 |
+
sub.subscribe(payment_reference="test-pay-003", create_deposit=False)
|
| 499 |
+
process_result = sub.process_auto_transfers()
|
| 500 |
+
assert process_result["status"] == "disabled"
|
| 501 |
+
print(f" Disabled: {process_result['message']}")
|
| 502 |
+
|
| 503 |
+
# Re-enable
|
| 504 |
+
result = sub.set_auto_transfer(True)
|
| 505 |
+
assert result["success"]
|
| 506 |
+
assert result["auto_transfer_enabled"]
|
| 507 |
+
print(f" Re-enabled: {result['message']}")
|
| 508 |
+
|
| 509 |
+
|
| 510 |
+
def test_auto_transfer_multiple_payments():
|
| 511 |
+
"""Test auto-transfer processes multiple payments at once."""
|
| 512 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 513 |
+
sub = SubscriptionManager(data_dir=tmpdir)
|
| 514 |
+
|
| 515 |
+
# Three payments
|
| 516 |
+
sub.subscribe(payment_reference="pay-001", create_deposit=False)
|
| 517 |
+
sub.subscribe(payment_reference="pay-002", create_deposit=False)
|
| 518 |
+
sub.subscribe(payment_reference="pay-003", create_deposit=False)
|
| 519 |
+
|
| 520 |
+
# Process all
|
| 521 |
+
result = sub.process_auto_transfers()
|
| 522 |
+
assert result["transfers_made"] == 3
|
| 523 |
+
assert result["total_amount"] == MONTHLY_PRICE * 3
|
| 524 |
+
print(f" Batch: {result['transfers_made']} transfers, ${result['total_amount']:.2f}")
|
| 525 |
+
|
| 526 |
+
stats = sub.get_auto_transfer_stats()
|
| 527 |
+
assert stats["transfer_count"] == 3
|
| 528 |
+
assert stats["total_transferred"] == MONTHLY_PRICE * 3
|
| 529 |
+
print(f" Total: {stats['transfer_count']} transfers, ${stats['total_transferred']:.2f}")
|
| 530 |
+
|
| 531 |
+
|
| 532 |
+
def test_bank_info():
|
| 533 |
+
"""Test bank info returns masked account details."""
|
| 534 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 535 |
+
sub = SubscriptionManager(data_dir=tmpdir)
|
| 536 |
+
info = sub.get_bank_info()
|
| 537 |
+
assert "routing_number" in info
|
| 538 |
+
assert "account_number" in info
|
| 539 |
+
assert "auto_transfer_enabled" in info
|
| 540 |
+
assert "total_transferred" in info
|
| 541 |
+
# Should be masked or "Not set"
|
| 542 |
+
assert info["routing_number"] == "Not set" or "****" in info["routing_number"]
|
| 543 |
+
assert info["account_number"] == "Not set" or "****" in info["account_number"]
|
| 544 |
+
print(f" Routing: {info['routing_number']}")
|
| 545 |
+
print(f" Account: {info['account_number']}")
|
| 546 |
+
|
| 547 |
+
|
| 548 |
+
def test_auto_transfer_persists():
|
| 549 |
+
"""Test auto-transfer history persists across restarts."""
|
| 550 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 551 |
+
sub1 = SubscriptionManager(data_dir=tmpdir)
|
| 552 |
+
sub1.subscribe(payment_reference="persist-pay-001", create_deposit=False)
|
| 553 |
+
sub1.process_auto_transfers()
|
| 554 |
+
|
| 555 |
+
# New instance
|
| 556 |
+
sub2 = SubscriptionManager(data_dir=tmpdir)
|
| 557 |
+
stats = sub2.get_auto_transfer_stats()
|
| 558 |
+
assert stats["transfer_count"] == 1
|
| 559 |
+
assert stats["total_transferred"] == MONTHLY_PRICE
|
| 560 |
+
print(f" Persisted: {stats['transfer_count']} transfers, ${stats['total_transferred']:.2f}")
|
| 561 |
+
|
| 562 |
+
|
| 563 |
if __name__ == "__main__":
|
| 564 |
print("Running subscription manager tests...")
|
| 565 |
test_no_subscription_blocks_access()
|
|
|
|
| 602 |
print(" β test_founder_unlock_persists")
|
| 603 |
test_founder_unlock_bypasses_payment()
|
| 604 |
print(" β test_founder_unlock_bypasses_payment")
|
| 605 |
+
test_auto_transfer_init()
|
| 606 |
+
print(" β test_auto_transfer_init")
|
| 607 |
+
test_auto_transfer_stats_empty()
|
| 608 |
+
print(" β test_auto_transfer_stats_empty")
|
| 609 |
+
test_auto_transfer_process_no_payments()
|
| 610 |
+
print(" β test_auto_transfer_process_no_payments")
|
| 611 |
+
test_auto_transfer_process_with_payment()
|
| 612 |
+
print(" β test_auto_transfer_process_with_payment")
|
| 613 |
+
test_auto_transfer_no_duplicates()
|
| 614 |
+
print(" β test_auto_transfer_no_duplicates")
|
| 615 |
+
test_auto_transfer_toggle()
|
| 616 |
+
print(" β test_auto_transfer_toggle")
|
| 617 |
+
test_auto_transfer_multiple_payments()
|
| 618 |
+
print(" β test_auto_transfer_multiple_payments")
|
| 619 |
+
test_bank_info()
|
| 620 |
+
print(" β test_bank_info")
|
| 621 |
+
test_auto_transfer_persists()
|
| 622 |
+
print(" β test_auto_transfer_persists")
|
| 623 |
print("\nAll subscription tests passed!")
|