|
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 |
|
5 | | -from cashu.core.base import Proof |
| 5 | +from cashu.core.base import MintQuoteState, Proof |
6 | 6 | from cashu.wallet.npc import NpubCash |
7 | 7 | from cashu.wallet.wallet import Wallet |
8 | 8 |
|
@@ -125,3 +125,61 @@ async def test_mint_quotes(npc, mock_wallet): |
125 | 125 |
|
126 | 126 | assert len(proofs) == 1 |
127 | 127 | mock_wallet.mint.assert_called_once_with(100, quote_id="q1") |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.asyncio |
| 131 | +@pytest.mark.parametrize( |
| 132 | + "error_code, expect_issued", |
| 133 | + [ |
| 134 | + (20002, True), # QuoteAlreadyIssuedError: the quote really was issued |
| 135 | + (11000, False), # generic TransactionError: unrelated failure, keep retrying |
| 136 | + (20001, False), # QuoteNotPaidError |
| 137 | + (20005, False), # QuotePendingError |
| 138 | + ], |
| 139 | +) |
| 140 | +async def test_mint_quotes_only_marks_issued_on_already_issued_code( |
| 141 | + npc, mock_wallet, error_code, expect_issued |
| 142 | +): |
| 143 | + """Only the mint's "quote already issued" code may mark the local quote issued. |
| 144 | +
|
| 145 | + Marking it on any other failure strands a paid invoice: the quote is skipped |
| 146 | + on every later poll, so no ecash is ever issued for it. |
| 147 | + """ |
| 148 | + with patch("cashu.wallet.npc.httpx.AsyncClient") as mock_client, \ |
| 149 | + patch("cashu.wallet.npc.get_bolt11_mint_quote", new_callable=AsyncMock) as mock_get_quote, \ |
| 150 | + patch("cashu.wallet.npc.update_bolt11_mint_quote", new_callable=AsyncMock) as mock_update_quote: |
| 151 | + |
| 152 | + mock_instance = mock_client.return_value.__aenter__.return_value |
| 153 | + |
| 154 | + mock_resp = MagicMock() |
| 155 | + mock_resp.status_code = 200 |
| 156 | + mock_resp.json.return_value = { |
| 157 | + "error": False, |
| 158 | + "data": { |
| 159 | + "quotes": [ |
| 160 | + {"quoteId": "q1", "amount": 100, "state": "PAID", "mintUrl": "https://mint.example.com"}, |
| 161 | + ] |
| 162 | + } |
| 163 | + } |
| 164 | + mock_instance.get.return_value = mock_resp |
| 165 | + |
| 166 | + mock_get_quote.return_value = None |
| 167 | + |
| 168 | + mock_mint_quote = MagicMock() |
| 169 | + mock_mint_quote.state = "PAID" |
| 170 | + mock_wallet.get_mint_quote = AsyncMock(return_value=mock_mint_quote) |
| 171 | + |
| 172 | + # the wallet flattens mint errors to "Mint Error: <detail> (Code: <n>)" |
| 173 | + mock_wallet.mint = AsyncMock( |
| 174 | + side_effect=Exception(f"Mint Error: something went wrong (Code: {error_code})") |
| 175 | + ) |
| 176 | + |
| 177 | + proofs = await npc.mint_quotes() |
| 178 | + |
| 179 | + assert proofs == [] |
| 180 | + if expect_issued: |
| 181 | + mock_update_quote.assert_awaited_once() |
| 182 | + assert mock_update_quote.await_args.kwargs["quote"] == "q1" |
| 183 | + assert mock_update_quote.await_args.kwargs["state"] == MintQuoteState.issued |
| 184 | + else: |
| 185 | + mock_update_quote.assert_not_awaited() |
0 commit comments