asupersync-conformance 0.3.3

Conformance test suite for async runtime specifications
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//! Logging infrastructure for conformance tests.
//!
//! Provides structured logging for test execution, with support for
//! capturing logs during test runs and reporting them in results.

use serde::{Deserialize, Serialize};
use std::cell::RefCell;
use std::sync::{Arc, Mutex};
use std::time::Instant;

/// Log level for conformance test logging.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum LogLevel {
    /// Detailed tracing information.
    Trace,
    /// Debug information.
    Debug,
    /// Informational messages.
    Info,
    /// Warning messages.
    Warn,
    /// Error messages.
    Error,
}

impl std::fmt::Display for LogLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            LogLevel::Trace => write!(f, "TRACE"),
            LogLevel::Debug => write!(f, "DEBUG"),
            LogLevel::Info => write!(f, "INFO"),
            LogLevel::Warn => write!(f, "WARN"),
            LogLevel::Error => write!(f, "ERROR"),
        }
    }
}

/// A log entry captured during test execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogEntry {
    /// Log level.
    pub level: LogLevel,
    /// Message text.
    pub message: String,
    /// Target (module/component).
    pub target: String,
    /// Timestamp (milliseconds from test start).
    pub timestamp_ms: u64,
    /// Optional structured fields.
    pub fields: std::collections::HashMap<String, serde_json::Value>,
}

impl LogEntry {
    /// Create a new log entry.
    pub fn new(level: LogLevel, message: impl Into<String>) -> Self {
        Self {
            level,
            message: message.into(),
            target: String::new(),
            timestamp_ms: 0,
            fields: std::collections::HashMap::new(),
        }
    }

    /// Set the target.
    pub fn with_target(mut self, target: impl Into<String>) -> Self {
        self.target = target.into();
        self
    }

    /// Set the timestamp.
    pub fn with_timestamp_ms(mut self, timestamp_ms: u64) -> Self {
        self.timestamp_ms = timestamp_ms;
        self
    }

    /// Add a field.
    pub fn with_field(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.fields.insert(key.into(), value);
        self
    }
}

/// Collector for capturing log entries during test execution.
///
/// Thread-safe and can be cloned to share across async boundaries.
#[derive(Clone)]
pub struct LogCollector {
    entries: Arc<Mutex<Vec<LogEntry>>>,
    start_time: Arc<Mutex<Option<Instant>>>,
    min_level: LogLevel,
}

impl LogCollector {
    /// Create a new log collector.
    pub fn new(min_level: LogLevel) -> Self {
        Self {
            entries: Arc::new(Mutex::new(Vec::new())),
            start_time: Arc::new(Mutex::new(None)),
            min_level,
        }
    }

    /// Start collecting (resets the timer).
    pub fn start(&self) {
        {
            let mut start = self.start_time.lock().unwrap();
            *start = Some(Instant::now());
        }
        self.entries.lock().unwrap().clear();
    }

    /// Log an entry if it meets the minimum level.
    pub fn log(&self, level: LogLevel, message: impl Into<String>) {
        if level < self.min_level {
            return;
        }

        let timestamp_ms = self
            .start_time
            .lock()
            .unwrap()
            .map(|start| start.elapsed().as_millis().min(u128::from(u64::MAX)) as u64)
            .unwrap_or(0);

        let entry = LogEntry::new(level, message).with_timestamp_ms(timestamp_ms);

        self.entries.lock().unwrap().push(entry);
    }

    /// Log with target.
    pub fn log_with_target(&self, level: LogLevel, target: &str, message: impl Into<String>) {
        if level < self.min_level {
            return;
        }

        let timestamp_ms = self
            .start_time
            .lock()
            .unwrap()
            .map(|start| start.elapsed().as_millis().min(u128::from(u64::MAX)) as u64)
            .unwrap_or(0);

        let entry = LogEntry::new(level, message)
            .with_target(target)
            .with_timestamp_ms(timestamp_ms);

        self.entries.lock().unwrap().push(entry);
    }

    /// Drain all collected entries.
    pub fn drain(&self) -> Vec<LogEntry> {
        std::mem::take(&mut *self.entries.lock().unwrap())
    }

    /// Get the number of collected entries.
    pub fn len(&self) -> usize {
        self.entries.lock().unwrap().len()
    }

    /// Check if empty.
    pub fn is_empty(&self) -> bool {
        self.entries.lock().unwrap().is_empty()
    }

    /// Trace-level log.
    pub fn trace(&self, message: impl Into<String>) {
        self.log(LogLevel::Trace, message);
    }

    /// Debug-level log.
    pub fn debug(&self, message: impl Into<String>) {
        self.log(LogLevel::Debug, message);
    }

    /// Info-level log.
    pub fn info(&self, message: impl Into<String>) {
        self.log(LogLevel::Info, message);
    }

    /// Warn-level log.
    pub fn warn(&self, message: impl Into<String>) {
        self.log(LogLevel::Warn, message);
    }

    /// Error-level log.
    pub fn error(&self, message: impl Into<String>) {
        self.log(LogLevel::Error, message);
    }
}

