# Test and Debug Your Integration

This guide outlines best practices for debugging your integration with the `redsys-tpv-business-lib` and running automated tests on physical devices.

## Enabling Debug Logs (Timber)

The Get Smart SDK integrates the **Timber** library to facilitate traceability and debugging. To view internal SDK logs in Android Studio's Logcat, you must "plant" a Debug Tree when your application starts.

### Configuration

Add the Timber configuration to your `Application` class's `onCreate` method.

```kotlin
@HiltAndroidApp
class Application : Application() {

    override fun onCreate() {
        super.onCreate()

        // Plant the debug tree only in Debug builds
        if (BuildConfig.DEBUG) {
            Timber.plant(Timber.DebugTree())
        }
    }
}
```

Once configured, you will see detailed SDK operations in Logcat, which is essential for diagnosing `ProtocolError` or connection issues.

## Running Tests on Physical Devices

The SDK is designed to be tested using **AndroidJUnit4**. Because the library relies on the **Get Smart SDK Payment Service** (which interacts with specific secure hardware and host connections), **you cannot run these tests on a standard Android Emulator**. You must use a physical TPV device.

### **Prerequisites**

1. **Physical TPV**: An Android device certified by Redsys/Getnet.  
2. **Service Installation**: The Get Smart SDK Payment Service app must be installed and running on the device.  
3. **Test Runner**: Your project must be configured to run instrumented tests (androidTest).

### Example: Initialization Test

This test validates that your app can successfully connect to the service and retrieve terminal configuration.

```kotlin
@Test
fun executeInitTpv() = runTest {
    // 1. Get the Context
    val appContext = InstrumentationRegistry.getInstrumentation().targetContext

    // 2. Instantiate the repository manually (bypassing Dagger for this isolated test)
    val initializationRepository = PsaInitializationRepository.newInstance(appContext)

    // 3. Execute Initialization
    val result = initializationRepository.initTpv()

    // 4. Validate results
    println("TPV Info: $result")
    assert(result is RepositoryResult.Success)
}
```

### Example: Payment Test

This test performs a real transaction. Use a test card or a nominal amount (e.g., 1.00€).

```kotlin
@Test
fun executePayment() = runTest {
    val appContext = InstrumentationRegistry.getInstrumentation().targetContext

    // Initialize first
    val initializationRepository = PsaInitializationRepository.newInstance(appContext)
    initializationRepository.initTpv()

    val paymentRepository = PsaPaymentRepository.newInstance(appContext)

    // Execute Payment
    val result = paymentRepository.makePayment(
        amount = Money(BigDecimal("1.00"), Currency.getInstance("EUR"))
    )

    println("Payment Result: $result")

    // Assert Success
    assert(result is RepositoryResult.Success)
}
```

### Example: Transaction History Test

This test validates that you can retrieve history for the current day.

```kotlin
@Test
fun getTransactions() = runTest {
    val appContext = InstrumentationRegistry.getInstrumentation().targetContext
    val transactionRepository = PsaTransactionRepository.newInstance(appContext)

    // Define query range (Today)
    val calendar = Calendar.getInstance()
    val endDate = calendar.time
    calendar.set(Calendar.HOUR_OF_DAY, 0)
    val initDate = calendar.time

    val result = transactionRepository.getTransactions(
        initDate = initDate,
        endDate = endDate
    )

    if (result is RepositoryResult.Success) {
        println("Found ${result.data.transactionList.size} transactions")
    } else {
        println("Error fetching transactions: $result")
    }
}
```

## Summary

* **Logs**: Use `Timber.plant(Timber.DebugTree())` to enable verbose SDK logging.  
* **Hardware**: Tests must run on physical hardware with the service installed.  
* **Framework**: Use `AndroidJUnit4` and `runTest` for coroutine support in tests.