Files
bunq/test/test.ts
Juergen Kunz 193524f15c update
2025-07-18 10:31:12 +00:00

139 lines
4.7 KiB
TypeScript

import { expect, tap } from '@git.zone/tstest/tapbundle';
import { Qenv } from '@push.rocks/qenv';
const testQenv = new Qenv('./', './.nogit/');
import * as bunq from '../ts';
let testBunqAccount: bunq.BunqAccount;
let sandboxApiKey: string;
tap.test('should create a sandbox API key when needed', async () => {
// Check if we have an API key from environment
const envApiKey = await testQenv.getEnvVarOnDemand('BUNQ_APIKEY');
if (!envApiKey) {
// Create a temporary bunq account to generate sandbox API key
const tempAccount = new bunq.BunqAccount({
apiKey: '',
deviceName: 'bunq-test-generator',
environment: 'SANDBOX',
});
sandboxApiKey = await tempAccount.createSandboxUser();
console.log('Generated new sandbox API key');
} else {
sandboxApiKey = envApiKey;
console.log('Using existing API key from environment');
}
expect(sandboxApiKey).toBeTypeofString();
expect(sandboxApiKey.length).toBeGreaterThan(0);
});
tap.test('should create a valid bunq account', async () => {
testBunqAccount = new bunq.BunqAccount({
apiKey: sandboxApiKey,
deviceName: 'bunq-api-test',
environment: 'SANDBOX',
});
expect(testBunqAccount).toBeInstanceOf(bunq.BunqAccount);
});
tap.test('should init the client', async () => {
await testBunqAccount.init();
expect(testBunqAccount.userId).toBeTypeofNumber();
expect(testBunqAccount.userType).toBeOneOf(['UserPerson', 'UserCompany', 'UserApiKey']);
console.log(`Initialized as ${testBunqAccount.userType} with ID ${testBunqAccount.userId}`);
});
tap.test('should get accounts', async () => {
const accounts = await testBunqAccount.getAccounts();
expect(accounts).toBeArray();
expect(accounts.length).toBeGreaterThan(0);
console.log(`Found ${accounts.length} accounts:`);
for (const account of accounts) {
console.log(`- ${account.description}: ${account.balance.currency} ${account.balance.value}`);
expect(account).toBeInstanceOf(bunq.BunqMonetaryAccount);
expect(account.id).toBeTypeofNumber();
expect(account.balance).toHaveProperty('value');
expect(account.balance).toHaveProperty('currency');
}
});
tap.test('should get transactions', async () => {
const accounts = await testBunqAccount.getAccounts();
const account = accounts[0];
const transactions = await account.getTransactions();
expect(transactions).toBeArray();
console.log(`Found ${transactions.length} transactions`);
if (transactions.length > 0) {
const firstTransaction = transactions[0];
expect(firstTransaction).toBeInstanceOf(bunq.BunqTransaction);
expect(firstTransaction.amount).toHaveProperty('value');
expect(firstTransaction.amount).toHaveProperty('currency');
console.log(`Latest transaction: ${firstTransaction.amount.value} ${firstTransaction.amount.currency} - ${firstTransaction.description}`);
}
});
tap.test('should test payment builder', async () => {
const accounts = await testBunqAccount.getAccounts();
const account = accounts[0];
// Test payment builder without actually creating the payment
const paymentBuilder = bunq.BunqPayment.builder(testBunqAccount, account)
.amount('10.00', 'EUR')
.toIban('NL91ABNA0417164300', 'Test Recipient')
.description('Test payment');
expect(paymentBuilder).toBeDefined();
console.log('Payment builder created successfully');
});
tap.test('should test user management', async () => {
const user = testBunqAccount.getUser();
expect(user).toBeInstanceOf(bunq.BunqUser);
const userInfo = await user.getInfo();
expect(userInfo).toBeDefined();
console.log(`User type: ${Object.keys(userInfo)[0]}`);
});
tap.test('should test notification filters', async () => {
const notification = new bunq.BunqNotification(testBunqAccount);
const urlFilters = await notification.listUrlFilters();
expect(urlFilters).toBeArray();
console.log(`Currently ${urlFilters.length} URL notification filters`);
const pushFilters = await notification.listPushFilters();
expect(pushFilters).toBeArray();
console.log(`Currently ${pushFilters.length} push notification filters`);
});
tap.test('should test card listing', async () => {
try {
const cards = await bunq.BunqCard.list(testBunqAccount);
expect(cards).toBeArray();
console.log(`Found ${cards.length} cards`);
for (const card of cards) {
expect(card).toBeInstanceOf(bunq.BunqCard);
console.log(`Card: ${card.nameOnCard} - ${card.type} (${card.status})`);
}
} catch (error) {
console.log('No cards found (normal for new sandbox accounts)');
}
});
tap.test('should stop the instance', async () => {
await testBunqAccount.stop();
console.log('bunq client stopped successfully');
});
tap.start();