33package co.touchlab.crashkios.bugsnag
44
55import co.touchlab.crashkios.bugsnag.objc.CrashKiOSBugsnagSinkProtocol
6+ import co.touchlab.crashkios.core.CrashKiOS
67import kotlin.test.Test
78import kotlin.test.assertEquals
89import kotlin.test.assertTrue
910import kotlinx.cinterop.ExperimentalForeignApi
1011import platform.Foundation.NSException
1112import platform.darwin.NSObject
1213
13- // Kotlin stand-in for the Swift sink: exercises the same ObjC-protocol boundary and
14- // proves the module links with zero linker flags (the old -U / RND-91 failure mode).
15- private class FakeBugsnagSink :
16- NSObject (),
17- CrashKiOSBugsnagSinkProtocol {
18- val breadcrumbs = mutableListOf<String >()
19- val notifications = mutableListOf<Pair <List <* >, Boolean >> ()
20- val metadata = mutableListOf<Triple <Any ?, String , String >>()
21- var fatalMarks = 0
22-
23- // Call order across notify/mark — proves the hook sequences them correctly,
24- // not just that both eventually happened.
25- val events = mutableListOf<String >()
26-
27- override fun leaveBreadcrumb (message : String ) {
28- breadcrumbs + = message
29- }
30-
31- override fun markFatalCrashRecorded () {
32- fatalMarks++
33- events + = " mark"
34- }
35-
36- override fun notifyWithExceptions (exceptions : List <* >, handled : Boolean ) {
37- notifications + = exceptions to handled
38- events + = " notify"
39- }
40-
41- override fun addMetadata (value : Any , key : String , section : String ) {
42- metadata + = Triple (value, key, section)
43- }
44- }
45-
4614class BugsnagSinkTest {
47- // registerBugsnagSink() mutates the module-level `bugsnagRegistry` singleton. Safe
48- // to call from every test that needs BugsnagKotlin's facade routed to its own sink
49- // (sinkRef always overwrites) — NOT safe to rely on for the real unhandled-exception
50- // hook actually firing with a given test's sink: hook-install is a once-ever guard,
51- // permanently bound to whichever sink registered first. That's why fatalHookNotifiesThenMarksSession
52- // below calls fatalHook() directly instead of triggering the real hook.
5315 @Test
5416 fun facadeForwardsToRegisteredSink () {
5517 val sink = FakeBugsnagSink ()
56- registerBugsnagSink( sink)
18+ CrashKiOS .configure( BugsnagCrashReporting ( sink) )
5719
5820 BugsnagKotlin .logMessage(" crumb" )
5921 BugsnagKotlin .setCustomValue(" section1" , " key1" , " value1" )
@@ -78,31 +40,51 @@ class BugsnagSinkTest {
7840 assertEquals(2 , fatalExceptions.size, " fatal should carry the cause chain" )
7941 assertEquals(" kotlin.IllegalStateException" , (fatalExceptions[1 ] as NSException ).name)
8042
81- // Only the terminating unhandled-exception hook may mark the session —
82- // a direct sendFatalException() must not (it would poison the OnSendError filter).
43+ // Only the terminating unhandled-exception hook may mark the session,
44+ // but a direct sendFatalException() must not (it would poison the OnSendError filter).
8345 assertEquals(0 , sink.fatalMarks)
8446 }
8547
8648 @Test
8749 fun fatalHookNotifiesThenMarksSession () {
88- // fatalHook() is the terminating hook's actual body (see Bugsnag.kt), exercised
89- // directly rather than through wrapUnhandledExceptionHook — invoking the real
90- // hook would terminate the process.
9150 val sink = FakeBugsnagSink ()
92- // Rebinds BugsnagKotlin.implementation to this sink (register() always
93- // overwrites sinkRef) so fatalHook's sendFatalException() call routes here
94- // rather than to whatever sink an earlier test left registered. Does NOT wire
95- // this sink into the real unhandled-exception hook — that binds once, to
96- // whichever sink registers first — which is exactly why fatalHook is called
97- // directly below instead of relying on the real hook firing.
98- registerBugsnagSink(sink)
51+ CrashKiOS .configure(BugsnagCrashReporting (sink))
9952
10053 fatalHook(sink)(RuntimeException (" terminating boom" ))
10154
10255 assertEquals(1 , sink.fatalMarks)
10356 assertEquals(1 , sink.notifications.size)
104- // Order matters: markFatalCrashRecorded() must run AFTER notify, never before —
105- // marking first would let the OnSendError filter drop a report that was never sent.
57+ // Order matters: markFatalCrashRecorded() must run AFTER notify, never before.
58+ // Marking first would let the OnSendError filter drop a report that was never sent.
10659 assertEquals(listOf (" notify" , " mark" ), sink.events)
10760 }
10861}
62+
63+ private class FakeBugsnagSink :
64+ NSObject (),
65+ CrashKiOSBugsnagSinkProtocol {
66+ val breadcrumbs = mutableListOf<String >()
67+ val notifications = mutableListOf<Pair <List <* >, Boolean >> ()
68+ val metadata = mutableListOf<Triple <Any ?, String , String >>()
69+ var fatalMarks = 0
70+
71+ val events = mutableListOf<String >()
72+
73+ override fun leaveBreadcrumb (message : String ) {
74+ breadcrumbs + = message
75+ }
76+
77+ override fun markFatalCrashRecorded () {
78+ fatalMarks++
79+ events + = " mark"
80+ }
81+
82+ override fun notifyWithExceptions (exceptions : List <* >, handled : Boolean ) {
83+ notifications + = exceptions to handled
84+ events + = " notify"
85+ }
86+
87+ override fun addMetadata (value : Any , key : String , section : String ) {
88+ metadata + = Triple (value, key, section)
89+ }
90+ }
0 commit comments