ios-core 0.1.7

High-level device API, pairing transport, and discovery for iOS devices
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//! XCTest result event parsing and run summary accumulation.
//!
//! Testmanager reports progress as DTX method invocations using private XCTest
//! selectors. This module translates the selectors into stable Rust events and
//! accumulates those events into a serializable summary for CLI and binding users.

use crate::services::dtx::{DtxMessage, DtxPayload, NSObject};
use serde::Serialize;

/// XCTest selector emitted when a test plan begins.
pub const DID_BEGIN_EXECUTING_TEST_PLAN_SELECTOR: &str = "_XCT_didBeginExecutingTestPlan";
/// XCTest selector emitted when a test plan finishes.
pub const DID_FINISH_EXECUTING_TEST_PLAN_SELECTOR: &str = "_XCT_didFinishExecutingTestPlan";
/// XCTest selector for normal log messages.
pub const LOG_MESSAGE_SELECTOR: &str = "_XCT_logMessage:";
/// XCTest selector for debug log messages.
pub const LOG_DEBUG_MESSAGE_SELECTOR: &str = "_XCT_logDebugMessage:";
/// XCTest selector emitted when a suite starts.
pub const TEST_SUITE_STARTED_SELECTOR: &str = "_XCT_testSuite:didStartAt:";
/// XCTest selector emitted when older XCTest runtimes finish a suite.
pub const TEST_SUITE_FINISHED_SELECTOR: &str =
    "_XCT_testSuite:didFinishAt:runCount:withFailures:unexpected:testDuration:totalDuration:";
/// XCTest selector emitted when newer XCTest runtimes finish a suite with skip counts.
pub const TEST_SUITE_FINISHED_WITH_SKIP_SELECTOR: &str =
    "_XCT_testSuiteWithIdentifier:didFinishAt:runCount:skipCount:failureCount:expectedFailureCount:uncaughtExceptionCount:testDuration:totalDuration:";
/// XCTest selector emitted when a test case starts.
pub const TEST_CASE_STARTED_SELECTOR: &str = "_XCT_testCaseDidStartForTestClass:method:";
/// XCTest selector emitted when a test case finishes.
pub const TEST_CASE_FINISHED_SELECTOR: &str =
    "_XCT_testCaseDidFinishForTestClass:method:withStatus:duration:";
/// XCTest selector emitted when a test case records a failure.
pub const TEST_CASE_FAILED_SELECTOR: &str =
    "_XCT_testCaseDidFailForTestClass:method:withMessage:file:line:";

/// A normalized XCTest execution event decoded from a DTX method invocation.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum TestExecutionEvent {
    /// The test plan started.
    BeganPlan,
    /// The test plan finished.
    FinishedPlan,
    /// XCTest emitted a log message.
    Log {
        /// Log message text.
        message: String,
        /// Whether the message came from the debug log selector.
        debug: bool,
    },
    /// A test suite started.
    SuiteStarted {
        /// Suite name.
        name: String,
        /// Start timestamp string as reported by XCTest.
        started_at: Option<String>,
    },
    /// A test suite finished and reported aggregate counts.
    SuiteFinished {
        /// Suite name.
        name: String,
        /// Finish timestamp string as reported by XCTest.
        finished_at: Option<String>,
        /// Number of tests reported by the suite.
        test_count: u64,
        /// Number of skipped tests.
        skipped: u64,
        /// Number of failures.
        failures: u64,
        /// Number of expected failures.
        expected_failures: u64,
        /// Number of unexpected failures.
        unexpected_failures: u64,
        /// Number of uncaught exceptions.
        uncaught_exceptions: u64,
        /// XCTest execution duration in seconds.
        test_duration_seconds: f64,
        /// Total suite duration in seconds.
        total_duration_seconds: f64,
    },
    /// A test case started.
    CaseStarted {
        /// XCTest class name.
        class_name: String,
        /// XCTest method name.
        method_name: String,
    },
    /// A test case reported a failure.
    CaseFailed {
        /// XCTest class name.
        class_name: String,
        /// XCTest method name.
        method_name: String,
        /// Failure message.
        message: String,
        /// Source file when XCTest reports one.
        file: Option<String>,
        /// Source line when XCTest reports one.
        line: Option<u64>,
    },
    /// A test case finished with a final status.
    CaseFinished {
        /// XCTest class name.
        class_name: String,
        /// XCTest method name.
        method_name: String,
        /// Final test status.
        status: TestCaseStatus,
        /// Test case duration in seconds.
        duration_seconds: f64,
    },
}

