Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(core): move legacy tx-display integration tests #3674

Merged
merged 16 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { baseLogger } from "@/services/logger"
import { LedgerService } from "@/services/ledger"
import { DealerPriceService } from "@/services/dealer-price"
import { NotificationsService } from "@/services/notifications"
import { DisplayAmountsConverter } from "@/domain/fiat/display-amounts-converter"
import { DisplayAmountsConverter } from "@/domain/fiat"

const dealer = DealerPriceService()
const { dustThreshold } = getOnChainWalletConfig()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { addPendingTransaction } from "@/app/wallets"

import { WalletCurrency } from "@/domain/shared"
import * as DisplayAmountsConverterImpl from "@/domain/fiat"

import { WalletOnChainAddressesRepository } from "@/services/mongoose"
import { Wallet, WalletOnChainPendingReceive } from "@/services/mongoose/schema"
Expand All @@ -19,13 +20,15 @@ afterEach(async () => {
{ $pull: { onchain: { address } } },
{ multi: true }, // This option updates all matching documents
)

jest.restoreAllMocks()
})

describe("addPendingTransaction", () => {
it("calls sendFilteredNotification on pending onchain receive", async () => {
// Setup mocks
const sendFilteredNotification = jest.fn()
const pushNotificationsServiceSpy = jest
jest
.spyOn(PushNotificationsServiceImpl, "PushNotificationsService")
.mockImplementationOnce(() => ({
sendFilteredNotification,
Expand All @@ -52,8 +55,33 @@ describe("addPendingTransaction", () => {
// Expect sent notification
expect(sendFilteredNotification.mock.calls.length).toBe(1)
expect(sendFilteredNotification.mock.calls[0][0].title).toBeTruthy()
})

it("calls DisplayConverter on pending onchain receive", async () => {
// Setup mocks
const displayAmountsConverterSpy = jest.spyOn(
DisplayAmountsConverterImpl,
"DisplayAmountsConverter",
)

// Create user
const { btcWalletDescriptor } = await createRandomUserAndWallets()

// Add address to user wallet
await WalletOnChainAddressesRepository().persistNew({
walletId: btcWalletDescriptor.id,
onChainAddress: { address },
})

// Restore system state
pushNotificationsServiceSpy.mockRestore()
// Add pending transaction
await addPendingTransaction({
txId: "txId" as OnChainTxHash,
vout: 0 as OnChainTxVout,
satoshis: btcAmount,
address,
})

// Expect sent notification
expect(displayAmountsConverterSpy).toHaveBeenCalledTimes(1)
})
})
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { addSettledTransaction } from "@/app/wallets"

import { LedgerTransactionType } from "@/domain/ledger"
import { WalletCurrency } from "@/domain/shared"
import { Transaction, TransactionMetadata } from "@/services/ledger/schema"
import * as DisplayAmountsConverterImpl from "@/domain/fiat"

import { Transaction, TransactionMetadata } from "@/services/ledger/schema"
import { WalletOnChainAddressesRepository } from "@/services/mongoose"
import { Wallet } from "@/services/mongoose/schema"
import * as PushNotificationsServiceImpl from "@/services/notifications/push-notifications"
import * as LedgerFacadeImpl from "@/services/ledger/facade"

import { createMandatoryUsers, createRandomUserAndWallets } from "test/helpers"

Expand All @@ -25,6 +28,8 @@ afterEach(async () => {
{ $pull: { onchain: { address } } },
{ multi: true }, // This option updates all matching documents
)

jest.restoreAllMocks()
})

describe("addSettledTransaction", () => {
Expand Down Expand Up @@ -62,4 +67,41 @@ describe("addSettledTransaction", () => {
// Restore system state
pushNotificationsServiceSpy.mockRestore()
})

it("records transaction with receive-onchain metadata on receive", async () => {
// Setup mocks
const displayAmountsConverterSpy = jest.spyOn(
DisplayAmountsConverterImpl,
"DisplayAmountsConverter",
)

const onChainReceiveLedgerMetadataSpy = jest.spyOn(
LedgerFacadeImpl,
"OnChainReceiveLedgerMetadata",
)
const recordOnChainReceiveSpy = jest.spyOn(LedgerFacadeImpl, "recordReceiveOnChain")

// Create user
const { btcWalletDescriptor } = await createRandomUserAndWallets()

// Add address to user wallet
await WalletOnChainAddressesRepository().persistNew({
walletId: btcWalletDescriptor.id,
onChainAddress: { address },
})

// Add settled transaction
await addSettledTransaction({
txId: "txId" as OnChainTxHash,
vout: 0 as OnChainTxVout,
satoshis: btcAmount,
address,
})

// Check record function was called with right metadata
expect(displayAmountsConverterSpy).toHaveBeenCalledTimes(1)
expect(onChainReceiveLedgerMetadataSpy).toHaveBeenCalledTimes(1)
const args = recordOnChainReceiveSpy.mock.calls[0][0]
expect(args.metadata.type).toBe(LedgerTransactionType.OnchainReceipt)
})
})
96 changes: 96 additions & 0 deletions core/api/test/integration/app/wallets/send-intraledger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import { Accounts, Payments } from "@/app"
import { AccountStatus } from "@/domain/accounts"
import { toSats } from "@/domain/bitcoin"
import { PaymentSendStatus } from "@/domain/bitcoin/lightning"
import { LedgerTransactionType } from "@/domain/ledger"
import { UsdDisplayCurrency, toCents } from "@/domain/fiat"
import {
InactiveAccountError,
IntraledgerLimitsExceededError,
SelfPaymentError,
TradeIntraAccountLimitsExceededError,
} from "@/domain/errors"
import * as DisplayAmountsConverterImpl from "@/domain/fiat"

import { AccountsRepository } from "@/services/mongoose"
import { Transaction } from "@/services/ledger/schema"
import * as LedgerFacadeImpl from "@/services/ledger/facade"
import * as PushNotificationsServiceImpl from "@/services/notifications/push-notifications"

import { AmountCalculator, WalletCurrency } from "@/domain/shared"
Expand All @@ -41,6 +44,8 @@ beforeEach(() => {
afterEach(async () => {
await Transaction.deleteMany({ memo })
await Transaction.deleteMany({ memoPayer: memo })

jest.restoreAllMocks()
})

const amount = toSats(10040)
Expand Down Expand Up @@ -317,4 +322,95 @@ describe("intraLedgerPay", () => {
// Restore system state
pushNotificationsServiceSpy.mockReset()
})

it("records transaction with wallet-id-trade-intra-account metadata on intraledger send", async () => {
// Setup mocks
const displayAmountsConverterSpy = jest.spyOn(
DisplayAmountsConverterImpl,
"DisplayAmountsConverter",
)

const walletIdTradeIntraAccountLedgerMetadataSpy = jest.spyOn(
LedgerFacadeImpl,
"WalletIdTradeIntraAccountLedgerMetadata",
)
const recordIntraledgerSpy = jest.spyOn(LedgerFacadeImpl, "recordIntraledger")

// Create users
const { btcWalletDescriptor: newWalletDescriptor, usdWalletDescriptor } =
await createRandomUserAndWallets()
const newAccount = await AccountsRepository().findById(newWalletDescriptor.accountId)
if (newAccount instanceof Error) throw newAccount

// Fund balance for send
const receive = await recordReceiveLnPayment({
walletDescriptor: newWalletDescriptor,
paymentAmount: receiveAmounts,
bankFee: receiveBankFee,
displayAmounts: receiveDisplayAmounts,
memo,
})
if (receive instanceof Error) throw receive

// Pay intraledger
await Payments.intraledgerPaymentSendWalletIdForBtcWallet({
recipientWalletId: usdWalletDescriptor.id,
memo,
amount,
senderWalletId: newWalletDescriptor.id,
senderAccount: newAccount,
})

// Check record function was called with right metadata
expect(displayAmountsConverterSpy).toHaveBeenCalledTimes(2)
expect(walletIdTradeIntraAccountLedgerMetadataSpy).toHaveBeenCalledTimes(1)
const args = recordIntraledgerSpy.mock.calls[0][0]
expect(args.metadata.type).toBe(LedgerTransactionType.WalletIdTradeIntraAccount)
})

it("records transaction with wallet-id-intraledger metadata on intraledger send", async () => {
// Setup mocks
const displayAmountsConverterSpy = jest.spyOn(
DisplayAmountsConverterImpl,
"DisplayAmountsConverter",
)

const walletIdIntraledgerLedgerMetadataSpy = jest.spyOn(
LedgerFacadeImpl,
"WalletIdIntraledgerLedgerMetadata",
)
const recordIntraledgerSpy = jest.spyOn(LedgerFacadeImpl, "recordIntraledger")

// Setup users and wallets
const newWalletDescriptor = await createRandomUserAndBtcWallet()
const newAccount = await AccountsRepository().findById(newWalletDescriptor.accountId)
if (newAccount instanceof Error) throw newAccount

const recipientWalletDescriptor = await createRandomUserAndBtcWallet()

// Fund balance for send
const receive = await recordReceiveLnPayment({
walletDescriptor: newWalletDescriptor,
paymentAmount: receiveAmounts,
bankFee: receiveBankFee,
displayAmounts: receiveDisplayAmounts,
memo,
})
if (receive instanceof Error) throw receive

// Pay intraledger
await Payments.intraledgerPaymentSendWalletIdForBtcWallet({
recipientWalletId: recipientWalletDescriptor.id,
memo,
amount,
senderWalletId: newWalletDescriptor.id,
senderAccount: newAccount,
})

// Check record function was called with right metadata
expect(displayAmountsConverterSpy).toHaveBeenCalledTimes(2)
expect(walletIdIntraledgerLedgerMetadataSpy).toHaveBeenCalledTimes(1)
const args = recordIntraledgerSpy.mock.calls[0][0]
expect(args.metadata.type).toBe(LedgerTransactionType.IntraLedger)
})
})
Loading
Loading