tigerbeetle-node 0.15.6 → 0.16.0

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
@@ -9,6 +9,7 @@ import {
9
9
  AccountFilter,
10
10
  AccountFilterFlags,
11
11
  AccountFlags,
12
+ amount_max,
12
13
  id,
13
14
  QueryFilter,
14
15
  QueryFilterFlags,
@@ -20,8 +21,6 @@ const client = createClient({
20
21
  })
21
22
 
22
23
  // Test data
23
- const Zeroed32Bytes = Buffer.alloc(32, 0)
24
- const Zeroed48Bytes = Buffer.alloc(48, 0)
25
24
  const accountA: Account = {
26
25
  id: 17n,
27
26
  debits_pending: 0n,
@@ -230,7 +229,7 @@ test('can post a two-phase transfer', async (): Promise<void> => {
230
229
  id: 3n,
231
230
  debit_account_id: BigInt(0),
232
231
  credit_account_id: BigInt(0),
233
- amount: 0n,
232
+ amount: amount_max,
234
233
  user_data_128: 0n,
235
234
  user_data_64: 0n,
236
235
  user_data_32: 0,
@@ -437,6 +436,61 @@ test('cannot void an expired transfer', async (): Promise<void> => {
437
436
  assert.deepStrictEqual(errors[0], { index: 0, result: CreateTransferError.pending_transfer_expired })
438
437
  })
439
438
 
439
+ test('can close accounts', async (): Promise<void> => {
440
+ const closing_transfer: Transfer = {
441
+ id: id(),
442
+ debit_account_id: accountB.id,
443
+ credit_account_id: accountA.id,
444
+ amount: 0n,
445
+ user_data_128: 0n,
446
+ user_data_64: 0n,
447
+ user_data_32: 0,
448
+ pending_id: 0n,
449
+ timeout: 0,
450
+ ledger: 1,
451
+ code: 1,
452
+ flags: TransferFlags.closing_debit | TransferFlags.closing_credit | TransferFlags.pending,
453
+ timestamp: 0n, // will be set correctly by the TigerBeetle server
454
+ }
455
+ let errors = await client.createTransfers([closing_transfer])
456
+ assert.strictEqual(errors.length, 0)
457
+
458
+ let accounts = await client.lookupAccounts([accountA.id, accountB.id])
459
+ assert.strictEqual(accounts.length, 2)
460
+ assert.ok(accountA.flags != accounts[0].flags)
461
+ assert.ok((accounts[0].flags & AccountFlags.closed) != 0)
462
+
463
+ assert.ok(accountB.flags != accounts[1].flags)
464
+ assert.ok((accounts[1].flags & AccountFlags.closed) != 0)
465
+
466
+ const voiding_transfer: Transfer = {
467
+ id: id(),
468
+ debit_account_id: accountB.id,
469
+ credit_account_id: accountA.id,
470
+ amount: 0n,
471
+ user_data_128: 0n,
472
+ user_data_64: 0n,
473
+ user_data_32: 0,
474
+ timeout: 0,
475
+ ledger: 1,
476
+ code: 1,
477
+ flags: TransferFlags.void_pending_transfer,
478
+ pending_id: closing_transfer.id,
479
+ timestamp: 0n, // will be set correctly by the TigerBeetle server
480
+ }
481
+
482
+ errors = await client.createTransfers([voiding_transfer])
483
+ assert.strictEqual(errors.length, 0)
484
+
485
+ accounts = await client.lookupAccounts([accountA.id, accountB.id])
486
+ assert.strictEqual(accounts.length, 2)
487
+ assert.strictEqual(accountA.flags, accounts[0].flags)
488
+ assert.ok((accounts[0].flags & AccountFlags.closed) == 0)
489
+
490
+ assert.strictEqual(accountB.flags, accounts[1].flags)
491
+ assert.ok((accounts[1].flags & AccountFlags.closed) == 0)
492
+ })
493
+
440
494
  test('can get account transfers', async (): Promise<void> => {
441
495
  const accountC: Account = {
442
496
  id: 21n,
@@ -1200,6 +1254,96 @@ test('query with invalid filter', async (): Promise<void> => {
1200
1254
  assert.deepStrictEqual((await client.queryTransfers(filter)), [])
1201
1255
  })
1202
1256
 
1257
+
1258
+ test('can import accounts and transfers', async (): Promise<void> => {
1259
+ const accountTmp: Account = {
1260
+ id: id(),
1261
+ debits_pending: 0n,
1262
+ debits_posted: 0n,
1263
+ credits_pending: 0n,
1264
+ credits_posted: 0n,
1265
+ user_data_128: 0n,
1266
+ user_data_64: 0n,
1267
+ user_data_32: 0,
1268
+ reserved: 0,
1269
+ ledger: 1,
1270
+ code: 718,
1271
+ flags: 0,
1272
+ timestamp: 0n // this will be set correctly by the TigerBeetle server
1273
+ }
1274
+ let accountsErrors = await client.createAccounts([accountTmp])
1275
+ assert.deepStrictEqual(accountsErrors, [])
1276
+
1277
+ let accountLookup = await client.lookupAccounts([accountTmp.id])
1278
+ assert.strictEqual(accountLookup.length, 1)
1279
+ const timestampMax = accountLookup[0].timestamp
1280
+
1281
+ // Wait 10 ms so we can use the account's timestamp as the reference for past time
1282
+ // after the last object inserted.
1283
+ await new Promise(_ => setTimeout(_, 10));
1284
+
1285
+ const accountA: Account = {
1286
+ id: id(),
1287
+ debits_pending: 0n,
1288
+ debits_posted: 0n,
1289
+ credits_pending: 0n,
1290
+ credits_posted: 0n,
1291
+ user_data_128: 0n,
1292
+ user_data_64: 0n,
1293
+ user_data_32: 0,
1294
+ reserved: 0,
1295
+ ledger: 1,
1296
+ code: 718,
1297
+ flags: AccountFlags.imported,
1298
+ timestamp: timestampMax + 1n // user-defined timestamp
1299
+ }
1300
+ const accountB: Account = {
1301
+ id: id(),
1302
+ debits_pending: 0n,
1303
+ debits_posted: 0n,
1304
+ credits_pending: 0n,
1305
+ credits_posted: 0n,
1306
+ user_data_128: 0n,
1307
+ user_data_64: 0n,
1308
+ user_data_32: 0,
1309
+ reserved: 0,
1310
+ ledger: 1,
1311
+ code: 718,
1312
+ flags: AccountFlags.imported,
1313
+ timestamp: timestampMax + 2n // user-defined timestamp
1314
+ }
1315
+ accountsErrors = await client.createAccounts([accountA, accountB])
1316
+ assert.deepStrictEqual(accountsErrors, [])
1317
+
1318
+ accountLookup = await client.lookupAccounts([accountA.id, accountB.id])
1319
+ assert.strictEqual(accountLookup.length, 2)
1320
+ assert.strictEqual(accountLookup[0].timestamp, accountA.timestamp)
1321
+ assert.strictEqual(accountLookup[1].timestamp, accountB.timestamp)
1322
+
1323
+ const transfer: Transfer = {
1324
+ id: id(),
1325
+ debit_account_id: accountA.id,
1326
+ credit_account_id: accountB.id,
1327
+ amount: 100n,
1328
+ user_data_128: 0n,
1329
+ user_data_64: 0n,
1330
+ user_data_32: 0,
1331
+ pending_id: 0n,
1332
+ timeout: 0,
1333
+ ledger: 1,
1334
+ code: 1,
1335
+ flags: TransferFlags.imported,
1336
+ timestamp: timestampMax + 3n, // user-defined timestamp.
1337
+ }
1338
+
1339
+ const errors = await client.createTransfers([transfer])
1340
+ assert.deepStrictEqual(errors, [])
1341
+
1342
+ const transfers = await client.lookupTransfers([transfer.id])
1343
+ assert.strictEqual(transfers.length, 1)
1344
+ assert.strictEqual(transfers[0].timestamp, timestampMax + 3n)
1345
+ })
1346
+
1203
1347
  async function main () {
1204
1348
  const start = new Date().getTime()
1205
1349
  try {