impl TestExecutionEvent {
    /// Decode a supported XCTest DTX method invocation.
    pub fn from_dtx_message(message: &DtxMessage) -> Option<Self> {
        let DtxPayload::MethodInvocation { selector, args } = &message.payload else {
            return None;
        };
        match selector.as_str() {
            DID_BEGIN_EXECUTING_TEST_PLAN_SELECTOR => Some(Self::BeganPlan),
            DID_FINISH_EXECUTING_TEST_PLAN_SELECTOR => Some(Self::FinishedPlan),
            LOG_MESSAGE_SELECTOR => Some(Self::Log {
                message: string_arg(args, 0)?,
                debug: false,
            }),
            LOG_DEBUG_MESSAGE_SELECTOR => Some(Self::Log {
                message: string_arg(args, 0)?,
                debug: true,
            }),
            TEST_SUITE_STARTED_SELECTOR => Some(Self::SuiteStarted {
                name: string_arg(args, 0)?,
                started_at: optional_string_arg(args, 1),
            }),
            TEST_SUITE_FINISHED_SELECTOR => Some(Self::SuiteFinished {
                name: string_arg(args, 0)?,
                finished_at: optional_string_arg(args, 1),
                test_count: uint_arg(args, 2).unwrap_or(0),
                skipped: 0,
                failures: uint_arg(args, 3).unwrap_or(0),
                expected_failures: 0,
                unexpected_failures: uint_arg(args, 4).unwrap_or(0),
                uncaught_exceptions: 0,
                test_duration_seconds: double_arg(args, 5).unwrap_or(0.0),
                total_duration_seconds: double_arg(args, 6).unwrap_or(0.0),
            }),
            TEST_SUITE_FINISHED_WITH_SKIP_SELECTOR => {
                let name = identifier_suite_name(args.first())?;
                Some(Self::SuiteFinished {
                    name,
                    finished_at: optional_string_arg(args, 1),
                    test_count: uint_arg(args, 2).unwrap_or(0),
                    skipped: uint_arg(args, 3).unwrap_or(0),
                    failures: uint_arg(args, 4).unwrap_or(0),
                    expected_failures: uint_arg(args, 5).unwrap_or(0),
                    unexpected_failures: 0,
                    uncaught_exceptions: uint_arg(args, 6).unwrap_or(0),
                    test_duration_seconds: double_arg(args, 7).unwrap_or(0.0),
                    total_duration_seconds: double_arg(args, 8).unwrap_or(0.0),
                })
            }
            TEST_CASE_STARTED_SELECTOR => Some(Self::CaseStarted {
                class_name: string_arg(args, 0)?,
                method_name: string_arg(args, 1)?,
            }),
            TEST_CASE_FAILED_SELECTOR => Some(Self::CaseFailed {
                class_name: string_arg(args, 0)?,
                method_name: string_arg(args, 1)?,
                message: string_arg(args, 2).unwrap_or_default(),
                file: optional_string_arg(args, 3),
                line: uint_arg(args, 4),
            }),
            TEST_CASE_FINISHED_SELECTOR => Some(Self::CaseFinished {
                class_name: string_arg(args, 0)?,
                method_name: string_arg(args, 1)?,
                status: TestCaseStatus::from_wda_status(&string_arg(args, 2)?),
                duration_seconds: double_arg(args, 3).unwrap_or(0.0),
            }),
            _ => None,
        }
    }

    /// Return true when this event marks the end of the plan.
    pub fn is_finished_plan(&self) -> bool {
        matches!(self, Self::FinishedPlan)
    }
}

/// Normalized XCTest case status.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum TestCaseStatus {
    /// Test passed.
    Passed,
    /// Test failed.
    Failed,
    /// XCTest reported an expected failure.
    ExpectedFailure,
    /// XCTest reported a stalled case.
    Stalled,
    /// Test was skipped.
    Skipped,
    /// Status string not modeled by ios-core yet.
    Other(String),
}

impl TestCaseStatus {
    fn from_wda_status(status: &str) -> Self {
        match status {
            "passed" => Self::Passed,
            "failed" => Self::Failed,
            "expected failure" => Self::ExpectedFailure,
            "stalled" => Self::Stalled,
            "skipped" => Self::Skipped,
            other => Self::Other(other.to_string()),
        }
    }
}

/// Failure details for a single test case.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct TestFailure {
    /// Failure message emitted by XCTest.
    pub message: String,
    /// Source file path when available.
    pub file: Option<String>,
    /// Source line when available.
    pub line: Option<u64>,
}

