Skip to main content

cmtraceopen_parser/parser/
detect.rs

1//! Format auto-detection.
2//!
3//! Examines the first non-empty lines of file content to determine
4//! whether it uses CCM, Simple, Timestamped, or Plain text format.
5//! Normally samples 20 lines; when DNS path hints are detected, the
6//! sample window extends to 50 lines to look past the ~29-line DNS
7//! debug log header.
8//!
9//! Detection strategy (matches CMTrace binary behavior, extended):
10//! - Check for `<![LOG[` marker → CCM format
11//! - Check for `$$<` delimiter → Simple format
12//! - Check for Panther path hints plus Panther setup log records → dedicated Panther parser
13//! - Check for ReportingEvents path hints plus tab-delimited update history rows → dedicated ReportingEvents parser
14//! - Check for ` type="` substring → CCM format (fallback indicator)
15//! - Check for timestamp patterns (ISO, slash-date, syslog, time-only) → Timestamped
16//! - Otherwise → Plain text
17
18use super::{
19    burn, cbs, cmtlog, dhcp, dism, dns_debug, iis_w3c, intune_macos, msi, panther,
20    patchmypc_detection, psadt, reporting_events, secureboot_log,
21    timestamped::{self, DateOrder},
22};
23use crate::models::log_entry::{
24    DateFieldOrder, LogFormat, ParseQuality, ParserImplementation, ParserKind, ParserProvenance,
25    ParserSelectionInfo, ParserSpecialization, RecordFraming,
26};
27
28/// Backend-owned parser selection used for both initial parsing and tailing.
29#[derive(Debug, Clone, PartialEq, Eq)]
30pub struct ResolvedParser {
31    pub parser: ParserKind,
32    pub implementation: ParserImplementation,
33    pub provenance: ParserProvenance,
34    pub parse_quality: ParseQuality,
35    pub record_framing: RecordFraming,
36    pub date_order: DateOrder,
37    pub specialization: Option<ParserSpecialization>,
38}
39
40impl ResolvedParser {
41    pub fn new(
42        parser: ParserKind,
43        implementation: ParserImplementation,
44        provenance: ParserProvenance,
45        parse_quality: ParseQuality,
46        record_framing: RecordFraming,
47        date_order: DateOrder,
48        specialization: Option<ParserSpecialization>,
49    ) -> Self {
50        Self {
51            parser,
52            implementation,
53            provenance,
54            parse_quality,
55            record_framing,
56            date_order,
57            specialization,
58        }
59    }
60
61    pub fn ccm() -> Self {
62        Self::new(
63            ParserKind::Ccm,
64            ParserImplementation::Ccm,
65            ParserProvenance::Dedicated,
66            ParseQuality::Structured,
67            RecordFraming::LogicalRecord,
68            DateOrder::default(),
69            None,
70        )
71    }
72
73    pub fn simple() -> Self {
74        Self::new(
75            ParserKind::Simple,
76            ParserImplementation::Simple,
77            ParserProvenance::Dedicated,
78            ParseQuality::Structured,
79            RecordFraming::PhysicalLine,
80            DateOrder::default(),
81            None,
82        )
83    }
84
85    pub fn ime() -> Self {
86        Self::new(
87            ParserKind::Ccm,
88            ParserImplementation::Ccm,
89            ParserProvenance::Dedicated,
90            ParseQuality::Structured,
91            RecordFraming::LogicalRecord,
92            DateOrder::default(),
93            Some(ParserSpecialization::Ime),
94        )
95    }
96
97    pub fn generic_timestamped(date_order: DateOrder) -> Self {
98        Self::new(
99            ParserKind::Timestamped,
100            ParserImplementation::GenericTimestamped,
101            ParserProvenance::Heuristic,
102            ParseQuality::SemiStructured,
103            RecordFraming::PhysicalLine,
104            date_order,
105            None,
106        )
107    }
108
109    pub fn panther() -> Self {
110        Self::new(
111            ParserKind::Panther,
112            ParserImplementation::GenericTimestamped,
113            ParserProvenance::Dedicated,
114            ParseQuality::SemiStructured,
115            RecordFraming::LogicalRecord,
116            DateOrder::default(),
117            None,
118        )
119    }
120
121    pub fn cbs() -> Self {
122        Self::new(
123            ParserKind::Cbs,
124            ParserImplementation::GenericTimestamped,
125            ParserProvenance::Dedicated,
126            ParseQuality::SemiStructured,
127            RecordFraming::LogicalRecord,
128            DateOrder::default(),
129            None,
130        )
131    }
132
133    pub fn dism() -> Self {
134        Self::new(
135            ParserKind::Dism,
136            ParserImplementation::GenericTimestamped,
137            ParserProvenance::Dedicated,
138            ParseQuality::SemiStructured,
139            RecordFraming::LogicalRecord,
140            DateOrder::default(),
141            None,
142        )
143    }
144
145    pub fn plain_text() -> Self {
146        Self::new(
147            ParserKind::Plain,
148            ParserImplementation::PlainText,
149            ParserProvenance::Fallback,
150            ParseQuality::TextFallback,
151            RecordFraming::PhysicalLine,
152            DateOrder::default(),
153            None,
154        )
155    }
156
157    pub fn iis_w3c() -> Self {
158        Self::new(
159            ParserKind::IisW3c,
160            ParserImplementation::IisW3c,
161            ParserProvenance::Dedicated,
162            ParseQuality::Structured,
163            RecordFraming::PhysicalLine,
164            DateOrder::default(),
165            None,
166        )
167    }
168
169    pub fn reporting_events() -> Self {
170        Self::new(
171            ParserKind::ReportingEvents,
172            ParserImplementation::ReportingEvents,
173            ParserProvenance::Dedicated,
174            ParseQuality::Structured,
175            RecordFraming::PhysicalLine,
176            DateOrder::default(),
177            None,
178        )
179    }
180
181    pub fn msi() -> Self {
182        Self::new(
183            ParserKind::Msi,
184            ParserImplementation::Msi,
185            ParserProvenance::Dedicated,
186            ParseQuality::SemiStructured,
187            RecordFraming::PhysicalLine,
188            DateOrder::MonthFirst,
189            None,
190        )
191    }
192
193    pub fn burn() -> Self {
194        Self::new(
195            ParserKind::Burn,
196            ParserImplementation::Burn,
197            ParserProvenance::Dedicated,
198            ParseQuality::Structured,
199            RecordFraming::PhysicalLine,
200            DateOrder::default(),
201            None,
202        )
203    }
204
205    pub fn patchmypc_detection() -> Self {
206        Self::new(
207            ParserKind::PatchMyPcDetection,
208            ParserImplementation::PatchMyPcDetection,
209            ParserProvenance::Dedicated,
210            ParseQuality::Structured,
211            RecordFraming::PhysicalLine,
212            DateOrder::MonthFirst,
213            None,
214        )
215    }
216
217    pub fn dhcp() -> Self {
218        Self::new(
219            ParserKind::Dhcp,
220            ParserImplementation::Dhcp,
221            ParserProvenance::Dedicated,
222            ParseQuality::Structured,
223            RecordFraming::PhysicalLine,
224            DateOrder::MonthFirst,
225            None,
226        )
227    }
228
229    pub fn intune_macos() -> Self {
230        Self::new(
231            ParserKind::IntuneMacOs,
232            ParserImplementation::IntuneMacOs,
233            ParserProvenance::Dedicated,
234            ParseQuality::Structured,
235            RecordFraming::PhysicalLine,
236            DateOrder::default(),
237            None,
238        )
239    }
240
241    pub fn registry() -> Self {
242        Self::new(
243            ParserKind::Registry,
244            ParserImplementation::Registry,
245            ParserProvenance::Dedicated,
246            ParseQuality::Structured,
247            RecordFraming::PhysicalLine,
248            DateOrder::default(),
249            None,
250        )
251    }
252
253    pub fn secureboot_log() -> Self {
254        Self::new(
255            ParserKind::SecureBootLog,
256            ParserImplementation::SecureBootLog,
257            ParserProvenance::Dedicated,
258            ParseQuality::Structured,
259            RecordFraming::PhysicalLine,
260            DateOrder::default(),
261            None,
262        )
263    }
264
265    pub fn cmtlog() -> Self {
266        Self::new(
267            ParserKind::CmtLog,
268            ParserImplementation::CmtLog,
269            ParserProvenance::Dedicated,
270            ParseQuality::Structured,
271            RecordFraming::PhysicalLine,
272            DateOrder::MonthFirst,
273            None,
274        )
275    }
276
277    pub fn cmtlog_heuristic() -> Self {
278        Self::new(
279            ParserKind::CmtLog,
280            ParserImplementation::CmtLog,
281            ParserProvenance::Heuristic,
282            ParseQuality::Structured,
283            RecordFraming::PhysicalLine,
284            DateOrder::MonthFirst,
285            None,
286        )
287    }
288
289    pub fn psadt_legacy() -> Self {
290        Self::new(
291            ParserKind::PsadtLegacy,
292            ParserImplementation::PsadtLegacy,
293            ParserProvenance::Dedicated,
294            ParseQuality::Structured,
295            RecordFraming::PhysicalLine,
296            DateOrder::default(),
297            None,
298        )
299    }
300
301    pub fn dns_debug(date_order: DateOrder) -> Self {
302        Self::new(
303            ParserKind::DnsDebug,
304            ParserImplementation::DnsDebug,
305            ParserProvenance::Dedicated,
306            ParseQuality::Structured,
307            RecordFraming::LogicalRecord,
308            date_order,
309            None,
310        )
311    }
312
313    pub fn dns_audit() -> Self {
314        Self::new(
315            ParserKind::DnsAudit,
316            ParserImplementation::DnsAudit,
317            ParserProvenance::Dedicated,
318            ParseQuality::Structured,
319            RecordFraming::PhysicalLine,
320            DateOrder::default(),
321            None,
322        )
323    }
324
325    pub fn compatibility_format(&self) -> LogFormat {
326        match self.implementation {
327            ParserImplementation::Ccm => LogFormat::Ccm,
328            ParserImplementation::Simple => LogFormat::Simple,
329            ParserImplementation::GenericTimestamped => LogFormat::Timestamped,
330            ParserImplementation::IisW3c => LogFormat::Timestamped,
331            ParserImplementation::ReportingEvents => LogFormat::Timestamped,
332            ParserImplementation::Msi => LogFormat::Timestamped,
333            ParserImplementation::PsadtLegacy => LogFormat::Timestamped,
334            ParserImplementation::IntuneMacOs => LogFormat::Timestamped,
335            ParserImplementation::Dhcp => LogFormat::Timestamped,
336            ParserImplementation::Burn => LogFormat::Timestamped,
337            ParserImplementation::PatchMyPcDetection => LogFormat::Timestamped,
338            ParserImplementation::PlainText => LogFormat::Plain,
339            ParserImplementation::Registry => LogFormat::Plain,
340            ParserImplementation::SecureBootLog => LogFormat::Timestamped,
341            ParserImplementation::DnsDebug => LogFormat::DnsDebug,
342            ParserImplementation::DnsAudit => LogFormat::DnsAudit,
343            ParserImplementation::CmtLog => LogFormat::CmtLog,
344        }
345    }
346
347    pub fn to_info(&self) -> ParserSelectionInfo {
348        ParserSelectionInfo {
349            parser: self.parser,
350            implementation: self.implementation,
351            provenance: self.provenance,
352            parse_quality: self.parse_quality,
353            record_framing: self.record_framing,
354            date_order: match (self.parser, self.implementation) {
355                (ParserKind::Timestamped, ParserImplementation::GenericTimestamped) => {
356                    Some(match self.date_order {
357                        DateOrder::MonthFirst => DateFieldOrder::MonthFirst,
358                        DateOrder::DayFirst => DateFieldOrder::DayFirst,
359                    })
360                }
361                _ => None,
362            },
363            specialization: self.specialization,
364        }
365    }
366
367    /// Parse a single line of text using this resolved parser, overriding the
368    /// resulting entry's `line_number` with the provided value.
369    ///
370    /// This is a thin wrapper over `parse_lines_with_selection` intended for
371    /// on-demand materialization (e.g. the unified timeline) where we already
372    /// know the parser and the original line number, and just need to rebuild
373    /// a single `LogEntry` from the raw text.
374    pub fn parse_one_line(
375        &self,
376        text: &str,
377        line_number: u32,
378    ) -> Result<crate::models::log_entry::LogEntry, String> {
379        // Trim a single trailing newline (the raw-entry reader keeps the newline).
380        let trimmed = text.strip_suffix('\n').unwrap_or(text);
381        let trimmed = trimmed.strip_suffix('\r').unwrap_or(trimmed);
382        let lines = [trimmed];
383        let (mut entries, _parse_errors) = super::parse_lines_with_selection(&lines, "", self);
384        let mut entry = entries
385            .drain(..)
386            .next()
387            .ok_or_else(|| "parser produced no entries".to_string())?;
388        entry.line_number = line_number;
389        Ok(entry)
390    }
391}
392
393/// Detect the parser selection from file content.
394/// Examines up to the first 20 non-empty lines.
395pub fn detect_parser(path: &str, content: &str) -> ResolvedParser {
396    // Registry export file — unambiguous header, check first.
397    {
398        let trimmed_start = content.trim_start();
399        if trimmed_start.starts_with("Windows Registry Editor Version 5.00")
400            || trimmed_start.starts_with("REGEDIT4")
401        {
402            return ResolvedParser::registry();
403        }
404    }
405
406    // Early detection: DHCP logs have a ~35-line header before any CSV data.
407    // The first 20 non-empty lines are all header text, so content-based matching
408    // won't find data rows. Detect via header signature or path hint + header.
409    {
410        let content_lower = content.to_ascii_lowercase();
411        if content_lower.starts_with("\t\tmicrosoft dhcp")
412            || content_lower.contains("microsoft dhcp service activity log")
413            || content_lower.contains("microsoft dhcpv6 service activity log")
414        {
415            return ResolvedParser::dhcp();
416        }
417    }
418
419    if content.lines().take(5).any(|line| {
420        line.trim_start()
421            .to_ascii_lowercase()
422            .starts_with("#software: microsoft internet information services")
423    }) {
424        return ResolvedParser::iis_w3c();
425    }
426
427    // Path hints must be computed before sample_lines so the sample limit can depend on them.
428    let path_lower = path.to_ascii_lowercase();
429
430    // .cmtlog extension — unambiguous dedicated format, return immediately.
431    if path_lower.ends_with(".cmtlog") {
432        return ResolvedParser::cmtlog();
433    }
434
435    let dns_debug_path_hint = path_lower.contains("dns")
436        || path_lower.ends_with("dns.log")
437        || path_lower.contains("\\dns\\")
438        || path_lower.contains("/dns/");
439
440    // DNS debug logs have a ~29-line header before any PACKET records.
441    // Extend the sample window when DNS path hints are present.
442    let sample_limit = if dns_debug_path_hint { 50 } else { 20 };
443    let sample_lines: Vec<&str> = content
444        .lines()
445        .filter(|l| !l.trim().is_empty())
446        .take(sample_limit)
447        .collect();
448
449    let panther_path_hint = path_lower.contains("panther")
450        || path_lower.ends_with("setupact.log")
451        || path_lower.ends_with("setuperr.log");
452    let cbs_path_hint = path_lower.ends_with("cbs.log")
453        || path_lower.contains("/logs/cbs/")
454        || path_lower.contains("\\logs\\cbs\\");
455    let dism_path_hint = path_lower.ends_with("dism.log")
456        || path_lower.contains("/logs/dism/")
457        || path_lower.contains("\\logs\\dism\\");
458    let reporting_events_path_hint = path_lower.ends_with("reportingevents.log")
459        || path_lower.contains("/softwaredistribution/reportingevents.log")
460        || path_lower.contains("\\softwaredistribution\\reportingevents.log");
461    let ime_file_name = path_lower.rsplit(['/', '\\']).next().unwrap_or("");
462    let ime_path_hint = [
463        "agentexecutor",
464        "appactionprocessor",
465        "appworkload",
466        "clienthealth",
467        "healthscripts",
468        "intunemanagementextension",
469    ]
470    .iter()
471    .any(|prefix| ime_file_name.starts_with(prefix));
472
473    let dhcp_path_hint = path_lower.contains("dhcpsrvlog")
474        || path_lower.contains("dhcpv6srvlog")
475        || path_lower.contains("dhcp_logs");
476    let iis_w3c_path_hint = path_lower.contains("/inetpub/logs/")
477        || path_lower.contains("\\inetpub\\logs\\")
478        || path_lower.contains("w3svc");
479
480    let secureboot_log_path_hint = path_lower.contains("securebootcertificateupdate");
481
482    let intune_macos_path_hint =
483        path_lower.contains("intunemdmdaemon") || path_lower.contains("/logs/microsoft/intune/");
484
485    let mut ccm_count = 0;
486    let mut cbs_count = 0;
487    let mut dism_count = 0;
488    let mut reporting_events_count = 0;
489    let mut simple_count = 0;
490    let mut panther_count = 0;
491    let mut msi_count = 0u32;
492    let mut psadt_legacy_count = 0u32;
493    let mut intune_macos_count = 0u32;
494    let mut dhcp_count = 0u32;
495    let mut iis_w3c_count = 0u32;
496    let mut burn_count = 0u32;
497    let mut patchmypc_detection_count = 0u32;
498    let mut secureboot_log_count = 0u32;
499    let mut dns_debug_count = 0u32;
500    let mut cmtlog_count = 0u32;
501    let mut timestamp_count = 0;
502    let mut has_day_first = false;
503
504    for line in &sample_lines {
505        if line.contains("<![LOG[") && line.contains("]LOG]!>") {
506            ccm_count += 1;
507            if cmtlog::matches_cmtlog_record(line) {
508                cmtlog_count += 1;
509            }
510        } else if line.contains(" type=\"") && line.contains("component=\"") {
511            // Fallback CCM detection from the binary's ` type="` check
512            ccm_count += 1;
513            if cmtlog::matches_cmtlog_record(line) {
514                cmtlog_count += 1;
515            }
516        } else if line.contains("$$<") {
517            simple_count += 1;
518        } else if reporting_events::matches_reporting_events_record(line.trim()) {
519            reporting_events_count += 1;
520        } else if dism::matches_dism_record(line.trim()) {
521            dism_count += 1;
522            timestamp_count += 1;
523        } else if cbs::matches_cbs_record(line.trim()) {
524            cbs_count += 1;
525            timestamp_count += 1;
526        } else if panther::matches_panther_record(line.trim()) {
527            panther_count += 1;
528        } else if patchmypc_detection::matches_patchmypc_detection_record(line.trim()) {
529            patchmypc_detection_count += 1;
530            timestamp_count += 1;
531        } else if burn::matches_burn_record(line.trim()) {
532            burn_count += 1;
533            timestamp_count += 1;
534        } else if secureboot_log::matches_secureboot_log_record(line.trim()) {
535            secureboot_log_count += 1;
536            timestamp_count += 1;
537        } else if dhcp::matches_dhcp_record(line.trim()) {
538            dhcp_count += 1;
539        } else if dns_debug::matches_dns_debug_record(line.trim()) {
540            dns_debug_count += 1;
541            timestamp_count += 1;
542        } else if iis_w3c::matches_iis_w3c_record(line.trim()) {
543            iis_w3c_count += 1;
544            timestamp_count += 1;
545        } else if intune_macos::matches_intune_macos(line.trim()) {
546            intune_macos_count += 1;
547            timestamp_count += 1;
548        } else {
549            msi_count += msi::matches_msi_content(line.trim());
550            psadt_legacy_count += psadt::matches_psadt_legacy_content(line.trim());
551        }
552        if timestamped::matches_any_timestamp(line.trim()) {
553            timestamp_count += 1;
554            // Check for EU-style dates (first field > 12 → must be day)
555            if let Some(first_field) = timestamped::slash_date_first_field(line.trim()) {
556                if first_field > 12 {
557                    has_day_first = true;
558                }
559            }
560        }
561    }
562
563    if cmtlog_count >= 1 && ccm_count > 0 {
564        // CmtLog markers found alongside CCM lines — prefer CmtLog with Heuristic provenance.
565        ResolvedParser::cmtlog_heuristic()
566    } else if (secureboot_log_path_hint && secureboot_log_count >= 1) || secureboot_log_count >= 2 {
567        ResolvedParser::secureboot_log()
568    } else if ime_path_hint && ccm_count > 0 {
569        ResolvedParser::ime()
570    } else if ccm_count > 0 && ccm_count >= simple_count {
571        ResolvedParser::ccm()
572    } else if simple_count > 0 {
573        ResolvedParser::simple()
574    } else if reporting_events_path_hint && reporting_events_count >= 1 {
575        ResolvedParser::reporting_events()
576    } else if cbs_path_hint && cbs_count >= 1 {
577        ResolvedParser::cbs()
578    } else if dism_path_hint && dism_count >= 1 {
579        ResolvedParser::dism()
580    } else if panther_path_hint && panther_count >= 1 {
581        ResolvedParser::panther()
582    } else if reporting_events_count >= 2 {
583        ResolvedParser::reporting_events()
584    } else if dism_count >= 2 {
585        ResolvedParser::dism()
586    } else if patchmypc_detection_count >= 2 {
587        ResolvedParser::patchmypc_detection()
588    } else if burn_count >= 2 {
589        ResolvedParser::burn()
590    } else if (iis_w3c_path_hint && iis_w3c_count >= 1) || iis_w3c_count >= 3 {
591        ResolvedParser::iis_w3c()
592    } else if (dhcp_path_hint && dhcp_count >= 1) || dhcp_count >= 3 {
593        ResolvedParser::dhcp()
594    } else if (dns_debug_path_hint && dns_debug_count >= 1) || dns_debug_count >= 2 {
595        let dns_date_order = if has_day_first {
596            DateOrder::DayFirst
597        } else {
598            DateOrder::MonthFirst
599        };
600        ResolvedParser::dns_debug(dns_date_order)
601    } else if (intune_macos_path_hint && intune_macos_count >= 1) || intune_macos_count >= 2 {
602        ResolvedParser::intune_macos()
603    } else if msi_count >= 2 {
604        ResolvedParser::msi()
605    } else if psadt_legacy_count >= 2 {
606        ResolvedParser::psadt_legacy()
607    } else if timestamp_count >= 2 {
608        // Require at least 2 timestamp matches to avoid false positives
609        ResolvedParser::generic_timestamped(if has_day_first {
610            DateOrder::DayFirst
611        } else {
612            DateOrder::MonthFirst
613        })
614    } else {
615        ResolvedParser::plain_text()
616    }
617}
618
619#[cfg(test)]
620mod tests {
621    use super::*;
622
623    #[test]
624    fn test_detect_ccm() {
625        let content = r#"<![LOG[Test message]LOG]!><time="08:00:00.000+000" date="01-01-2024" component="Test" context="" type="1" thread="100" file="">
626<![LOG[Another message]LOG]!><time="08:00:01.000+000" date="01-01-2024" component="Test" context="" type="1" thread="100" file="">"#;
627        let detected = detect_parser("sample.log", content);
628        assert_eq!(detected.parser, ParserKind::Ccm);
629        assert_eq!(detected.compatibility_format(), LogFormat::Ccm);
630        assert_eq!(detected.provenance, ParserProvenance::Dedicated);
631        assert_eq!(detected.specialization, None);
632    }
633
634    #[test]
635    fn test_detect_ime_family_from_known_path_hint() {
636        let content = r#"<![LOG[Client Health evaluation starts.]LOG]!><time="23:00:10.6893636" date="11-12-2025" component="ClientHealth" context="" type="1" thread="1" file="">
637<![LOG[OnStart, public cloud env.]LOG]!><time="23:00:11.4573058" date="11-12-2025" component="ClientHealth" context="" type="1" thread="1" file="">"#;
638
639        let detected = detect_parser(
640            "C:/ProgramData/Microsoft/IntuneManagementExtension/Logs/ClientHealth.log",
641            content,
642        );
643        let info = detected.to_info();
644
645        assert_eq!(detected.parser, ParserKind::Ccm);
646        assert_eq!(detected.implementation, ParserImplementation::Ccm);
647        assert_eq!(detected.record_framing, RecordFraming::LogicalRecord);
648        assert_eq!(detected.specialization, Some(ParserSpecialization::Ime));
649        assert_eq!(info.specialization, Some(ParserSpecialization::Ime));
650    }
651
652    #[test]
653    fn test_detect_known_ime_path_requires_ccm_content_before_specializing() {
654        let content = "2026-03-12 11:16:37.309 ClientHealth check starts\n2026-03-12 11:16:38.000 ClientHealth check ends";
655
656        let detected = detect_parser(
657            "C:/ProgramData/Microsoft/IntuneManagementExtension/Logs/ClientHealth.log",
658            content,
659        );
660
661        assert_eq!(detected.parser, ParserKind::Timestamped);
662        assert_eq!(
663            detected.implementation,
664            ParserImplementation::GenericTimestamped
665        );
666        assert_eq!(detected.specialization, None);
667    }
668
669    #[test]
670    fn test_detect_simple() {
671        let content = r#"Message one $$<Comp1><01-01-2024 08:00:00.000+000><thread=100>
672Message two $$<Comp2><01-01-2024 08:00:01.000+000><thread=200>"#;
673        let detected = detect_parser("sample.log", content);
674        assert_eq!(detected.parser, ParserKind::Simple);
675        assert_eq!(detected.compatibility_format(), LogFormat::Simple);
676    }
677
678    #[test]
679    fn test_detect_iis_w3c_from_header_signature() {
680        let content = r#"#Software: Microsoft Internet Information Services 10.0
681#Version: 1.0
682#Date: 2026-03-29 18:48:23
683#Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) sc-status sc-substatus sc-win32-status time-taken
6842026-03-29 18:48:23 10.0.0.5 GET /default.htm - 443 - 203.0.113.10 Mozilla/5.0 200 0 0 12"#;
685        let detected = detect_parser("C:/temp/u_ex260329.log", content);
686
687        assert_eq!(detected.parser, ParserKind::IisW3c);
688        assert_eq!(detected.implementation, ParserImplementation::IisW3c);
689        assert_eq!(detected.compatibility_format(), LogFormat::Timestamped);
690        assert_eq!(detected.provenance, ParserProvenance::Dedicated);
691    }
692
693    #[test]
694    fn test_detect_plain() {
695        let content = "Just some plain text\nAnother line\nNothing special here";
696        let detected = detect_parser("sample.log", content);
697        assert_eq!(detected.parser, ParserKind::Plain);
698        assert_eq!(detected.compatibility_format(), LogFormat::Plain);
699        assert_eq!(detected.provenance, ParserProvenance::Fallback);
700    }
701
702    #[test]
703    fn test_detect_timestamped_iso() {
704        let content = "2024-01-15T08:00:00.000Z Starting application\n\
705                        2024-01-15T08:00:01.000Z Loading config\n\
706                        2024-01-15T08:00:02.000Z Ready";
707        let detected = detect_parser("sample.log", content);
708        assert_eq!(detected.parser, ParserKind::Timestamped);
709        assert_eq!(detected.compatibility_format(), LogFormat::Timestamped);
710        assert_eq!(detected.parse_quality, ParseQuality::SemiStructured);
711    }
712
713    #[test]
714    fn test_detect_timestamped_us_date() {
715        let content = "01/15/2024 08:00:00 Starting application\n\
716                        01/15/2024 08:00:01 Loading config";
717        let detected = detect_parser("sample.log", content);
718        assert_eq!(detected.compatibility_format(), LogFormat::Timestamped);
719        assert_eq!(detected.date_order, DateOrder::MonthFirst);
720    }
721
722    #[test]
723    fn test_detect_timestamped_eu_date() {
724        let content = "25/01/2024 08:00:00 Starting application\n\
725                        15/01/2024 08:00:01 Loading config";
726        let detected = detect_parser("sample.log", content);
727        assert_eq!(detected.compatibility_format(), LogFormat::Timestamped);
728        assert_eq!(detected.date_order, DateOrder::DayFirst);
729    }
730
731    #[test]
732    fn test_single_timestamp_line_stays_plain() {
733        // Only 1 timestamped line should not trigger Timestamped format
734        let content = "2024-01-15T08:00:00Z Starting\nRandom text\nMore text";
735        let detected = detect_parser("sample.log", content);
736        assert_eq!(detected.compatibility_format(), LogFormat::Plain);
737    }
738
739    #[test]
740    fn test_detect_panther_from_path_and_content() {
741        let content = "2024-01-15 08:00:00, Info SP Setup started\n\
742                        2024-01-15 08:00:01, Warning MIG Retry required";
743
744        let detected = detect_parser("C:/Windows/Panther/setupact.log", content);
745        let info = detected.to_info();
746
747        assert_eq!(detected.parser, ParserKind::Panther);
748        assert_eq!(detected.provenance, ParserProvenance::Dedicated);
749        assert_eq!(detected.record_framing, RecordFraming::LogicalRecord);
750        assert_eq!(info.parser, ParserKind::Panther);
751        assert_eq!(info.date_order, None);
752    }
753
754    #[test]
755    fn test_detect_cbs_from_path_and_content() {
756        let content = "2024-01-15 08:00:00, Info                  CBS    Exec: Processing package\n\
757                        2024-01-15 08:00:01, Warning               CSI    [SR] Repair retry scheduled";
758
759        let detected = detect_parser("C:/Windows/Logs/CBS/CBS.log", content);
760        let info = detected.to_info();
761
762        assert_eq!(detected.parser, ParserKind::Cbs);
763        assert_eq!(
764            detected.implementation,
765            ParserImplementation::GenericTimestamped
766        );
767        assert_eq!(detected.provenance, ParserProvenance::Dedicated);
768        assert_eq!(detected.record_framing, RecordFraming::LogicalRecord);
769        assert_eq!(detected.compatibility_format(), LogFormat::Timestamped);
770        assert_eq!(info.date_order, None);
771    }
772
773    #[test]
774    fn test_detect_reporting_events_from_path_and_content() {
775        let content = "{11111111-1111-1111-1111-111111111111}\t2024-01-15 08:00:00:123\t1\tSoftware Update\t1\t{22222222-2222-2222-2222-222222222222}\t0x00000000\tWindows Update Agent\tSuccess\tInstallation\tInstallation Successful: KB5034123\n\
776                        {33333333-3333-3333-3333-333333333333}\t2024-01-15 08:05:00:456\t2\tSoftware Update\t3\t{44444444-4444-4444-4444-444444444444}\t0x80240022\tWindows Update Agent\tFailure\tInstallation\tInstallation failed for KB5034441";
777
778        let detected = detect_parser(
779            "C:/Windows/SoftwareDistribution/ReportingEvents.log",
780            content,
781        );
782        let info = detected.to_info();
783
784        assert_eq!(detected.parser, ParserKind::ReportingEvents);
785        assert_eq!(
786            detected.implementation,
787            ParserImplementation::ReportingEvents
788        );
789        assert_eq!(detected.provenance, ParserProvenance::Dedicated);
790        assert_eq!(detected.parse_quality, ParseQuality::Structured);
791        assert_eq!(detected.record_framing, RecordFraming::PhysicalLine);
792        assert_eq!(detected.compatibility_format(), LogFormat::Timestamped);
793        assert_eq!(info.parser, ParserKind::ReportingEvents);
794        assert_eq!(info.implementation, ParserImplementation::ReportingEvents);
795        assert_eq!(info.date_order, None);
796    }
797
798    #[test]
799    fn test_detect_reporting_events_from_content_without_path_hint() {
800        let content = "{11111111-1111-1111-1111-111111111111}\t2024-01-15 08:00:00:123\t1\tSoftware Update\t1\t{22222222-2222-2222-2222-222222222222}\t0x00000000\tWindows Update Agent\tSuccess\tInstallation\tInstallation Successful: KB5034123\n\
801                        {33333333-3333-3333-3333-333333333333}\t2024-01-15 08:05:00:456\t2\tSoftware Update\t3\t{44444444-4444-4444-4444-444444444444}\t0x80240022\tWindows Update Agent\tFailure\tInstallation\tInstallation failed for KB5034441";
802
803        let detected = detect_parser("C:/Temp/update-history.txt", content);
804
805        assert_eq!(detected.parser, ParserKind::ReportingEvents);
806        assert_eq!(
807            detected.implementation,
808            ParserImplementation::ReportingEvents
809        );
810        assert_eq!(detected.record_framing, RecordFraming::PhysicalLine);
811    }
812
813    #[test]
814    fn test_detect_dism_from_path_and_content() {
815        let content = "2024-01-15 08:00:00, Info                  DISM   DISM Provider Store: PID=100 TID=200 loaded provider\n\
816                        2024-01-15 08:00:01, Error                 DISM   DISM Package Manager: Failed finalizing changes";
817
818        let detected = detect_parser("C:/Windows/Logs/DISM/dism.log", content);
819        let info = detected.to_info();
820
821        assert_eq!(detected.parser, ParserKind::Dism);
822        assert_eq!(
823            detected.implementation,
824            ParserImplementation::GenericTimestamped
825        );
826        assert_eq!(detected.provenance, ParserProvenance::Dedicated);
827        assert_eq!(detected.record_framing, RecordFraming::LogicalRecord);
828        assert_eq!(detected.compatibility_format(), LogFormat::Timestamped);
829        assert_eq!(info.date_order, None);
830    }
831
832    #[test]
833    fn test_detect_panther_still_wins_for_panther_paths() {
834        let content = "2024-01-15 08:00:00, Info SP Setup started\n\
835                        2024-01-15 08:00:01, Warning MIG Retry required";
836
837        let detected = detect_parser("C:/Windows/Panther/setupact.log", content);
838
839        assert_eq!(detected.parser, ParserKind::Panther);
840    }
841
842    #[test]
843    fn test_detect_dism_from_content_without_path_hint() {
844        let content = "2024-01-15 08:00:00, Info                  DISM   DISM Provider Store: PID=100 TID=200 loaded provider\n\
845                        2024-01-15 08:00:01, Warning               DISM   DISM Package Manager: Retry required";
846
847        let detected = detect_parser("C:/Temp/servicing.txt", content);
848
849        assert_eq!(detected.parser, ParserKind::Dism);
850        assert_eq!(detected.compatibility_format(), LogFormat::Timestamped);
851    }
852
853    #[test]
854    fn test_selection_info_can_distinguish_dedicated_parser_from_generic_fallback() {
855        let selection = ResolvedParser::new(
856            ParserKind::Panther,
857            ParserImplementation::GenericTimestamped,
858            ParserProvenance::Dedicated,
859            ParseQuality::SemiStructured,
860            RecordFraming::LogicalRecord,
861            DateOrder::MonthFirst,
862            None,
863        );
864
865        let info = selection.to_info();
866
867        assert_eq!(info.parser, ParserKind::Panther);
868        assert_eq!(
869            info.implementation,
870            ParserImplementation::GenericTimestamped
871        );
872        assert_eq!(info.provenance, ParserProvenance::Dedicated);
873        assert_eq!(info.parse_quality, ParseQuality::SemiStructured);
874        assert_eq!(info.record_framing, RecordFraming::LogicalRecord);
875        assert_eq!(info.date_order, None);
876        assert_eq!(info.specialization, None);
877        assert_eq!(selection.compatibility_format(), LogFormat::Timestamped);
878    }
879
880    #[test]
881    fn test_detect_dns_debug_from_path_and_content() {
882        // Use the full 29-line header + one PACKET line
883        let content = "DNS Server log file creation at 4/11/2026 3:29:17 PM\n\
884            \n\
885            Message logging key (for packets):\n\
886            \tField #  Information         Values\n\
887            \t-------  -----------         ------\n\
888            \t   1     Date\n\
889            \t   2     Time\n\
890            \t   3     Thread ID\n\
891            \t   4     Context\n\
892            \t   5     Internal packet identifier\n\
893            \t   6     UDP/TCP indicator\n\
894            \t   7     Send/Receive indicator\n\
895            \t   8     Remote IP\n\
896            \t   9     Xid (hex)\n\
897            \t  10     Query/Response      R = Response\n\
898            \t                             blank = Query\n\
899            \t  11     Opcode              Q = Standard Query\n\
900            \t                             N = Notify\n\
901            \t                             U = Update\n\
902            \t                             ? = Unknown\n\
903            \t  12     [ Flags (hex)\n\
904            \t  13     Flags (char codes)  A = Authoritative Answer\n\
905            \t                             T = Truncated Response\n\
906            \t                             D = Recursion Desired\n\
907            \t                             R = Recursion Available\n\
908            \t  14     ResponseCode ]\n\
909            \t  15     Question Type\n\
910            \t  16     Question Name\n\
911            \n\
912            4/11/2026 3:29:17 PM 0294 PACKET  000002DAEC36D650 UDP Rcv 127.0.0.1       d07e   Q [0001   D   NOERROR] SOA    (4)home(4)gell(3)one(0)\n";
913
914        let detected = detect_parser("C:/Logs/DNSServer/DNSServer_debug.log", content);
915        assert_eq!(detected.parser, ParserKind::DnsDebug);
916        assert_eq!(detected.implementation, ParserImplementation::DnsDebug);
917        assert_eq!(detected.record_framing, RecordFraming::LogicalRecord);
918        assert_eq!(detected.parse_quality, ParseQuality::Structured);
919    }
920
921    #[test]
922    fn test_generic_timestamped_with_dns_in_path_does_not_false_positive() {
923        let content = "2026-04-11 15:29:17 DNS resolution started\n\
924                        2026-04-11 15:29:18 DNS resolution complete";
925        let detected = detect_parser("C:/logs/dns-resolver/app.log", content);
926        assert_eq!(detected.parser, ParserKind::Timestamped);
927    }
928}