tigerbeetle-node 0.14.178 → 0.14.180

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/test.ts CHANGED
@@ -6,8 +6,10 @@ import {
6
6
  TransferFlags,
7
7
  CreateAccountError,
8
8
  CreateTransferError,
9
- GetAccountTransfers,
10
- GetAccountTransfersFlags,
9
+ AccountFilter,
10
+ AccountFilterFlags,
11
+ AccountFlags,
12
+ id,
11
13
  } from '.'
12
14
 
13
15
  const client = createClient({
@@ -57,6 +59,20 @@ test.skip = (name: string, fn: () => Promise<void>) => {
57
59
  console.log(name + ': SKIPPED')
58
60
  }
59
61
 
62
+ test('id() monotonically increasing', async (): Promise<void> => {
63
+ let idA = id();
64
+ for (let i = 0; i < 10_000_000; i++) {
65
+ // Ensure ID is monotonic between milliseconds if the loop executes too fast.
66
+ if (i % 10_000 == 0) {
67
+ await new Promise(resolve => setTimeout(resolve, 1))
68
+ }
69
+
70
+ const idB = id();
71
+ assert.ok(idB > idA, 'id() returned an id that did not monotonically increase');
72
+ idA = idB;
73
+ }
74
+ })
75
+
60
76
  test('range check `code` on Account to be u16', async (): Promise<void> => {
61
77
  const account = { ...accountA, id: 0n }
62
78
 
@@ -65,12 +81,12 @@ test('range check `code` on Account to be u16', async (): Promise<void> => {
65
81
  assert.strictEqual(codeError.message, 'code must be a u16.')
66
82
 
67
83
  const accounts = await client.lookupAccounts([account.id])
68
- assert.strictEqual(accounts.length, 0)
84
+ assert.deepStrictEqual(accounts, [])
69
85
  })
70
86
 
71
87
  test('can create accounts', async (): Promise<void> => {
72
88
  const errors = await client.createAccounts([accountA])
73
- assert.strictEqual(errors.length, 0)
89
+ assert.deepStrictEqual(errors, [])
74
90
  })
75
91
 
76
92
  test('can return error on account', async (): Promise<void> => {
@@ -85,7 +101,7 @@ test('error if timestamp is not set to 0n on account', async (): Promise<void> =
85
101
  const errors = await client.createAccounts([account])
86
102
 
87
103
  assert.strictEqual(errors.length, 1)
88
- assert.deepStrictEqual(errors[0], { index: 0, result: CreateAccountError.timestamp_must_be_zero })
104
+ assert.deepStrictEqual(errors[0], { index: 0, result: CreateAccountError.timestamp_must_be_zero })
89
105
  })
90
106
 
91
107
  test('can lookup accounts', async (): Promise<void> => {
@@ -139,7 +155,7 @@ test('can create a transfer', async (): Promise<void> => {
139
155
  }
140
156
 
141
157
  const errors = await client.createTransfers([transfer])
142
- assert.strictEqual(errors.length, 0)
158
+ assert.deepStrictEqual(errors, [])
143
159
 
144
160
  const accounts = await client.lookupAccounts([accountA.id, accountB.id])
145
161
  assert.strictEqual(accounts.length, 2)
@@ -174,7 +190,7 @@ test('can create a two-phase transfer', async (): Promise<void> => {
174
190
  }
175
191
 
176
192
  const errors = await client.createTransfers([transfer])
177
- assert.strictEqual(errors.length, 0)
193
+ assert.deepStrictEqual(errors, [])
178
194
 
179
195
  const accounts = await client.lookupAccounts([accountA.id, accountB.id])
180
196
  assert.strictEqual(accounts.length, 2)
@@ -225,7 +241,7 @@ test('can post a two-phase transfer', async (): Promise<void> => {
225
241
  }
226
242
 
227
243
  const errors = await client.createTransfers([commit])
228
- assert.strictEqual(errors.length, 0)
244
+ assert.deepStrictEqual(errors, [])
229
245
 
230
246
  const accounts = await client.lookupAccounts([accountA.id, accountB.id])
231
247
  assert.strictEqual(accounts.length, 2)
@@ -258,7 +274,7 @@ test('can reject a two-phase transfer', async (): Promise<void> => {
258
274
  timestamp: 0n, // this will be set correctly by the TigerBeetle server
259
275
  }
260
276
  const transferErrors = await client.createTransfers([transfer])
261
- assert.strictEqual(transferErrors.length, 0)
277
+ assert.deepStrictEqual(transferErrors, [])
262
278
 
263
279
  // send in the reject
264
280
  const reject: Transfer = {
@@ -278,7 +294,7 @@ test('can reject a two-phase transfer', async (): Promise<void> => {
278
294
  }
279
295
 
280
296
  const errors = await client.createTransfers([reject])
281
- assert.strictEqual(errors.length, 0)
297
+ assert.deepStrictEqual(errors, [])
282
298
 
283
299
  const accounts = await client.lookupAccounts([accountA.id, accountB.id])
284
300
  assert.strictEqual(accounts.length, 2)
@@ -363,12 +379,12 @@ test('cannot void an expired transfer', async (): Promise<void> => {
363
379
  timestamp: 0n, // this will be set correctly by the TigerBeetle server
364
380
  }
365
381
  const transferErrors = await client.createTransfers([transfer])
366
- assert.strictEqual(transferErrors.length, 0)
382
+ assert.deepStrictEqual(transferErrors, [])
367
383
 
368
384
  // Wait for the transfer to expire.
369
385
  // TODO: Use `await setTimeout(1000)` when upgrade to Node > 15.
370
386
  await new Promise(_ => setTimeout(_, 1000));
371
-
387
+
372
388
  // send in the reject
373
389
  const reject: Transfer = {
374
390
  id: 7n,
@@ -388,7 +404,7 @@ test('cannot void an expired transfer', async (): Promise<void> => {
388
404
 
389
405
  const errors = await client.createTransfers([reject])
390
406
  assert.strictEqual(errors.length, 1)
391
- assert.deepStrictEqual(errors[0], { index: 0, result: CreateTransferError.pending_transfer_expired })
407
+ assert.deepStrictEqual(errors[0], { index: 0, result: CreateTransferError.pending_transfer_expired })
392
408
  })
393
409
 
394
410
  test('can get account transfers', async (): Promise<void> => {
@@ -404,11 +420,11 @@ test('can get account transfers', async (): Promise<void> => {
404
420
  reserved: 0,
405
421
  ledger: 1,
406
422
  code: 718,
407
- flags: 0,
423
+ flags: AccountFlags.history,
408
424
  timestamp: 0n
409
425
  }
410
426
  const account_errors = await client.createAccounts([accountC])
411
- assert.strictEqual(account_errors.length, 0)
427
+ assert.deepStrictEqual(account_errors, [])
412
428
 
413
429
  var transfers_created : Transfer[] = [];
414
430
  // Create transfers where the new account is either the debit or credit account:
@@ -431,22 +447,29 @@ test('can get account transfers', async (): Promise<void> => {
431
447
  }
432
448
 
433
449
  const transfers_created_result = await client.createTransfers(transfers_created)
434
- assert.strictEqual(transfers_created_result.length, 0)
450
+ assert.deepStrictEqual(transfers_created_result, [])
435
451
 
436
452
  // Query all transfers for accountC:
437
- var filter: GetAccountTransfers = {
453
+ var filter: AccountFilter = {
438
454
  account_id: accountC.id,
439
455
  timestamp_min: 0n,
440
456
  timestamp_max: 0n,
441
457
  limit: 8190,
442
- flags: GetAccountTransfersFlags.credits | GetAccountTransfersFlags.debits,
458
+ flags: AccountFilterFlags.credits | AccountFilterFlags.debits,
443
459
  }
444
460
  var transfers = await client.getAccountTransfers(filter)
461
+ var history = await client.getAccountHistory(filter)
445
462
  assert.strictEqual(transfers.length, transfers_created.length)
463
+ assert.strictEqual(history.length, transfers.length)
464
+
446
465
  var timestamp = 0n;
466
+ var i = 0;
447
467
  for (var transfer of transfers) {
448
468
  assert.ok(timestamp < transfer.timestamp);
449
469
  timestamp = transfer.timestamp;
470
+
471
+ assert.ok(history[i].timestamp == transfer.timestamp);
472
+ i++;
450
473
  }
451
474
 
452
475
  // Query only the debit transfers for accountC, descending:
@@ -455,14 +478,22 @@ test('can get account transfers', async (): Promise<void> => {
455
478
  timestamp_min: 0n,
456
479
  timestamp_max: 0n,
457
480
  limit: 8190,
458
- flags: GetAccountTransfersFlags.debits | GetAccountTransfersFlags.reversed,
481
+ flags: AccountFilterFlags.debits | AccountFilterFlags.reversed,
459
482
  }
460
483
  transfers = await client.getAccountTransfers(filter)
484
+ history = await client.getAccountHistory(filter)
485
+
461
486
  assert.strictEqual(transfers.length, transfers_created.length / 2)
487
+ assert.strictEqual(history.length, transfers.length)
488
+
462
489
  timestamp = 1n << 64n;
490
+ i = 0;
463
491
  for (var transfer of transfers) {
464
492
  assert.ok(transfer.timestamp < timestamp);
465
493
  timestamp = transfer.timestamp;
494
+
495
+ assert.ok(history[i].timestamp == transfer.timestamp);
496
+ i++;
466
497
  }
467
498
 
468
499
  // Query only the credit transfers for accountC, descending:
@@ -471,14 +502,22 @@ test('can get account transfers', async (): Promise<void> => {
471
502
  timestamp_min: 0n,
472
503
  timestamp_max: 0n,
473
504
  limit: 8190,
474
- flags: GetAccountTransfersFlags.credits | GetAccountTransfersFlags.reversed,
505
+ flags: AccountFilterFlags.credits | AccountFilterFlags.reversed,
475
506
  }
476
507
  transfers = await client.getAccountTransfers(filter)
508
+ history = await client.getAccountHistory(filter)
509
+
477
510
  assert.strictEqual(transfers.length, transfers_created.length / 2)
511
+ assert.strictEqual(history.length, transfers.length)
512
+
478
513
  timestamp = 1n << 64n;
514
+ i = 0;
479
515
  for (var transfer of transfers) {
480
516
  assert.ok(transfer.timestamp < timestamp);
481
517
  timestamp = transfer.timestamp;
518
+
519
+ assert.ok(history[i].timestamp == transfer.timestamp);
520
+ i++;
482
521
  }
483
522
 
484
523
  // Query the first 5 transfers for accountC:
@@ -487,14 +526,22 @@ test('can get account transfers', async (): Promise<void> => {
487
526
  timestamp_min: 0n,
488
527
  timestamp_max: 0n,
489
528
  limit: transfers_created.length / 2,
490
- flags: GetAccountTransfersFlags.credits | GetAccountTransfersFlags.debits,
529
+ flags: AccountFilterFlags.credits | AccountFilterFlags.debits,
491
530
  }
492
531
  transfers = await client.getAccountTransfers(filter)
532
+ history = await client.getAccountHistory(filter)
533
+
493
534
  assert.strictEqual(transfers.length, transfers_created.length / 2)
535
+ assert.strictEqual(history.length, transfers.length)
536
+
494
537
  timestamp = 0n;
538
+ i = 0;
495
539
  for (var transfer of transfers) {
496
540
  assert.ok(timestamp < transfer.timestamp);
497
541
  timestamp = transfer.timestamp;
542
+
543
+ assert.ok(history[i].timestamp == transfer.timestamp);
544
+ i++;
498
545
  }
499
546
 
500
547
  // Query the next 5 transfers for accountC, with pagination:
@@ -503,13 +550,21 @@ test('can get account transfers', async (): Promise<void> => {
503
550
  timestamp_min: timestamp + 1n,
504
551
  timestamp_max: 0n,
505
552
  limit: transfers_created.length / 2,
506
- flags: GetAccountTransfersFlags.credits | GetAccountTransfersFlags.debits,
553
+ flags: AccountFilterFlags.credits | AccountFilterFlags.debits,
507
554
  }
508
555
  transfers = await client.getAccountTransfers(filter)
556
+ history = await client.getAccountHistory(filter)
557
+
509
558
  assert.strictEqual(transfers.length, transfers_created.length / 2)
559
+ assert.strictEqual(history.length, transfers.length)
560
+
561
+ i = 0;
510
562
  for (var transfer of transfers) {
511
563
  assert.ok(timestamp < transfer.timestamp);
512
564
  timestamp = transfer.timestamp;
565
+
566
+ assert.ok(history[i].timestamp == transfer.timestamp);
567
+ i++;
513
568
  }
514
569
 
515
570
  // Query again, no more transfers should be found:
@@ -518,10 +573,13 @@ test('can get account transfers', async (): Promise<void> => {
518
573
  timestamp_min: timestamp + 1n,
519
574
  timestamp_max: 0n,
520
575
  limit: transfers_created.length / 2,
521
- flags: GetAccountTransfersFlags.credits | GetAccountTransfersFlags.debits,
576
+ flags: AccountFilterFlags.credits | AccountFilterFlags.debits,
522
577
  }
523
578
  transfers = await client.getAccountTransfers(filter)
524
- assert.strictEqual(transfers.length, 0)
579
+ history = await client.getAccountHistory(filter)
580
+
581
+ assert.deepStrictEqual(transfers, [])
582
+ assert.strictEqual(history.length, transfers.length)
525
583
 
526
584
  // Query the first 5 transfers for accountC ORDER BY DESC:
527
585
  filter = {
@@ -529,14 +587,22 @@ test('can get account transfers', async (): Promise<void> => {
529
587
  timestamp_min: 0n,
530
588
  timestamp_max: 0n,
531
589
  limit: transfers_created.length / 2,
532
- flags: GetAccountTransfersFlags.credits | GetAccountTransfersFlags.debits | GetAccountTransfersFlags.reversed,
590
+ flags: AccountFilterFlags.credits | AccountFilterFlags.debits | AccountFilterFlags.reversed,
533
591
  }
534
592
  transfers = await client.getAccountTransfers(filter)
593
+ history = await client.getAccountHistory(filter)
594
+
535
595
  assert.strictEqual(transfers.length, transfers_created.length / 2)
596
+ assert.strictEqual(history.length, transfers.length)
597
+
536
598
  timestamp = 1n << 64n;
599
+ i = 0;
537
600
  for (var transfer of transfers) {
538
601
  assert.ok(timestamp > transfer.timestamp);
539
602
  timestamp = transfer.timestamp;
603
+
604
+ assert.ok(history[i].timestamp == transfer.timestamp);
605
+ i++;
540
606
  }
541
607
 
542
608
  // Query the next 5 transfers for accountC, with pagination:
@@ -545,13 +611,21 @@ test('can get account transfers', async (): Promise<void> => {
545
611
  timestamp_min: 0n,
546
612
  timestamp_max: timestamp - 1n,
547
613
  limit: transfers_created.length / 2,
548
- flags: GetAccountTransfersFlags.credits | GetAccountTransfersFlags.debits | GetAccountTransfersFlags.reversed,
614
+ flags: AccountFilterFlags.credits | AccountFilterFlags.debits | AccountFilterFlags.reversed,
549
615
  }
550
616
  transfers = await client.getAccountTransfers(filter)
617
+ history = await client.getAccountHistory(filter)
618
+
551
619
  assert.strictEqual(transfers.length, transfers_created.length / 2)
620
+ assert.strictEqual(history.length, transfers.length)
621
+
622
+ i = 0;
552
623
  for (var transfer of transfers) {
553
624
  assert.ok(timestamp > transfer.timestamp);
554
625
  timestamp = transfer.timestamp;
626
+
627
+ assert.ok(history[i].timestamp == transfer.timestamp);
628
+ i++;
555
629
  }
556
630
 
557
631
  // Query again, no more transfers should be found:
@@ -560,73 +634,90 @@ test('can get account transfers', async (): Promise<void> => {
560
634
  timestamp_min: 0n,
561
635
  timestamp_max: timestamp - 1n,
562
636
  limit: transfers_created.length / 2,
563
- flags: GetAccountTransfersFlags.credits | GetAccountTransfersFlags.debits | GetAccountTransfersFlags.reversed,
637
+ flags: AccountFilterFlags.credits | AccountFilterFlags.debits | AccountFilterFlags.reversed,
564
638
  }
565
639
  transfers = await client.getAccountTransfers(filter)
566
- assert.strictEqual(transfers.length, 0)
640
+ history = await client.getAccountHistory(filter)
641
+
642
+ assert.deepStrictEqual(transfers, [])
643
+ assert.strictEqual(history.length, transfers.length)
567
644
 
568
645
  // Invalid account:
569
- assert.strictEqual((await client.getAccountTransfers({
646
+ filter = {
570
647
  account_id: 0n,
571
648
  timestamp_min: 0n,
572
649
  timestamp_max: 0n,
573
650
  limit: 8190,
574
- flags: GetAccountTransfersFlags.credits | GetAccountTransfersFlags.debits,
575
- })).length, 0)
651
+ flags: AccountFilterFlags.credits | AccountFilterFlags.debits,
652
+ }
653
+ assert.deepStrictEqual((await client.getAccountTransfers(filter)), [])
654
+ assert.deepStrictEqual((await client.getAccountHistory(filter)), [])
576
655
 
577
656
  // Invalid timestamp min:
578
- assert.strictEqual((await client.getAccountTransfers({
657
+ filter = {
579
658
  account_id: accountC.id,
580
659
  timestamp_min: (1n << 64n) - 1n, // ulong max value
581
660
  timestamp_max: 0n,
582
661
  limit: 8190,
583
- flags: GetAccountTransfersFlags.credits | GetAccountTransfersFlags.debits,
584
- })).length, 0)
662
+ flags: AccountFilterFlags.credits | AccountFilterFlags.debits,
663
+ }
664
+ assert.deepStrictEqual((await client.getAccountTransfers(filter)), [])
665
+ assert.deepStrictEqual((await client.getAccountHistory(filter)), [])
585
666
 
586
667
  // Invalid timestamp max:
587
- assert.strictEqual((await client.getAccountTransfers({
668
+ filter = {
588
669
  account_id: accountC.id,
589
670
  timestamp_min: 0n,
590
671
  timestamp_max: (1n << 64n) - 1n, // ulong max value
591
672
  limit: 8190,
592
- flags: GetAccountTransfersFlags.credits | GetAccountTransfersFlags.debits,
593
- })).length, 0)
673
+ flags: AccountFilterFlags.credits | AccountFilterFlags.debits,
674
+ }
675
+ assert.deepStrictEqual((await client.getAccountTransfers(filter)), [])
676
+ assert.deepStrictEqual((await client.getAccountHistory(filter)), [])
594
677
 
595
678
  // Invalid timestamp range:
596
- assert.strictEqual((await client.getAccountTransfers({
679
+ filter = {
597
680
  account_id: accountC.id,
598
681
  timestamp_min: (1n << 64n) - 2n, // ulong max - 1
599
682
  timestamp_max: 1n,
600
683
  limit: 8190,
601
- flags: GetAccountTransfersFlags.credits | GetAccountTransfersFlags.debits,
602
- })).length, 0)
684
+ flags: AccountFilterFlags.credits | AccountFilterFlags.debits,
685
+ }
686
+ assert.deepStrictEqual((await client.getAccountTransfers(filter)), [])
687
+ assert.deepStrictEqual((await client.getAccountHistory(filter)), [])
603
688
 
604
689
  // Zero limit:
605
- assert.strictEqual((await client.getAccountTransfers({
690
+ filter = {
606
691
  account_id: accountC.id,
607
692
  timestamp_min: 0n,
608
693
  timestamp_max: 0n,
609
694
  limit: 0,
610
- flags: GetAccountTransfersFlags.credits | GetAccountTransfersFlags.debits,
611
- })).length, 0)
695
+ flags: AccountFilterFlags.credits | AccountFilterFlags.debits,
696
+ }
697
+ assert.deepStrictEqual((await client.getAccountTransfers(filter)), [])
698
+ assert.deepStrictEqual((await client.getAccountHistory(filter)), [])
612
699
 
613
700
  // Empty flags:
614
- assert.strictEqual((await client.getAccountTransfers({
701
+ filter = {
615
702
  account_id: accountC.id,
616
703
  timestamp_min: 0n,
617
704
  timestamp_max: 0n,
618
705
  limit: 8190,
619
- flags: GetAccountTransfersFlags.none,
620
- })).length, 0)
706
+ flags: AccountFilterFlags.none,
707
+ }
708
+ assert.deepStrictEqual((await client.getAccountTransfers(filter)), [])
709
+ assert.deepStrictEqual((await client.getAccountHistory(filter)), [])
621
710
 
622
711
  // Invalid flags:
623
- assert.strictEqual((await client.getAccountTransfers({
712
+ filter = {
624
713
  account_id: accountC.id,
625
714
  timestamp_min: 0n,
626
715
  timestamp_max: 0n,
627
716
  limit: 8190,
628
717
  flags: 0xFFFF,
629
- })).length, 0)
718
+ }
719
+ assert.deepStrictEqual((await client.getAccountTransfers(filter)), [])
720
+ assert.deepStrictEqual((await client.getAccountHistory(filter)), [])
630
721
 
631
722
  })
632
723