mattermost-mobile-upstream/test-runner
Pavel Zeman b4c94ccbc8
Fix push notification crash when server returns non-Map error data (#9618)
* fix: guard against non-Map data type in push notification error handler

Check ReadableType before calling getMap("data") in the push notification
fetch error path. The server may return "data" as a String (e.g. HTML error
pages, proxy errors) instead of a Map, causing UnexpectedNativeTypeException.

Sentry: MATTERMOST-MOBILE-ANDROID-AWY6
https://mattermost-mr.sentry.io/issues/6306939631/

Co-authored-by: Claude <claude@anthropic.com>

* test: add unit tests for push notification error type guard

Add standalone test-runner with mocked ReadableMap to verify the
type check in General.kt's fetch() error handling:

- Map data type: extracts status_code and message correctly
- String data type: falls back to getString (HTML error pages, proxy errors)
- Null data type: handles gracefully
- Number data type: never calls getMap
- Null error map: handles gracefully

Uses lightweight React Native bridge stubs (ReadableMap interface,
ReadableType enum) to avoid full RN build dependency.

Sentry: MATTERMOST-MOBILE-ANDROID-AWY6
https://mattermost-mr.sentry.io/issues/6911684498/

Co-authored-by: Claude <claude@anthropic.com>

* fix: handle all ReadableType variants explicitly per CodeRabbit review

Replace if/else with exhaustive when-expression so every ReadableType
(Map, String, Number, Boolean, Array, Null) uses its type-safe getter.
Prevents UnexpectedNativeTypeException from the React Native bridge
when data is a non-String, non-Map type.

Tests updated: 7 cases covering every branch including missing key.
All pass locally (gradlew test).

Co-authored-by: Claude <claude@anthropic.com>

* test: add coverage for ReadableType.Array branch

Addresses CodeRabbit nitpick — the Array branch of the exhaustive
when-expression was the only one without a dedicated test case.

Co-authored-by: Claude <claude@anthropic.com>

* refactor: extract formatErrorMessage to eliminate test duplication

Extract the error formatting when-expression from General.kt into a
standalone formatErrorMessage() function in ErrorFormatting.kt. This
allows the test-runner to call the production function directly via
symlink instead of maintaining a duplicated extractErrorMessage() helper.

The test already covered Array type; all 8 tests pass against the
production function.

Co-authored-by: Claude <claude@anthropic.com>

* fix(android): harden nested map access and add comprehensive test coverage

Add defensive hasKey/getType checks for nested map fields (status_code,
message) in formatErrorMessage to prevent NoSuchKeyException and
UnexpectedNativeTypeException.

Add test coverage for all ReadableType branches and edge cases including
missing keys, wrong types, and null map.

Fix test-runner symlink to use relative path instead of absolute.

Co-authored-by: Claude <claude@anthropic.com>

---------

Co-authored-by: Claude <claude@anthropic.com>
2026-04-16 19:26:50 -04:00
..
gradle/wrapper Fix push notification crash when server returns non-Map error data (#9618) 2026-04-16 19:26:50 -04:00
src Fix push notification crash when server returns non-Map error data (#9618) 2026-04-16 19:26:50 -04:00
.gitignore Fix push notification crash when server returns non-Map error data (#9618) 2026-04-16 19:26:50 -04:00
build.gradle.kts Fix push notification crash when server returns non-Map error data (#9618) 2026-04-16 19:26:50 -04:00
gradlew Fix push notification crash when server returns non-Map error data (#9618) 2026-04-16 19:26:50 -04:00
settings.gradle.kts Fix push notification crash when server returns non-Map error data (#9618) 2026-04-16 19:26:50 -04:00