From a654c88a584b1249d1e60c0b8de0aeff72e5979e Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 13 Sep 2023 11:09:33 -0300 Subject: failing p2p test case --- packages/taler-wallet-core/src/dbless.ts | 2 +- .../src/util/coinSelection.test.ts | 64 ++++++++++++++++++++++ .../taler-wallet-core/src/util/coinSelection.ts | 6 ++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/packages/taler-wallet-core/src/dbless.ts b/packages/taler-wallet-core/src/dbless.ts index 65c293bdf..d70eab888 100644 --- a/packages/taler-wallet-core/src/dbless.ts +++ b/packages/taler-wallet-core/src/dbless.ts @@ -31,6 +31,7 @@ import { AmountJson, Amounts, AmountString, + BankAccessApiClient, codecForAny, codecForBankWithdrawalOperationPostResponse, codecForBatchDepositSuccess, @@ -53,7 +54,6 @@ import { HttpRequestLibrary, readSuccessResponseJsonOrThrow, } from "@gnu-taler/taler-util/http"; -import { BankAccessApiClient } from "../../taler-util/src/bank-api-client.js"; import { TalerCryptoInterface } from "./crypto/cryptoImplementation.js"; import { DenominationRecord } from "./db.js"; import { isWithdrawableDenom } from "./index.js"; diff --git a/packages/taler-wallet-core/src/util/coinSelection.test.ts b/packages/taler-wallet-core/src/util/coinSelection.test.ts index b907eb160..f678e75e7 100644 --- a/packages/taler-wallet-core/src/util/coinSelection.test.ts +++ b/packages/taler-wallet-core/src/util/coinSelection.test.ts @@ -18,8 +18,72 @@ import { AgeRestriction, AmountJson, Amounts, + DenomKeyType, Duration, TransactionAmountMode, } from "@gnu-taler/taler-util"; import test, { ExecutionContext } from "ava"; +import { testing_greedySelectPeer } from "./coinSelection.js" +type Tester = { + deep: { + equal(another: T): ReturnType; + equals(another: T): ReturnType; + } +} + +function expect(t: ExecutionContext, thing: T): Tester { + return { + deep: { + equal: (another: T) => t.deepEqual(thing, another), + equals: (another: T) => t.deepEqual(thing, another), + }, + }; +} + +const inTheDistantFuture = AbsoluteTime.toProtocolTimestamp( + AbsoluteTime.addDuration(AbsoluteTime.now(), Duration.fromSpec({ hours: 1 })) +) +const inThePast = AbsoluteTime.toProtocolTimestamp( + AbsoluteTime.subtractDuraction(AbsoluteTime.now(), Duration.fromSpec({ hours: 1 })) +) +test("should select the coin", (t) => { + const instructedAmount = Amounts.parseOrThrow("LOCAL:2") + const tally = { + amountAcc: Amounts.zeroOfCurrency(instructedAmount.currency), + depositFeesAcc: Amounts.zeroOfCurrency(instructedAmount.currency), + lastDepositFee: Amounts.zeroOfCurrency(instructedAmount.currency), + }; + const coins = testing_greedySelectPeer( + // candidates available + [{ + "denomPub": { + "age_mask": 0, + "cipher": DenomKeyType.Rsa, + "rsa_public_key": "PPP" + }, + "denomPubHash": "XXX", + "value": "LOCAL:10", + "feeDeposit": "LOCAL:0.1", + "feeRefresh": "LOCAL:0", + "feeRefund": "LOCAL:0", + "feeWithdraw": "LOCAL:0", + "stampExpireDeposit": inTheDistantFuture, + "stampExpireLegal": inTheDistantFuture, + "stampExpireWithdraw": inTheDistantFuture, + "stampStart": inThePast, + "exchangeBaseUrl": "http://exchange.localhost/", + "numAvailable": 5, + "maxAge": 32 + }], + instructedAmount, tally); + + expect(t, coins).deep.equal({ + "XXX;32;http://exchange.localhost/": { + exchangeBaseUrl: "http://exchange.localhost/", + denomPubHash: "XXX", + maxAge: 32, + contributions: [Amounts.parseOrThrow("LOCAL:2")], + } + }); +}); \ No newline at end of file diff --git a/packages/taler-wallet-core/src/util/coinSelection.ts b/packages/taler-wallet-core/src/util/coinSelection.ts index 6fd0f1b86..b8ce5e0f2 100644 --- a/packages/taler-wallet-core/src/util/coinSelection.ts +++ b/packages/taler-wallet-core/src/util/coinSelection.ts @@ -894,6 +894,12 @@ interface PeerCoinSelectionTally { lastDepositFee: AmountJson; } +/** + * exporting for testing + */ +export function testing_greedySelectPeer(...args: Parameters): ReturnType { + return greedySelectPeer(...args) +} function greedySelectPeer( candidates: AvailableDenom[], instructedAmount: AmountLike, -- cgit v1.2.3