@bugsee/react-native 0.0.0-stage → 7.0.0-beta1

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.
Files changed (250) hide show
  1. package/BugseeReactNative.podspec +95 -0
  2. package/LICENSE +21 -0
  3. package/README.md +552 -2
  4. package/android/build.gradle +162 -0
  5. package/android/consumer-rules.pro +2 -0
  6. package/android/src/main/AndroidManifest.xml +33 -0
  7. package/android/src/main/java/com/bugsee/reactnative/AppearanceBridge.java +82 -0
  8. package/android/src/main/java/com/bugsee/reactnative/AttributeBridge.java +209 -0
  9. package/android/src/main/java/com/bugsee/reactnative/BreadcrumbFilterBridge.java +443 -0
  10. package/android/src/main/java/com/bugsee/reactnative/BridgeJson.java +168 -0
  11. package/android/src/main/java/com/bugsee/reactnative/BugseeModule.java +1869 -0
  12. package/android/src/main/java/com/bugsee/reactnative/BugseeOptionEnums.java +136 -0
  13. package/android/src/main/java/com/bugsee/reactnative/BugseePackage.java +42 -0
  14. package/android/src/main/java/com/bugsee/reactnative/BugseeReactNativeWrapper.java +200 -0
  15. package/android/src/main/java/com/bugsee/reactnative/BugseeStatusMapper.java +66 -0
  16. package/android/src/main/java/com/bugsee/reactnative/BugseeTokens.java +22 -0
  17. package/android/src/main/java/com/bugsee/reactnative/ConsoleEchoDedup.java +202 -0
  18. package/android/src/main/java/com/bugsee/reactnative/CreatedReports.java +127 -0
  19. package/android/src/main/java/com/bugsee/reactnative/DataRequestBridge.java +375 -0
  20. package/android/src/main/java/com/bugsee/reactnative/ExceptionBridge.java +88 -0
  21. package/android/src/main/java/com/bugsee/reactnative/ExceptionExecutors.java +33 -0
  22. package/android/src/main/java/com/bugsee/reactnative/LogFilterBridge.java +375 -0
  23. package/android/src/main/java/com/bugsee/reactnative/NetworkEvents.java +227 -0
  24. package/android/src/main/java/com/bugsee/reactnative/NetworkFilterBridge.java +498 -0
  25. package/android/src/main/java/com/bugsee/reactnative/PluginEndpoint.java +48 -0
  26. package/android/src/main/java/com/bugsee/reactnative/ReactNativeWebException.java +19 -0
  27. package/android/src/main/java/com/bugsee/reactnative/ReactNativeWrapperInitProvider.java +61 -0
  28. package/android/src/main/java/com/bugsee/reactnative/ReactRootOriginTracker.java +867 -0
  29. package/android/src/main/java/com/bugsee/reactnative/ReportArgs.java +58 -0
  30. package/android/src/main/java/com/bugsee/reactnative/ReportHandlerBridge.java +336 -0
  31. package/android/src/main/java/com/bugsee/reactnative/ReportHandlerDeadlines.java +57 -0
  32. package/android/src/main/java/com/bugsee/reactnative/ReportOps.java +287 -0
  33. package/android/src/main/java/com/bugsee/reactnative/SecureRectanglePulls.java +113 -0
  34. package/android/src/main/java/com/bugsee/reactnative/SecureRectangleStore.java +692 -0
  35. package/android/src/main/java/com/bugsee/reactnative/SpanHandles.java +143 -0
  36. package/android/src/main/java/com/bugsee/reactnative/WrapperChannelHolder.java +137 -0
  37. package/android/src/main/java/com/bugsee/reactnative/WrapperEventBus.java +110 -0
  38. package/android/src/main/java/com/bugsee/reactnative/WrapperRegistrar.java +54 -0
  39. package/app.plugin.js +4 -0
  40. package/ios/BGSRNConsoleCapture.h +52 -0
  41. package/ios/BGSRNConsoleCapture.mm +344 -0
  42. package/ios/BugseeModule.h +25 -0
  43. package/ios/BugseeModule.mm +2728 -0
  44. package/ios/Package.swift +71 -0
  45. package/ios/Support/Package.resolved +14 -0
  46. package/ios/Support/Package.swift +44 -0
  47. package/ios/Support/Sources/BugseeRNSupport/BGSRNAttributes.m +52 -0
  48. package/ios/Support/Sources/BugseeRNSupport/BGSRNCreatedReportOps.m +236 -0
  49. package/ios/Support/Sources/BugseeRNSupport/BGSRNCreatedReports.m +184 -0
  50. package/ios/Support/Sources/BugseeRNSupport/BGSRNDataRequestBridge.m +431 -0
  51. package/ios/Support/Sources/BugseeRNSupport/BGSRNErrorMessage.m +40 -0
  52. package/ios/Support/Sources/BugseeRNSupport/BGSRNEventBus.m +70 -0
  53. package/ios/Support/Sources/BugseeRNSupport/BGSRNExceptions.m +63 -0
  54. package/ios/Support/Sources/BugseeRNSupport/BGSRNGuardedEmit.mm +32 -0
  55. package/ios/Support/Sources/BugseeRNSupport/BGSRNJSON.m +40 -0
  56. package/ios/Support/Sources/BugseeRNSupport/BGSRNLaunchHold.m +147 -0
  57. package/ios/Support/Sources/BugseeRNSupport/BGSRNMainThread.m +23 -0
  58. package/ios/Support/Sources/BugseeRNSupport/BGSRNNetworkEvents.m +159 -0
  59. package/ios/Support/Sources/BugseeRNSupport/BGSRNNetworkFilter.m +155 -0
  60. package/ios/Support/Sources/BugseeRNSupport/BGSRNPluginEndpoint.m +21 -0
  61. package/ios/Support/Sources/BugseeRNSupport/BGSRNReactRootOriginTracker.m +63 -0
  62. package/ios/Support/Sources/BugseeRNSupport/BGSRNReactWindow.m +251 -0
  63. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportArgs.m +47 -0
  64. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportDeadlines.m +8 -0
  65. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportHandlerBridge.m +308 -0
  66. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportOps.m +345 -0
  67. package/ios/Support/Sources/BugseeRNSupport/BGSRNSecureRectangles.m +508 -0
  68. package/ios/Support/Sources/BugseeRNSupport/BGSRNSettleOnce.m +62 -0
  69. package/ios/Support/Sources/BugseeRNSupport/BGSRNSpanHandles.m +136 -0
  70. package/ios/Support/Sources/BugseeRNSupport/BGSRNStatusMapper.m +18 -0
  71. package/ios/Support/Sources/BugseeRNSupport/BGSRNTokens.m +9 -0
  72. package/ios/Support/Sources/BugseeRNSupport/BGSRNValues.m +5 -0
  73. package/ios/Support/Sources/BugseeRNSupport/BGSRNWrapper.m +54 -0
  74. package/ios/Support/Sources/BugseeRNSupport/BGSRNWrapperChannelHolder.m +59 -0
  75. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNAttributes.h +63 -0
  76. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNCreatedReportOps.h +51 -0
  77. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNCreatedReports.h +81 -0
  78. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNDataRequestBridge.h +118 -0
  79. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNErrorMessage.h +30 -0
  80. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNEventBus.h +45 -0
  81. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNExceptions.h +31 -0
  82. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNGuardedEmit.h +31 -0
  83. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNJSON.h +25 -0
  84. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNLaunchHold.h +86 -0
  85. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNMainThread.h +32 -0
  86. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNNetworkEvents.h +28 -0
  87. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNNetworkFilter.h +28 -0
  88. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNPluginEndpoint.h +17 -0
  89. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReactRootOriginTracker.h +48 -0
  90. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReactWindow.h +135 -0
  91. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportArgs.h +17 -0
  92. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportDeadlines.h +37 -0
  93. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportHandlerBridge.h +94 -0
  94. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportOps.h +86 -0
  95. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSecureRectangles.h +198 -0
  96. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSettleOnce.h +25 -0
  97. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSpanHandles.h +49 -0
  98. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNStatusMapper.h +26 -0
  99. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNTokens.h +14 -0
  100. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNValues.h +18 -0
  101. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNWrapper.h +40 -0
  102. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNWrapperChannelHolder.h +74 -0
  103. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNAttributesTests.m +194 -0
  104. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNCreatedReportOpsTests.m +274 -0
  105. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNCreatedReportsTests.m +240 -0
  106. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNDataRequestBridgeTests.m +685 -0
  107. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNErrorMessageTests.m +102 -0
  108. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNEventBusTests.m +136 -0
  109. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNExceptionsTests.m +68 -0
  110. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNFakeReport.h +41 -0
  111. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNFakeReport.m +166 -0
  112. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNGuardedEmitTests.mm +85 -0
  113. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNJSONTests.m +281 -0
  114. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNLaunchHoldTests.m +230 -0
  115. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNMainThreadTests.m +97 -0
  116. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNNetworkEventsTests.m +99 -0
  117. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNNetworkFilterTests.m +134 -0
  118. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNPluginEndpointTests.m +38 -0
  119. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReactRootOriginTrackerTests.m +210 -0
  120. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReactWindowTests.m +548 -0
  121. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportArgsTests.m +51 -0
  122. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportDeadlinesTests.m +22 -0
  123. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportHandlerBridgeTests.m +382 -0
  124. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportOpsTests.m +295 -0
  125. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSecureRectanglesTests.m +568 -0
  126. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSettleOnceTests.m +134 -0
  127. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSpanHandlesTests.m +153 -0
  128. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNStatusMapperTests.m +81 -0
  129. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNValuesTests.m +32 -0
  130. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNWrapperChannelHolderTests.m +205 -0
  131. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNWrapperTests.m +66 -0
  132. package/ios/react-native-spm-prefix.h +10 -0
  133. package/native-versions.json +10 -0
  134. package/package.json +62 -3
  135. package/plugin/build/bundle-phase.d.ts +41 -0
  136. package/plugin/build/bundle-phase.js +116 -0
  137. package/plugin/build/bundle-phase.js.map +1 -0
  138. package/plugin/build/dsym-script.d.ts +32 -0
  139. package/plugin/build/dsym-script.js +159 -0
  140. package/plugin/build/dsym-script.js.map +1 -0
  141. package/plugin/build/endpoint.d.ts +30 -0
  142. package/plugin/build/endpoint.js +69 -0
  143. package/plugin/build/endpoint.js.map +1 -0
  144. package/plugin/build/gradle.d.ts +65 -0
  145. package/plugin/build/gradle.js +1066 -0
  146. package/plugin/build/gradle.js.map +1 -0
  147. package/plugin/build/index.d.ts +85 -0
  148. package/plugin/build/index.js +217 -0
  149. package/plugin/build/index.js.map +1 -0
  150. package/plugin/build/manifest.d.ts +48 -0
  151. package/plugin/build/manifest.js +69 -0
  152. package/plugin/build/manifest.js.map +1 -0
  153. package/plugin/build/native-versions.baked.json +4 -0
  154. package/plugin/build/native-versions.d.ts +10 -0
  155. package/plugin/build/native-versions.js +27 -0
  156. package/plugin/build/native-versions.js.map +1 -0
  157. package/plugin/build/pbx-string.d.ts +8 -0
  158. package/plugin/build/pbx-string.js +31 -0
  159. package/plugin/build/pbx-string.js.map +1 -0
  160. package/plugin/build/podfile.d.ts +3 -0
  161. package/plugin/build/podfile.js +28 -0
  162. package/plugin/build/podfile.js.map +1 -0
  163. package/plugin/build/properties.d.ts +19 -0
  164. package/plugin/build/properties.js +41 -0
  165. package/plugin/build/properties.js.map +1 -0
  166. package/plugin/build/scheme.d.ts +20 -0
  167. package/plugin/build/scheme.js +107 -0
  168. package/plugin/build/scheme.js.map +1 -0
  169. package/react-native.config.js +26 -0
  170. package/scripts/bugsee-sourcemaps.gradle +201 -0
  171. package/scripts/bugsee-xcode.sh +83 -0
  172. package/scripts/compose-then-inject.js +113 -0
  173. package/scripts/hermes-sourcemaps.js +634 -0
  174. package/scripts/hermesc-preserve-js.cmd +6 -0
  175. package/scripts/hermesc-preserve-js.js +200 -0
  176. package/scripts/hermesc-preserve-js.sh +7 -0
  177. package/src/NativeBugsee.ts +622 -0
  178. package/src/apm/notify.ts +58 -0
  179. package/src/apm/span.ts +403 -0
  180. package/src/apm/types.ts +57 -0
  181. package/src/appearance/report.ts +171 -0
  182. package/src/architecture.ts +48 -0
  183. package/src/attributes/errors.ts +39 -0
  184. package/src/attributes/validate.ts +150 -0
  185. package/src/breadcrumbs/add.ts +58 -0
  186. package/src/breadcrumbs/data.ts +33 -0
  187. package/src/breadcrumbs/filter.ts +215 -0
  188. package/src/breadcrumbs/types.ts +64 -0
  189. package/src/bridge/json.ts +110 -0
  190. package/src/console/capture.ts +180 -0
  191. package/src/console/dedup.ts +175 -0
  192. package/src/data/validate.ts +259 -0
  193. package/src/errorName.ts +32 -0
  194. package/src/exceptions/ErrorBoundary.tsx +92 -0
  195. package/src/exceptions/RootErrorReporter.tsx +61 -0
  196. package/src/exceptions/debugIds.ts +118 -0
  197. package/src/exceptions/handlers.ts +236 -0
  198. package/src/exceptions/options.ts +85 -0
  199. package/src/exceptions/payload.ts +456 -0
  200. package/src/exceptions/report.ts +170 -0
  201. package/src/exceptions/sha1.ts +148 -0
  202. package/src/exceptions/stack.ts +355 -0
  203. package/src/index.ts +944 -0
  204. package/src/logs/filter.ts +90 -0
  205. package/src/logs/log.ts +36 -0
  206. package/src/network/add.ts +165 -0
  207. package/src/network/filter.ts +198 -0
  208. package/src/options/AndroidLaunchOptions.ts +78 -0
  209. package/src/options/BugseeLaunchOptions.ts +190 -0
  210. package/src/options/IOSLaunchOptions.ts +57 -0
  211. package/src/options/android-options-manifest.json +646 -0
  212. package/src/options/createDefaultLaunchOptions.ts +29 -0
  213. package/src/options/endpoint.ts +58 -0
  214. package/src/options/enums.ts +104 -0
  215. package/src/options/ios-enums.json +19 -0
  216. package/src/options/keys.ts +39 -0
  217. package/src/options/option-enums.json +34 -0
  218. package/src/options/option-keys.json +107 -0
  219. package/src/report/BugseeReport.ts +264 -0
  220. package/src/report/CreatedReport.ts +127 -0
  221. package/src/report/dispatcher.ts +95 -0
  222. package/src/report/errors.ts +48 -0
  223. package/src/report/fields.ts +40 -0
  224. package/src/report/types.ts +157 -0
  225. package/src/report/validate.ts +197 -0
  226. package/src/secure/BugseeSecure.tsx +202 -0
  227. package/src/secure/ancestry.ts +622 -0
  228. package/src/secure/drivers.ts +342 -0
  229. package/src/secure/followEvents.ts +57 -0
  230. package/src/secure/hold.ts +99 -0
  231. package/src/secure/measureLoop.ts +323 -0
  232. package/src/secure/nativeAnimation.ts +1005 -0
  233. package/src/secure/rectangles.ts +93 -0
  234. package/src/secure/registry.ts +237 -0
  235. package/src/secure/surface.ts +83 -0
  236. package/src/secure/tracker.ts +723 -0
  237. package/src/secure/transition.ts +488 -0
  238. package/src/secure/unit.ts +22 -0
  239. package/src/status.ts +16 -0
  240. package/src/version.ts +9 -0
  241. package/src/viewtree/anchor.tsx +105 -0
  242. package/src/viewtree/constants.ts +20 -0
  243. package/src/viewtree/fiber.ts +251 -0
  244. package/src/viewtree/requests.ts +347 -0
  245. package/src/viewtree/walk.ts +902 -0
  246. package/src/wrapper/channel.ts +33 -0
  247. package/src/wrapper/collect.ts +52 -0
  248. package/src/wrapper/events.ts +45 -0
  249. package/src/wrapper/identity.ts +54 -0
  250. package/src/wrapper/lifecycle-events.ts +89 -0