/// Summary for one XCTest case.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct TestCaseSummary {
    /// XCTest class name.
    pub class_name: String,
    /// XCTest method name.
    pub method_name: String,
    /// Final case status, if observed.
    pub status: Option<TestCaseStatus>,
    /// Case duration in seconds, if reported.
    pub duration_seconds: Option<f64>,
    /// First failure associated with the case, if any.
    pub failure: Option<TestFailure>,
}

/// Summary for one XCTest suite.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct TestSuiteSummary {
    /// Suite name.
    pub name: String,
    /// Start timestamp string as reported by XCTest.
    pub started_at: Option<String>,
    /// Finish timestamp string as reported by XCTest.
    pub finished_at: Option<String>,
    /// Total tests reported by the suite.
    pub test_count: Option<u64>,
    /// Skipped test count.
    pub skipped: Option<u64>,
    /// Failure count.
    pub failures: Option<u64>,
    /// Expected failure count.
    pub expected_failures: Option<u64>,
    /// Unexpected failure count.
    pub unexpected_failures: Option<u64>,
    /// Uncaught exception count.
    pub uncaught_exceptions: Option<u64>,
    /// XCTest execution duration in seconds.
    pub test_duration_seconds: Option<f64>,
    /// Total suite duration in seconds.
    pub total_duration_seconds: Option<f64>,
    /// Case summaries accumulated for this suite.
    pub cases: Vec<TestCaseSummary>,
}

/// Summary for an XCTest run.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct TestRunSummary {
    /// Whether a plan-start event was observed.
    pub began: bool,
    /// Whether a plan-finish event was observed.
    pub finished: bool,
    /// Total test count across suites.
    pub total_tests: u64,
    /// Total failed test count across suites.
    pub failed_tests: u64,
    /// Total skipped test count across suites.
    pub skipped_tests: u64,
    /// Non-debug log messages.
    pub logs: Vec<String>,
    /// Debug log messages.
    pub debug_logs: Vec<String>,
    /// Suite summaries.
    pub suites: Vec<TestSuiteSummary>,
}

/// Stateful accumulator for XCTest events.
#[derive(Debug, Default, Clone)]
pub struct TestRunRecorder {
    began: bool,
    finished: bool,
    logs: Vec<String>,
    debug_logs: Vec<String>,
    suites: Vec<TestSuiteSummary>,
}

impl TestRunRecorder {
    /// Apply one event to the current run summary.
    pub fn apply(&mut self, event: TestExecutionEvent) {
        match event {
            TestExecutionEvent::BeganPlan => self.began = true,
            TestExecutionEvent::FinishedPlan => self.finished = true,
            TestExecutionEvent::Log { message, debug } => {
                if debug {
                    self.debug_logs.push(message);
                } else {
                    self.logs.push(message);
                }
            }
            TestExecutionEvent::SuiteStarted { name, started_at } => {
                self.suites.push(TestSuiteSummary {
                    name,
                    started_at,
                    finished_at: None,
                    test_count: None,
                    skipped: None,
                    failures: None,
                    expected_failures: None,
                    unexpected_failures: None,
                    uncaught_exceptions: None,
                    test_duration_seconds: None,
                    total_duration_seconds: None,
                    cases: Vec::new(),
                });
            }
            TestExecutionEvent::SuiteFinished {
                name,
                finished_at,
                test_count,
                skipped,
                failures,
                expected_failures,
                unexpected_failures,
                uncaught_exceptions,
                test_duration_seconds,
                total_duration_seconds,
            } => {
                let suite = self.find_or_create_suite(&name);
                suite.finished_at = finished_at;
                suite.test_count = Some(test_count);
                suite.skipped = Some(skipped);
                suite.failures = Some(failures);
                suite.expected_failures = Some(expected_failures);
                suite.unexpected_failures = Some(unexpected_failures);
                suite.uncaught_exceptions = Some(uncaught_exceptions);
                suite.test_duration_seconds = Some(test_duration_seconds);
                suite.total_duration_seconds = Some(total_duration_seconds);
            }
            TestExecutionEvent::CaseStarted {
                class_name,
                method_name,
            } => {
                let suite = self.find_or_create_suite(&class_name);
                suite.cases.push(TestCaseSummary {
                    class_name,
                    method_name,
                    status: None,
                    duration_seconds: None,
                    failure: None,
                });
            }
            TestExecutionEvent::CaseFailed {
                class_name,
                method_name,
                message,
                file,
                line,
            } => {
                let case = self.find_or_create_case(&class_name, &method_name);
                case.status = Some(TestCaseStatus::Failed);
                case.failure = Some(TestFailure {
                    message,
                    file,
                    line,
                });
            }
            TestExecutionEvent::CaseFinished {
                class_name,
                method_name,
                status,
                duration_seconds,
            } => {
                let case = self.find_or_create_case(&class_name, &method_name);
                if case.status != Some(TestCaseStatus::Stalled) {
                    case.status = Some(status);
                }
                case.duration_seconds = Some(duration_seconds);
            }
        }
    }

