package com.yorvana.ui.util

import androidx.compose.ui.test.SemanticsNodeInteraction
import com.github.takahirom.roborazzi.ExperimentalRoborazziApi
import com.github.takahirom.roborazzi.captureRoboImage
import com.github.takahirom.roborazzi.captureScreenRoboImage
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34])
abstract class BaseScreenshotTest {
    /**
     * Automatically captures a Roborazzi snapshot of a specific node and places it in a `snapshots` directory
     * adjacent to the executing test class, maintaining the package structure.
     *
     * @param fileName The name of the snapshot file (without .png extension)
     */
    protected fun SemanticsNodeInteraction.captureScreen(fileName: String) {
        val path = getSnapshotPath(fileName)
        this.captureRoboImage(path)
    }

    /**
     * Captures the entire screen including popups and dialogs.
     */
    @OptIn(ExperimentalRoborazziApi::class)
    protected fun captureFull(fileName: String) {
        val path = getSnapshotPath(fileName)
        captureScreenRoboImage(path)
    }

    private fun getSnapshotPath(fileName: String): String {
        val pkgPath = this@BaseScreenshotTest.javaClass.packageName.replace(".", "/")
        return "src/test/java/$pkgPath/snapshots/$fileName.png"
    }
}
