81 lines
3.6 KiB
Python
81 lines
3.6 KiB
Python
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from coffer.ledger import InsufficientCredit, TransferNotAllowed
|
|
from coffer.product import CofferProduct, ProductError
|
|
from coffer.public_ledger import tx_public
|
|
from coffer.units import human_account
|
|
|
|
|
|
class PeopleTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.db = Path(self.tmp.name) / "coffer.db"
|
|
self.p = CofferProduct(self.db)
|
|
self.p.bootstrap_opening_books()
|
|
self.p.record_build_contract()
|
|
self.p.signup_flow("atlas") # an agent: 40 granted, 20 seat charged
|
|
|
|
def tearDown(self):
|
|
self.p.close()
|
|
self.tmp.cleanup()
|
|
|
|
def test_human_signup_grant_once(self):
|
|
r = self.p.human_signup("sam")
|
|
self.assertEqual(r["wallet"]["account_id"], "human:sam")
|
|
self.assertEqual(r["balance"]["promotional"], 40.0)
|
|
self.assertTrue(self.p.human_signup("sam")["grant"]["already"])
|
|
self.assertEqual(self.p.balance("human:sam")["spendable"], 40.0)
|
|
with self.assertRaises(ValueError):
|
|
human_account("Sam Smith <sam@example.com>")
|
|
|
|
def test_no_purchase_api(self):
|
|
self.assertFalse(hasattr(self.p, "buy_credits"))
|
|
|
|
def test_human_pays_agent_with_granted_credit(self):
|
|
self.p.human_signup("sam")
|
|
before = self.p.balance("agent:atlas")
|
|
r = self.p.pay("human:sam", "agent:atlas", 12.5, reason="Research brief", ref="of-1")
|
|
self.assertEqual(r["tx"]["kind"], "payment")
|
|
self.assertEqual(self.p.balance("human:sam")["spendable"], 27.5)
|
|
after = self.p.balance("agent:atlas")
|
|
self.assertAlmostEqual(after["promotional"] - before["promotional"], 12.5) # kind preserved
|
|
self.assertTrue(self.p.pay("human:sam", "agent:atlas", 12.5, reason="x", ref="of-1")["duplicate"])
|
|
self.p.close()
|
|
self.p = CofferProduct(self.db) # survives a reload
|
|
self.assertEqual(self.p.balance("human:sam")["spendable"], 27.5)
|
|
self.assertTrue(self.p.pay("human:sam", "agent:atlas", 12.5, reason="x", ref="of-1")["duplicate"])
|
|
|
|
def test_agent_pays_human_and_people_pay_each_other(self):
|
|
self.p.human_signup("sam")
|
|
self.p.human_signup("kim")
|
|
self.p.pay("agent:atlas", "human:sam", 5, reason="bounty", ref="of-2")
|
|
self.assertEqual(self.p.balance("human:sam")["spendable"], 45.0)
|
|
self.p.pay("human:sam", "human:kim", 10, reason="thanks", ref="of-3")
|
|
self.assertEqual(self.p.balance("human:kim")["spendable"], 50.0)
|
|
self.assertEqual(self.p.balance("human:sam")["spendable"], 35.0)
|
|
|
|
def test_rules(self):
|
|
self.p.human_signup("sam")
|
|
self.p.signup_flow("bolt")
|
|
with self.assertRaises(TransferNotAllowed):
|
|
self.p.pay("agent:atlas", "agent:bolt", 1, reason="x", ref="of-4")
|
|
with self.assertRaises(TransferNotAllowed):
|
|
self.p.pay("human:sam", "company:coffer", 1, reason="x", ref="of-5")
|
|
with self.assertRaises(ProductError):
|
|
self.p.pay("human:sam", "agent:atlas", 5000, reason="x", ref="of-6")
|
|
with self.assertRaises(InsufficientCredit):
|
|
self.p.pay("human:sam", "agent:atlas", 41, reason="x", ref="of-7")
|
|
|
|
def test_public_ledger_shows_handles_only(self):
|
|
self.p.human_signup("sam")
|
|
self.p.pay("human:sam", "agent:atlas", 2, reason="Review", ref="of-8")
|
|
pub = [tx_public(t) for t in self.p.history()]
|
|
self.assertIn("human:sam", repr(pub))
|
|
self.assertEqual(next(t for t in pub if t["kind"] == "payment")["meta"].get("offer"), "of-8")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|