    /// Build a serializable summary from the events applied so far.
    pub fn summary(&self) -> TestRunSummary {
        let total_tests = self
            .suites
            .iter()
            .map(|suite| suite.test_count.unwrap_or(suite.cases.len() as u64))
            .sum();
        let failed_tests = self
            .suites
            .iter()
            .map(|suite| {
                suite.failures.unwrap_or_else(|| {
                    suite
                        .cases
                        .iter()
                        .filter(|case| case.status == Some(TestCaseStatus::Failed))
                        .count() as u64
                })
            })
            .sum();
        let skipped_tests = self
            .suites
            .iter()
            .map(|suite| {
                suite.skipped.unwrap_or_else(|| {
                    suite
                        .cases
                        .iter()
                        .filter(|case| case.status == Some(TestCaseStatus::Skipped))
                        .count() as u64
                })
            })
            .sum();

        TestRunSummary {
            began: self.began,
            finished: self.finished,
            total_tests,
            failed_tests,
            skipped_tests,
            logs: self.logs.clone(),
            debug_logs: self.debug_logs.clone(),
            suites: self.suites.clone(),
        }
    }

    fn find_or_create_case(&mut self, class_name: &str, method_name: &str) -> &mut TestCaseSummary {
        let suite = self.find_or_create_suite(class_name);
        if let Some(index) = suite
            .cases
            .iter()
            .rposition(|case| case.class_name == class_name && case.method_name == method_name)
        {
            return &mut suite.cases[index];
        }
        let index = suite.cases.len();
        suite.cases.push(TestCaseSummary {
            class_name: class_name.to_string(),
            method_name: method_name.to_string(),
            status: None,
            duration_seconds: None,
            failure: None,
        });
        &mut suite.cases[index]
    }

    fn find_or_create_suite(&mut self, name: &str) -> &mut TestSuiteSummary {
        if let Some(index) = self.suites.iter().rposition(|suite| suite.name == name) {
            return &mut self.suites[index];
        }
        let index = self.suites.len();
        self.suites.push(TestSuiteSummary {
            name: name.to_string(),
            started_at: None,
            finished_at: None,
            test_count: None,
            skipped: None,
            failures: None,
            expected_failures: None,
            unexpected_failures: None,
            uncaught_exceptions: None,
            test_duration_seconds: None,
            total_duration_seconds: None,
            cases: Vec::new(),
        });
        &mut self.suites[index]
    }
}

fn string_arg(args: &[NSObject], index: usize) -> Option<String> {
    args.get(index)
        .and_then(NSObject::as_str)
        .map(ToString::to_string)
}

fn optional_string_arg(args: &[NSObject], index: usize) -> Option<String> {
    string_arg(args, index).filter(|value| !value.is_empty())
}

fn uint_arg(args: &[NSObject], index: usize) -> Option<u64> {
    match args.get(index)? {
        NSObject::Uint(value) => Some(*value),
        NSObject::Int(value) if *value >= 0 => Some(*value as u64),
        _ => None,
    }
}

fn double_arg(args: &[NSObject], index: usize) -> Option<f64> {
    match args.get(index)? {
        NSObject::Double(value) => Some(*value),
        NSObject::Int(value) => Some(*value as f64),
        NSObject::Uint(value) => Some(*value as f64),
        _ => None,
    }
}

fn identifier_suite_name(value: Option<&NSObject>) -> Option<String> {
    match value? {
        NSObject::String(value) => Some(value.clone()),
        NSObject::Array(values) => values.first().and_then(|value| match value {
            NSObject::String(name) => Some(name.clone()),
            _ => None,
        }),
        NSObject::Dict(dict) => dict
            .get("container")
            .or_else(|| dict.get("suite"))
            .or_else(|| dict.get("testClass"))
            .and_then(NSObject::as_str)
            .map(ToString::to_string),
        _ => None,
    }
}