Upload tests/test_ledger.py
Browse files- tests/test_ledger.py +66 -0
tests/test_ledger.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Unit tests for CreditLedger."""
|
| 2 |
+
import sys
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
sys.path.insert(0, str(Path(__file__).parent.parent))
|
| 5 |
+
|
| 6 |
+
from ledger.ledger import CreditLedger
|
| 7 |
+
|
| 8 |
+
def test_earn_and_balance():
|
| 9 |
+
ledger = CreditLedger(decay_lambda=0.0)
|
| 10 |
+
ledger.earn("agent1", "task1", "action1", 10.0, 1.0, 100.0, "test", "model_call")
|
| 11 |
+
bal = ledger.balance("agent1", "model_call")
|
| 12 |
+
assert abs(bal - 10.0) < 0.01, f"Expected 10.0, got {bal}"
|
| 13 |
+
print("PASS: test_earn_and_balance")
|
| 14 |
+
|
| 15 |
+
def test_spend():
|
| 16 |
+
ledger = CreditLedger(decay_lambda=0.0)
|
| 17 |
+
ledger.earn("agent1", "task1", "action1", 10.0, 1.0, 100.0, "test")
|
| 18 |
+
ok = ledger.spend("agent1", "task2", "action2", 3.0)
|
| 19 |
+
assert ok
|
| 20 |
+
bal = ledger.balance("agent1")
|
| 21 |
+
assert abs(bal - 7.0) < 0.01, f"Expected 7.0, got {bal}"
|
| 22 |
+
print("PASS: test_spend")
|
| 23 |
+
|
| 24 |
+
def test_spend_insufficient():
|
| 25 |
+
ledger = CreditLedger(decay_lambda=0.0)
|
| 26 |
+
ledger.earn("agent1", "task1", "action1", 2.0, 1.0, 100.0, "test")
|
| 27 |
+
ok = ledger.spend("agent1", "task2", "action2", 5.0)
|
| 28 |
+
assert not ok
|
| 29 |
+
print("PASS: test_spend_insufficient")
|
| 30 |
+
|
| 31 |
+
def test_transfer_blocked():
|
| 32 |
+
ledger = CreditLedger(decay_lambda=0.0)
|
| 33 |
+
ledger.earn("alice", "task1", "action1", 10.0, 1.0, 100.0, "test")
|
| 34 |
+
ok = ledger.transfer("alice", "bob", 5.0)
|
| 35 |
+
assert not ok
|
| 36 |
+
assert ledger.balance("alice") > 9.0, "Credits should not be transferred"
|
| 37 |
+
assert ledger.balance("bob") < 0.1, "Bob should not receive credits"
|
| 38 |
+
print("PASS: test_transfer_blocked")
|
| 39 |
+
|
| 40 |
+
def test_decay():
|
| 41 |
+
ledger = CreditLedger(decay_lambda=0.1)
|
| 42 |
+
ledger.earn("agent1", "task1", "action1", 10.0, 1.0, 100.0, "test")
|
| 43 |
+
import time; time.sleep(0.05)
|
| 44 |
+
bal = ledger.balance("agent1")
|
| 45 |
+
assert bal < 10.0, f"Expected <10.0 after decay, got {bal}"
|
| 46 |
+
print("PASS: test_decay")
|
| 47 |
+
|
| 48 |
+
def test_capability_scope():
|
| 49 |
+
ledger = CreditLedger(decay_lambda=0.0)
|
| 50 |
+
ledger.earn("agent1", "t1", "a1", 10.0, 1.0, 100.0, "test", "retrieval")
|
| 51 |
+
ledger.earn("agent1", "t1", "a1", 5.0, 1.0, 100.0, "test", "model_call")
|
| 52 |
+
assert abs(ledger.balance("agent1", "retrieval") - 10.0) < 0.01
|
| 53 |
+
assert abs(ledger.balance("agent1", "model_call") - 5.0) < 0.01
|
| 54 |
+
print("PASS: test_capability_scope")
|
| 55 |
+
|
| 56 |
+
def run_all():
|
| 57 |
+
test_earn_and_balance()
|
| 58 |
+
test_spend()
|
| 59 |
+
test_spend_insufficient()
|
| 60 |
+
test_transfer_blocked()
|
| 61 |
+
test_decay()
|
| 62 |
+
test_capability_scope()
|
| 63 |
+
print("\nAll ledger tests passed!")
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
run_all()
|