Files
gridpilot.gg/core/payments/domain/entities/Wallet.test.ts
2026-01-22 18:05:30 +01:00

285 lines
9.1 KiB
TypeScript

import {
ReferenceType,
Transaction,
TransactionType,
Wallet,
} from '@core/payments/domain/entities/Wallet';
import { describe, expect, it } from 'vitest';
describe('payments/domain/entities/Wallet', () => {
describe('TransactionType enum', () => {
it('should have correct transaction type values', () => {
expect(TransactionType.DEPOSIT).toBe('deposit');
expect(TransactionType.WITHDRAWAL).toBe('withdrawal');
expect(TransactionType.PLATFORM_FEE).toBe('platform_fee');
});
});
describe('ReferenceType enum', () => {
it('should have correct reference type values', () => {
expect(ReferenceType.SPONSORSHIP).toBe('sponsorship');
expect(ReferenceType.MEMBERSHIP_FEE).toBe('membership_fee');
expect(ReferenceType.PRIZE).toBe('prize');
});
});
describe('Wallet interface', () => {
it('should have all required properties', () => {
const wallet: Wallet = {
id: 'wallet-123',
leagueId: 'league-456',
balance: 1000,
totalRevenue: 5000,
totalPlatformFees: 250,
totalWithdrawn: 3750,
currency: 'USD',
createdAt: new Date('2024-01-01'),
};
expect(wallet.id).toBe('wallet-123');
expect(wallet.leagueId).toBe('league-456');
expect(wallet.balance).toBe(1000);
expect(wallet.totalRevenue).toBe(5000);
expect(wallet.totalPlatformFees).toBe(250);
expect(wallet.totalWithdrawn).toBe(3750);
expect(wallet.currency).toBe('USD');
expect(wallet.createdAt).toEqual(new Date('2024-01-01'));
});
});
describe('Wallet.rehydrate', () => {
it('should rehydrate a Wallet from props', () => {
const props: Wallet = {
id: 'wallet-123',
leagueId: 'league-456',
balance: 1000,
totalRevenue: 5000,
totalPlatformFees: 250,
totalWithdrawn: 3750,
currency: 'USD',
createdAt: new Date('2024-01-01'),
};
const rehydrated = Wallet.rehydrate(props);
expect(rehydrated).toEqual(props);
expect(rehydrated.id).toBe('wallet-123');
expect(rehydrated.leagueId).toBe('league-456');
expect(rehydrated.balance).toBe(1000);
expect(rehydrated.totalRevenue).toBe(5000);
expect(rehydrated.totalPlatformFees).toBe(250);
expect(rehydrated.totalWithdrawn).toBe(3750);
expect(rehydrated.currency).toBe('USD');
expect(rehydrated.createdAt).toEqual(new Date('2024-01-01'));
});
});
describe('Transaction interface', () => {
it('should have all required properties', () => {
const transaction: Transaction = {
id: 'txn-123',
walletId: 'wallet-456',
type: TransactionType.DEPOSIT,
amount: 1000,
description: 'Sponsorship payment',
createdAt: new Date('2024-01-01'),
};
expect(transaction.id).toBe('txn-123');
expect(transaction.walletId).toBe('wallet-456');
expect(transaction.type).toBe(TransactionType.DEPOSIT);
expect(transaction.amount).toBe(1000);
expect(transaction.description).toBe('Sponsorship payment');
expect(transaction.createdAt).toEqual(new Date('2024-01-01'));
});
it('should support optional referenceId and referenceType properties', () => {
const transaction: Transaction = {
id: 'txn-123',
walletId: 'wallet-456',
type: TransactionType.DEPOSIT,
amount: 1000,
description: 'Sponsorship payment',
referenceId: 'payment-789',
referenceType: ReferenceType.SPONSORSHIP,
createdAt: new Date('2024-01-01'),
};
expect(transaction.referenceId).toBe('payment-789');
expect(transaction.referenceType).toBe(ReferenceType.SPONSORSHIP);
});
});
describe('Transaction.rehydrate', () => {
it('should rehydrate a Transaction from props', () => {
const props: Transaction = {
id: 'txn-123',
walletId: 'wallet-456',
type: TransactionType.DEPOSIT,
amount: 1000,
description: 'Sponsorship payment',
createdAt: new Date('2024-01-01'),
};
const rehydrated = Transaction.rehydrate(props);
expect(rehydrated).toEqual(props);
expect(rehydrated.id).toBe('txn-123');
expect(rehydrated.walletId).toBe('wallet-456');
expect(rehydrated.type).toBe(TransactionType.DEPOSIT);
expect(rehydrated.amount).toBe(1000);
expect(rehydrated.description).toBe('Sponsorship payment');
expect(rehydrated.createdAt).toEqual(new Date('2024-01-01'));
});
it('should preserve optional referenceId and referenceType when rehydrating', () => {
const props: Transaction = {
id: 'txn-123',
walletId: 'wallet-456',
type: TransactionType.DEPOSIT,
amount: 1000,
description: 'Sponsorship payment',
referenceId: 'payment-789',
referenceType: ReferenceType.SPONSORSHIP,
createdAt: new Date('2024-01-01'),
};
const rehydrated = Transaction.rehydrate(props);
expect(rehydrated.referenceId).toBe('payment-789');
expect(rehydrated.referenceType).toBe(ReferenceType.SPONSORSHIP);
});
});
describe('Business rules and invariants', () => {
it('should calculate balance correctly', () => {
const wallet: Wallet = {
id: 'wallet-123',
leagueId: 'league-456',
balance: 1000,
totalRevenue: 5000,
totalPlatformFees: 250,
totalWithdrawn: 3750,
currency: 'USD',
createdAt: new Date('2024-01-01'),
};
// Balance should be: totalRevenue - totalPlatformFees - totalWithdrawn
const expectedBalance = wallet.totalRevenue - wallet.totalPlatformFees - wallet.totalWithdrawn;
expect(wallet.balance).toBe(expectedBalance);
});
it('should support different transaction types', () => {
const depositTransaction: Transaction = {
id: 'txn-123',
walletId: 'wallet-456',
type: TransactionType.DEPOSIT,
amount: 1000,
description: 'Sponsorship payment',
createdAt: new Date('2024-01-01'),
};
const withdrawalTransaction: Transaction = {
id: 'txn-124',
walletId: 'wallet-456',
type: TransactionType.WITHDRAWAL,
amount: 500,
description: 'Withdrawal to bank',
createdAt: new Date('2024-01-01'),
};
const platformFeeTransaction: Transaction = {
id: 'txn-125',
walletId: 'wallet-456',
type: TransactionType.PLATFORM_FEE,
amount: 50,
description: 'Platform fee deduction',
createdAt: new Date('2024-01-01'),
};
expect(depositTransaction.type).toBe(TransactionType.DEPOSIT);
expect(withdrawalTransaction.type).toBe(TransactionType.WITHDRAWAL);
expect(platformFeeTransaction.type).toBe(TransactionType.PLATFORM_FEE);
});
it('should support different reference types', () => {
const sponsorshipTransaction: Transaction = {
id: 'txn-123',
walletId: 'wallet-456',
type: TransactionType.DEPOSIT,
amount: 1000,
description: 'Sponsorship payment',
referenceId: 'payment-789',
referenceType: ReferenceType.SPONSORSHIP,
createdAt: new Date('2024-01-01'),
};
const membershipFeeTransaction: Transaction = {
id: 'txn-124',
walletId: 'wallet-456',
type: TransactionType.DEPOSIT,
amount: 100,
description: 'Membership fee payment',
referenceId: 'payment-790',
referenceType: ReferenceType.MEMBERSHIP_FEE,
createdAt: new Date('2024-01-01'),
};
const prizeTransaction: Transaction = {
id: 'txn-125',
walletId: 'wallet-456',
type: TransactionType.WITHDRAWAL,
amount: 500,
description: 'Prize payout',
referenceId: 'prize-791',
referenceType: ReferenceType.PRIZE,
createdAt: new Date('2024-01-01'),
};
expect(sponsorshipTransaction.referenceType).toBe(ReferenceType.SPONSORSHIP);
expect(membershipFeeTransaction.referenceType).toBe(ReferenceType.MEMBERSHIP_FEE);
expect(prizeTransaction.referenceType).toBe(ReferenceType.PRIZE);
});
it('should handle zero and negative amounts', () => {
const zeroTransaction: Transaction = {
id: 'txn-123',
walletId: 'wallet-456',
type: TransactionType.DEPOSIT,
amount: 0,
description: 'Zero amount transaction',
createdAt: new Date('2024-01-01'),
};
expect(zeroTransaction.amount).toBe(0);
});
it('should handle different currencies', () => {
const usdWallet: Wallet = {
id: 'wallet-123',
leagueId: 'league-456',
balance: 1000,
totalRevenue: 5000,
totalPlatformFees: 250,
totalWithdrawn: 3750,
currency: 'USD',
createdAt: new Date('2024-01-01'),
};
const eurWallet: Wallet = {
id: 'wallet-124',
leagueId: 'league-457',
balance: 1000,
totalRevenue: 5000,
totalPlatformFees: 250,
totalWithdrawn: 3750,
currency: 'EUR',
createdAt: new Date('2024-01-01'),
};
expect(usdWallet.currency).toBe('USD');
expect(eurWallet.currency).toBe('EUR');
});
});
});