-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBlocktankTest.kt
More file actions
217 lines (196 loc) · 8.22 KB
/
BlocktankTest.kt
File metadata and controls
217 lines (196 loc) · 8.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package to.bitkit.services
import androidx.test.platform.app.InstrumentationRegistry
import com.synonym.bitkitcore.BtOrderState
import com.synonym.bitkitcore.BtOrderState2
import com.synonym.bitkitcore.CreateCjitOptions
import com.synonym.bitkitcore.CreateOrderOptions
import com.synonym.bitkitcore.IBtEstimateFeeResponse2
import com.synonym.bitkitcore.initDb
import com.synonym.bitkitcore.updateBlocktankUrl
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import to.bitkit.env.Env
import to.bitkit.test.annotations.CoreServiceIntegration
import to.bitkit.test.annotations.DeviceIntegration
import javax.inject.Inject
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
@HiltAndroidTest
@DeviceIntegration
@CoreServiceIntegration
class BlocktankTest {
@get:Rule
var hiltRule = HiltAndroidRule(this)
@Inject
lateinit var service: CoreService
private val targetContext by lazy { InstrumentationRegistry.getInstrumentation().targetContext }
@Before
fun init() {
hiltRule.inject()
val testDbPath = targetContext.cacheDir.absolutePath
Env.initAppStoragePath(testDbPath)
initDb(testDbPath)
runBlocking {
updateBlocktankUrl(Env.blocktankApiUrl)
}
}
@Test
fun testGetInfo() = runBlocking {
val info = service.blocktank.info(refresh = true)
assertNotNull(info, "Info should not be null")
// Verify info structure
// Test options
assertTrue(info.options.minChannelSizeSat > 0u, "Minimum channel size should be greater than 0")
assertTrue(
info.options.maxChannelSizeSat > info.options.minChannelSizeSat,
"Maximum channel size should be greater than minimum"
)
assertTrue(info.options.minExpiryWeeks > 0u, "Minimum expiry weeks should be greater than 0")
assertTrue(
info.options.maxExpiryWeeks > info.options.minExpiryWeeks,
"Maximum expiry weeks should be greater than minimum"
)
// Test nodes
assertTrue(info.nodes.isNotEmpty(), "LSP nodes list should not be empty")
for (node in info.nodes) {
assertTrue(node.pubkey.isNotEmpty(), "Node pubkey should not be empty")
assertTrue(
node.connectionStrings.isNotEmpty(),
"Node connection strings should not be empty"
)
}
// Test versions
assertTrue(info.versions.http.isNotEmpty(), "HTTP version should not be empty")
assertTrue(info.versions.btc.isNotEmpty(), "BTC version should not be empty")
assertTrue(info.versions.ln2.isNotEmpty(), "LN2 version should not be empty")
// Test onchain info
assertTrue(info.onchain.feeRates.fast > 0u, "Fast fee rate should be greater than 0")
assertTrue(info.onchain.feeRates.mid > 0u, "Mid fee rate should be greater than 0")
assertTrue(info.onchain.feeRates.slow > 0u, "Slow fee rate should be greater than 0")
}
@Test
fun testCreateCjitEntry() = runBlocking {
// Test creating a CJIT order
val channelSizeSat = 100_000uL // 100k sats
val invoiceSat = 10_000uL // 10k sats for the invoice
val invoiceDescription = "Test CJIT"
val nodeId = "03e7156ae33b0a208d0744199163177e909e80176e55d97a2f221ede0f934dd9ad" // Example node ID
val channelExpiryWeeks = 6u
val options = CreateCjitOptions(source = "bitkit", discountCode = null)
// Create CJIT entry
val cjitEntry = service.blocktank.createCjit(
channelSizeSat = channelSizeSat,
invoiceSat = invoiceSat,
invoiceDescription = invoiceDescription,
nodeId = nodeId,
channelExpiryWeeks = channelExpiryWeeks,
options = options
)
// Verify CJIT entry
assertNotNull(cjitEntry, "CJIT entry should not be null")
assertTrue(cjitEntry.id.isNotEmpty(), "CJIT entry ID should not be empty")
assertEquals(cjitEntry.channelSizeSat, channelSizeSat, "Channel size should match requested amount")
assertTrue(
cjitEntry.source == "bitkit",
"Source should match expected value"
)
assertNotNull(cjitEntry.lspNode, "LSP node should not be null")
assertTrue(
cjitEntry.lspNode.pubkey.isNotEmpty(),
"LSP node pubkey should not be empty"
)
assertEquals(cjitEntry.nodeId, nodeId, "Node ID should match requested ID")
assertEquals(
cjitEntry.channelExpiryWeeks,
channelExpiryWeeks,
"Channel expiry weeks should match the requested value"
)
// Test getting CJIT entries
val entries = service.blocktank.cjitEntries(
entryIds = listOf(cjitEntry.id),
filter = null,
refresh = true
)
assertTrue(entries.isNotEmpty(), "Should retrieve created CJIT entry")
assertEquals(entries.first().id, cjitEntry.id, "Retrieved entry should match created entry")
}
@Test
fun testOrders() = runBlocking {
// Test creating an order
val lspBalanceSat = 100_000uL
val channelExpiryWeeks = 2u
val options = CreateOrderOptions(
clientBalanceSat = 0uL,
lspNodeId = null,
couponCode = "",
source = "bitkit-android",
discountCode = null,
zeroConf = false,
zeroConfPayment = true,
zeroReserve = false,
clientNodeId = null,
signature = null,
timestamp = null,
refundOnchainAddress = null,
announceChannel = true
)
// Create Order
val order = service.blocktank.newOrder(
lspBalanceSat = lspBalanceSat,
channelExpiryWeeks = channelExpiryWeeks,
options = options
)
// Verify Order
assertNotNull(order, "Order should not be null")
assertTrue(order.id.isNotEmpty(), "Order ID should not be empty")
assertEquals(order.state, BtOrderState.CREATED, "Initial state should be created")
assertEquals(order.state2, BtOrderState2.CREATED, "Initial state2 should be created")
assertEquals(order.lspBalanceSat, lspBalanceSat, "LSP balance should match requested amount")
assertEquals(order.clientBalanceSat, 0uL, "Client balance should be zero")
assertEquals(order.channelExpiryWeeks, channelExpiryWeeks, "Channel expiry weeks should match")
assertEquals(order.source, "bitkit-android", "Source should match the expected value")
// Test getting orders
val orders = service.blocktank.orders(
orderIds = listOf(order.id),
filter = null,
refresh = true
)
assertTrue(orders.isNotEmpty(), "Orders list should not be empty")
assertEquals(orders.first().id, order.id, "Retrieved order should match created order")
}
@Test
fun testOrderEstimate() = runBlocking {
val lspBalanceSat = 100_000uL
val channelExpiryWeeks = 2u
val options = CreateOrderOptions(
clientBalanceSat = 0uL,
lspNodeId = null,
couponCode = "",
source = "bitkit-android",
discountCode = null,
zeroConf = false,
zeroConfPayment = true,
zeroReserve = false,
clientNodeId = null,
signature = null,
timestamp = null,
refundOnchainAddress = null,
announceChannel = true,
)
val estimate: IBtEstimateFeeResponse2 = service.blocktank.estimateFee(
lspBalanceSat = lspBalanceSat,
channelExpiryWeeks = channelExpiryWeeks,
options = options
)
assertNotNull(estimate.min0ConfTxFee, "Min 0 conf tx fee should not be null")
assertNotEquals(0uL, estimate.feeSat, "Fee should not be zero")
assertNotEquals(0uL, estimate.networkFeeSat, "Network fee should not be zero")
assertNotEquals(0uL, estimate.serviceFeeSat, "Service fee should not be zero")
}
}