Skip to content

Commit

Permalink
fix: use noop insights by default on maestro
Browse files Browse the repository at this point in the history
  • Loading branch information
amanjeetsingh150 committed Nov 12, 2024
1 parent 673e4ff commit 75c8847
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ import maestro.orchestra.CompositeCommand
import maestro.orchestra.MaestroCommand
import maestro.orchestra.Orchestra
import maestro.orchestra.yaml.YamlCommandReader
import maestro.utils.Insight
import maestro.utils.Insights
import org.slf4j.LoggerFactory
import java.util.IdentityHashMap
import maestro.cli.util.ScreenshotUtils
import maestro.utils.Insight

/**
* Knows how to run a list of Maestro commands and update the UI.
Expand Down Expand Up @@ -91,6 +92,7 @@ object MaestroCommandRunner {

val orchestra = Orchestra(
maestro = maestro,
insights = Insights,
onCommandStart = { _, command ->
logger.info("${command.description()} RUNNING")
commandStatuses[command] = CommandStatus.RUNNING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import maestro.Maestro
import maestro.cli.device.Device
import maestro.cli.device.PickDeviceInteractor
import maestro.cli.device.Platform
import maestro.utils.Insights
import maestro.cli.util.ScreenReporter
import maestro.drivers.AndroidDriver
import maestro.drivers.IOSDriver
Expand Down Expand Up @@ -304,7 +305,9 @@ object MaestroSessionManager {
deviceId = deviceId,
xcTestDevice = xcTestDevice,
simctlIOSDevice = simctlIOSDevice,
)
insights = Insights
),
insights = Insights
)

return Maestro.ios(
Expand Down
5 changes: 3 additions & 2 deletions maestro-client/src/main/java/maestro/drivers/IOSDriver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import kotlin.collections.set

class IOSDriver(
private val iosDevice: IOSDevice,
private val insights: InsightsInterface = NoopInsights
) : Driver {

private var appId: String? = null
Expand Down Expand Up @@ -138,9 +139,9 @@ class IOSDriver(
"If you are using React native, consider migrating to the new " +
"architecture where view flattening is available. For more information on the " +
"migration process, please visit: https://reactnative.dev/docs/new-architecture-intro"
Insights.report(Insight(message, Insight.Level.INFO))
insights.report(Insight(message, Insight.Level.INFO))
} else {
Insights.report(Insight("", Insight.Level.NONE))
insights.report(Insight("", Insight.Level.NONE))
}
val hierarchy = hierarchyResult.axElement
return mapViewHierarchy(hierarchy)
Expand Down
6 changes: 4 additions & 2 deletions maestro-ios/src/main/java/ios/LocalIOSDevice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import java.io.InputStream
import java.util.UUID
import hierarchy.ViewHierarchy
import maestro.utils.Insight
import maestro.utils.Insights
import maestro.utils.InsightsInterface
import maestro.utils.NoopInsights
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

class LocalIOSDevice(
override val deviceId: String?,
private val xcTestDevice: XCTestIOSDevice,
private val simctlIOSDevice: SimctlIOSDevice,
private val insights: InsightsInterface = NoopInsights
) : IOSDevice {

private val executor by lazy { Executors.newSingleThreadScheduledExecutor() }
Expand All @@ -34,7 +36,7 @@ class LocalIOSDevice(
val future = executor.schedule(
{
if (isViewHierarchyInProgress) {
Insights.report(
insights.report(
Insight(
message = "Retrieving the hierarchy is taking longer than usual. This might be due to a " +
"deep hierarchy in the current view. Please wait a bit more to complete the operation.",
Expand Down
14 changes: 8 additions & 6 deletions maestro-orchestra/src/main/java/maestro/orchestra/Orchestra.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ import maestro.orchestra.geo.Traveller
import maestro.orchestra.util.Env.evaluateScripts
import maestro.orchestra.yaml.YamlCommandReader
import maestro.utils.Insight
import maestro.utils.Insights
import maestro.utils.InsightsInterface
import maestro.utils.MaestroTimer
import maestro.utils.NoopInsights
import maestro.utils.StringUtils.toRegexSafe
import okhttp3.OkHttpClient
import okio.Buffer
Expand Down Expand Up @@ -76,11 +77,12 @@ class Orchestra(
private val lookupTimeoutMs: Long = 17000L,
private val optionalLookupTimeoutMs: Long = 7000L,
private val httpClient: OkHttpClient? = null,
private val insights: InsightsInterface = NoopInsights,
private val onFlowStart: (List<MaestroCommand>) -> Unit = {},
private val onCommandStart: (Int, MaestroCommand) -> Unit = { _, _ -> },
private val onCommandComplete: (Int, MaestroCommand) -> Unit = { _, _ -> },
private val onCommandFailed: (Int, MaestroCommand, Throwable) -> ErrorResolution = { _, _, e -> throw e },
private val onCommandWarned: (Int, MaestroCommand) -> Unit = { _, _ -> },
private val onCommandWarned: (Int, MaestroCommand) -> Unit = { _, _ -> },
private val onCommandSkipped: (Int, MaestroCommand) -> Unit = { _, _ -> },
private val onCommandReset: (MaestroCommand) -> Unit = {},
private val onCommandMetadataUpdate: (MaestroCommand, CommandMetadata) -> Unit = { _, _ -> },
Expand Down Expand Up @@ -186,7 +188,7 @@ class Orchestra(
)
)
}
Insights.onInsightsUpdated(callback)
insights.onInsightsUpdated(callback)

try {
try {
Expand All @@ -199,7 +201,7 @@ class Orchestra(
}
} catch (ignored: CommandWarned) {
// Swallow exception, but add a warning as an insight
Insights.report(Insight(message = ignored.message, level = Insight.Level.WARNING))
insights.report(Insight(message = ignored.message, level = Insight.Level.WARNING))
onCommandWarned(index, command)
} catch (ignored: CommandSkipped) {
// Swallow exception
Expand All @@ -211,7 +213,7 @@ class Orchestra(
ErrorResolution.CONTINUE -> {} // Do nothing
}
}
Insights.unregisterListener(callback)
insights.unregisterListener(callback)
}
return true
}
Expand Down Expand Up @@ -690,7 +692,7 @@ class Orchestra(
}
} catch (ignored: CommandWarned) {
// Swallow exception, but add a warning as an insight
Insights.report(Insight(message = ignored.message, level = Insight.Level.WARNING))
insights.report(Insight(message = ignored.message, level = Insight.Level.WARNING))
onCommandWarned(index, command)
false
} catch (ignored: CommandSkipped) {
Expand Down
21 changes: 5 additions & 16 deletions maestro-utils/src/main/kotlin/Insight.kt
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
package maestro.utils

object Insights {
object Insights: InsightsInterface {

private var insight: Insight = Insight("", Insight.Level.NONE)
private val listeners = mutableListOf<(Insight) -> Unit>()

fun report(insight: Insight) {
this.insight = insight
override fun report(insight: Insight) {
Insights.insight = insight
listeners.forEach { it.invoke(insight) }
}

fun onInsightsUpdated(callback: (Insight) -> Unit) {
override fun onInsightsUpdated(callback: (Insight) -> Unit) {
listeners.add(callback)
}

fun unregisterListener(callback: (Insight) -> Unit) {
override fun unregisterListener(callback: (Insight) -> Unit) {
listeners.remove(callback)
}
}

data class Insight(
val message: String,
val level: Level
) {
enum class Level {
WARNING,
INFO,
NONE
}
}
38 changes: 38 additions & 0 deletions maestro-utils/src/main/kotlin/InsightsInterface.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package maestro.utils

interface InsightsInterface {

fun report(insight: Insight)

fun onInsightsUpdated(callback: (Insight) -> Unit)

fun unregisterListener(callback: (Insight) -> Unit)
}

object NoopInsights: InsightsInterface {

override fun report(insight: Insight) {
/* no-op */
}

override fun onInsightsUpdated(callback: (Insight) -> Unit) {
/* no-op */
}

override fun unregisterListener(callback: (Insight) -> Unit) {
/* no-op */
}

}


data class Insight(
val message: String,
val level: Level
) {
enum class Level {
WARNING,
INFO,
NONE
}
}

0 comments on commit 75c8847

Please sign in to comment.