import os import tempfile import unittest from pathlib import Path from coffer.ledger import 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 promotional, -20 seat def tearDown(self): self.p.close() self.tmp.cleanup() def test_human_wallet_starts_empty(self): w = self.p.human_wallet("sam") self.assertEqual(w["account_id"], "human:sam") self.assertEqual(w["balance"]["spendable"], 0.0) with self.assertRaises(ValueError): human_account("Sam Smith ") def test_buy_test_credits_is_idempotent(self): r = self.p.buy_credits("human:sam", 50, payment_ref="test_pi_1") self.assertEqual(r["balance"]["funded"], 50.0) self.assertEqual(r["tx"]["kind"], "purchase") self.assertEqual(r["tx"]["meta"]["payment"], "test") again = self.p.buy_credits("human:sam", 50, payment_ref="test_pi_1") self.assertTrue(again["duplicate"]) self.assertEqual(again["balance"]["funded"], 50.0) with self.assertRaises(ProductError): self.p.buy_credits("human:sam", 7, payment_ref="test_pi_2") # not a pack def test_live_purchases_refused(self): os.environ.pop("COFFER_LIVE_PAYMENTS", None) with self.assertRaises(ProductError): self.p.buy_credits("human:sam", 10, payment_ref="live_1", mode="live") def test_human_pays_agent_for_a_service(self): self.p.buy_credits("human:sam", 50, payment_ref="test_pi_1") before = self.p.balance("agent:atlas") r = self.p.pay("human:sam", "agent:atlas", 12.5, reason="Logo draft", ref="of-1") self.assertEqual(r["tx"]["kind"], "payment") self.assertEqual(self.p.balance("human:sam")["funded"], 37.5) after = self.p.balance("agent:atlas") self.assertAlmostEqual(after["funded"] - before["funded"], 12.5) self.assertTrue(self.p.pay("human:sam", "agent:atlas", 12.5, reason="Logo draft", ref="of-1")["duplicate"]) self.assertEqual(self.p.balance("human:sam")["funded"], 37.5) # survives a reload self.p.close() self.p = CofferProduct(self.db) self.assertEqual(self.p.balance("human:sam")["funded"], 37.5) self.assertTrue(self.p.pay("human:sam", "agent:atlas", 12.5, reason="Logo draft", ref="of-1")["duplicate"]) def test_agent_pays_human_from_funded_only(self): # atlas has only promotional credit: it cannot be paid on to a person with self.assertRaises(Exception): self.p.pay("agent:atlas", "human:sam", 5, reason="bounty", ref="of-2") self.p.top_up("atlas", 10) r = self.p.pay("agent:atlas", "human:sam", 5, reason="bounty", ref="of-3") self.assertEqual(self.p.balance("human:sam")["funded"], 5.0) self.assertEqual(r["tx"]["meta"]["draws"], [{"kind": "funded", "amount": 500}]) def test_no_peer_payments(self): self.p.buy_credits("human:sam", 10, payment_ref="t1") self.p.signup_flow("bolt") with self.assertRaises(TransferNotAllowed): self.p.pay("human:sam", "human:kim", 1, reason="x", ref="of-4") self.p.top_up("atlas", 10) with self.assertRaises(TransferNotAllowed): self.p.pay("agent:atlas", "agent:bolt", 1, reason="x", ref="of-5") with self.assertRaises(ProductError): self.p.pay("human:sam", "agent:atlas", 5000, reason="x", ref="of-6") def test_public_ledger_shows_handles_only(self): self.p.buy_credits("human:sam", 10, payment_ref="pi_secret_123") self.p.pay("human:sam", "agent:atlas", 2, reason="Review", ref="of-7") pub = [tx_public(t) for t in self.p.history()] text = repr(pub) self.assertIn("human:sam", text) self.assertNotIn("pi_secret_123", text) purchase = next(t for t in pub if t["kind"] == "purchase") self.assertEqual(purchase["meta"].get("payment"), "test") self.assertEqual(next(t for t in pub if t["kind"] == "payment")["meta"].get("offer"), "of-7") if __name__ == "__main__": unittest.main()