Skip to content

Commit 663ea54

Browse files
committed
add list_transactions test
1 parent b851920 commit 663ea54

File tree

1 file changed

+133
-2
lines changed

1 file changed

+133
-2
lines changed

tests/integration/test_all.py

Lines changed: 133 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ async def close(self):
183183

184184
async def _wait_for_connection(self):
185185
while not self.connected:
186-
await asyncio.sleep(0.2)
186+
try:
187+
await asyncio.sleep(0.2)
188+
except asyncio.CancelledError:
189+
logger.debug("Connection wait cancelled")
190+
return
187191

188192
async def start(self):
189193
self.task = asyncio.create_task(self._run())
@@ -335,7 +339,7 @@ async def send_event(self, method, params):
335339
await self.ws.send(self._json_dumps(["EVENT", event]))
336340

337341
async def wait_for(
338-
self, result_type, callback=None, on_error_callback=None, timeout=60
342+
self, result_type, callback=None, on_error_callback=None, timeout=120
339343
):
340344
now = time.time()
341345
while True:
@@ -992,3 +996,130 @@ async def create_valid_invoice(wallet, amount=1000):
992996
if error:
993997
raise Exception(f"Failed to create invoice: {error}")
994998
return result["invoice"]
999+
1000+
@pytest.mark.asyncio
1001+
async def test_list_transactions():
1002+
# Create wallets with required permissions
1003+
nwc1 = await create_nwc(
1004+
"wallet1", "test_list_transactions", ["invoice", "pay", "balance", "history"], [], 0
1005+
)
1006+
nwc2 = await create_nwc(
1007+
"wallet2", "test_list_transactions", ["invoice", "pay", "balance", "history"], [], 0
1008+
)
1009+
1010+
wallet1 = NWCWallet(nwc1["pairing"])
1011+
wallet2 = NWCWallet(nwc2["pairing"])
1012+
1013+
await wallet1.start()
1014+
await wallet2.start()
1015+
1016+
# Create some transactions with different timestamps
1017+
start_time = int(time.time())
1018+
1019+
# First invoice
1020+
await wallet1.send_event(
1021+
"make_invoice", {"amount": 100000, "description": "test invoice 1"}
1022+
)
1023+
result1, _, error = await wallet1.wait_for("make_invoice")
1024+
assert not error
1025+
invoice1 = result1["invoice"]
1026+
1027+
# Pay first invoice
1028+
await wallet2.send_event("pay_invoice", {"invoice": invoice1})
1029+
_, _, error = await wallet2.wait_for("pay_invoice")
1030+
assert not error
1031+
1032+
mid_timestamp = int(time.time())
1033+
1034+
# Second invoice
1035+
await wallet1.send_event(
1036+
"make_invoice", {"amount": 200000, "description": "test invoice 2"}
1037+
)
1038+
result2, _, error = await wallet1.wait_for("make_invoice")
1039+
assert not error
1040+
invoice2 = result2["invoice"]
1041+
1042+
# Pay second invoice
1043+
await wallet2.send_event("pay_invoice", {"invoice": invoice2})
1044+
_, _, error = await wallet2.wait_for("pay_invoice")
1045+
assert not error
1046+
1047+
# Create an unpaid invoice
1048+
await wallet1.send_event(
1049+
"make_invoice", {"amount": 300000, "description": "test invoice 3 (unpaid)"}
1050+
)
1051+
_, _, error = await wallet1.wait_for("make_invoice")
1052+
assert not error
1053+
1054+
end_time = int(time.time())
1055+
1056+
# Test 1: List all transactions
1057+
await wallet1.send_event("list_transactions", {})
1058+
result, _, error = await wallet1.wait_for("list_transactions")
1059+
assert not error
1060+
assert "transactions" in result
1061+
transactions = result["transactions"]
1062+
1063+
# Should have at least 2 paid invoices
1064+
assert len(transactions) >= 2
1065+
1066+
# Verify transaction structure
1067+
for tx in transactions:
1068+
assert "type" in tx
1069+
assert "amount" in tx
1070+
assert "created_at" in tx
1071+
assert "payment_hash" in tx
1072+
1073+
# Test 2: Filter by type (incoming)
1074+
await wallet1.send_event("list_transactions", {"type": "incoming"})
1075+
result, _, error = await wallet1.wait_for("list_transactions")
1076+
assert not error
1077+
incoming_txs = result["transactions"]
1078+
assert all(tx["type"] == "incoming" for tx in incoming_txs)
1079+
1080+
# Test 3: Filter by time range
1081+
await wallet1.send_event(
1082+
"list_transactions", {"from": start_time, "until": mid_timestamp}
1083+
)
1084+
result, _, error = await wallet1.wait_for("list_transactions")
1085+
assert not error
1086+
early_txs = result["transactions"]
1087+
# Should only include transactions from the first time period
1088+
for tx in early_txs:
1089+
assert tx["created_at"] >= start_time
1090+
assert tx["created_at"] <= mid_timestamp
1091+
1092+
# Test 4: Test limit and offset
1093+
await wallet1.send_event("list_transactions", {"limit": 1})
1094+
result, _, error = await wallet1.wait_for("list_transactions")
1095+
assert not error
1096+
limited_txs = result["transactions"]
1097+
assert len(limited_txs) == 1
1098+
1099+
await wallet1.send_event("list_transactions", {"limit": 1, "offset": 1})
1100+
result, _, error = await wallet1.wait_for("list_transactions")
1101+
assert not error
1102+
offset_txs = result["transactions"]
1103+
assert len(offset_txs) == 1
1104+
# Should be different transactions
1105+
assert offset_txs[0]["payment_hash"] != limited_txs[0]["payment_hash"]
1106+
1107+
# Test 5: Include unpaid invoices
1108+
await wallet1.send_event("list_transactions", {"unpaid": True})
1109+
result, _, error = await wallet1.wait_for("list_transactions")
1110+
assert not error
1111+
all_txs = result["transactions"]
1112+
# Should find at least one unpaid invoice
1113+
assert any(tx.get("settled_at") is None for tx in all_txs)
1114+
1115+
# Test 6: Check outgoing transactions from wallet2
1116+
await wallet2.send_event("list_transactions", {"type": "outgoing"})
1117+
result, _, error = await wallet2.wait_for("list_transactions")
1118+
assert not error
1119+
outgoing_txs = result["transactions"]
1120+
assert all(tx["type"] == "outgoing" for tx in outgoing_txs)
1121+
assert len(outgoing_txs) >= 2
1122+
1123+
# Cleanup
1124+
await wallet1.close()
1125+
await wallet2.close()

0 commit comments

Comments
 (0)