-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.test.ts
More file actions
680 lines (578 loc) · 20.7 KB
/
storage.test.ts
File metadata and controls
680 lines (578 loc) · 20.7 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
import { describe, it, expect, beforeAll, afterAll, beforeEach, afterEach } from "bun:test";
import { Miniflare } from "miniflare";
import { BtcNet, type BlockQueueRecord } from "@gonative-cc/lib/nbtc";
import { toSuiNet } from "@gonative-cc/lib/nsui";
import { dropTables, initDb } from "@gonative-cc/lib/test-helpers/init_db";
import { fetchNbtcAddresses, fetchPackageConfigs } from "./storage";
import { CFStorage as CFStorageImpl } from "./cf-storage";
import { MintTxStatus, InsertBlockStatus, type NbtcBroadcastedDeposit } from "./models";
let mf: Miniflare;
beforeAll(async () => {
mf = new Miniflare({
script: "",
modules: true,
d1Databases: ["DB"],
kvNamespaces: ["BtcBlocks", "nbtc_txs"],
d1Persist: false,
kvPersist: false,
cachePersist: false,
});
});
afterAll(async () => {
await mf.dispose();
});
beforeEach(async () => {
const db = await mf.getD1Database("DB");
await initDb(db);
});
afterEach(async () => dropTables(await mf.getD1Database("DB")));
describe("Storage Helper Functions", () => {
it("fetchPackageConfigs should return active packages", async () => {
const db = await mf.getD1Database("DB");
await db
.prepare(
`
INSERT INTO setups (id, btc_network, sui_network, nbtc_pkg, nbtc_contract, lc_pkg, lc_contract, nbtc_fallback_addr, is_active)
VALUES
(1, 'regtest', 'devnet', '0xPkg1', '0xContract1', '0xLC1', '0xLCC1', '0xFallback1', 1),
(2, 'regtest', 'devnet', '0xPkg2', '0xContract2', '0xLC2', '0xLCC2', '0xFallback2', 0)
`,
)
.run();
const configs = await fetchPackageConfigs(db);
expect(configs.length).toBe(1);
expect(configs[0]!.nbtc_pkg).toBe("0xPkg1");
});
it("fetchNbtcAddresses should return active addresses mapped to packages", async () => {
const db = await mf.getD1Database("DB");
await db
.prepare(
`
INSERT INTO setups (id, btc_network, sui_network, nbtc_pkg, nbtc_contract, lc_pkg, lc_contract, nbtc_fallback_addr, is_active)
VALUES (1, 'regtest', 'devnet', '0xPkg1', '0xContract1', '0xLC1', '0xLCC1', '0xFallback1', 1)
`,
)
.run();
await db
.prepare(
`
INSERT INTO nbtc_deposit_addresses (setup_id, deposit_address, is_active)
VALUES (1, 'bcrt1qAddress1', 1), (1, 'bcrt1qAddress2', 0)
`,
)
.run();
const addrMap = await fetchNbtcAddresses(db);
expect(addrMap.size).toBe(2); // we track deposits for both active and inactive addresses
expect(addrMap.has("bcrt1qAddress1")).toBe(true);
expect(addrMap.get("bcrt1qAddress1")!.is_active).toBe(true);
expect(addrMap.has("bcrt1qAddress2")).toBe(true);
expect(addrMap.get("bcrt1qAddress2")?.is_active).toBe(false);
});
});
describe("CFStorage", () => {
let storage: CFStorageImpl;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let env: any;
beforeEach(async () => {
env = await mf.getBindings();
storage = new CFStorageImpl(env.DB, env.BtcBlocks, env.nbtc_txs);
const db = env.DB;
await db
.prepare(
`
INSERT INTO setups (id, btc_network, sui_network, nbtc_pkg, nbtc_contract, lc_pkg, lc_contract, nbtc_fallback_addr, is_active)
VALUES (1, 'regtest', 'devnet', '0xPkg1', '0xContract1', '0xLC1', '0xLCC1', '0xFallback1', 1)
`,
)
.run();
await db
.prepare(
`
INSERT INTO nbtc_deposit_addresses (id, setup_id, deposit_address, is_active)
VALUES (10, 1, 'bcrt1qAddress1', 1)
`,
)
.run();
});
it("getDepositAddresses should return addresses for specific network", async () => {
const addrs = await storage.getDepositAddresses(BtcNet.REGTEST);
expect(addrs).toContain("bcrt1qAddress1");
const mainnetAddrs = await storage.getDepositAddresses(BtcNet.MAINNET);
expect(mainnetAddrs.length).toBe(0);
});
describe("Block Operations", () => {
it("insertBlockInfo should insert new block", async () => {
const block: BlockQueueRecord = {
hash: "0000hash1",
height: 100,
network: BtcNet.REGTEST,
timestamp_ms: 1000,
};
const result = await storage.insertBlockInfo(block);
expect(result).toBe(InsertBlockStatus.Inserted);
const saved = await storage.getBlockHash(100, BtcNet.REGTEST);
expect(saved).toBe("0000hash1");
});
it("insertBlockInfo should update if newer timestamp", async () => {
await storage.insertBlockInfo({
hash: "0000hashOld",
height: 100,
network: BtcNet.REGTEST,
timestamp_ms: 1000,
});
const result = await storage.insertBlockInfo({
hash: "0000hashNew",
height: 100,
network: BtcNet.REGTEST,
timestamp_ms: 2000,
});
expect(result).toBe(InsertBlockStatus.Updated);
const saved = await storage.getBlockHash(100, BtcNet.REGTEST);
expect(saved).toBe("0000hashNew");
});
it("insertBlockInfo should ignore if older timestamp", async () => {
await storage.insertBlockInfo({
hash: "0000hashNew",
height: 100,
network: BtcNet.REGTEST,
timestamp_ms: 2000,
});
const result = await storage.insertBlockInfo({
hash: "0000hashOld",
height: 100,
network: BtcNet.REGTEST,
timestamp_ms: 1000,
});
expect(result).toBe(InsertBlockStatus.Skipped);
const saved = await storage.getBlockHash(100, BtcNet.REGTEST);
expect(saved).toBe("0000hashNew");
});
it("markBlockAsProcessed should update is_scanned", async () => {
await storage.insertBlockInfo({
hash: "0000hash1",
height: 100,
network: BtcNet.REGTEST,
timestamp_ms: 1000,
});
let blocks = await storage.getBlocksToProcess(10);
expect(blocks.length).toBe(1);
await storage.markBlockAsProcessed("0000hash1", BtcNet.REGTEST);
blocks = await storage.getBlocksToProcess(10);
expect(blocks.length).toBe(0);
});
it("getLatestBlockHeight should return max height", async () => {
await storage.insertBlockInfo({
hash: "h1",
height: 100,
network: BtcNet.REGTEST,
timestamp_ms: 1,
});
await storage.insertBlockInfo({
hash: "h2",
height: 102,
network: BtcNet.REGTEST,
timestamp_ms: 2,
});
const max = await storage.getLatestBlockHeight(BtcNet.REGTEST);
expect(max).toBe(102);
});
it("KV operations for ChainTip and Block", async () => {
await storage.setChainTip(500, BtcNet.REGTEST);
expect(await storage.getChainTip(BtcNet.REGTEST)).toBe(500);
await env.BtcBlocks.put("hash123", new Uint8Array([1, 2, 3]));
const blockData = await storage.getBlock("hash123");
expect(blockData).not.toBeNull();
expect(new Uint8Array(blockData!)).toEqual(new Uint8Array([1, 2, 3]));
});
});
describe("Transaction Operations", () => {
const txBase = {
txId: "tx1",
btcNetwork: BtcNet.REGTEST,
suiNetwork: toSuiNet("devnet"),
nbtcPkg: "0xPkg1",
depositAddress: "bcrt1qAddress1",
sender: "sender1",
vout: 0,
blockHash: "blockHash1",
blockHeight: 100,
suiRecipient: "0xSui1",
amount: 5000,
};
it("insertOrUpdateNbtcTxs should insert transaction", async () => {
await storage.insertOrUpdateNbtcTxs([txBase]);
const tx = await storage.getNbtcMintTx("tx1");
expect(tx).not.toBeNull();
expect(tx!.status).toBe(MintTxStatus.Confirming);
expect(tx!.amount).toBe(5000);
});
it("getNbtcMintCandidates should return correct candidates", async () => {
await storage.insertOrUpdateNbtcTxs([txBase]);
await storage.finalizeNbtcTxs(["tx1"]);
const candidates = await storage.getNbtcMintCandidates(3);
expect(candidates.length).toBe(1);
expect(candidates[0]!.tx_id).toBe("tx1");
});
it("batchUpdateNbtcTxs should update statuses", async () => {
await storage.insertOrUpdateNbtcTxs([txBase]);
await storage.batchUpdateNbtcMintTxs([
{
txId: "tx1",
vout: 0,
status: MintTxStatus.Minted,
suiTxDigest: "digest1",
},
]);
const tx = await storage.getNbtcMintTx("tx1");
expect(tx!.status).toBe(MintTxStatus.Minted);
expect(tx!.sui_tx_id).toBe("digest1");
});
it("getReorgedMintedTxs should detect reorg", async () => {
await storage.insertOrUpdateNbtcTxs([txBase]);
await storage.batchUpdateNbtcMintTxs([
{
txId: "tx1",
vout: 0,
status: MintTxStatus.Minted,
suiTxDigest: "digest1",
},
]);
// new block at the same height with different hash (reorg)
await storage.insertBlockInfo({
hash: "blockHash2_Reorg",
height: 100,
network: BtcNet.REGTEST,
timestamp_ms: 2000,
});
const reorged = await storage.getReorgedMintedTxs(100);
expect(reorged.length).toBe(1);
expect(reorged[0]!.tx_id).toBe("tx1");
expect(reorged[0]!.old_block_hash).toBe("blockHash1");
expect(reorged[0]!.new_block_hash).toBe("blockHash2_Reorg");
});
it("registerBroadcastedNbtcTx should insert with Broadcasting status", async () => {
await storage.registerBroadcastedNbtcTx([
{
txId: "txBroadcast",
btcNetwork: BtcNet.REGTEST,
suiNetwork: "devnet",
nbtcPkg: "0xPkg1",
depositAddress: "bcrt1qAddress1",
sender: "sender2",
vout: 1,
suiRecipient: "0xSui2",
amount: 1000,
},
]);
const tx = await storage.getNbtcMintTx("txBroadcast");
expect(tx!.status).toBe(MintTxStatus.Broadcasting);
});
it("getConfirmingBlocks should return unique blocks for confirming txs", async () => {
await storage.insertOrUpdateNbtcTxs([txBase]);
// another tx in same block
const tx2 = { ...txBase, txId: "tx2", vout: 1 };
await storage.insertOrUpdateNbtcTxs([tx2]);
const blocks = await storage.getConfirmingBlocks();
expect(blocks.length).toBe(1);
expect(blocks[0]!.block_hash).toBe(txBase.blockHash);
});
it("getMintedTxs should return minted txs after specific height", async () => {
await storage.insertOrUpdateNbtcTxs([txBase]);
await storage.updateNbtcTxsStatus(["tx1"], MintTxStatus.Minted);
const minted = await storage.getMintedTxs(90);
expect(minted.length).toBe(1);
expect(minted[0]!.tx_id).toBe("tx1");
const mintedHigh = await storage.getMintedTxs(101);
expect(mintedHigh.length).toBe(0);
});
it("updateNbtcTxsStatus should update single status", async () => {
await storage.insertOrUpdateNbtcTxs([txBase]);
await storage.updateNbtcTxsStatus(["tx1"], MintTxStatus.MintFailed);
const tx = await storage.getNbtcMintTx("tx1");
expect(tx!.status).toBe(MintTxStatus.MintFailed);
});
it("updateConfirmingTxsToReorg should mark confirming txs as reorg", async () => {
await storage.insertOrUpdateNbtcTxs([txBase]);
await storage.updateConfirmingTxsToReorg([txBase.blockHash]);
const tx = await storage.getNbtcMintTx("tx1");
expect(tx!.status).toBe(MintTxStatus.Reorg);
});
it("getConfirmingTxs should return confirming transactions", async () => {
await storage.insertOrUpdateNbtcTxs([txBase]);
const confirming = await storage.getConfirmingTxs();
expect(confirming.length).toBe(1);
expect(confirming[0]!.tx_id).toBe("tx1");
});
it("getNbtcMintTxsBySuiAddr should return txs for recipient", async () => {
await storage.insertOrUpdateNbtcTxs([txBase]);
const txs = await storage.getNbtcMintTxsBySuiAddr(txBase.suiRecipient);
expect(txs.length).toBe(1);
expect(txs[0]!.tx_id).toBe("tx1");
});
it("getNbtcMintTxsByBtcSender should return txs for sender", async () => {
await storage.insertOrUpdateNbtcTxs([txBase]);
const txs = await storage.getNbtcMintTxsByBtcSender(txBase.sender, BtcNet.REGTEST);
expect(txs.length).toBe(1);
expect(txs[0]!.tx_id).toBe("tx1");
});
it("getTxStatus should return status for existing tx", async () => {
await storage.insertOrUpdateNbtcTxs([txBase]);
const status = await storage.getTxStatus("tx1");
expect(status).toBe(MintTxStatus.Confirming);
});
it("getTxStatus should return null for non-existent tx", async () => {
const status = await storage.getTxStatus("nonexistent");
expect(status).toBeNull();
});
it("insertOrUpdateNbtcTxs should handle empty array", async () => {
expect(await storage.insertOrUpdateNbtcTxs([])).toBeUndefined();
});
it("updateNbtcTxsStatus should handle empty array", async () => {
expect(await storage.updateNbtcTxsStatus([], MintTxStatus.Minted)).not.toBeNull();
});
it("finalizeNbtcTxs should handle empty array", async () => {
expect(await storage.finalizeNbtcTxs([])).not.toBeNull();
});
it("batchUpdateNbtcMintTxs should handle MintFailed status", async () => {
await storage.insertOrUpdateNbtcTxs([txBase]);
await storage.batchUpdateNbtcMintTxs([
{
txId: "tx1",
vout: 0,
status: MintTxStatus.MintFailed,
suiTxDigest: "failedDigest",
},
]);
const tx = await storage.getNbtcMintTx("tx1");
expect(tx!.status).toBe(MintTxStatus.MintFailed);
expect(tx!.retry_count).toBe(1);
});
it("getNbtcMintCandidates should include failed txs within retry limit", async () => {
await storage.insertOrUpdateNbtcTxs([txBase]);
await storage.finalizeNbtcTxs(["tx1"]);
await storage.batchUpdateNbtcMintTxs([
{
txId: "tx1",
vout: 0,
status: MintTxStatus.MintFailed,
},
]);
const candidates = await storage.getNbtcMintCandidates(3);
expect(candidates.length).toBe(1);
});
it("getNbtcMintCandidates should exclude failed txs exceeding retry limit", async () => {
await storage.insertOrUpdateNbtcTxs([txBase]);
await storage.finalizeNbtcTxs(["tx1"]);
// Simulate multiple failures
for (let i = 0; i < 4; i++) {
await storage.batchUpdateNbtcMintTxs([
{
txId: "tx1",
vout: 0,
status: MintTxStatus.MintFailed,
},
]);
}
const candidates = await storage.getNbtcMintCandidates(3);
expect(candidates.length).toBe(0);
});
it("insertOrUpdateNbtcTxs should update existing tx with new block info", async () => {
await storage.insertOrUpdateNbtcTxs([txBase]);
const updatedTx = {
...txBase,
blockHash: "newBlockHash",
blockHeight: 101,
};
await storage.insertOrUpdateNbtcTxs([updatedTx]);
const tx = await storage.getNbtcMintTx("tx1");
expect(tx!.block_hash).toBe("newBlockHash");
expect(tx!.block_height).toBe(101);
expect(tx!.status).toBe(MintTxStatus.Confirming);
});
it("registerBroadcastedNbtcTx should ignore duplicate broadcasts", async () => {
const broadcast: NbtcBroadcastedDeposit = {
txId: "txNoBlock",
btcNetwork: BtcNet.REGTEST,
suiNetwork: "devnet",
nbtcPkg: "0xPkg1",
depositAddress: "bcrt1qAddress1",
sender: "sender",
vout: 0,
suiRecipient: "0xSui",
amount: 1000,
};
await storage.registerBroadcastedNbtcTx([broadcast]);
await storage.registerBroadcastedNbtcTx([broadcast]); // duplicate
const tx = await storage.getNbtcMintTx("txNoBlock");
expect(tx!.status).toBe(MintTxStatus.Broadcasting);
});
it("updateNbtcTxsStatus should update multiple txs", async () => {
const tx2 = { ...txBase, txId: "tx2", vout: 1 };
await storage.insertOrUpdateNbtcTxs([txBase, tx2]);
await storage.updateNbtcTxsStatus(["tx1", "tx2"], MintTxStatus.Minted);
const tx1Result = await storage.getNbtcMintTx("tx1");
const tx2Result = await storage.getNbtcMintTx("tx2");
expect(tx1Result!.status).toBe(MintTxStatus.Minted);
expect(tx2Result!.status).toBe(MintTxStatus.Minted);
});
it("getConfirmingBlocks should not return blocks with null hash", async () => {
await storage.registerBroadcastedNbtcTx([
{
txId: "txNoBlock",
btcNetwork: BtcNet.REGTEST,
suiNetwork: "devnet",
nbtcPkg: "0xPkg1",
depositAddress: "bcrt1qAddress1",
sender: "sender",
vout: 0,
suiRecipient: "0xSui",
amount: 1000,
},
]);
// Update to Confirming but no block hash
await storage.updateNbtcTxsStatus(["txNoBlock"], MintTxStatus.Confirming);
const blocks = await storage.getConfirmingBlocks();
expect(blocks.length).toBe(0);
});
it("getNbtcMintTxsBySuiAddr should return empty for non-existent address", async () => {
const txs = await storage.getNbtcMintTxsBySuiAddr("0xNonExistent");
expect(txs.length).toBe(0);
});
it("getNbtcMintTxsByBtcSender should return empty for non-existent sender", async () => {
const txs = await storage.getNbtcMintTxsByBtcSender("nonexistent", BtcNet.REGTEST);
expect(txs.length).toBe(0);
});
it("getNbtcMintTx should return null for non-existent tx", async () => {
const tx = await storage.getNbtcMintTx("nonexistent");
expect(tx).toBeNull();
});
});
describe("Block Operations - Edge Cases", () => {
it("getLatestBlockHeight should return null for empty database", async () => {
const height = await storage.getLatestBlockHeight(BtcNet.REGTEST);
expect(height).toBeNull();
});
it("getChainTip should return null when not set", async () => {
const tip = await storage.getChainTip(BtcNet.TESTNET);
expect(tip).toBeNull();
});
it("getBlock should return null for non-existent hash", async () => {
const block = await storage.getBlock("nonexistent");
expect(block).toBeNull();
});
it("getBlockHash should return null for non-existent height", async () => {
const hash = await storage.getBlockHash(999, BtcNet.REGTEST);
expect(hash).toBeNull();
});
it("getBlocksToProcess should return empty array when all processed", async () => {
await storage.insertBlockInfo({
hash: "hash1",
height: 100,
network: BtcNet.REGTEST,
timestamp_ms: 1000,
});
await storage.markBlockAsProcessed("hash1", BtcNet.REGTEST);
const blocks = await storage.getBlocksToProcess(10);
expect(blocks.length).toBe(0);
});
it("getBlocksToProcess should respect batch size limit", async () => {
for (let i = 0; i < 5; i++) {
await storage.insertBlockInfo({
hash: `hash${i}`,
height: 100 + i,
network: BtcNet.REGTEST,
timestamp_ms: 1000 + i,
});
}
const blocks = await storage.getBlocksToProcess(3);
expect(blocks.length).toBe(3);
});
it("getBlocksToProcess should return blocks in ascending height order", async () => {
await storage.insertBlockInfo({
hash: "hash102",
height: 102,
network: BtcNet.REGTEST,
timestamp_ms: 1002,
});
await storage.insertBlockInfo({
hash: "hash100",
height: 100,
network: BtcNet.REGTEST,
timestamp_ms: 1000,
});
await storage.insertBlockInfo({
hash: "hash101",
height: 101,
network: BtcNet.REGTEST,
timestamp_ms: 1001,
});
const blocks = await storage.getBlocksToProcess(10);
expect(blocks.length).toBe(3);
expect(blocks[0]!.height).toBe(100);
expect(blocks[1]!.height).toBe(101);
expect(blocks[2]!.height).toBe(102);
});
it("insertBlockInfo should handle same timestamp", async () => {
await storage.insertBlockInfo({
hash: "hash1",
height: 100,
network: BtcNet.REGTEST,
timestamp_ms: 1000,
});
const result = await storage.insertBlockInfo({
hash: "hash2",
height: 100,
network: BtcNet.REGTEST,
timestamp_ms: 1000,
});
expect(result).toBe(InsertBlockStatus.Skipped);
});
it("setChainTip and getChainTip should work for different networks", async () => {
await storage.setChainTip(100, BtcNet.REGTEST);
await storage.setChainTip(200, BtcNet.MAINNET);
expect(await storage.getChainTip(BtcNet.REGTEST)).toBe(100);
expect(await storage.getChainTip(BtcNet.MAINNET)).toBe(200);
});
});
describe("Storage Helper Functions - Edge Cases", () => {
it("fetchPackageConfigs should only return active packages", async () => {
const db = await mf.getD1Database("DB");
await db
.prepare(
`INSERT INTO setups (id, btc_network, sui_network, nbtc_pkg, nbtc_contract, lc_pkg, lc_contract, nbtc_fallback_addr, is_active)
VALUES (2, 'testnet', 'testnet', '0xPkg2', '0xContract2', '0xLC2', '0xLCC2', '0xFallback2', 0)`,
)
.run();
const configs = await fetchPackageConfigs(db);
expect(configs.length).toBe(1);
});
it("fetchNbtcAddresses should return empty map when no active setups", async () => {
const db = await mf.getD1Database("DB");
await db
.prepare(
`INSERT INTO setups (id, btc_network, sui_network, nbtc_pkg, nbtc_contract, lc_pkg, lc_contract, nbtc_fallback_addr, is_active)
VALUES (2, 'testnet', 'testnet', '0xPkg2', '0xContract2', '0xLC2', '0xLCC2', '0xFallback2', 0)`,
)
.run();
await db
.prepare(
`INSERT INTO nbtc_deposit_addresses (setup_id, deposit_address, is_active)
VALUES (2, 'bcrt1qAddress2', 1)`,
)
.run();
const addrMap = await fetchNbtcAddresses(db);
expect(addrMap.size).toBe(1);
});
it("fetchPackageConfigs should handle multiple active packages", async () => {
const db = await mf.getD1Database("DB");
await db
.prepare(
`INSERT INTO setups (id, btc_network, sui_network, nbtc_pkg, nbtc_contract, lc_pkg, lc_contract, nbtc_fallback_addr, is_active)
VALUES (2, 'mainnet', 'mainnet', '0xPkg2', '0xContract2', '0xLC2', '0xLCC2', '0xFallback2', 1)`,
)
.run();
const configs = await fetchPackageConfigs(db);
expect(configs.length).toBe(2);
});
});
});