impl Default for LogCollector {
    fn default() -> Self {
        Self::new(LogLevel::Info)
    }
}

impl std::fmt::Debug for LogCollector {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LogCollector")
            .field("entries_count", &self.len())
            .field("min_level", &self.min_level)
            .finish()
    }
}

// ============================================================================
// Conformance Test Logger
// ============================================================================

/// Event types recorded during a conformance test.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TestEventKind {
    /// A named phase transition in the test.
    Phase,
    /// An assertion evaluated during the test.
    Assertion,
    /// Runtime-level event details captured during the test.
    RuntimeEvent,
    /// A warning emitted by the test.
    Warning,
    /// A checkpoint marker with structured data.
    Checkpoint,
}

/// Structured event recorded during a conformance test run.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestEvent {
    /// Event kind.
    pub kind: TestEventKind,
    /// Event name or description.
    pub name: String,
    /// Timestamp in milliseconds since test start.
    pub timestamp_ms: u64,
    /// Structured details for the event.
    pub details: serde_json::Value,
}

impl TestEvent {
    /// Create a new test event.
    pub fn new(
        kind: TestEventKind,
        name: impl Into<String>,
        timestamp_ms: u64,
        details: serde_json::Value,
    ) -> Self {
        Self {
            kind,
            name: name.into(),
            timestamp_ms,
            details,
        }
    }
}

#[derive(Debug)]
struct ConformanceTestLogState {
    test_name: String,
    spec_section: String,
    start_time: Instant,
    events: Vec<TestEvent>,
}

/// Structured logger for conformance test execution.
#[derive(Clone)]
pub struct ConformanceTestLogger {
    inner: Arc<Mutex<ConformanceTestLogState>>,
}

impl ConformanceTestLogger {
    /// Create a new logger for a conformance test.
    pub fn new(test_name: impl Into<String>, spec_section: impl Into<String>) -> Self {
        Self {
            inner: Arc::new(Mutex::new(ConformanceTestLogState {
                test_name: test_name.into(),
                spec_section: spec_section.into(),
                start_time: Instant::now(),
                events: Vec::new(),
            })),
        }
    }

    /// Record a phase transition.
    pub fn phase(&self, name: &'static str) {
        self.record(TestEventKind::Phase, name, serde_json::Value::Null);
    }

    /// Record an assertion and panic if it fails.
    #[track_caller]
    pub fn assert_with_context(&self, condition: bool, description: &str) {
        let location = std::panic::Location::caller().to_string();
        let details = serde_json::json!({
            "passed": condition,
            "location": location,
        });
        self.record(TestEventKind::Assertion, description, details);
        assert!(condition, "Conformance assertion failed: {}", description);
    }

    /// Record a runtime event with structured details.
    pub fn runtime_event(&self, description: &str, details: serde_json::Value) {
        self.record(TestEventKind::RuntimeEvent, description, details);
    }

    /// Record a warning event.
    pub fn warning(&self, message: &str) {
        self.record(TestEventKind::Warning, message, serde_json::Value::Null);
    }

    /// Record a checkpoint.
    pub fn checkpoint(&self, name: &str, data: serde_json::Value) {
        self.record(TestEventKind::Checkpoint, name, data);
    }

    /// Return a snapshot of recorded events.
    pub fn events(&self) -> Vec<TestEvent> {
        self.inner
            .lock()
            .expect("conformance log lock poisoned")
            .events
            .clone()
    }

    /// Get the test name.
    pub fn test_name(&self) -> String {
        self.inner
            .lock()
            .expect("conformance log lock poisoned")
            .test_name
            .clone()
    }

    /// Get the spec section label.
    pub fn spec_section(&self) -> String {
        self.inner
            .lock()
            .expect("conformance log lock poisoned")
            .spec_section
            .clone()
    }

    fn record(&self, kind: TestEventKind, name: &str, details: serde_json::Value) {
        let mut guard = self.inner.lock().expect("conformance log lock poisoned");
        let timestamp_ms = guard
            .start_time
            .elapsed()
            .as_millis()
            .min(u128::from(u64::MAX)) as u64;
        guard
            .events
            .push(TestEvent::new(kind, name, timestamp_ms, details));
    }
}

thread_local! {
    static CURRENT_TEST_LOGGER: RefCell<Option<ConformanceTestLogger>> =
        const { RefCell::new(None) };
}

/// Execute a closure with a logger installed for checkpoint capture.
pub fn with_test_logger<T>(logger: &ConformanceTestLogger, f: impl FnOnce() -> T) -> T {
    struct Guard {
        prev: Option<ConformanceTestLogger>,
    }

    impl Drop for Guard {
        fn drop(&mut self) {
            let prev = self.prev.take();
            CURRENT_TEST_LOGGER.with(|slot| {
                *slot.borrow_mut() = prev;
            });
        }
    }

    let prev = CURRENT_TEST_LOGGER.with(|slot| slot.replace(Some(logger.clone())));
    let _guard = Guard { prev };
    f()
}

