Files
iyzico/tests/setup.ts
Duhan BALCI c65195d26d init
2026-01-01 18:30:21 +03:00

86 lines
2.4 KiB
TypeScript

/**
* Test setup and utilities
*/
import { config } from 'dotenv';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
import { IyzicoClient } from '../src/client';
// Get the directory of this file (for ESM compatibility)
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Load environment variables from .env file
// Resolve path relative to project root (two levels up from tests/setup.ts)
const envPath = resolve(__dirname, '..', '.env');
config({ path: envPath });
/**
* Creates an İyzico client for testing using environment variables
* @returns Configured IyzicoClient instance
*/
export function createTestClient(): IyzicoClient {
const apiKey = process.env.IYZICO_API_KEY;
const secretKey = process.env.IYZICO_API_SECRET;
const baseUrl = process.env.IYZICO_BASE_URL || 'https://sandbox-api.iyzipay.com';
if (!apiKey) {
throw new Error('IYZICO_API_KEY environment variable is required for tests');
}
if (!secretKey) {
throw new Error('IYZICO_API_SECRET environment variable is required for tests');
}
// Enable retry on rate limit for tests
const retryOnRateLimit = process.env.IYZICO_RETRY_ON_RATE_LIMIT !== 'false';
const maxRetries = Number.parseInt(process.env.IYZICO_MAX_RETRIES || '3', 10);
const retryDelay = Number.parseInt(process.env.IYZICO_RETRY_DELAY_MS || '10000', 10);
return new IyzicoClient({
apiKey,
secretKey,
baseUrl,
locale: 'tr',
retryOnRateLimit,
maxRetries,
retryDelay,
});
}
/**
* Asserts that a response has a success status
*/
export function assertSuccessResponse(response: { status: string }): void {
if (response.status !== 'success') {
throw new Error(`Expected success status, got: ${response.status}`);
}
}
/**
* Asserts that a response has a failure status
*/
export function assertFailureResponse(response: { status: string }): void {
if (response.status !== 'failure') {
throw new Error(`Expected failure status, got: ${response.status}`);
}
}
/**
* Delay utility to prevent rate limiting
* Adds a small delay between test requests
*/
export function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Default delay between API calls to avoid rate limiting
* Can be overridden via IYZICO_TEST_DELAY_MS environment variable
*/
export const DEFAULT_TEST_DELAY_MS = Number.parseInt(
process.env.IYZICO_TEST_DELAY_MS || '200',
10
);