@momo-kits/native-kits 0.169.13-beta.3 → 0.169.13-beta.3-debug
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build.gradle.kts +11 -0
- package/compose/build.gradle.kts +202 -0
- package/compose/compose.podspec +47 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/const/HostFont.android.kt +19 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/navigation/ScrollToTop.android.kt +6 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/platform/BackGesture.android.kt +12 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/platform/OsFontScale.android.kt +7 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/platform/Platform.android.kt +154 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/platform/ReduceMotion.android.kt +17 -0
- package/compose/src/androidMain/kotlin/vn/momo/kits/platform/ReservedRegions.android.kt +6 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiActionsTest.kt +647 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiExprTest.kt +1411 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiHostTest.kt +130 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiKitArgsTest.kt +524 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiKitNodesTest.kt +559 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiRenderPlanTest.kt +741 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiRenderReuseTest.kt +221 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiRendererPropsTest.kt +270 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiStylePlanTest.kt +849 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiStyleTest.kt +212 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiTrackingTest.kt +239 -0
- package/compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiViewTest.kt +252 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/Context.kt +110 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/DesignSystemKits.kt +61 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/LiteScreen.kt +1067 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/Localize.kt +268 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/application/WidgetContainer.kt +56 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Avatar.kt +157 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Badge.kt +81 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/BadgeDot.kt +32 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/BadgeRibbon.kt +338 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/BaselineView.kt +175 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Button.kt +354 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Carousel.kt +115 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/CheckBox.kt +89 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Chip.kt +130 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Collapse.kt +209 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/CupertinoOverscroll.kt +482 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Divider.kt +49 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Icon.kt +86 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/IconButton.kt +125 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Image.kt +224 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Information.kt +137 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Input.kt +473 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputDropDown.kt +113 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputMoney.kt +262 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputOTP.kt +237 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputPhoneNumber.kt +228 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputSearch.kt +280 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputTextArea.kt +183 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/LazyColumnWithBouncing.kt +342 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Loader.kt +108 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationDot.kt +56 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationNumber.kt +40 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationScroll.kt +82 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationWhiteDot.kt +39 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PopupNotify.kt +362 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/PopupPromotion.kt +108 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/ProgressInfo.kt +304 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Radio.kt +59 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Rating.kt +87 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/ScaleSizeScope.kt +21 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Skeleton.kt +88 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Slider.kt +308 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Stepper.kt +218 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Steps.kt +456 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/SuggestAction.kt +122 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Swipe.kt +205 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Switch.kt +89 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/TabView.kt +542 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Tag.kt +86 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Text.kt +175 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Title.kt +201 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Tooltip.kt +498 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/TrustBanner.kt +176 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/Uploader.kt +182 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePicker.kt +193 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePickerTypes.kt +29 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePickerUtils.kt +208 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/WheelPicker.kt +180 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/Colors.kt +325 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/HostFont.kt +12 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/Radius.kt +12 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/Spacing.kt +16 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/Theme.kt +194 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/const/Typography.kt +291 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Card.kt +54 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/HeaderLayout.kt +71 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Item.kt +50 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/ModalLayout.kt +184 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/ScreenLayout.kt +315 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Section.kt +41 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/AutomationId.kt +54 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Clickable.kt +66 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Conditional.kt +11 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/DeprecatedModifier.kt +104 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Shadow.kt +48 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Size.kt +51 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/BottomSheet.kt +397 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/HostBackGestureBridge.kt +58 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/ModalScreen.kt +153 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/Navigation.kt +118 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/NavigationContainer.kt +206 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/Navigator.kt +421 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/ScrollToTop.kt +8 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/StackScreen.kt +750 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/StackTransition.kt +152 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/SwipeBackHaptic.kt +35 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/BottomTab.kt +242 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/BottomTabBar.kt +398 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/CurvedContainer.kt +103 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/DuoBarCompression.kt +93 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/DuoTabBar.kt +208 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/TabBarMetrics.kt +133 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/FloatingButton.kt +167 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/Header.kt +446 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderBackground.kt +105 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderJourney.kt +106 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderLocation.kt +74 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderRight.kt +400 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderTitle.kt +39 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderUser.kt +363 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/SideBar.kt +197 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/SnackBar.kt +133 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/tracking/ScreenTracker.kt +127 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/BackGesture.kt +12 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ComposeLottieAnimation.kt +114 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/OsFontScale.kt +7 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/Platform.kt +76 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ReduceMotion.kt +7 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ReservedRegions.kt +21 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiActions.kt +154 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiExpr.kt +1038 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiKitNodes.kt +848 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiLog.kt +47 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiModel.kt +39 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiPropsRenderer.kt +463 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiRenderer.kt +1146 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiStyle.kt +667 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiTracking.kt +240 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiValidate.kt +185 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/sdui/SduiView.kt +200 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Icons.kt +1329 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Resources.kt +55 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Tracking.kt +15 -0
- package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Utils.kt +117 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/const/HostFont.ios.kt +42 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/navigation/HostBackGesture.ios.kt +45 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/navigation/ScrollToTop.ios.kt +32 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/platform/BackGesture.ios.kt +38 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/platform/OsFontScale.ios.kt +65 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/platform/Platform.ios.kt +217 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/platform/ReduceMotion.ios.kt +48 -0
- package/compose/src/iosMain/kotlin/vn/momo/kits/platform/ReservedRegions.ios.kt +143 -0
- package/gradle/libs.versions.toml +69 -0
- package/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/gradle/wrapper/gradle-wrapper.properties +8 -0
- package/gradle.properties +24 -0
- package/gradlew +252 -0
- package/gradlew.bat +94 -0
- package/ios-demo/README.md +120 -0
- package/package.json +1 -1
- package/scripts/sdui-visual/README.md +64 -0
- package/settings.gradle.kts +64 -0
|
@@ -0,0 +1,647 @@
|
|
|
1
|
+
// File: compose/src/androidUnitTest/kotlin/vn/momo/kits/sdui/SduiActionsTest.kt
|
|
2
|
+
// Created At: 2026-10-27 15:50:00 +07:00
|
|
3
|
+
// Created By: AI
|
|
4
|
+
// AI Agent: Claude Code
|
|
5
|
+
// Model: claude-sonnet-5-5
|
|
6
|
+
|
|
7
|
+
package vn.momo.kits.sdui
|
|
8
|
+
|
|
9
|
+
import androidx.compose.runtime.mutableStateMapOf
|
|
10
|
+
import org.junit.Assert.assertEquals
|
|
11
|
+
import org.junit.Assert.assertFalse
|
|
12
|
+
import org.junit.Assert.assertNull
|
|
13
|
+
import org.junit.Assert.assertTrue
|
|
14
|
+
import org.junit.Test
|
|
15
|
+
import java.lang.reflect.Proxy
|
|
16
|
+
import vn.momo.kits.const.defaultTheme
|
|
17
|
+
import vn.momo.maxapi.IMaxApi
|
|
18
|
+
|
|
19
|
+
// AI-GENERATED START: unit tests for repeat expansion, the action runner
|
|
20
|
+
class SduiActionsTest {
|
|
21
|
+
private val features = mutableListOf<Pair<String, Map<String, Any>>>()
|
|
22
|
+
private val featureApi: IMaxApi = Proxy.newProxyInstance(IMaxApi::class.java.classLoader, arrayOf(IMaxApi::class.java)) { _, m, args ->
|
|
23
|
+
@Suppress("UNCHECKED_CAST")
|
|
24
|
+
if (m.name == "startFeatureCode") {
|
|
25
|
+
val a = (args!![0] as? Array<*>)?.toList() ?: args.toList()
|
|
26
|
+
features += (a[0] as String) to (a[1] as Map<String, Any>)
|
|
27
|
+
}
|
|
28
|
+
when (m.returnType) { Boolean::class.javaPrimitiveType -> false; Int::class.javaPrimitiveType -> 0; Long::class.javaPrimitiveType -> 0L; else -> null }
|
|
29
|
+
} as IMaxApi
|
|
30
|
+
private val featureMaxApi = SduiMaxApi({ featureApi })
|
|
31
|
+
|
|
32
|
+
private val state = mutableStateMapOf<String, Any?>("onboarded" to false, "page" to 0L)
|
|
33
|
+
private val sink = object : TrackSink {
|
|
34
|
+
override val id = "rec"
|
|
35
|
+
val records = mutableListOf<TrackRecord>()
|
|
36
|
+
override fun write(record: TrackRecord) { records += record }
|
|
37
|
+
}
|
|
38
|
+
private val session = Tracker(listOf(sink), "sess").begin(SessionCtx("sdui:view:t"))
|
|
39
|
+
private val runner = SduiActionRunner(state, { defaultTheme }, { session }, featureMaxApi)
|
|
40
|
+
private val scope = SduiScope(emptyMap(), state)
|
|
41
|
+
private fun run(action: Map<String, Any?>, sc: SduiScope = scope, args: List<Any?> = emptyList()) = runner.runOne(action, sc, args)
|
|
42
|
+
|
|
43
|
+
// ---- repeat expansion ---------------------------------------------------------------------------------------
|
|
44
|
+
@Test fun repeatExpandsPerItemWithAliasAndIndex() {
|
|
45
|
+
val sc = SduiScope(mapOf("categories" to listOf(mapOf("id" to "a"), mapOf("id" to "b"), mapOf("id" to "c"))), state)
|
|
46
|
+
val node = mapOf("type" to "Item", "repeat" to mapOf("source" to "categories", "as" to "c", "index" to "i"))
|
|
47
|
+
val out = expand(listOf(node), sc, defaultTheme, null)
|
|
48
|
+
assertEquals(3, out.size)
|
|
49
|
+
assertEquals(listOf("a", "b", "c"), out.map { evalExpr("c.id", it.scope) })
|
|
50
|
+
assertEquals(listOf(0L, 1L, 2L), out.map { evalExpr("i", it.scope) })
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@Test fun repeatAcceptsBindingSourcesAndFromAndDropsInvisible() {
|
|
54
|
+
val sc = SduiScope(mapOf("xs" to listOf(1L, 2L, 3L, 4L)), state)
|
|
55
|
+
val filtered = mapOf("type" to "Text", "repeat" to mapOf("source" to "{xs.filter(x => x > 2)}", "as" to "x"))
|
|
56
|
+
assertEquals(2, expand(listOf(filtered), sc, defaultTheme, null).size)
|
|
57
|
+
val from = mapOf("type" to "Text", "repeat" to mapOf("from" to "xs", "as" to "x"), "visible" to "{x != 2}")
|
|
58
|
+
assertEquals(3, expand(listOf(from), sc, defaultTheme, null).size)
|
|
59
|
+
val unknown = mapOf("type" to "Text", "repeat" to mapOf("source" to "nope", "as" to "x"))
|
|
60
|
+
assertEquals(0, expand(listOf(unknown), sc, defaultTheme, null).size)
|
|
61
|
+
val hidden = mapOf("type" to "View", "visible" to "{state.onboarded}")
|
|
62
|
+
assertEquals(0, expand(listOf(hidden), sc, defaultTheme, null).size)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ---- typed form {"type": "startFeatureCode"} -------------------------------------------------------------------
|
|
66
|
+
@Test fun typedStartFeatureCodeForwardsFeatureCodeAndRestParamsWithoutNulls() {
|
|
67
|
+
val sc = scope.with("p", mapOf("id" to "pvi"))
|
|
68
|
+
run(mapOf("name" to "onPress", "type" to "startFeatureCode", "params" to mapOf(
|
|
69
|
+
"featureCode" to "ins_marketplace", "screenName" to "detail", "partnerId" to "{p.id}", "gone" to null)), sc)
|
|
70
|
+
assertEquals(listOf("ins_marketplace" to mapOf<String, Any>("screenName" to "detail", "partnerId" to "pvi")), features)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
@Test fun typedStartFeatureCodeRejectsInvalidFeatureCode() {
|
|
74
|
+
run(mapOf("type" to "startFeatureCode", "params" to mapOf("featureCode" to "momo://x")))
|
|
75
|
+
run(mapOf("type" to "startFeatureCode", "params" to mapOf("featureCode" to "a b")))
|
|
76
|
+
run(mapOf("type" to "startFeatureCode", "params" to mapOf("x" to 1L)))
|
|
77
|
+
assertTrue(features.isEmpty())
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// ---- typed form {"type": "trackEvent"} -------------------------------------------------------------------------
|
|
81
|
+
private val tracked = mutableListOf<Pair<String, Map<String, Any>>>()
|
|
82
|
+
private val warnings = mutableListOf<String>()
|
|
83
|
+
private val trackApi: IMaxApi = Proxy.newProxyInstance(IMaxApi::class.java.classLoader, arrayOf(IMaxApi::class.java)) { _, m, args ->
|
|
84
|
+
@Suppress("UNCHECKED_CAST")
|
|
85
|
+
if (m.name == "startFeatureCode") {
|
|
86
|
+
val a = (args!![0] as? Array<*>)?.toList() ?: args.toList()
|
|
87
|
+
features += (a[0] as String) to (a[1] as Map<String, Any>)
|
|
88
|
+
}
|
|
89
|
+
if (m.name == "track") tracked += (args!![0] as String) to (args[1] as Map<String, Any>)
|
|
90
|
+
when (m.returnType) { Boolean::class.javaPrimitiveType -> false; Int::class.javaPrimitiveType -> 0; Long::class.javaPrimitiveType -> 0L; else -> null }
|
|
91
|
+
} as IMaxApi
|
|
92
|
+
private val trackLog = capturingLog(warnings)
|
|
93
|
+
private val trackRunner = SduiActionRunner(state, { defaultTheme }, { session }, SduiMaxApi({ trackApi }), trackLog)
|
|
94
|
+
private fun runTrack(action: Map<String, Any?>, sc: SduiScope = scope) = trackRunner.runOne(action, sc, emptyList())
|
|
95
|
+
private fun trackAction(params: Map<String, Any?>, extra: Map<String, Any?> = emptyMap()) =
|
|
96
|
+
mapOf("name" to "onPress", "type" to "trackEvent", "params" to params) + extra
|
|
97
|
+
|
|
98
|
+
@Test fun typedTrackEventCallsTrackWithCleanedParams() {
|
|
99
|
+
val sc = scope.with("total", 1500L)
|
|
100
|
+
runTrack(trackAction(mapOf(
|
|
101
|
+
"eventName" to "service_component_clicked", "screen_name" to "paylater_bills", "amount" to "{total}", "ok" to true,
|
|
102
|
+
"gone" to null, "map" to mapOf("a" to 1L), "list" to listOf(1L), "bad-key" to "x", "1bad" to "y")), sc)
|
|
103
|
+
assertEquals(listOf("service_component_clicked" to mapOf<String, Any>(
|
|
104
|
+
"screen_name" to "paylater_bills", "amount" to 1500L, "ok" to true)), tracked)
|
|
105
|
+
assertTrue(warnings.any { "map" in it }); assertTrue(warnings.any { "list" in it }); assertTrue(warnings.any { "bad-key" in it })
|
|
106
|
+
assertFalse(warnings.any { "gone" in it })
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
@Test fun typedTrackEventRejectsMissingOrInvalidEventName() {
|
|
110
|
+
runTrack(trackAction(mapOf("x" to 1L)))
|
|
111
|
+
runTrack(trackAction(mapOf("eventName" to "1bad")))
|
|
112
|
+
runTrack(trackAction(mapOf("eventName" to "has-dash")))
|
|
113
|
+
runTrack(trackAction(mapOf("eventName" to "a".repeat(65))))
|
|
114
|
+
runTrack(trackAction(mapOf("eventName" to 5L)))
|
|
115
|
+
assertTrue(tracked.isEmpty())
|
|
116
|
+
assertEquals(1, warnings.count { it.endsWith("trackEvent: invalid eventName") }) // same message: written once
|
|
117
|
+
assertTrue(sink.records.none { it.event.fields["verb"] == "track" })
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
@Test fun trackEventDropsNonFiniteNumbers() {
|
|
121
|
+
runTrack(trackAction(mapOf("eventName" to "tapped", "nan" to Double.NaN, "inf" to Double.POSITIVE_INFINITY, "n" to 2.5)))
|
|
122
|
+
assertEquals(listOf("tapped" to mapOf<String, Any>("n" to 2.5)), tracked)
|
|
123
|
+
assertEquals(2, warnings.count { "not a finite number" in it })
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
@Test fun trackEventWithoutApiIsSilentAndDoesNotThrow() {
|
|
127
|
+
val lines = ArrayList<String>()
|
|
128
|
+
val noApi = SduiActionRunner(state, { defaultTheme }, { session }, SduiMaxApi({ null }), capturingLog(lines))
|
|
129
|
+
noApi.runOne(trackAction(mapOf("eventName" to "tapped")), scope, emptyList())
|
|
130
|
+
assertTrue(tracked.isEmpty())
|
|
131
|
+
assertTrue(lines.isEmpty())
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
@Test fun trackEventRejectsTheReservedRendererEventName() {
|
|
135
|
+
runTrack(trackAction(mapOf("eventName" to "ops_info_checked", "step" to "render_done")))
|
|
136
|
+
runTrack(trackAction(mapOf("eventName" to "OPS_Info_Checked")))
|
|
137
|
+
assertTrue(tracked.isEmpty())
|
|
138
|
+
assertEquals(1, warnings.count { it.endsWith("trackEvent: reserved eventName") })
|
|
139
|
+
assertTrue(sink.records.none { it.event.fields["verb"] == "track" })
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
@Test fun invalidFeatureCodeWarningsDoNotCarryTheValue() {
|
|
143
|
+
runTrack(mapOf("name" to "onPress", "type" to "startFeatureCode", "params" to mapOf("featureCode" to "bad code!SECRET")))
|
|
144
|
+
runTrack(trackAction(mapOf("eventName" to "bad-name!SECRET4")))
|
|
145
|
+
assertEquals(2, warnings.size)
|
|
146
|
+
assertTrue(warnings.none { "SECRET" in it })
|
|
147
|
+
assertTrue(warnings.any { it.endsWith("startFeatureCode: invalid featureCode") })
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
@Test fun paramKeysThatCameFromDataAreNotLogged() {
|
|
151
|
+
val sc = scope.with("p", mapOf("eventName" to "tapped", "SECRET-key" to "x", "SECRETmap" to mapOf("a" to 1L)))
|
|
152
|
+
runTrack(mapOf("name" to "onPress", "type" to "trackEvent", "params" to "{p}"), sc)
|
|
153
|
+
assertEquals(listOf("tapped" to emptyMap<String, Any>()), tracked)
|
|
154
|
+
assertEquals(2, warnings.size)
|
|
155
|
+
assertTrue(warnings.none { "SECRET" in it })
|
|
156
|
+
assertTrue(warnings.all { "dropped param (from binding)" in it })
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
@Test fun trackEventThenStartFeatureCodeRunInSpecOrder() {
|
|
160
|
+
val actions = listOf(
|
|
161
|
+
trackAction(mapOf("eventName" to "tapped")),
|
|
162
|
+
mapOf("name" to "onPress", "type" to "startFeatureCode", "params" to mapOf("featureCode" to "ins_marketplace")))
|
|
163
|
+
trackRunner.run(actions, "onPress", scope, emptyList())
|
|
164
|
+
assertEquals(listOf("tapped"), tracked.map { it.first })
|
|
165
|
+
assertEquals(listOf("ins_marketplace"), features.map { it.first })
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
@Test fun trackEventWithFalseWhenTracksNothing() {
|
|
169
|
+
runTrack(trackAction(mapOf("eventName" to "tapped"), mapOf("when" to "{state.page > 5}")))
|
|
170
|
+
assertTrue(tracked.isEmpty())
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
@Test fun unknownTypeIsSkippedAndReported() {
|
|
174
|
+
run(mapOf("type" to "teleport", "params" to mapOf("page" to 9L, "featureCode" to "ok")), scope)
|
|
175
|
+
assertTrue(features.isEmpty())
|
|
176
|
+
assertEquals(0L, state["page"])
|
|
177
|
+
val err = sink.records.single { it.event.step == "sdui_node_error" }
|
|
178
|
+
assertEquals("invalid_action", err.event.fields["code"])
|
|
179
|
+
assertEquals("unknown_type", err.event.fields["reason"])
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ---- every invalid-action warning has its sdui_node_error twin ----------------------------------------------------
|
|
183
|
+
private fun nodeErrors() = sink.records.filter { it.event.step == "sdui_node_error" }.map { it.event.fields }
|
|
184
|
+
|
|
185
|
+
@Test fun eachRunnerReasonReportsInvalidActionWithNodeContext() {
|
|
186
|
+
val undeclared = SduiActionRunner(setState, { defaultTheme }, { session }, featureMaxApi, capturingLog(warnings), declaredState = setOf("a"))
|
|
187
|
+
fun case(reason: String, go: (Int) -> Unit) {
|
|
188
|
+
val before = nodeErrors().size
|
|
189
|
+
go(before)
|
|
190
|
+
val got = nodeErrors().drop(before)
|
|
191
|
+
assertEquals(reason, 1, got.size)
|
|
192
|
+
assertEquals(reason, "invalid_action", got[0]["code"])
|
|
193
|
+
assertEquals(reason, got[0]["reason"])
|
|
194
|
+
assertEquals("body.children.$reason", got[0]["nodePath"])
|
|
195
|
+
assertEquals("n_$reason", got[0]["nodeId"])
|
|
196
|
+
assertEquals("Button", got[0]["nodeType"])
|
|
197
|
+
}
|
|
198
|
+
fun at(r: String, a: Map<String, Any?>, runner: SduiActionRunner = trackRunner) = runner.runOne(a, scope, emptyList(), "body.children.$r", "n_$r", "Button")
|
|
199
|
+
case("missing_type") { at("missing_type", mapOf("name" to "onPress")) }
|
|
200
|
+
case("unknown_type") { at("unknown_type", mapOf("type" to "teleport")) }
|
|
201
|
+
case("invalid_feature_code") { at("invalid_feature_code", mapOf("type" to "startFeatureCode", "params" to mapOf("featureCode" to "a b"))) }
|
|
202
|
+
case("invalid_event_name") { at("invalid_event_name", trackAction(mapOf("eventName" to "1bad"))) }
|
|
203
|
+
case("reserved_event_name") { at("reserved_event_name", trackAction(mapOf("eventName" to "ops_info_checked"))) }
|
|
204
|
+
case("invalid_param") { at("invalid_param", trackAction(mapOf("eventName" to "ok_name", "bad-key" to "x"))) }
|
|
205
|
+
case("state_not_declared") { at("state_not_declared", mapOf("type" to "setState", "params" to mapOf("zzz" to 1L)), undeclared) }
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// ---- failures deep in value resolution are attributed to the node + path passed in (no ambient frame) ------------------
|
|
209
|
+
private fun attributed(data: Map<String, Any?> = emptyMap()): Pair<SduiScope, SduiCtx> {
|
|
210
|
+
val sc = SduiScope(data, state, nodeErrors = NodeErrorSink { session })
|
|
211
|
+
return sc to SduiCtx(defaultTheme, state, { _, _, _, _, _, _, _ -> }, session = session)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
@Test fun brokenExpressionInPropsReportsExprFailedOnThatNode() {
|
|
215
|
+
val (sc, ctx) = attributed()
|
|
216
|
+
val node = mapOf("type" to "Text", "id" to "t1", "props" to mapOf("children" to "{((}", "other" to "{[[}"))
|
|
217
|
+
propsOf(node, sc, ctx, "body.children.3")
|
|
218
|
+
val e = nodeErrors().single()
|
|
219
|
+
assertEquals(mapOf("step" to "sdui_node_error", "code" to "expr_failed", "reason" to "expr_failed", "nodePath" to "body.children.3", "nodeId" to "t1", "nodeType" to "Text"), e)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
@Test fun unknownTokenInStylesReportsUnknownTokenOnThatNode() {
|
|
223
|
+
val (sc, ctx) = attributed()
|
|
224
|
+
val node = mapOf("type" to "View", "styles" to mapOf("padding" to "Spacing.ZZZ"))
|
|
225
|
+
styOf(node, sc, ctx, "body")
|
|
226
|
+
val e = nodeErrors().single()
|
|
227
|
+
assertEquals("unknown_token", e["code"]); assertEquals("unknown_token", e["reason"]); assertEquals("body", e["nodePath"]); assertEquals("View", e["nodeType"])
|
|
228
|
+
assertFalse(e.containsKey("nodeId"))
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
@Test fun aQuietProbeReportsNothing() {
|
|
232
|
+
val (sc, ctx) = attributed()
|
|
233
|
+
val child = mapOf("type" to "Text", "styles" to mapOf("color" to "Colors.nope", "flex" to "{((}"))
|
|
234
|
+
styOf(child, sc, ctx, null) // a parent probing a child's styles for layout
|
|
235
|
+
propsOf(mapOf("props" to mapOf("x" to "{((}")), sc, ctx, null)
|
|
236
|
+
assertTrue(nodeErrors().isEmpty())
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
@Test fun aQuietProbeInsideAReportingStepDoesNotLeakIntoIt() {
|
|
240
|
+
val (sc, ctx) = attributed()
|
|
241
|
+
val parent = mapOf("type" to "View")
|
|
242
|
+
sc.nodeErrors.forNode(parent, "body") { sc.nodeErrors.quiet { evalExpr("{((}", sc) } }
|
|
243
|
+
assertTrue(nodeErrors().isEmpty())
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
@Test fun brokenVisibleAndRepeatAreAttributedToTheChildPathUnderTheGivenBase() {
|
|
247
|
+
val (sc, _) = attributed()
|
|
248
|
+
val kids = listOf(mapOf("type" to "Text"), mapOf("type" to "Text", "id" to "v", "visible" to "{((}"), mapOf("type" to "Item", "repeat" to mapOf("source" to "{[[}")))
|
|
249
|
+
expand(kids, sc, defaultTheme, "body.children.4")
|
|
250
|
+
val got = nodeErrors()
|
|
251
|
+
assertEquals(listOf("body.children.4.children.1", "body.children.4.children.2"), got.map { it["nodePath"] })
|
|
252
|
+
assertEquals(listOf("v", null), got.map { it["nodeId"] }); assertEquals(listOf("Text", "Item"), got.map { it["nodeType"] })
|
|
253
|
+
assertTrue(got.all { it["code"] == "expr_failed" })
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
@Test fun expandWithoutABasePathIsQuiet() {
|
|
257
|
+
val (sc, _) = attributed()
|
|
258
|
+
expand(listOf(mapOf("type" to "Text", "visible" to "{((}")), sc, defaultTheme, null)
|
|
259
|
+
assertTrue(nodeErrors().isEmpty())
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
@Test fun theSameNodeEvaluatedTwiceReportsOnce() {
|
|
263
|
+
val (sc, ctx) = attributed()
|
|
264
|
+
val node = mapOf("type" to "Text", "props" to mapOf("children" to "{((}"))
|
|
265
|
+
repeat(2) { propsOf(node, sc, ctx, "body.children.0") } // recomposition re-runs the node's composable
|
|
266
|
+
repeat(2) { expand(listOf(mapOf("type" to "Text", "visible" to "{((}")), sc, defaultTheme, "body.children.5") }
|
|
267
|
+
assertEquals(listOf("body.children.0", "body.children.5.children.0"), nodeErrors().map { it["nodePath"] })
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
@Test fun aBrokenDataExpressionIsReportedOnEveryNodeThatUsesIt() {
|
|
271
|
+
val (sc, ctx) = attributed(mapOf("bad" to "{((}"))
|
|
272
|
+
val a = mapOf("type" to "Text", "props" to mapOf("children" to "{bad}")); val b = mapOf("type" to "Title", "props" to mapOf("children" to "{bad}"))
|
|
273
|
+
propsOf(a, sc, ctx, "body.children.0")
|
|
274
|
+
propsOf(b, sc, ctx, "body.children.1") // the second reader must report too
|
|
275
|
+
assertEquals(listOf("body.children.0", "body.children.1"), nodeErrors().map { it["nodePath"] })
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
@Test fun aBrokenDataExpressionReadThroughAnotherKeyOrInARepeatIsReportedOnEachNode() {
|
|
279
|
+
val (sc, ctx) = attributed(mapOf("bad" to "{item.(}", "label" to "{bad ?? 'x'}", "rows" to listOf(1L, 2L)))
|
|
280
|
+
propsOf(mapOf("type" to "Text", "props" to mapOf("children" to "{label}")), sc, ctx, "body.children.0")
|
|
281
|
+
val kids = listOf(mapOf("type" to "Text", "repeat" to mapOf("source" to "rows"), "props" to mapOf("children" to "{data.bad}")))
|
|
282
|
+
expand(kids, sc, defaultTheme, "body.children.1").forEachIndexed { i, x -> propsOf(x.node, x.scope, ctx, "body.children.1.children.$i") }
|
|
283
|
+
assertEquals(listOf("body.children.0", "body.children.1.children.0", "body.children.1.children.1"), nodeErrors().map { it["nodePath"] })
|
|
284
|
+
assertTrue(nodeErrors().all { it["code"] == "expr_failed" })
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
@Test fun resolutionOutsideAnyNodeReportsNothing() {
|
|
288
|
+
val (sc, ctx) = attributed()
|
|
289
|
+
resolveValue(mapOf("x" to "{((}", "y" to "Spacing.ZZZ"), sc, ctx.theme)
|
|
290
|
+
assertTrue(nodeErrors().isEmpty())
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
@Test fun validActionsReportNoNodeError() {
|
|
294
|
+
runTrack(trackAction(mapOf("eventName" to "tapped", "n" to 1L)))
|
|
295
|
+
run(mapOf("type" to "setState", "params" to mapOf("page" to 1L)))
|
|
296
|
+
assertTrue(nodeErrors().isEmpty())
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// ---- when / args / code / missing type ------------------------------------------------------------------------
|
|
300
|
+
private val typedState = mutableStateMapOf<String, Any?>("page" to 0L, "onboarded" to false)
|
|
301
|
+
private val typedRunner = SduiActionRunner(typedState, { defaultTheme }, { session }, featureMaxApi, capturingLog(warnings))
|
|
302
|
+
private val typedScope = SduiScope(emptyMap(), typedState)
|
|
303
|
+
private fun runTyped(action: Map<String, Any?>, args: List<Any?> = emptyList()) = typedRunner.runOne(action, typedScope, args)
|
|
304
|
+
|
|
305
|
+
@Test fun setStateWhenAndArgs() {
|
|
306
|
+
runTyped(mapOf("type" to "setState", "params" to mapOf("page" to "{state.page + 1}")))
|
|
307
|
+
assertEquals(1L, typedState["page"])
|
|
308
|
+
runTyped(mapOf("type" to "setState", "params" to mapOf("onboarded" to true), "when" to "{state.page > 5}"))
|
|
309
|
+
assertEquals(false, typedState["onboarded"]) // `when` false: nothing happens
|
|
310
|
+
runTyped(mapOf("type" to "setState", "args" to listOf("i"), "params" to mapOf("page" to "{i}")), listOf(2L))
|
|
311
|
+
assertEquals(2L, typedState["page"])
|
|
312
|
+
runTyped(mapOf("type" to "setState", "params" to mapOf("page" to "{args[0]}")), listOf(1L))
|
|
313
|
+
assertEquals(1L, typedState["page"])
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
@Test fun onPressActionsOfTheSameNameRunInSpecOrderWithWhen() {
|
|
317
|
+
val actions = listOf(
|
|
318
|
+
mapOf("name" to "onPress", "when" to "{!g.featureCode}", "type" to "startFeatureCode", "params" to mapOf("featureCode" to "ins_marketplace")),
|
|
319
|
+
mapOf("name" to "onPress", "when" to "{!!g.featureCode}", "type" to "startFeatureCode", "params" to mapOf("featureCode" to "{g.featureCode}")),
|
|
320
|
+
mapOf("name" to "other", "type" to "startFeatureCode", "params" to mapOf("featureCode" to "never")),
|
|
321
|
+
)
|
|
322
|
+
runner.run(actions, "onPress", scope.with("g", mapOf("featureCode" to "guide_x")), emptyList())
|
|
323
|
+
assertEquals(listOf("guide_x"), features.map { it.first })
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
@Test fun actionWithoutTypeRunsNothingAndWarns() {
|
|
327
|
+
warnings.clear()
|
|
328
|
+
val legacy = listOf(
|
|
329
|
+
mapOf("name" to "a", "set" to mapOf("page" to 9L)),
|
|
330
|
+
mapOf("name" to "a", "navigate" to "push", "params" to mapOf("featureCode" to "ok")),
|
|
331
|
+
mapOf("name" to "a", "tab" to "t"),
|
|
332
|
+
mapOf("name" to "a", "params" to mapOf("page" to 9L)),
|
|
333
|
+
)
|
|
334
|
+
legacy.forEachIndexed { i, a -> typedRunner.runOne(a, typedScope, emptyList(), path = "body.actions[$i]") }
|
|
335
|
+
assertEquals(0L, typedState["page"])
|
|
336
|
+
assertTrue(features.isEmpty())
|
|
337
|
+
assertEquals(4, warnings.size)
|
|
338
|
+
assertEquals((0..3).map { "[SDUI] t: body.actions[$it]: action skipped: missing type" }, warnings)
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
@Test fun missingTypeIsReportedEvenWhenWhenIsFalse() {
|
|
342
|
+
val before = nodeErrors().size
|
|
343
|
+
runTyped(mapOf("name" to "a", "when" to false, "params" to mapOf("page" to 9L)))
|
|
344
|
+
val got = nodeErrors().drop(before)
|
|
345
|
+
assertEquals(1, got.size)
|
|
346
|
+
assertEquals("invalid_action", got[0]["code"])
|
|
347
|
+
assertEquals("missing_type", got[0]["reason"])
|
|
348
|
+
assertEquals(0L, typedState["page"])
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
@Test fun actionCarryingCodeIsNeverExecuted() {
|
|
352
|
+
run(mapOf("code" to "alert(1)", "type" to "setState", "params" to mapOf("page" to 99L)))
|
|
353
|
+
assertEquals(0L, state["page"])
|
|
354
|
+
assertFalse(state.containsKey("x"))
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
@Test fun anActionNeverThrows() {
|
|
358
|
+
run(mapOf("type" to "setState", "params" to mapOf("page" to "{((}")))
|
|
359
|
+
run(mapOf("type" to 5L, "params" to "nope"))
|
|
360
|
+
run(emptyMap())
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// ---- typed form {"type": "set"} --------------------------------------------------------------------------------
|
|
364
|
+
private val setState = mutableStateMapOf<String, Any?>("expanded" to false, "query" to "", "count" to 0L)
|
|
365
|
+
private val setLog = capturingLog(warnings)
|
|
366
|
+
private val setRunner = SduiActionRunner(setState, { defaultTheme }, { session }, featureMaxApi, setLog, declaredState = setState.keys.toSet())
|
|
367
|
+
private val setScope = SduiScope(emptyMap(), setState)
|
|
368
|
+
private fun runSet(action: Map<String, Any?>, args: List<Any?> = emptyList()) = setRunner.runOne(action, setScope, args)
|
|
369
|
+
private fun setAction(params: Map<String, Any?>, extra: Map<String, Any?> = emptyMap()) =
|
|
370
|
+
mapOf<String, Any?>("name" to "onPress", "type" to "setState", "params" to params) + extra
|
|
371
|
+
|
|
372
|
+
@Test fun typedSetStateWritesDeclaredKeys() {
|
|
373
|
+
runSet(setAction(mapOf("expanded" to true, "count" to 5L)))
|
|
374
|
+
assertEquals(true, setState["expanded"]); assertEquals(5L, setState["count"])
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
@Test fun typedSetStateSkipsUndeclaredKeyWithWarningButWritesTheRest() {
|
|
378
|
+
warnings.clear()
|
|
379
|
+
runSet(setAction(mapOf("ghost" to 1L, "count" to 2L)))
|
|
380
|
+
assertEquals(2L, setState["count"])
|
|
381
|
+
assertFalse(setState.containsKey("ghost"))
|
|
382
|
+
assertTrue(warnings.any { it.contains("setState: 'ghost' is not declared in state") })
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
@Test fun typedSetStateToggleTwiceReturnsToFalse() {
|
|
386
|
+
val toggle = setAction(mapOf("expanded" to "{!expanded}"))
|
|
387
|
+
runSet(toggle); assertEquals(true, setState["expanded"])
|
|
388
|
+
runSet(toggle); assertEquals(false, setState["expanded"])
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
@Test fun typedSetStateWhenFalseWritesNothing() {
|
|
392
|
+
val before = sink.records.size
|
|
393
|
+
runSet(setAction(mapOf("count" to 9L), mapOf("when" to "{state.expanded}")))
|
|
394
|
+
assertEquals(0L, setState["count"])
|
|
395
|
+
assertEquals(before, sink.records.size)
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
@Test fun typedSetStateOnlyUndeclaredWritesNothingAndReportsStateNotDeclared() {
|
|
399
|
+
val before = sink.records.size
|
|
400
|
+
runSet(setAction(mapOf("ghost" to 1L)))
|
|
401
|
+
assertEquals(before + 1, sink.records.size)
|
|
402
|
+
assertEquals("state_not_declared", sink.records.last().event.fields["reason"])
|
|
403
|
+
assertFalse(setState.containsKey("ghost"))
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
@Test fun typedSetStateThenStartFeatureCodeSeesNewValue() {
|
|
407
|
+
val actions = listOf(
|
|
408
|
+
setAction(mapOf("query" to "{v}"), mapOf("args" to listOf("v"))),
|
|
409
|
+
mapOf("name" to "onPress", "type" to "startFeatureCode", "args" to listOf("v"), "params" to mapOf("featureCode" to "search", "q" to "{query}")),
|
|
410
|
+
)
|
|
411
|
+
setRunner.run(actions, "onPress", setScope, listOf("abc"))
|
|
412
|
+
assertEquals(listOf("search" to mapOf<String, Any>("q" to "abc")), features)
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
@Test fun typedSetStateTakesArgsFromTheCallback() {
|
|
416
|
+
runSet(setAction(mapOf("query" to "{v}"), mapOf("args" to listOf("v"))), listOf("hello"))
|
|
417
|
+
assertEquals("hello", setState["query"])
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// ---- action runner gaps: validation, params, when, args, setState, run(), SduiMaxApi ----------------------------
|
|
421
|
+
private fun feature(code: Any?) = mapOf<String, Any?>("name" to "onPress", "type" to "startFeatureCode", "params" to mapOf("featureCode" to code))
|
|
422
|
+
private fun nodeErrorReasons() = nodeErrors().map { it["reason"] }
|
|
423
|
+
|
|
424
|
+
@Test fun featureCodeAllowsDotSlashDashAfterFirstChar() {
|
|
425
|
+
run(feature("a.b/c-d_1"))
|
|
426
|
+
run(feature("_x"))
|
|
427
|
+
run(feature("9a"))
|
|
428
|
+
assertEquals(listOf("a.b/c-d_1", "_x", "9a"), features.map { it.first })
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
@Test fun featureCodeRejectsBadFirstCharEmptySpaceAndColon() {
|
|
432
|
+
listOf(".a", "-a", "/a", "", " ", "a b", "a:b", " a").forEach { run(feature(it)) }
|
|
433
|
+
assertTrue(features.isEmpty())
|
|
434
|
+
assertEquals(listOf<Any?>("invalid_feature_code"), nodeErrorReasons()) // same path/code/reason: one event
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
@Test fun featureCodeRegexDirectly() {
|
|
438
|
+
assertTrue(FEATURE_CODE_RE.matches("a.b/c-d_1"))
|
|
439
|
+
listOf(".", "-", "/", "", " ", ":", "a b", "a:b", "a\n").forEach { assertFalse("'$it'", FEATURE_CODE_RE.matches(it)) }
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
@Test fun numericFeatureCodeIsRejectedAsInvalidFeatureCode() {
|
|
443
|
+
run(feature(123L), args = emptyList())
|
|
444
|
+
assertTrue(features.isEmpty())
|
|
445
|
+
assertEquals(listOf<Any?>("invalid_feature_code"), nodeErrorReasons())
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
@Test fun eventNameOf64CharsIsAcceptedAnd65Rejected() {
|
|
449
|
+
runTrack(trackAction(mapOf("eventName" to "a".repeat(64))))
|
|
450
|
+
runTrack(trackAction(mapOf("eventName" to "a".repeat(65))))
|
|
451
|
+
assertEquals(listOf("a".repeat(64)), tracked.map { it.first })
|
|
452
|
+
assertEquals(listOf<Any?>("invalid_event_name"), nodeErrorReasons())
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
@Test fun eventNameRejectsLeadingDigitOrUnderscore() {
|
|
456
|
+
runTrack(trackAction(mapOf("eventName" to "9tap")))
|
|
457
|
+
runTrack(trackAction(mapOf("eventName" to "_tap")))
|
|
458
|
+
runTrack(trackAction(mapOf("eventName" to "")))
|
|
459
|
+
runTrack(trackAction(mapOf("eventName" to "tap_9")))
|
|
460
|
+
assertEquals(listOf("tap_9"), tracked.map { it.first })
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
@Test fun reservedEventNameIsCaseInsensitive() {
|
|
464
|
+
runTrack(trackAction(mapOf("eventName" to "OPS_INFO_CHECKED")), scope)
|
|
465
|
+
runTrack(trackAction(mapOf("eventName" to "Ops_Info_Checked")), scope)
|
|
466
|
+
assertTrue(tracked.isEmpty())
|
|
467
|
+
assertEquals(listOf<Any?>("reserved_event_name"), nodeErrorReasons()) // same path/code/reason: one event
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
@Test fun trackEventKeepsTypedValuesAndDropsEventNameNullAndInvalidKeys() {
|
|
471
|
+
runTrack(trackAction(mapOf(
|
|
472
|
+
"eventName" to "typed", "l" to 5L, "i" to 6, "d" to 1.5, "b" to true, "s" to "x", "nul" to null,
|
|
473
|
+
"1abc" to "v", "a-b" to "v", "_ok" to 1L)))
|
|
474
|
+
val (name, p) = tracked.single()
|
|
475
|
+
assertEquals("typed", name)
|
|
476
|
+
assertEquals(mapOf<String, Any>("l" to 5L, "i" to 6, "d" to 1.5, "b" to true, "s" to "x", "_ok" to 1L), p)
|
|
477
|
+
assertEquals(Long::class, p["l"]!!::class); assertEquals(Int::class, p["i"]!!::class); assertEquals(Double::class, p["d"]!!::class)
|
|
478
|
+
assertFalse(p.containsKey("eventName"))
|
|
479
|
+
assertFalse(warnings.any { "nul" in it })
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
@Test fun trackEventDropsMapListAndNonFiniteFloatAndDouble() {
|
|
483
|
+
runTrack(trackAction(mapOf(
|
|
484
|
+
"eventName" to "bad", "m" to mapOf("a" to 1L), "l" to listOf(1L),
|
|
485
|
+
"fnan" to Float.NaN, "finf" to Float.POSITIVE_INFINITY, "dninf" to Double.NEGATIVE_INFINITY, "ok" to 1.5f)))
|
|
486
|
+
assertEquals(listOf("bad" to mapOf<String, Any>("ok" to 1.5f)), tracked)
|
|
487
|
+
assertEquals(5, warnings.size)
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
@Test fun severalBadParamsGiveOneNodeErrorButOneLogLineEach() {
|
|
491
|
+
val r = SduiActionRunner(state, { defaultTheme }, { session }, SduiMaxApi({ trackApi }), trackLog)
|
|
492
|
+
r.runOne(trackAction(mapOf("eventName" to "t", "1abc" to "x", "a-b" to "y", "m" to mapOf("a" to 1L), "n" to Double.NaN)), scope, emptyList(), "body.children.0", "n0", "Button")
|
|
493
|
+
assertEquals(listOf<Any?>("invalid_param"), nodeErrorReasons())
|
|
494
|
+
assertEquals(4, warnings.count { "dropped param" in it })
|
|
495
|
+
assertEquals(listOf("t" to emptyMap<String, Any>()), tracked)
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
@Test fun startFeatureCodeForwardsMapAndListValuesUnchanged() {
|
|
499
|
+
val m = mapOf("a" to 1L); val l = listOf(1L, "x")
|
|
500
|
+
run(mapOf("type" to "startFeatureCode", "params" to mapOf("featureCode" to "fc", "m" to m, "l" to l, "gone" to null, "s" to "v")))
|
|
501
|
+
assertEquals(listOf("fc" to mapOf<String, Any>("m" to m, "l" to l, "s" to "v")), features)
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
private fun whenRuns(w: Any?, sc: SduiScope = scope): Boolean {
|
|
505
|
+
val s = mutableStateMapOf<String, Any?>("hit" to 0L)
|
|
506
|
+
SduiActionRunner(s, { defaultTheme }, { session }).runOne(mapOf("type" to "setState", "when" to w, "params" to mapOf("hit" to 1L)), SduiScope(emptyMap(), s), emptyList())
|
|
507
|
+
return s["hit"] == 1L
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
@Test fun whenLiteralBooleanControlsExecution() {
|
|
511
|
+
assertTrue(whenRuns(true)); assertFalse(whenRuns(false))
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
@Test fun whenExpressionsResolvingToZeroEmptyOrNullSkip() {
|
|
515
|
+
assertFalse(whenRuns("{0}")); assertFalse(whenRuns("{''}")); assertFalse(whenRuns("{state.missing}"))
|
|
516
|
+
assertFalse(whenRuns("{0.0}")); assertFalse(whenRuns("{false}"))
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
@Test fun whenNonEmptyStringOrNonZeroNumberRuns() {
|
|
520
|
+
assertTrue(whenRuns("{'x'}")); assertTrue(whenRuns("{1}")); assertTrue(whenRuns("{-1}")); assertTrue(whenRuns("{1.5}"))
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
@Test fun whenPlainStringWithoutBindingIsTruthyEvenIfItSaysFalse() {
|
|
524
|
+
// NOTE: a literal (non-binding) string such as "false" or "0" is a non-empty string, so it counts as true.
|
|
525
|
+
assertTrue(whenRuns("false")); assertTrue(whenRuns("0"))
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
@Test fun whenOnlyNullMeansNoCondition() {
|
|
529
|
+
assertTrue(whenRuns(null))
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
@Test fun argsWithMoreNamesThanCallArgsBindNull() {
|
|
533
|
+
setState.clear(); setState["w"] = "old"; setState["v"] = "old"
|
|
534
|
+
val r = SduiActionRunner(setState, { defaultTheme }, { session })
|
|
535
|
+
r.runOne(mapOf("type" to "setState", "args" to listOf("v", "w"), "params" to mapOf("v" to "{v}", "w" to "{w}")), setScope, listOf("one"))
|
|
536
|
+
assertEquals("one", setState["v"]); assertTrue(setState.containsKey("w")); assertNull(setState["w"])
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
@Test fun nonStringArgsEntryIsIgnoredWithoutShiftingPositions() {
|
|
540
|
+
val s = mutableStateMapOf<String, Any?>()
|
|
541
|
+
val r = SduiActionRunner(s, { defaultTheme }, { session })
|
|
542
|
+
r.runOne(mapOf("type" to "setState", "args" to listOf(1L, "b", null, "d"), "params" to mapOf("b" to "{b}", "d" to "{d}")), SduiScope(emptyMap(), s), listOf("A", "B", "C", "D"))
|
|
543
|
+
assertEquals("B", s["b"]); assertEquals("D", s["d"])
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
@Test fun argsIndexExpressionReadsCallbackValues() {
|
|
547
|
+
val s = mutableStateMapOf<String, Any?>()
|
|
548
|
+
val r = SduiActionRunner(s, { defaultTheme }, { session })
|
|
549
|
+
r.runOne(mapOf("type" to "setState", "params" to mapOf("a" to "{args[0]}", "b" to "{args[1]}", "c" to "{args[2]}", "n" to "{args.length}")), SduiScope(emptyMap(), s), listOf("x", 7L))
|
|
550
|
+
assertEquals("x", s["a"]); assertEquals(7L, s["b"]); assertNull(s["c"]); assertEquals(2L, s["n"])
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
@Test fun argsDotIndexReadsCallbackValueToo() {
|
|
554
|
+
val s = mutableStateMapOf<String, Any?>("a" to "old")
|
|
555
|
+
val r = SduiActionRunner(s, { defaultTheme }, { session })
|
|
556
|
+
r.runOne(mapOf("type" to "setState", "params" to mapOf("a" to "{args.0}")), SduiScope(emptyMap(), s), listOf("x"))
|
|
557
|
+
assertEquals("x", s["a"])
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
@Test fun setStateSwapsTwoKeysUsingValuesFromBeforeAnyWrite() {
|
|
561
|
+
val s = mutableStateMapOf<String, Any?>("a" to 1L, "b" to 2L)
|
|
562
|
+
val r = SduiActionRunner(s, { defaultTheme }, { session }, declaredState = setOf("a", "b"))
|
|
563
|
+
r.runOne(mapOf("type" to "setState", "params" to mapOf("a" to "{state.b}", "b" to "{state.a}")), SduiScope(emptyMap(), s), emptyList())
|
|
564
|
+
assertEquals(2L, s["a"]); assertEquals(1L, s["b"])
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
@Test fun setStateWithoutDeclaredStateWritesAnyKey() {
|
|
568
|
+
run(mapOf("type" to "setState", "params" to mapOf("brandNew" to "v", "nul" to null)))
|
|
569
|
+
assertEquals("v", state["brandNew"]); assertTrue(state.containsKey("nul"))
|
|
570
|
+
assertTrue(nodeErrors().isEmpty())
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
@Test fun runOnlyExecutesActionsWithMatchingName() {
|
|
574
|
+
val actions = listOf(
|
|
575
|
+
feature("one"), feature("two") + ("name" to "other"), feature("three") + ("name" to null))
|
|
576
|
+
runner.run(actions, "onPress", scope, emptyList())
|
|
577
|
+
assertEquals(listOf("one"), features.map { it.first })
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
@Test fun runIgnoresNullListNonMapEntriesAndNamelessEntries() {
|
|
581
|
+
runner.run(null, "onPress", scope, emptyList())
|
|
582
|
+
runner.run(listOf(null, "str", 5L, listOf(1L), mapOf("type" to "startFeatureCode", "params" to mapOf("featureCode" to "nameless"))), "onPress", scope, emptyList())
|
|
583
|
+
assertTrue(features.isEmpty())
|
|
584
|
+
assertTrue(nodeErrors().isEmpty())
|
|
585
|
+
assertTrue(warnings.isEmpty())
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
// SduiMaxApi
|
|
589
|
+
@Test fun maxApiResolvesTheApiOnEveryCall() {
|
|
590
|
+
val a = ArrayList<String>(); val b = ArrayList<String>()
|
|
591
|
+
val apiA = fakeMaxApi { n, _ -> a += n }; val apiB = fakeMaxApi { n, _ -> b += n }
|
|
592
|
+
var cur: IMaxApi? = apiA
|
|
593
|
+
val m = SduiMaxApi({ cur })
|
|
594
|
+
m.track("e", emptyMap())
|
|
595
|
+
cur = apiB
|
|
596
|
+
m.track("e", emptyMap()); m.startFeatureCode("f", emptyMap()); m.logFile("l")
|
|
597
|
+
cur = null
|
|
598
|
+
m.track("e", emptyMap())
|
|
599
|
+
assertEquals(listOf("track"), a)
|
|
600
|
+
assertEquals(listOf("track", "startFeatureCode", "logFile"), b)
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
@Test fun maxApiSwallowsThrowingTrackAndStartFeatureCodeAndKeepsWorking() {
|
|
604
|
+
val calls = ArrayList<String>()
|
|
605
|
+
val api = fakeMaxApi { n, _ -> calls += n; if (n == "track" || n == "startFeatureCode") error("boom") }
|
|
606
|
+
val m = SduiMaxApi({ api })
|
|
607
|
+
m.track("e", emptyMap()); m.startFeatureCode("f", emptyMap()); m.logFile("still works")
|
|
608
|
+
assertEquals(listOf("track", "startFeatureCode", "logFile"), calls)
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
@Test fun runnerWithThrowingApiDoesNotEscapeOrReportAnError() {
|
|
612
|
+
val api = fakeMaxApi { _, _ -> error("boom") }
|
|
613
|
+
val r = SduiActionRunner(state, { defaultTheme }, { session }, SduiMaxApi({ api }))
|
|
614
|
+
r.runOne(trackAction(mapOf("eventName" to "t")), scope, emptyList())
|
|
615
|
+
r.runOne(feature("fc"), scope, emptyList())
|
|
616
|
+
assertTrue(nodeErrors().isEmpty())
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
@Test fun unexpectedThrowReportsActionErrorOnceWithNodeContextAndDoesNotEscape() {
|
|
620
|
+
// the theme provider throws while the action's params are resolved: the only path that reaches runOne's catch
|
|
621
|
+
val r = SduiActionRunner(state, { error("boom") }, { session }, featureMaxApi, capturingLog(warnings))
|
|
622
|
+
val before = nodeErrors().size
|
|
623
|
+
r.runOne(mapOf("type" to "setState", "params" to mapOf("page" to 1L)), scope, emptyList(), "body.children.x", "n_x", "Button")
|
|
624
|
+
val got = nodeErrors().drop(before)
|
|
625
|
+
assertEquals(1, got.size)
|
|
626
|
+
assertEquals("invalid_action", got[0]["code"])
|
|
627
|
+
assertEquals("action_error", got[0]["reason"])
|
|
628
|
+
assertEquals("body.children.x", got[0]["nodePath"])
|
|
629
|
+
assertEquals("n_x", got[0]["nodeId"])
|
|
630
|
+
assertEquals("Button", got[0]["nodeType"])
|
|
631
|
+
assertEquals(0L, state["page"])
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
@Test fun throwingSessionDuringActionErrorReportDoesNotEscape() {
|
|
635
|
+
val bad = object : RenderSession by RenderSession.NOOP {
|
|
636
|
+
override fun nodeError(code: String, reason: String, nodePath: String, nodeType: String?, nodeId: String?) = error("sink down")
|
|
637
|
+
}
|
|
638
|
+
SduiActionRunner(state, { error("boom") }, { bad }).runOne(mapOf("type" to "setState", "params" to mapOf("page" to 1L)), scope, emptyList())
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
@Test fun maxApiWithNoApiIsANoOpForAllThreeCalls() {
|
|
642
|
+
val m = SduiMaxApi({ null })
|
|
643
|
+
m.track("e", mapOf("a" to 1L)); m.startFeatureCode("f", emptyMap()); m.logFile("x")
|
|
644
|
+
SduiMaxApi.NONE.track("e", emptyMap())
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
// AI-GENERATED END: unit tests for repeat expansion, the action runner
|