/// Record a checkpoint into the current test logger, if one is installed.
pub fn record_checkpoint(name: &str, data: serde_json::Value) {
    CURRENT_TEST_LOGGER.with(|slot| {
        if let Some(logger) = slot.borrow().as_ref() {
            logger.checkpoint(name, data);
        }
    });
}

/// Configuration for logging output.
#[derive(Debug, Clone)]
pub struct LogConfig {
    /// Minimum log level to display.
    pub min_level: LogLevel,
    /// Whether to include timestamps.
    pub show_timestamps: bool,
    /// Whether to include targets.
    pub show_targets: bool,
    /// Whether to use colors (for terminal output).
    pub use_colors: bool,
}

impl Default for LogConfig {
    fn default() -> Self {
        Self {
            min_level: LogLevel::Info,
            show_timestamps: true,
            show_targets: true,
            use_colors: false,
        }
    }
}

impl LogConfig {
    /// Create a new configuration with default settings.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set minimum log level.
    pub fn with_min_level(mut self, level: LogLevel) -> Self {
        self.min_level = level;
        self
    }

    /// Set whether to show timestamps.
    pub fn with_timestamps(mut self, show: bool) -> Self {
        self.show_timestamps = show;
        self
    }

    /// Set whether to show targets.
    pub fn with_targets(mut self, show: bool) -> Self {
        self.show_targets = show;
        self
    }

    /// Set whether to use colors.
    pub fn with_colors(mut self, use_colors: bool) -> Self {
        self.use_colors = use_colors;
        self
    }
}

/// Format a log entry as a string.
pub fn format_entry(entry: &LogEntry, config: &LogConfig) -> String {
    let mut parts = Vec::new();

    if config.show_timestamps {
        parts.push(format!("[{:>8}ms]", entry.timestamp_ms));
    }

    parts.push(format!("{:5}", entry.level));

    if config.show_targets && !entry.target.is_empty() {
        parts.push(format!("[{}]", entry.target));
    }

    parts.push(entry.message.clone());

    if !entry.fields.is_empty() {
        let fields: Vec<String> = entry
            .fields
            .iter()
            .map(|(k, v)| format!("{}={}", k, v))
            .collect();
        parts.push(format!("{{{}}}", fields.join(", ")));
    }

    parts.join(" ")
}

/// Print log entries to stdout.
pub fn print_logs(entries: &[LogEntry], config: &LogConfig) {
    for entry in entries {
        if entry.level >= config.min_level {
            println!("{}", format_entry(entry, config));
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn log_level_ordering() {
        assert!(LogLevel::Trace < LogLevel::Debug);
        assert!(LogLevel::Debug < LogLevel::Info);
        assert!(LogLevel::Info < LogLevel::Warn);
        assert!(LogLevel::Warn < LogLevel::Error);
    }

    #[test]
    fn log_collector_basic() {
        let collector = LogCollector::new(LogLevel::Debug);
        collector.start();

        collector.trace("trace message"); // Should be filtered
        collector.debug("debug message");
        collector.info("info message");

        let entries = collector.drain();
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].message, "debug message");
        assert_eq!(entries[1].message, "info message");
    }

    #[test]
    fn log_collector_with_target() {
        let collector = LogCollector::new(LogLevel::Info);
        collector.start();

        collector.log_with_target(LogLevel::Info, "test::module", "test message");

        let entries = collector.drain();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].target, "test::module");
    }

    #[test]
    fn log_entry_builder() {
        let entry = LogEntry::new(LogLevel::Info, "message")
            .with_target("target")
            .with_timestamp_ms(100)
            .with_field("key", serde_json::json!("value"));

        assert_eq!(entry.level, LogLevel::Info);
        assert_eq!(entry.message, "message");
        assert_eq!(entry.target, "target");
        assert_eq!(entry.timestamp_ms, 100);
        assert_eq!(entry.fields.get("key"), Some(&serde_json::json!("value")));
    }

    #[test]
    fn format_entry_basic() {
        let entry = LogEntry::new(LogLevel::Info, "test message").with_timestamp_ms(42);

        let config = LogConfig::new().with_timestamps(true).with_targets(false);

        let formatted = format_entry(&entry, &config);
        assert!(formatted.contains("42ms"));
        assert!(formatted.contains("INFO"));
        assert!(formatted.contains("test message"));
    }

    #[test]
    fn log_collector_drain_clears() {
        let collector = LogCollector::new(LogLevel::Info);
        collector.start();

        collector.info("message 1");
        let entries = collector.drain();
        assert_eq!(entries.len(), 1);

        collector.info("message 2");
        let entries = collector.drain();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].message, "message 2");
    }

    #[test]
    fn log_config_builder() {
        let config = LogConfig::new()
            .with_min_level(LogLevel::Debug)
            .with_timestamps(false)
            .with_targets(true)
            .with_colors(true);

        assert_eq!(config.min_level, LogLevel::Debug);
        assert!(!config.show_timestamps);
        assert!(config.show_targets);
        assert!(config.use_colors);
    }
}