Skip to content

Commit d542b23

Browse files
committed
test: Bip21UrlBuilderTest
1 parent 0fae567 commit d542b23

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package to.bitkit.utils
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
import org.junit.runner.RunWith
6+
import org.junit.runners.JUnit4
7+
import to.bitkit.utils.Bip21Utils.buildBip21Url
8+
9+
@RunWith(JUnit4::class)
10+
class Bip21UrlBuilderTest {
11+
12+
// Test helper for expected URL without worrying about encoding
13+
private fun expectedUrl(base: String, vararg params: Pair<String, String>): String {
14+
val query = params.joinToString("&") { "${it.first}=${it.second.encodeToUrl()}" }
15+
return if (params.isNotEmpty()) "$base?$query" else base
16+
}
17+
18+
@Test
19+
fun `basic address without parameters`() {
20+
val address = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"
21+
val expected = "bitcoin:$address"
22+
Assert.assertEquals(expected, buildBip21Url(address))
23+
}
24+
25+
@Test
26+
fun `address with amount in sats`() {
27+
val address = "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"
28+
val amount = 100000uL // 0.001 BTC
29+
val expected = "bitcoin:$address?amount=0.001"
30+
Assert.assertEquals(expected, buildBip21Url(address, amount))
31+
}
32+
33+
@Test
34+
fun `amount with exact 1 BTC`() {
35+
val address = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"
36+
val amount = 100000000uL
37+
val expected = "bitcoin:$address?amount=1"
38+
Assert.assertEquals(expected, buildBip21Url(address, amount))
39+
}
40+
41+
@Test
42+
fun `amount with fractional sats rounds correctly`() {
43+
val address = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"
44+
val amount = 12345678uL
45+
val expected = "bitcoin:$address?amount=0.12345678"
46+
Assert.assertEquals(expected, buildBip21Url(address, amount))
47+
}
48+
49+
@Test
50+
fun `address with label and message`() {
51+
val address = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"
52+
val expected = expectedUrl(
53+
"bitcoin:$address",
54+
"label" to "Donation",
55+
"message" to "Thanks for your work!"
56+
)
57+
Assert.assertEquals(
58+
expected,
59+
buildBip21Url(address, label = "Donation", message = "Thanks for your work!")
60+
)
61+
}
62+
63+
@Test
64+
fun `address with lightning parameter`() {
65+
val address = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"
66+
val invoice = "lnbc500n1p3k9v3pp5kzmj..."
67+
val expected = "bitcoin:$address?lightning=${invoice.encodeToUrl()}"
68+
Assert.assertEquals(expected, buildBip21Url(address, lightningInvoice = invoice))
69+
}
70+
71+
@Test
72+
fun `address with all parameters`() {
73+
val address = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"
74+
val amount = 50000uL
75+
val label = "Donation"
76+
val message = "Thanks!"
77+
val invoice = "lnbc500n1p3k9v3pp5kzmj..."
78+
79+
val expected = expectedUrl(
80+
"bitcoin:$address",
81+
"amount" to "0.0005",
82+
"label" to label,
83+
"message" to message
84+
) + "&lightning=${invoice.encodeToUrl()}"
85+
86+
Assert.assertEquals(expected, buildBip21Url(address, amount, label, message, invoice))
87+
}
88+
89+
@Test
90+
fun `special characters in label and message are encoded`() {
91+
val address = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"
92+
val label = "Coffee & Tea"
93+
val message = "For Alice's Birthday!"
94+
95+
val expected = expectedUrl(
96+
"bitcoin:$address",
97+
"label" to label,
98+
"message" to message
99+
)
100+
101+
Assert.assertEquals(expected, buildBip21Url(address, label = label, message = message))
102+
}
103+
104+
@Test
105+
fun `zero sats is handled correctly`() {
106+
val address = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"
107+
val amount = 0uL
108+
val expected = "bitcoin:$address?amount=0"
109+
Assert.assertEquals(expected, buildBip21Url(address, amount))
110+
}
111+
112+
@Test
113+
fun `maximum ULong value is handled correctly`() {
114+
val address = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"
115+
val amount = ULong.MAX_VALUE
116+
val expected = "bitcoin:$address?amount=184467440737.09551615"
117+
Assert.assertEquals(expected, buildBip21Url(address, amount))
118+
}
119+
120+
@Test
121+
fun `lightning parameter without other parameters`() {
122+
val address = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"
123+
val invoice = "lnbc100n1p3k9v3pp5kzmj..."
124+
val expected = "bitcoin:$address?lightning=${invoice.encodeToUrl()}"
125+
Assert.assertEquals(expected, buildBip21Url(address, lightningInvoice = invoice))
126+
}
127+
128+
@Test
129+
fun `lightning parameter with existing parameters`() {
130+
val address = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"
131+
val amount = 10000uL
132+
val invoice = "lnbc100n1p3k9v3pp5kzmj..."
133+
val expected = "bitcoin:$address?amount=0.0001&lightning=${invoice.encodeToUrl()}"
134+
Assert.assertEquals(expected, buildBip21Url(address, amount, lightningInvoice = invoice))
135+
}
136+
}

0 commit comments

Comments
 (0)