@@ -0,0 +1,281 @@
1
+ @import XCTest;
2
+ @import Bugsee;
3
+ @import BugseeRNSupport;
4
+
5
+ #import "BGSRNFakeReport.h"
6
+
7
+ /// The iOS half of the object transport: JSON text in, Foundation out. Mirrors
8
+ /// Android's `BridgeJsonTest` by name where the rule is shared.
9
+ ///
10
+ /// The defect it exists for: React Native's TurboModule conversion of an
11
+ /// object argument (`convertJSIObjectToNSDictionary`) skips every member whose
12
+ /// value is JS `null`, unless an app-level feature flag is on. A dictionary
13
+ /// that reaches native with the key missing cannot say "clear this".
14
+ @interface BGSRNJSONTests : XCTestCase
15
+ @end
16
+
17
+ @implementation BGSRNJSONTests
18
+
19
+ - (NSDictionary *)parse:(NSString *)json {
20
+ NSError *error = nil;
21
+ NSDictionary *result = BGSRNJSONObject(json, &error);
22
+ XCTAssertNotNil(result, @"rejected %@: %@", json, error);
23
+ XCTAssertNil(error);
24
+ return result;
25
+ }
26
+
27
+ - (void)assertBad:(NSString *_Nullable)json {
28
+ NSError *error = nil;
29
+ XCTAssertNil(BGSRNJSONObject(json, &error), @"accepted %@", json);
30
+ XCTAssertNotNil(error, @"no error for %@", json);
31
+ XCTAssertEqualObjects(error.domain, BGSRNJSONErrorDomain);
32
+ XCTAssertGreaterThan(BGSRNErrorMessage(error).length, 0u);
33
+ }
34
+
35
+ static BOOL IsCFBoolean(id value) {
36
+ return [value isKindOfClass:NSNumber.class] &&
37
+ CFGetTypeID((__bridge CFTypeRef)value) == CFBooleanGetTypeID();
38
+ }
39
+
40
+ static BOOL IsIntegerNumber(id value) {
41
+ return [value isKindOfClass:NSNumber.class] && !IsCFBoolean(value) &&
42
+ !CFNumberIsFloatType((__bridge CFNumberRef)value);
43
+ }
44
+
45
+ static BOOL IsFloatNumber(id value) {
46
+ return [value isKindOfClass:NSNumber.class] && !IsCFBoolean(value) &&
47
+ CFNumberIsFloatType((__bridge CFNumberRef)value);
48
+ }
49
+
50
+ #pragma mark - Null
51
+
52
+ - (void)testATopLevelNullIsKeptAsNSNull {
53
+ NSDictionary *map = [self parse:@"{\"nil\":null,\"s\":\"x\"}"];
54
+ XCTAssertEqualObjects(map[@"nil"], NSNull.null);
55
+ XCTAssertEqualObjects(map[@"s"], @"x");
56
+ XCTAssertEqual(map.count, 2u);
57
+ }
58
+
59
+ - (void)testANestedNullIsKeptInAnObjectAndInAnArray {
60
+ NSDictionary *map = [self parse:@"{\"attributes\":{\"gone\":null},\"list\":[null,1]}"];
61
+ XCTAssertEqualObjects(map[@"attributes"], @{ @"gone" : NSNull.null });
62
+ XCTAssertEqualObjects(map[@"list"], (@[ NSNull.null, @1 ]));
63
+ }
64
+
65
+ #pragma mark - Numbers and booleans
66
+
67
+ /// The SDK's JSON writer prints an integer NSNumber as `3`; a float-typed one
68
+ /// holding 3 would still print `3`, but the attribute and param stores take
69
+ /// the number as given, so the type is pinned, not only the value.
70
+ - (void)testIntegralNumbersAreIntegerNumbers {
71
+ NSDictionary *map =
72
+ [self parse:@"{\"int\":3,\"neg\":-7,\"zero\":0,\"big\":9007199254740991,\"minSafe\":-9007199254740992}"];
73
+ for (NSString *key in @[ @"int", @"neg", @"zero", @"big", @"minSafe" ]) {
74
+ XCTAssertTrue(IsIntegerNumber(map[key]), @"%@ is %@ (%s)", key, [map[key] class], [map[key] objCType]);
75
+ }
76
+ XCTAssertEqual([map[@"int"] longLongValue], 3);
77
+ XCTAssertEqual([map[@"neg"] longLongValue], -7);
78
+ XCTAssertEqual([map[@"zero"] longLongValue], 0);
79
+ XCTAssertEqual([map[@"big"] longLongValue], 9007199254740991LL);
80
+ XCTAssertEqual([map[@"minSafe"] longLongValue], -9007199254740992LL);
81
+ }
82
+
83
+ - (void)testFractionalNumbersAreDoubles {
84
+ NSDictionary *map = [self parse:@"{\"frac\":1.5,\"tiny\":1e-7,\"huge\":1e+300,\"neg\":-0.25}"];
85
+ for (NSString *key in @[ @"frac", @"tiny", @"huge", @"neg" ]) {
86
+ XCTAssertTrue(IsFloatNumber(map[key]), @"%@ is %@ (%s)", key, [map[key] class], [map[key] objCType]);
87
+ }
88
+ XCTAssertEqual([map[@"frac"] doubleValue], 1.5);
89
+ XCTAssertEqual([map[@"tiny"] doubleValue], 1e-7);
90
+ XCTAssertEqual([map[@"huge"] doubleValue], 1e300);
91
+ XCTAssertEqual([map[@"neg"] doubleValue], -0.25);
92
+ }
93
+
94
+ /// By identity: the SDK's writer special-cases CFBoolean, and anything else
95
+ /// holding 1 prints as `1`.
96
+ - (void)testBooleansAreCFBooleans {
97
+ NSDictionary *map = [self parse:@"{\"yes\":true,\"no\":false}"];
98
+ XCTAssertEqual((__bridge CFBooleanRef)map[@"yes"], kCFBooleanTrue);
99
+ XCTAssertEqual((__bridge CFBooleanRef)map[@"no"], kCFBooleanFalse);
100
+ }
101
+
102
+ /// And a number is never one: 1 and 0 must not come back as true and false.
103
+ - (void)testOneAndZeroAreNotBooleans {
104
+ NSDictionary *map = [self parse:@"{\"one\":1,\"zero\":0}"];
105
+ XCTAssertFalse(IsCFBoolean(map[@"one"]));
106
+ XCTAssertFalse(IsCFBoolean(map[@"zero"]));
107
+ }
108
+
109
+ /// An integer literal past `long long` range -- what `JSON.stringify` writes
110
+ /// for 1e20 -- is not a plain NSNumber: `NSJSONSerialization` returns an
111
+ /// `NSDecimalNumber`. Pinned so the report path below is known to see one.
112
+ - (void)testAnIntegerPastLongLongRangeIsADecimalNumber {
113
+ id big = [self parse:@"{\"n\":100000000000000000000}"][@"n"];
114
+ XCTAssertTrue([big isKindOfClass:NSDecimalNumber.class], @"%@ (%s)", [big class], [big objCType]);
115
+ XCTAssertFalse(IsCFBoolean(big));
116
+ XCTAssertEqual([big doubleValue], 1e20);
117
+ }
118
+
119
+ /// ...and `BGSRNReportOps` handles it: `IsFiniteNumber` accepts it as an
120
+ /// attribute value, `WireNumber` stores it as a double (it is past 2^53, so
121
+ /// not an exact integer), and as a severity it is out of range, not a crash.
122
+ - (void)testADecimalNumberAttributeIsStoredAsADouble {
123
+ BGSRNFakeReport *report = [BGSRNFakeReport new];
124
+ NSError *error = nil;
125
+ XCTAssertTrue([BGSRNReportOps applyPatchJSON:@"{\"attributes\":{\"big\":100000000000000000000}}"
126
+ toReport:report
127
+ error:&error],
128
+ @"%@", error);
129
+ id stored = report.fakeAttributes[@"big"];
130
+ XCTAssertFalse([stored isKindOfClass:NSDecimalNumber.class], @"%@", [stored class]);
131
+ XCTAssertTrue(IsFloatNumber(stored), @"%@ (%s)", [stored class], [stored objCType]);
132
+ XCTAssertEqual([stored doubleValue], 1e20);
133
+
134
+ error = nil;
135
+ XCTAssertFalse([BGSRNReportOps applyPatchJSON:@"{\"severity\":100000000000000000000}"
136
+ toReport:report
137
+ error:&error]);
138
+ XCTAssertEqual(error.code, BGSRNReportErrorBadArgument);
139
+ }
140
+
141
+ #pragma mark - Lone surrogates
142
+
143
+ /// Why `encodeBridgeObject` replaces lone surrogates in JS: `JSON.stringify`
144
+ /// escapes one as `\ud800`, and `NSJSONSerialization` rejects the WHOLE text
145
+ /// for it. Unnormalised, a summary cut mid-emoji rejected on iOS alone.
146
+ - (void)testNSJSONSerializationRejectsALoneSurrogateEscape {
147
+ [self assertBad:@"{\"s\":\"\\ud800\"}"];
148
+ [self assertBad:@"{\"s\":\"a\\udc00b\"}"];
149
+ }
150
+
151
+ /// What JS sends instead -- U+FFFD, raw or escaped -- and a valid escaped pair, parse.
152
+ - (void)testTheReplacementCharacterAndAValidPairParse {
153
+ XCTAssertEqualObjects([self parse:@"{\"s\":\"a\uFFFD\"}"][@"s"], @"a\uFFFD");
154
+ XCTAssertEqualObjects([self parse:@"{\"s\":\"a\\ufffd\"}"][@"s"], @"a\uFFFD");
155
+ XCTAssertEqualObjects([self parse:@"{\"s\":\"\\ud83d\\ude00\"}"][@"s"], @"\U0001F600");
156
+ }
157
+
158
+ #pragma mark - Structure
159
+
160
+ - (void)testNestedArraysAndObjectsBecomeArraysAndDictionaries {
161
+ NSDictionary *map = [self
162
+ parse:@"{\"nested\":{\"list\":[1,\"two\",{\"deep\":false},[true]],\"empty\":{}},\"none\":[]}"];
163
+ NSArray *list = map[@"nested"][@"list"];
164
+ XCTAssertEqualObjects(list[0], @1);
165
+ XCTAssertEqualObjects(list[1], @"two");
166
+ XCTAssertEqualObjects(list[2], @{ @"deep" : @NO });
167
+ XCTAssertEqualObjects(list[3], @[ @YES ]);
168
+ XCTAssertEqualObjects(map[@"nested"][@"empty"], @{});
169
+ XCTAssertEqualObjects(map[@"none"], @[]);
170
+ }
171
+
172
+ - (void)testStringsAreUnescaped {
173
+ XCTAssertEqualObjects([self parse:@"{\"s\":\"a\\\"b\\\\c\\nd\\u00e9\"}"][@"s"], @"a\"b\\c\ndé");
174
+ }
175
+
176
+ - (void)testProtoIsAnOrdinaryKey {
177
+ XCTAssertEqualObjects([self parse:@"{\"__proto__\":\"x\"}"][@"__proto__"], @"x");
178
+ }
179
+
180
+ - (void)testAnEmptyObjectIsAnEmptyDictionary {
181
+ XCTAssertEqualObjects([self parse:@"{}"], @{});
182
+ XCTAssertEqualObjects([self parse:@" {}\n"], @{});
183
+ }
184
+
185
+ #pragma mark - Bad input
186
+
187
+ - (void)testMalformedJsonIsBad {
188
+ [self assertBad:@"{\"a\":"];
189
+ [self assertBad:@"{\"a\" 1}"];
190
+ [self assertBad:@""];
191
+ [self assertBad:@" "];
192
+ }
193
+
194
+ - (void)testAnythingButAnObjectIsBad {
195
+ [self assertBad:@"[1,2]"];
196
+ [self assertBad:@"\"s\""];
197
+ [self assertBad:@"3"];
198
+ [self assertBad:@"null"];
199
+ [self assertBad:@"true"];
200
+ [self assertBad:@"not json"];
201
+ }
202
+
203
+ - (void)testTrailingTextAfterTheObjectIsBad {
204
+ [self assertBad:@"{\"a\":1} x"];
205
+ [self assertBad:@"{\"a\":1}{}"];
206
+ }
207
+
208
+ - (void)testANilStringIsBad {
209
+ [self assertBad:nil];
210
+ }
211
+
212
+ /// A NULL error out-parameter is allowed, as everywhere in Foundation.
213
+ - (void)testANullErrorPointerIsAllowed {
214
+ XCTAssertNil(BGSRNJSONObject(@"[", NULL));
215
+ XCTAssertEqualObjects(BGSRNJSONObject(@"{}", NULL), @{});
216
+ }
217
+
218
+ #pragma mark - The report patch, end to end
219
+
220
+ /// `reportUpdate` as it now arrives: JSON text. The three edits the object
221
+ /// conversion dropped -- a null summary, a null description, a null
222
+ /// attribute -- all take effect.
223
+ - (void)testAParsedPatchClearsAndRemoves {
224
+ BGSRNFakeReport *report = [BGSRNFakeReport new];
225
+ report.fakeSummary = @"old summary";
226
+ report.fakeDescription = @"old description";
227
+ report.fakeAttributes[@"gone"] = @"value";
228
+ report.fakeAttributes[@"kept"] = @"value";
229
+
230
+ NSError *error = nil;
231
+ XCTAssertTrue([BGSRNReportOps applyPatchJSON:@"{\"summary\":null,\"description\":null,"
232
+ @"\"attributes\":{\"gone\":null,\"count\":3}}"
233
+ toReport:report
234
+ error:&error],
235
+ @"%@", error);
236
+ XCTAssertNil(error);
237
+ XCTAssertNil(report.fakeSummary);
238
+ XCTAssertNil(report.fakeDescription);
239
+ XCTAssertNil(report.fakeAttributes[@"gone"]);
240
+ XCTAssertEqualObjects(report.fakeAttributes[@"kept"], @"value");
241
+ XCTAssertEqualObjects(report.fakeAttributes[@"count"], @3);
242
+ XCTAssertTrue([report.mutations containsObject:@"removeAttributeForName:"]);
243
+ }
244
+
245
+ /// Text that is not a JSON object is a bad argument, and touches nothing.
246
+ - (void)testMalformedPatchJsonIsABadArgument {
247
+ BGSRNFakeReport *report = [BGSRNFakeReport new];
248
+ report.fakeSummary = @"old summary";
249
+ for (NSString *json in @[ @"{\"summary\":", @"[]", @"" ]) {
250
+ NSError *error = nil;
251
+ XCTAssertFalse([BGSRNReportOps applyPatchJSON:json toReport:report error:&error], @"accepted %@", json);
252
+ XCTAssertEqualObjects(error.domain, BGSRNReportErrorDomain);
253
+ XCTAssertEqual(error.code, BGSRNReportErrorBadArgument);
254
+ XCTAssertEqualObjects(BGSRNReportErrorWireCode(error), @"E_REPORT_BAD_ARGUMENT");
255
+ }
256
+ XCTAssertEqualObjects(report.fakeSummary, @"old summary");
257
+ XCTAssertEqualObjects(report.mutations, @[]);
258
+ }
259
+
260
+ /// Parsing does not bypass validation: a bad field still applies nothing.
261
+ - (void)testAParsedPatchIsStillAllOrNothing {
262
+ BGSRNFakeReport *report = [BGSRNFakeReport new];
263
+ report.fakeSummary = @"old summary";
264
+ NSError *error = nil;
265
+ XCTAssertFalse([BGSRNReportOps applyPatchJSON:@"{\"summary\":null,\"attributes\":{\"gone\":null},\"severity\":0}"
266
+ toReport:report
267
+ error:&error]);
268
+ XCTAssertEqual(error.code, BGSRNReportErrorBadArgument);
269
+ XCTAssertEqualObjects(report.fakeSummary, @"old summary");
270
+ XCTAssertEqualObjects(report.mutations, @[]);
271
+ }
272
+
273
+ /// The system parser's own diagnostic can repeat fragments of the text it
274
+ /// choked on; the message is fixed instead.
275
+ - (void)testMalformedTextIsAFixedMessage {
276
+ NSError *error = nil;
277
+ XCTAssertNil(BGSRNJSONObject(@"{\"a\": \"s3cret", &error));
278
+ XCTAssertEqualObjects(BGSRNErrorMessage(error), @"malformed JSON");
279
+ }
280
+
281
+ @end
@@ -0,0 +1,230 @@
1
+ @import XCTest;
2
+ @import BugseeRNSupport;
3
+
4
+ /// Pins `BGSRNLaunchHold`, the WORKAROUND for bugsee-cocoa#227: beta5 drops a
5
+ /// report-creating call made while the SDK is Launching, and `launch()`
6
+ /// resolves before Launched. Calls made then are held and replayed in order
7
+ /// on Launched; a launch that does not complete settles them as dropped.
8
+ /// Every test runs on main, where the hold lives.
9
+ @interface BGSRNLaunchHoldTests : XCTestCase
10
+ @end
11
+
12
+ @implementation BGSRNLaunchHoldTests {
13
+ BugseeStatus _status;
14
+ NSMutableArray<NSString *> *_runs;
15
+ }
16
+
17
+ - (void)setUp {
18
+ [super setUp];
19
+ _status = BugseeStatusLaunching;
20
+ _runs = [NSMutableArray array];
21
+ }
22
+
23
+ - (BGSRNLaunchHold *)holdWithCapacity:(NSUInteger)capacity deadlineMs:(int64_t)deadlineMs {
24
+ __weak __typeof(self) weakSelf = self;
25
+ return [[BGSRNLaunchHold alloc] initWithCapacity:capacity
26
+ deadlineMs:deadlineMs
27
+ status:^BugseeStatus {
28
+ __typeof(self) strongSelf = weakSelf;
29
+ return strongSelf != nil ? strongSelf->_status : BugseeStatusStopped;
30
+ }];
31
+ }
32
+
33
+ - (BGSRNGatedCall)recorder:(NSString *)name {
34
+ NSMutableArray<NSString *> *runs = _runs;
35
+ return ^(BGSRNGatedRun run) {
36
+ NSString *how = run == BGSRNGatedRunNow ? @"now" : run == BGSRNGatedRunReplay ? @"replay" : @"drop";
37
+ [runs addObject:[NSString stringWithFormat:@"%@:%@", name, how]];
38
+ };
39
+ }
40
+
41
+ /// Lets a main-queue hop or a short deadline run. Pumps the main run loop for
42
+ /// `seconds` and asserts nothing about timing: a starved runner only makes it
43
+ /// return late.
44
+ - (void)spinMain:(NSTimeInterval)seconds {
45
+ NSDate *end = [NSDate dateWithTimeIntervalSinceNow:seconds];
46
+ while (end.timeIntervalSinceNow > 0) {
47
+ if (![NSRunLoop.mainRunLoop runMode:NSDefaultRunLoopMode beforeDate:end]) {
48
+ [NSThread sleepForTimeInterval:0.005];
49
+ }
50
+ }
51
+ }
52
+
53
+ /// Pumps main until `done` holds. The generous limit (runner main-queue
54
+ /// starvation) is only a backstop; the wait asserts nothing about timing.
55
+ - (void)spinMainUntil:(BOOL (^)(void))done {
56
+ NSDate *limit = [NSDate dateWithTimeIntervalSinceNow:30];
57
+ while (!done() && limit.timeIntervalSinceNow > 0) {
58
+ [NSRunLoop.mainRunLoop runMode:NSDefaultRunLoopMode
59
+ beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
60
+ }
61
+ XCTAssertTrue(done(), @"main never reached the expected state");
62
+ }
63
+
64
+ - (void)testTheSharedHoldHasTheDocumentedBounds {
65
+ XCTAssertEqual(BGSRNLaunchHoldCapacity, 64u);
66
+ XCTAssertEqual(BGSRNLaunchHoldDeadlineMs, 30000);
67
+ XCTAssertNotNil(BGSRNLaunchHold.shared);
68
+ XCTAssertEqual(BGSRNLaunchHold.shared, BGSRNLaunchHold.shared);
69
+ }
70
+
71
+ - (void)testACallWhileLaunchingIsHeldNotRun {
72
+ BGSRNLaunchHold *hold = [self holdWithCapacity:8 deadlineMs:0];
73
+ [hold submit:@"a" call:[self recorder:@"a"]];
74
+ XCTAssertEqualObjects(_runs, @[]);
75
+ XCTAssertEqual(hold.heldCount, 1u);
76
+ }
77
+
78
+ - (void)testLaunchedReplaysHeldCallsInOrder {
79
+ BGSRNLaunchHold *hold = [self holdWithCapacity:8 deadlineMs:0];
80
+ [hold submit:@"a" call:[self recorder:@"a"]];
81
+ [hold submit:@"b" call:[self recorder:@"b"]];
82
+ [hold submit:@"c" call:[self recorder:@"c"]];
83
+ _status = BugseeStatusLaunched;
84
+ [hold lifecycleEvent:@"com.bugsee.lifecycle.Launched"];
85
+ NSArray *expected = @[ @"a:replay", @"b:replay", @"c:replay" ];
86
+ XCTAssertEqualObjects(_runs, expected);
87
+ XCTAssertEqual(hold.heldCount, 0u);
88
+ // Once only: a second Launched has nothing left to replay.
89
+ [hold lifecycleEvent:@"com.bugsee.lifecycle.Launched"];
90
+ XCTAssertEqualObjects(_runs, expected);
91
+ }
92
+
93
+ /// The SDK delivers the event on its own queue; the hold hops to main.
94
+ - (void)testLaunchedFromAnotherThreadReplaysOnMain {
95
+ BGSRNLaunchHold *hold = [self holdWithCapacity:8 deadlineMs:0];
96
+ __block BOOL onMain = NO;
97
+ [hold submit:@"a" call:^(BGSRNGatedRun run) {
98
+ onMain = NSThread.isMainThread;
99
+ }];
100
+ _status = BugseeStatusLaunched;
101
+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
102
+ [hold lifecycleEvent:@"com.bugsee.lifecycle.Launched"];
103
+ });
104
+ [self spinMain:0.2];
105
+ XCTAssertTrue(onMain);
106
+ XCTAssertEqual(hold.heldCount, 0u);
107
+ }
108
+
109
+ /// Launched can be visible before its event arrives: a new call then runs
110
+ /// after the held ones, never ahead of them.
111
+ - (void)testACallAfterLaunchedRunsBehindTheHeldOnes {
112
+ BGSRNLaunchHold *hold = [self holdWithCapacity:8 deadlineMs:0];
113
+ [hold submit:@"a" call:[self recorder:@"a"]];
114
+ _status = BugseeStatusLaunched;
115
+ [hold submit:@"b" call:[self recorder:@"b"]];
116
+ XCTAssertEqualObjects(_runs, (@[ @"a:replay", @"b:now" ]));
117
+ XCTAssertEqual(hold.heldCount, 0u);
118
+ }
119
+
120
+ /// Unchanged behaviour: with nothing held, a call runs at once whatever the
121
+ /// status -- after Launched, and before launch() (where the SDK drops it).
122
+ - (void)testNothingIsHeldOutsideLaunching {
123
+ BGSRNLaunchHold *hold = [self holdWithCapacity:8 deadlineMs:0];
124
+ _status = BugseeStatusLaunched;
125
+ [hold submit:@"a" call:[self recorder:@"a"]];
126
+ _status = BugseeStatusStopped;
127
+ [hold submit:@"b" call:[self recorder:@"b"]];
128
+ _status = BugseeStatusStopping;
129
+ [hold submit:@"c" call:[self recorder:@"c"]];
130
+ XCTAssertEqualObjects(_runs, (@[ @"a:now", @"b:now", @"c:now" ]));
131
+ XCTAssertEqual(hold.heldCount, 0u);
132
+ }
133
+
134
+ - (void)testTheCapDropsTheCallThatWouldExceedIt {
135
+ BGSRNLaunchHold *hold = [self holdWithCapacity:2 deadlineMs:0];
136
+ [hold submit:@"a" call:[self recorder:@"a"]];
137
+ [hold submit:@"b" call:[self recorder:@"b"]];
138
+ [hold submit:@"c" call:[self recorder:@"c"]];
139
+ XCTAssertEqualObjects(_runs, @[ @"c:drop" ]);
140
+ XCTAssertEqual(hold.heldCount, 2u);
141
+ _status = BugseeStatusLaunched;
142
+ [hold lifecycleEvent:@"com.bugsee.lifecycle.Launched"];
143
+ XCTAssertEqualObjects(_runs, (@[ @"c:drop", @"a:replay", @"b:replay" ]));
144
+ }
145
+
146
+ /// A stop overtaking the launch: the SDK never reaches Launched, so the held
147
+ /// calls settle as dropped calls do, and are not replayed later.
148
+ - (void)testAStopDropsTheHeldCalls {
149
+ for (NSString *event in @[ @"com.bugsee.lifecycle.Stopping", @"com.bugsee.lifecycle.Stopped" ]) {
150
+ [_runs removeAllObjects];
151
+ _status = BugseeStatusLaunching;
152
+ BGSRNLaunchHold *hold = [self holdWithCapacity:8 deadlineMs:0];
153
+ [hold submit:@"a" call:[self recorder:@"a"]];
154
+ [hold submit:@"b" call:[self recorder:@"b"]];
155
+ _status = [event hasSuffix:@"Stopping"] ? BugseeStatusStopping : BugseeStatusStopped;
156
+ [hold lifecycleEvent:event];
157
+ XCTAssertEqualObjects(_runs, (@[ @"a:drop", @"b:drop" ]), @"%@", event);
158
+ XCTAssertEqual(hold.heldCount, 0u);
159
+ _status = BugseeStatusLaunched;
160
+ [hold lifecycleEvent:@"com.bugsee.lifecycle.Launched"];
161
+ XCTAssertEqualObjects(_runs, (@[ @"a:drop", @"b:drop" ]), @"%@", event);
162
+ }
163
+ }
164
+
165
+ /// A Launched from an earlier launch, arriving after a stop and a relaunch,
166
+ /// must not release the new launch's calls, and unrelated events do nothing.
167
+ - (void)testOnlyALaunchedStatusReleases {
168
+ BGSRNLaunchHold *hold = [self holdWithCapacity:8 deadlineMs:0];
169
+ [hold submit:@"a" call:[self recorder:@"a"]];
170
+ [hold lifecycleEvent:@"com.bugsee.lifecycle.Launched"];
171
+ [hold lifecycleEvent:@"com.bugsee.lifecycle.BlackoutStarted"];
172
+ [hold lifecycleEvent:@"com.bugsee.lifecycle.Launching"];
173
+ XCTAssertEqualObjects(_runs, @[]);
174
+ XCTAssertEqual(hold.heldCount, 1u);
175
+ }
176
+
177
+ /// A launch that never completes: the deadline settles the held calls as
178
+ /// dropped, so no promise stays pending for good.
179
+ - (void)testTheDeadlineDropsCallsStillHeld {
180
+ BGSRNLaunchHold *hold = [self holdWithCapacity:8 deadlineMs:50];
181
+ [hold submit:@"a" call:[self recorder:@"a"]];
182
+ [hold submit:@"b" call:[self recorder:@"b"]];
183
+ [self spinMain:0.3];
184
+ XCTAssertEqualObjects(_runs, (@[ @"a:drop", @"b:drop" ]));
185
+ XCTAssertEqual(hold.heldCount, 0u);
186
+ }
187
+
188
+ /// A deadline armed for a batch that Launched already released does nothing
189
+ /// to the next batch, which gets its own.
190
+ ///
191
+ /// No wall-clock race: the "b is still held" check runs inside a main-queue
192
+ /// block due BETWEEN a's stale timer and b's own, so it runs after the former
193
+ /// and before the latter whatever the runner does (timers fire in due order).
194
+ /// With D = 2 s, t = seconds after a was submitted:
195
+ /// a's stale timer due t=2.0 < the check due t=2.5 < b's timer due >= t=3.0
196
+ /// (b is submitted no earlier than t=1.0, and its deadline is D after that).
197
+ - (void)testAnOldDeadlineDoesNotTouchALaterBatch {
198
+ BGSRNLaunchHold *hold = [self holdWithCapacity:8 deadlineMs:2000];
199
+ [hold submit:@"a" call:[self recorder:@"a"]];
200
+ _status = BugseeStatusLaunched;
201
+ [hold lifecycleEvent:@"com.bugsee.lifecycle.Launched"];
202
+ _status = BugseeStatusLaunching;
203
+ __block BOOL checked = NO;
204
+ __weak __typeof(self) weakSelf = self;
205
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
206
+ __typeof(self) strongSelf = weakSelf;
207
+ // a's stale timer has run; b's has not.
208
+ XCTAssertEqualObjects(strongSelf->_runs, @[ @"a:replay" ]);
209
+ XCTAssertEqual(hold.heldCount, 1u);
210
+ checked = YES;
211
+ });
212
+ [self spinMain:1.0];
213
+ [hold submit:@"b" call:[self recorder:@"b"]];
214
+ XCTAssertEqual(hold.heldCount, 1u);
215
+ [self spinMainUntil:^BOOL {
216
+ return checked && hold.heldCount == 0;
217
+ }];
218
+ XCTAssertEqualObjects(_runs, (@[ @"a:replay", @"b:drop" ]));
219
+ }
220
+
221
+ /// At the deadline the status decides: Launched (its event lost) replays.
222
+ - (void)testTheDeadlineReplaysWhenTheSDKIsLaunched {
223
+ BGSRNLaunchHold *hold = [self holdWithCapacity:8 deadlineMs:50];
224
+ [hold submit:@"a" call:[self recorder:@"a"]];
225
+ _status = BugseeStatusLaunched;
226
+ [self spinMain:0.3];
227
+ XCTAssertEqualObjects(_runs, @[ @"a:replay" ]);
228
+ }
229
+
230
+ @end
@@ -0,0 +1,97 @@
1
+ @import XCTest;
2
+ @import BugseeRNSupport;
3
+
4
+ @interface BGSRNMainThreadTests : XCTestCase
5
+ @end
6
+
7
+ @implementation BGSRNMainThreadTests
8
+
9
+ /// The property that matters: whatever thread the caller is on, the block runs
10
+ /// on the main one. Without the hop, an off-main caller's block runs off-main
11
+ /// and the SDK touches UIKit from a background queue.
12
+ - (void)testRunsOnMainWhenCalledFromABackgroundQueue {
13
+ XCTestExpectation *ran = [self expectationWithDescription:@"block ran"];
14
+ __block BOOL onMain = NO;
15
+
16
+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
17
+ XCTAssertFalse([NSThread isMainThread], @"the probe must start off-main");
18
+ BGSRNRunOnMain(^{
19
+ onMain = [NSThread isMainThread];
20
+ [ran fulfill];
21
+ });
22
+ });
23
+
24
+ [self waitForExpectations:@[ ran ] timeout:5];
25
+ XCTAssertTrue(onMain, @"block did not reach the main thread");
26
+ }
27
+
28
+ /// dispatch_sync onto the main queue from the main thread is a hard deadlock.
29
+ /// If this test ever hangs rather than fails, the implementation went back to
30
+ /// dispatch_sync.
31
+ - (void)testDoesNotDeadlockWhenAlreadyOnMain {
32
+ XCTAssertTrue([NSThread isMainThread], @"XCTest runs -test methods on main");
33
+
34
+ __block BOOL ran = NO;
35
+ BGSRNRunOnMain(^{
36
+ ran = YES;
37
+ });
38
+
39
+ // Synchronously true: the already-on-main path must run the block inline
40
+ // rather than deferring it, or a caller that reads state straight after the
41
+ // call sees it unset.
42
+ XCTAssertTrue(ran, @"block was deferred instead of run inline");
43
+ }
44
+
45
+ - (void)testRunsTheBlockExactlyOnce {
46
+ __block NSInteger count = 0;
47
+ BGSRNRunOnMain(^{
48
+ count += 1;
49
+ });
50
+ XCTAssertEqual(count, 1);
51
+ }
52
+
53
+ - (void)testToleratesANilBlock {
54
+ XCTAssertNoThrow(BGSRNRunOnMain(nil));
55
+ }
56
+
57
+ /// The init registration must not return until `+[Bugsee setWrapper:]` has
58
+ /// run. Off main that means the hop waits; an async queue leaves a race with
59
+ /// a later main-thread `startBlackout`.
60
+ - (void)testSyncWaitsForBlockWhenCalledOffMain {
61
+ XCTestExpectation *done = [self expectationWithDescription:@"off-main call returned"];
62
+
63
+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
64
+ XCTAssertFalse([NSThread isMainThread], @"the probe must start off-main");
65
+
66
+ __block BOOL finished = NO;
67
+ BGSRNRunOnMainSync(^{
68
+ // A real main-queue turn so an async hop cannot finish the block before
69
+ // the caller checks `finished` by accident of scheduling luck.
70
+ XCTAssertTrue([NSThread isMainThread]);
71
+ finished = YES;
72
+ });
73
+ XCTAssertTrue(finished,
74
+ @"BGSRNRunOnMainSync returned before the block finished; "
75
+ @"off-main registration would still be pending");
76
+ [done fulfill];
77
+ });
78
+
79
+ [self waitForExpectations:@[ done ] timeout:5];
80
+ }
81
+
82
+ /// Same deadlock rule as BGSRNRunOnMain: sync onto main from main hangs.
83
+ - (void)testSyncDoesNotDeadlockWhenAlreadyOnMain {
84
+ XCTAssertTrue([NSThread isMainThread], @"XCTest runs -test methods on main");
85
+
86
+ __block BOOL ran = NO;
87
+ BGSRNRunOnMainSync(^{
88
+ ran = YES;
89
+ });
90
+ XCTAssertTrue(ran, @"block was deferred instead of run inline");
91
+ }
92
+
93
+ - (void)testSyncToleratesANilBlock {
94
+ XCTAssertNoThrow(BGSRNRunOnMainSync(nil));
95
+ }
96
+
97
+ @end