RefundAvailabilityTest.kt (2970B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2026 Taler Systems S.A. 4 * 5 * GNU Taler is free software; you can redistribute it and/or modify it under the 6 * terms of the GNU General Public License as published by the Free Software 7 * Foundation; either version 3, or (at your option) any later version. 8 * 9 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 package net.taler.merchantpos.refund 18 19 import net.taler.common.Amount 20 import net.taler.common.ExternalPayment 21 import net.taler.common.Timestamp 22 import net.taler.merchantlib.ExternalRefundInfo 23 import net.taler.merchantlib.OrderHistoryEntry 24 25 import org.junit.Assert.assertEquals 26 import org.junit.Test 27 28 class RefundAvailabilityTest 29 { 30 private val item = 31 OrderHistoryEntry( 32 "order", 33 timestamp = Timestamp(1), 34 amount = chf(5), 35 summary = "Lunch", 36 paid = true, 37 refundable = true, 38 ) 39 40 private fun chf(n: Long) = Amount("CHF", n, 0) 41 42 @Test 43 fun `Taler availability excludes unpaid expired pending and exhausted payments`() 44 { 45 assertEquals(chf(5), item.talerRefundRemaining()) 46 assertEquals(chf(3), item.copy(refundAmount = chf(2)).talerRefundRemaining()) 47 listOf( 48 item.copy(paid = false), 49 item.copy(refundable = false), 50 item.copy(refundPending = true), 51 item.copy(refundAmount = chf(5)), 52 item.copy(refunded = true), 53 ).forEach { 54 assertEquals(chf(0), it.talerRefundRemaining()) 55 } 56 } 57 58 @Test 59 fun `external refund cap is per original payment and never negative`() 60 { 61 val cash = ExternalPayment("cash", "cash-1", chf(5)) 62 val refund = ExternalRefundInfo("refund-1", "cash", chf(2), "return", Timestamp(1), cash.id) 63 assertEquals(chf(3), item.copy(refundsExternal = listOf(refund)).externalRefundRemaining(cash, null)) 64 assertEquals( 65 chf(5), 66 item.copy(refundsExternal = listOf(refund.copy(paymentId = "cash-2"))).externalRefundRemaining(cash, null), 67 ) 68 assertEquals( 69 chf(0), 70 item.copy(refundsExternal = listOf(refund.copy(amount = chf(8)))).externalRefundRemaining(cash, null), 71 ) 72 } 73 74 @Test 75 fun `legacy unassigned refunds reduce availability conservatively`() 76 { 77 val cash = ExternalPayment("cash", "cash-1", chf(5)) 78 val refund = ExternalRefundInfo("refund-1", "cash", chf(2), "return", Timestamp(1)) 79 assertEquals(chf(3), item.copy(refundsExternal = listOf(refund)).externalRefundRemaining(cash, null)) 80 } 81 }