Skip to main content

supercov_engine/
coverage_query.rs

1//! Language-neutral coverage query operators.
2//!
3//! Querying is deliberately separated from the CLI and storage container.
4//! This module accepts the frozen analyzed view and owns structural query
5//! semantics shared by every language frontend.
6
7use std::collections::{BTreeMap, BTreeSet, HashMap};
8
9use serde::{Deserialize, Serialize};
10
11use crate::interned::Id;
12use crate::{
13    agent_json::pagination,
14    coverage_analysis::{CoverageSummary, is_independence_pair},
15    coverage_index::{
16        CoverageDimension, CoverageIndex, CoverageIndexError, CoverageViewId, IndexedCoverageModel,
17        IndexedDecisionGap, IndexedDimensionCoverage, IndexedFileGap, IndexedGapDimensions,
18        IndexedHitMetadata, IndexedMeasurement, IndexedOutcomeCounts, IndexedScopeEntry,
19        IndexedSourceScope, IndexedSummaryConfidence, IndexedTestSummary,
20    },
21    coverage_report::{
22        CoverageConfidence, CoverageView, DecisionMeta, ReportError, SourceLine, TestAttempt,
23        TestProvenance, TransportStats, coverage_summary_for_tests,
24    },
25};
26use supercov_contracts::AgentPagination;
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
29#[serde(rename_all = "lowercase")]
30pub enum MinimizeMetric {
31    All,
32    Lines,
33    Statements,
34    Functions,
35    Branches,
36    Mcdc,
37}
38
39#[derive(Debug, Clone, PartialEq, Serialize)]
40#[serde(rename_all = "camelCase")]
41pub struct MinimumTestSetResult {
42    pub optimal: bool,
43    #[serde(serialize_with = "crate::coverage_analysis::serialize_javascript_number")]
44    pub target: f64,
45    pub metric: MinimizeMetric,
46    pub selected: Vec<String>,
47    pub expanded: Vec<String>,
48    pub summary: CoverageSummary,
49    pub explored_states: usize,
50}
51
52#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
53pub struct CoverageMinimizedTest {
54    pub id: String,
55    pub name: String,
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub file: Option<String>,
58    pub runner: String,
59    pub kind: String,
60}
61
62#[derive(Debug, Clone, PartialEq, Serialize)]
63#[serde(rename_all = "camelCase")]
64pub struct CoverageMinimizeData {
65    pub run: String,
66    pub filters: CoverageQueryFilters,
67    pub optimal: bool,
68    #[serde(serialize_with = "crate::coverage_analysis::serialize_javascript_number")]
69    pub target: f64,
70    pub metric: MinimizeMetric,
71    pub selected: Vec<String>,
72    pub expanded: Vec<String>,
73    pub summary: CoverageSummary,
74    pub explored_states: usize,
75    pub selected_count: usize,
76    pub total_candidate_tests: usize,
77    pub tests: Vec<CoverageMinimizedTest>,
78}
79
80#[derive(Debug, Clone, Copy)]
81pub struct CoverageMinimizeQueryOptions<'a> {
82    pub run: &'a str,
83    pub view_id: CoverageViewId,
84    pub kind: Option<&'a str>,
85    pub runner: Option<&'a str>,
86    pub target: f64,
87    pub metric: MinimizeMetric,
88    pub max_states: usize,
89    pub offset: usize,
90    pub limit: usize,
91}
92
93pub fn coverage_minimize_query(
94    view: &CoverageView,
95    options: CoverageMinimizeQueryOptions<'_>,
96) -> Result<(CoverageMinimizeData, AgentPagination), QueryError> {
97    if options.limit == 0 {
98        return Err(QueryError::InvalidPagination);
99    }
100    let selected_ids = if options.kind.is_none() && options.runner.is_none() {
101        None
102    } else {
103        let ids = view
104            .tests
105            .iter()
106            .filter(|test| {
107                options.kind.is_none_or(|kind| test.provenance.kind == kind)
108                    && options
109                        .runner
110                        .is_none_or(|runner| test.provenance.runner == runner)
111            })
112            .map(|test| test.id.clone())
113            .collect::<BTreeSet<_>>();
114        if ids.is_empty() {
115            return Err(QueryError::TestFilterEmpty {
116                kind: options.kind.map(str::to_owned),
117                runner: options.runner.map(str::to_owned),
118            });
119        }
120        Some(ids)
121    };
122    let mut solver_view = view.clone();
123    if let Some(selected) = &selected_ids {
124        solver_view.tests.retain(|test| selected.contains(&test.id));
125    }
126    let minimized = minimum_test_set(
127        &solver_view,
128        options.target,
129        options.metric,
130        options.max_states,
131    )?;
132    let selected_details = minimized
133        .selected
134        .iter()
135        .map(|id| {
136            let test = view
137                .tests
138                .iter()
139                .find(|test| test.id == *id)
140                .ok_or(QueryError::InvalidRecordSelection)?;
141            Ok(CoverageMinimizedTest {
142                id: id.clone(),
143                name: test.name.clone(),
144                file: test.file.clone(),
145                runner: test.provenance.runner.clone(),
146                kind: test.provenance.kind.clone(),
147            })
148        })
149        .collect::<Result<Vec<_>, QueryError>>()?;
150    let total = selected_details.len();
151    let tests = selected_details
152        .iter()
153        .skip(options.offset)
154        .take(options.limit)
155        .cloned()
156        .collect::<Vec<_>>();
157    let returned = tests.len();
158    let total_candidate_tests = solver_view
159        .tests
160        .iter()
161        .filter(|test| test.role == "test")
162        .count();
163    Ok((
164        CoverageMinimizeData {
165            run: options.run.into(),
166            filters: query_filters(options.view_id, options.kind, options.runner),
167            optimal: minimized.optimal,
168            target: minimized.target,
169            metric: minimized.metric,
170            selected: minimized.selected,
171            expanded: minimized.expanded,
172            summary: minimized.summary,
173            explored_states: minimized.explored_states,
174            selected_count: total,
175            total_candidate_tests,
176            tests,
177        },
178        pagination(options.offset, options.limit, returned, total),
179    ))
180}
181
182#[derive(Debug)]
183pub enum QueryError {
184    InvalidTarget(f64),
185    UnattributedEvidence,
186    TargetUnreachable {
187        metric: MinimizeMetric,
188        target: f64,
189        reachable: f64,
190    },
191    ComplexityLimit {
192        candidate_tests: usize,
193        obligations: usize,
194        explored_states: usize,
195        max_states: usize,
196        target: f64,
197        metric: MinimizeMetric,
198    },
199    Analysis(ReportError),
200    Index(CoverageIndexError),
201    InvalidPagination,
202    TestFilterEmpty {
203        kind: Option<String>,
204        runner: Option<String>,
205    },
206    TestNotFound(String),
207    DecisionNotFound(String),
208    SourceNotFound(String),
209    AmbiguousSelector {
210        selector: String,
211        matches: Vec<String>,
212    },
213    InvalidRecordSelection,
214    ScopeUnavailable,
215}
216
217impl From<ReportError> for QueryError {
218    fn from(value: ReportError) -> Self {
219        Self::Analysis(value)
220    }
221}
222
223impl From<CoverageIndexError> for QueryError {
224    fn from(value: CoverageIndexError) -> Self {
225        Self::Index(value)
226    }
227}
228
229#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
230#[serde(rename_all = "camelCase")]
231pub struct CoverageQueryFilters {
232    pub outcome: String,
233    pub kind: Option<String>,
234    pub runner: Option<String>,
235}
236
237#[derive(Debug, Clone, PartialEq, Serialize)]
238#[serde(rename_all = "camelCase")]
239pub struct CoverageFilesData {
240    pub run: String,
241    pub filters: CoverageQueryFilters,
242    pub metric: MinimizeMetric,
243    pub files: Vec<IndexedFileGap>,
244}
245
246#[derive(Debug, Clone, PartialEq, Serialize)]
247#[serde(rename_all = "camelCase")]
248pub struct CoverageGapsData {
249    pub run: String,
250    pub filters: CoverageQueryFilters,
251    pub metric: MinimizeMetric,
252    pub gaps: Vec<IndexedFileGap>,
253}
254
255#[derive(Debug, Clone, PartialEq, Serialize)]
256#[serde(rename_all = "camelCase")]
257pub struct CoverageKindsData {
258    pub run: String,
259    pub filters: CoverageQueryFilters,
260    pub kinds: Vec<IndexedDimensionCoverage>,
261}
262
263#[derive(Debug, Clone, PartialEq, Serialize)]
264#[serde(rename_all = "camelCase")]
265pub struct CoverageRunnersData {
266    pub run: String,
267    pub filters: CoverageQueryFilters,
268    pub runners: Vec<IndexedDimensionCoverage>,
269}
270
271/// Tests by how completely their coverage is their own.
272#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
273#[serde(rename_all = "camelCase")]
274pub struct TestAttributionCounts {
275    /// Credited with exactly what they reached.
276    pub exact: usize,
277    /// Credited with exactly what they reached, and with server work no
278    /// request named that ran while each was the only test running.
279    pub by_time: usize,
280    /// Credited with some of what they reached, and no way to say how much.
281    pub partial: usize,
282    /// Credited with none of it; what they reached is in the run's totals.
283    pub run_wide: usize,
284}
285
286impl TestAttributionCounts {
287    pub fn total(&self) -> usize {
288        self.exact + self.by_time + self.partial + self.run_wide
289    }
290}
291
292#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
293#[serde(rename_all = "camelCase")]
294pub struct CoverageDiagnostic {
295    pub code: String,
296    pub severity: String,
297    pub message: String,
298}
299
300#[derive(Debug, Clone, PartialEq, Serialize)]
301#[serde(rename_all = "camelCase")]
302pub struct CoverageSummaryData {
303    pub test_kind_sources: BTreeMap<String, usize>,
304    #[serde(skip_serializing_if = "Option::is_none")]
305    pub assertion_coverage: Option<serde_json::Value>,
306    pub run: String,
307    #[serde(default)]
308    pub command: Vec<String>,
309    /// For a merge made with `--allow-changed`: what differed between the
310    /// runs it was merged from.
311    #[serde(default, skip_serializing_if = "Vec::is_empty")]
312    pub merged_across: Vec<String>,
313    /// The runs the same command published beside this one: its servers'
314    /// runs, or the browser run a server's run took its tests from.
315    #[serde(default, skip_serializing_if = "Vec::is_empty")]
316    pub companions: Vec<crate::run_store::CompanionRun>,
317    #[serde(default, skip_serializing_if = "Vec::is_empty")]
318    pub hints: Vec<String>,
319    #[serde(skip_serializing_if = "Option::is_none")]
320    pub workspace: Option<String>,
321    pub filters: CoverageQueryFilters,
322    pub model: IndexedCoverageModel,
323    pub generated_at: String,
324    pub valid: bool,
325    #[serde(skip_serializing_if = "Option::is_none")]
326    pub test_exit_code: Option<i32>,
327    pub stale: bool,
328    pub stale_reasons: Vec<String>,
329    pub structurally_complete: bool,
330    pub complete: bool,
331    pub coverage: CoverageSummary,
332    pub measurement: IndexedMeasurement,
333    pub coverage_by_kind: Vec<IndexedDimensionCoverage>,
334    /// What the tests cover between them. `coverage` less this ran while no
335    /// test was running, which is why a run with one kind of test can show a
336    /// total above that kind's own.
337    #[serde(skip_serializing_if = "Option::is_none")]
338    pub coverage_by_tests: Option<CoverageSummary>,
339    #[serde(skip_serializing_if = "Option::is_none")]
340    pub e2e_gap_context: Option<CoverageKindGapContext>,
341    pub coverage_by_runner: Vec<IndexedDimensionCoverage>,
342    pub attribution: crate::coverage_index::IndexedAttribution,
343    /// How completely each test's coverage could be credited to it.
344    ///
345    /// Counted here rather than stored, because the per-test answer is already
346    /// in the run and a total is a read of it. A run written before the field
347    /// existed reports every test as exact, which is what its records say.
348    pub test_attribution: TestAttributionCounts,
349    #[serde(skip_serializing_if = "Option::is_none")]
350    pub transport: Option<TransportStats>,
351    pub diagnostics: Vec<CoverageDiagnostic>,
352    #[serde(skip_serializing_if = "Option::is_none")]
353    pub confidence: Option<IndexedSummaryConfidence>,
354    pub files_with_gaps: usize,
355    pub files_with_coverage_gaps: usize,
356    pub files_with_measurement_limitations: usize,
357    pub tests: usize,
358    pub setups: usize,
359    pub test_outcomes: IndexedOutcomeCounts,
360    #[serde(skip_serializing_if = "Option::is_none")]
361    pub source_scope: Option<IndexedSourceScope>,
362    /// Where the files discovery could not classify are, and the roots that
363    /// would settle them.
364    #[serde(skip_serializing_if = "Option::is_none")]
365    pub unclassified_source: Option<UnclassifiedSource>,
366}
367
368/// One row of the scope overview: the files of one place that share a status
369/// and the reason for it.
370#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
371#[serde(rename_all = "camelCase")]
372pub struct ScopeGroup {
373    pub status: String,
374    pub directory: String,
375    pub reason: String,
376    pub files: usize,
377}
378
379#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
380#[serde(rename_all = "camelCase")]
381pub struct ScopeDirectory {
382    pub directory: String,
383    pub files: usize,
384}
385
386/// The unclassified files of a run by where they are, and the source roots
387/// to name if they are the project's own code: the roots discovery found
388/// plus each of those places.
389#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
390#[serde(rename_all = "camelCase")]
391pub struct UnclassifiedSource {
392    pub directories: Vec<ScopeDirectory>,
393    pub roots: Vec<String>,
394}
395
396/// Where a reader would look for a file: the directory at the top of the
397/// project, or of its package in a workspace. A file that sits at that top
398/// is in `.`.
399fn scope_directory(entry: &IndexedScopeEntry) -> String {
400    let package = entry.package_root.as_deref().filter(|root| *root != ".");
401    let local = package
402        .and_then(|root| entry.file.strip_prefix(root))
403        .and_then(|rest| rest.strip_prefix('/'))
404        .unwrap_or(&entry.file);
405    match (package, local.split_once('/')) {
406        (Some(package), Some((first, _))) => format!("{package}/{first}"),
407        (Some(package), None) => package.to_owned(),
408        (None, Some((first, _))) => first.to_owned(),
409        (None, None) => ".".to_owned(),
410    }
411}
412
413fn scope_status_rank(status: &str) -> u8 {
414    match status {
415        "ambiguous" => 0,
416        "included" => 1,
417        "excluded" => 2,
418        _ => 3,
419    }
420}
421
422/// Every scope entry, grouped. 845 entries took five pages to read and an
423/// `awk` to see that 79 of 89 unclassified files were one directory.
424pub fn scope_groups(entries: &[IndexedScopeEntry]) -> Vec<ScopeGroup> {
425    let mut groups = BTreeMap::<(u8, String, String, String), usize>::new();
426    for entry in entries {
427        *groups
428            .entry((
429                scope_status_rank(&entry.status),
430                entry.status.clone(),
431                scope_directory(entry),
432                entry.reason.clone(),
433            ))
434            .or_default() += 1;
435    }
436    let mut groups = groups
437        .into_iter()
438        .map(|((rank, status, directory, reason), files)| {
439            (
440                rank,
441                ScopeGroup {
442                    status,
443                    directory,
444                    reason,
445                    files,
446                },
447            )
448        })
449        .collect::<Vec<_>>();
450    groups.sort_by(|(left_rank, left), (right_rank, right)| {
451        left_rank
452            .cmp(right_rank)
453            .then_with(|| right.files.cmp(&left.files))
454            .then_with(|| left.directory.cmp(&right.directory))
455            .then_with(|| left.reason.cmp(&right.reason))
456    });
457    groups.into_iter().map(|(_, group)| group).collect()
458}
459
460pub fn unclassified_source(
461    roots: &[String],
462    entries: &[IndexedScopeEntry],
463) -> Option<UnclassifiedSource> {
464    let mut places = BTreeMap::<String, usize>::new();
465    let mut named = Vec::<(String, usize)>::new();
466    for entry in entries.iter().filter(|entry| entry.status == "ambiguous") {
467        let directory = scope_directory(entry);
468        *places.entry(directory.clone()).or_default() += 1;
469        // A file at the top is named itself; naming `.` would take the tests
470        // and the configuration with it.
471        let root = if entry.file.contains('/') && directory != "." {
472            directory
473        } else {
474            entry.file.clone()
475        };
476        match named.iter_mut().find(|(name, _)| *name == root) {
477            Some((_, files)) => *files += 1,
478            None => named.push((root, 1)),
479        }
480    }
481    if places.is_empty() {
482        return None;
483    }
484    let mut directories = places
485        .into_iter()
486        .map(|(directory, files)| ScopeDirectory { directory, files })
487        .collect::<Vec<_>>();
488    directories.sort_by(|left, right| {
489        right
490            .files
491            .cmp(&left.files)
492            .then_with(|| left.directory.cmp(&right.directory))
493    });
494    named.sort_by(|left, right| right.1.cmp(&left.1).then_with(|| left.0.cmp(&right.0)));
495    let mut all = roots.to_vec();
496    for (root, _) in named {
497        if !all.contains(&root) {
498            all.push(root);
499        }
500    }
501    Some(UnclassifiedSource {
502        directories,
503        roots: all,
504    })
505}
506
507#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
508#[serde(rename_all = "camelCase")]
509pub struct CoverageKindGapContext {
510    pub kind: String,
511    pub other_kinds: Vec<String>,
512    pub covered_elsewhere: IndexedGapDimensions,
513    pub uncovered_everywhere: IndexedGapDimensions,
514}
515
516#[derive(Debug, Clone)]
517pub struct CoverageSummaryQueryOptions<'a> {
518    pub run: &'a str,
519    pub view: CoverageViewId,
520    pub kind: Option<&'a str>,
521    pub runner: Option<&'a str>,
522    pub valid: bool,
523    pub test_exit_code: Option<i32>,
524    pub stale: bool,
525    pub stale_reasons: Vec<String>,
526}
527
528#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
529pub struct ScopeCounts {
530    pub included: usize,
531    pub excluded: usize,
532    pub ambiguous: usize,
533}
534
535#[derive(Debug, Clone, PartialEq, Serialize)]
536#[serde(rename_all = "camelCase")]
537pub struct CoverageScopeData {
538    pub run: String,
539    pub filters: CoverageQueryFilters,
540    pub kind: String,
541    pub language: String,
542    pub model: String,
543    #[serde(skip_serializing_if = "Option::is_none")]
544    pub mode: Option<String>,
545    pub roots: Vec<String>,
546    #[serde(skip_serializing_if = "Option::is_none")]
547    pub unit: Option<String>,
548    #[serde(skip_serializing_if = "Option::is_none")]
549    pub measurement_complete: Option<bool>,
550    pub counts: ScopeCounts,
551    pub measurement: IndexedMeasurement,
552    /// Every entry of the scope, grouped; `entries` is one page of them.
553    pub groups: Vec<ScopeGroup>,
554    #[serde(skip_serializing_if = "Option::is_none")]
555    pub unclassified_source: Option<UnclassifiedSource>,
556    pub entries: Vec<IndexedScopeEntry>,
557}
558
559#[derive(Debug, Clone, Copy)]
560pub struct CoverageScopeQueryOptions<'a> {
561    pub run: &'a str,
562    pub view: CoverageViewId,
563    pub kind: Option<&'a str>,
564    pub runner: Option<&'a str>,
565    pub offset: usize,
566    pub limit: usize,
567}
568
569#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
570pub struct CoverageLocation {
571    pub file: String,
572    pub line: usize,
573}
574
575#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
576pub struct CoverageCoveringTest {
577    pub id: String,
578    pub name: String,
579    pub provenance: TestProvenance,
580}
581
582#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
583#[serde(rename_all = "camelCase")]
584pub struct CoverageCoveringPhase {
585    pub id: String,
586    pub kind: String,
587    pub operation: String,
588    #[serde(skip_serializing_if = "Option::is_none")]
589    pub source: Option<String>,
590    pub test: String,
591    #[serde(skip_serializing_if = "Option::is_none")]
592    pub status: Option<String>,
593    #[serde(skip_serializing_if = "Option::is_none")]
594    pub caused_by_phase_id: Option<String>,
595}
596
597#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
598#[serde(rename_all = "camelCase")]
599pub struct CoverageAnchor {
600    pub kind: String,
601    pub id: String,
602    pub column: usize,
603    pub covered: bool,
604    #[serde(skip_serializing_if = "Option::is_none")]
605    pub source: Option<String>,
606    #[serde(skip_serializing_if = "Option::is_none")]
607    pub missing: Option<String>,
608    pub covering_tests: usize,
609    #[serde(skip_serializing_if = "Option::is_none")]
610    pub covered_conditions: Option<usize>,
611    #[serde(skip_serializing_if = "Option::is_none")]
612    pub conditions: Option<usize>,
613}
614
615#[derive(Debug, Clone, PartialEq, Serialize)]
616#[serde(rename_all = "camelCase")]
617pub struct CoverageCoversLineData {
618    pub run: String,
619    pub filters: CoverageQueryFilters,
620    pub location: CoverageLocation,
621    #[serde(skip_serializing_if = "Option::is_none")]
622    pub source: Option<String>,
623    #[serde(skip_serializing_if = "Option::is_none")]
624    pub source_origin: Option<String>,
625    pub covered: bool,
626    pub confidence: CoverageConfidence,
627    pub total_tests: usize,
628    pub total_phases: usize,
629    pub total_anchored: usize,
630    pub covered_anchored: usize,
631    pub total_limitations: usize,
632    pub total_remaining: usize,
633    pub tests: Vec<CoverageCoveringTest>,
634    pub phases: Vec<CoverageCoveringPhase>,
635    pub anchored: Vec<CoverageAnchor>,
636    pub limitations: Vec<CoverageFileLimitation>,
637    pub remaining: Vec<CoverageFileObligation>,
638    /// The run's saved assertion assessment for this place, attached by the
639    /// CLI when the run has one.
640    #[serde(skip_serializing_if = "Option::is_none")]
641    pub assertions: Option<serde_json::Value>,
642}
643
644#[derive(Debug, Clone, PartialEq, Serialize)]
645#[serde(rename_all = "camelCase")]
646pub struct CoverageCoversAnchorsData {
647    pub run: String,
648    pub filters: CoverageQueryFilters,
649    pub location: CoverageLocation,
650    #[serde(skip_serializing_if = "Option::is_none")]
651    pub source: Option<String>,
652    #[serde(skip_serializing_if = "Option::is_none")]
653    pub source_origin: Option<String>,
654    pub line_obligation: bool,
655    pub anchored: Vec<CoverageAnchor>,
656    pub total_anchored: usize,
657    pub covered_anchored: usize,
658    pub total_limitations: usize,
659    pub limitations: Vec<CoverageFileLimitation>,
660    pub total_remaining: usize,
661    pub remaining: Vec<CoverageFileObligation>,
662    pub total_tests: usize,
663    pub tests: Vec<CoverageCoveringTest>,
664    /// The run's saved assertion assessment for this place, attached by the
665    /// CLI when the run has one.
666    #[serde(skip_serializing_if = "Option::is_none")]
667    pub assertions: Option<serde_json::Value>,
668}
669
670#[derive(Debug, Clone, PartialEq, Serialize)]
671#[serde(untagged)]
672pub enum CoverageCoversData {
673    Line(CoverageCoversLineData),
674    Anchors(CoverageCoversAnchorsData),
675}
676
677#[derive(Debug, Clone, Copy)]
678pub struct CoverageCoversQueryOptions<'a> {
679    pub run: &'a str,
680    pub view: CoverageViewId,
681    pub kind: Option<&'a str>,
682    pub runner: Option<&'a str>,
683    pub file: &'a str,
684    pub line: usize,
685    pub offset: usize,
686    pub limit: usize,
687}
688
689fn query_filters(
690    view: CoverageViewId,
691    kind: Option<&str>,
692    runner: Option<&str>,
693) -> CoverageQueryFilters {
694    CoverageQueryFilters {
695        outcome: match view {
696            CoverageViewId::All => "all",
697            CoverageViewId::Passed => "passed",
698            CoverageViewId::Failed => "failed",
699        }
700        .into(),
701        kind: kind.map(str::to_owned),
702        runner: runner.map(str::to_owned),
703    }
704}
705
706fn selected_test_ids(
707    tests: &[IndexedTestSummary],
708    kind: Option<&str>,
709    runner: Option<&str>,
710) -> Result<Option<BTreeSet<String>>, QueryError> {
711    if kind.is_none() && runner.is_none() {
712        return Ok(None);
713    }
714    let selected = tests
715        .iter()
716        .filter(|test| {
717            kind.is_none_or(|kind| test.provenance.kind == kind)
718                && runner.is_none_or(|runner| test.provenance.runner == runner)
719        })
720        .map(|test| test.id.clone())
721        .collect::<BTreeSet<_>>();
722    if selected.is_empty() {
723        return Err(QueryError::TestFilterEmpty {
724            kind: kind.map(str::to_owned),
725            runner: runner.map(str::to_owned),
726        });
727    }
728    Ok(Some(selected))
729}
730
731pub fn coverage_covers_query(
732    index: &CoverageIndex<'_>,
733    options: CoverageCoversQueryOptions<'_>,
734) -> Result<(CoverageCoversData, AgentPagination), QueryError> {
735    if options.limit == 0 {
736        return Err(QueryError::InvalidPagination);
737    }
738    let tests = index.test_summaries(options.view)?;
739    let selected = selected_test_ids(&tests, options.kind, options.runner)?;
740    let selected_includes = |id: &str| selected.as_ref().is_none_or(|ids| ids.contains(id));
741    let filters = query_filters(options.view, options.kind, options.runner);
742    let location = CoverageLocation {
743        file: options.file.into(),
744        line: options.line,
745    };
746    let metadata = index
747        .hit_metadata(options.view)?
748        .into_iter()
749        .map(|value| (value.id.clone(), value))
750        .collect::<HashMap<_, _>>();
751    let decisions = index
752        .decision_details(options.view)?
753        .into_iter()
754        .map(|value| (value.meta.id.clone(), value))
755        .collect::<HashMap<_, _>>();
756    let anchors = index.anchors(options.view, options.file, options.line)?;
757    let tests_by_id = tests
758        .iter()
759        .map(|test| (test.id.clone(), test.clone()))
760        .collect::<HashMap<_, _>>();
761    let mut anchor_test_ids = anchors
762        .iter()
763        .flat_map(|anchor| anchor.tests.iter())
764        .filter(|id| selected_includes(id))
765        .cloned()
766        .collect::<BTreeSet<_>>();
767    for anchor in anchors.iter().filter(|anchor| anchor.kind == "branch") {
768        anchor_test_ids.extend(
769            metadata
770                .values()
771                .filter(|detail| detail.parent_id.as_deref() == Some(anchor.id.as_str()))
772                .flat_map(|detail| detail.tests.iter())
773                .filter(|id| selected_includes(id))
774                .cloned(),
775        );
776    }
777    let all_anchor_tests = anchor_test_ids
778        .iter()
779        .map(|id| {
780            let test = tests_by_id.get(id);
781            CoverageCoveringTest {
782                id: id.clone(),
783                name: test.map_or_else(|| id.clone(), |test| test.name.clone()),
784                provenance: test
785                    .map_or_else(TestProvenance::default, |test| test.provenance.clone()),
786            }
787        })
788        .collect::<Vec<_>>();
789    let total_anchor_tests = all_anchor_tests.len();
790    let anchor_tests_page = all_anchor_tests
791        .iter()
792        .skip(options.offset)
793        .take(options.limit)
794        .cloned()
795        .collect::<Vec<_>>();
796    let render_anchor = |anchor: crate::coverage_index::IndexedAnchor| {
797        let branch_alternatives = if anchor.kind == "branch" {
798            metadata
799                .values()
800                .filter(|detail| {
801                    detail.obligation == "branch"
802                        && detail.parent_id.as_deref() == Some(anchor.id.as_str())
803                })
804                .collect::<Vec<_>>()
805        } else {
806            Vec::new()
807        };
808        let branch_tests = branch_alternatives
809            .iter()
810            .flat_map(|detail| detail.tests.iter())
811            .filter(|test| selected_includes(test))
812            .cloned()
813            .collect::<BTreeSet<_>>();
814        let covering_tests = if anchor.kind == "branch" {
815            branch_tests.len()
816        } else {
817            anchor
818                .tests
819                .iter()
820                .filter(|test| selected_includes(test))
821                .count()
822        };
823        let detail = metadata.get(&anchor.id);
824        let decision = decisions
825            .get(&anchor.id)
826            .cloned()
827            .map(|decision| selected_decision(decision, selected.as_ref()));
828        let conditions = decision.as_ref().map_or(anchor.conditions, |decision| {
829            Some(decision.conditions.len())
830        });
831        let covered_conditions = decision
832            .as_ref()
833            .map_or(anchor.covered_conditions, |decision| {
834                Some(
835                    decision
836                        .conditions
837                        .iter()
838                        .filter(|condition| condition.covered)
839                        .count(),
840                )
841            });
842        let covered = match anchor.kind.as_str() {
843            "decision" => conditions == covered_conditions,
844            "branch" if !branch_alternatives.is_empty() => branch_alternatives
845                .iter()
846                .all(|detail| detail.tests.iter().any(|test| selected_includes(test))),
847            "branch" => anchor.covered,
848            _ => covering_tests > 0,
849        };
850        let branch_source = branch_alternatives
851            .first()
852            .map(|detail| detail.source.clone());
853        let missing_branch_alternatives = branch_alternatives
854            .iter()
855            .filter(|detail| !detail.tests.iter().any(|test| selected_includes(test)))
856            .filter_map(|detail| detail.alternative.clone())
857            .collect::<Vec<_>>();
858        CoverageAnchor {
859            kind: anchor.kind,
860            id: anchor.id,
861            column: anchor.column,
862            covered,
863            source: decision
864                .as_ref()
865                .map(|decision| decision.meta.source.clone())
866                .or(branch_source)
867                .or_else(|| {
868                    detail.map(|detail| {
869                        detail
870                            .label
871                            .clone()
872                            .unwrap_or_else(|| detail.source.clone())
873                    })
874                })
875                .and_then(|source| compact_source(&source)),
876            missing: if missing_branch_alternatives.is_empty() {
877                detail.and_then(|detail| detail.alternative.clone())
878            } else {
879                Some(missing_branch_alternatives.join("; "))
880            },
881            covering_tests,
882            covered_conditions,
883            conditions,
884        }
885    };
886    let rendered_anchors = anchors.into_iter().map(render_anchor).collect::<Vec<_>>();
887    let total_anchored = rendered_anchors.len();
888    let covered_anchored = rendered_anchors
889        .iter()
890        .filter(|anchor| anchor.covered)
891        .count();
892    let all_limitations = index
893        .limitations(options.view)?
894        .into_iter()
895        .filter(|limitation| limitation.file == options.file && limitation.line == options.line)
896        .map(|limitation| CoverageFileLimitation {
897            id: limitation.id,
898            kind: limitation.kind,
899            file: limitation.file,
900            line: limitation.line,
901            column: limitation.column,
902            source: limitation.source,
903            reason: limitation.reason,
904            blocking: limitation.blocking,
905            effect: "outside-measured-denominator".into(),
906        })
907        .collect::<Vec<_>>();
908    let total_limitations = all_limitations.len();
909    let limitations_page = all_limitations
910        .iter()
911        .skip(options.offset)
912        .take(options.limit)
913        .cloned()
914        .collect::<Vec<_>>();
915    let (file_detail, _) = coverage_file_detail_query(
916        index,
917        CoverageFileDetailOptions {
918            run: options.run,
919            view: options.view,
920            kind: options.kind,
921            runner: options.runner,
922            selector: options.file,
923            metric: MinimizeMetric::All,
924            offset: 0,
925            limit: usize::MAX,
926        },
927    )?;
928    let gap_line = file_detail
929        .gap_lines
930        .into_iter()
931        .find(|gap| gap.line == options.line);
932    // Covered lines are absent from the gap projection, but their anchored
933    // statement/branch/decision metadata still carries the source snippet.
934    // Prefer the exact gap-line text when present and otherwise retain that
935    // anchored source so a successful line query never contradicts itself by
936    // claiming the source is unavailable while printing it below.
937    let source = gap_line
938        .as_ref()
939        .and_then(|gap| gap.source.clone())
940        .or_else(|| {
941            rendered_anchors
942                .iter()
943                .filter_map(|anchor| anchor.source.clone())
944                .min_by_key(|source| source.len())
945        });
946    let all_remaining = gap_line.map_or_else(Vec::new, |gap| gap.obligations);
947    let total_remaining = all_remaining.len();
948    let remaining_page = all_remaining
949        .iter()
950        .skip(options.offset)
951        .take(options.limit)
952        .cloned()
953        .collect::<Vec<_>>();
954    let Some(line) = index.line(options.view, options.file, options.line)? else {
955        let anchored = rendered_anchors
956            .iter()
957            .skip(options.offset)
958            .take(options.limit)
959            .cloned()
960            .collect::<Vec<_>>();
961        let total = total_anchored.max(total_limitations).max(total_remaining);
962        let total = total.max(total_anchor_tests);
963        let returned = anchored
964            .len()
965            .max(limitations_page.len())
966            .max(remaining_page.len())
967            .max(anchor_tests_page.len());
968        return Ok((
969            CoverageCoversData::Anchors(CoverageCoversAnchorsData {
970                run: options.run.into(),
971                filters,
972                location,
973                source,
974                source_origin: None,
975                line_obligation: false,
976                anchored,
977                total_anchored,
978                covered_anchored,
979                total_limitations,
980                limitations: limitations_page,
981                total_remaining,
982                remaining: remaining_page,
983                total_tests: total_anchor_tests,
984                tests: anchor_tests_page,
985                assertions: None,
986            }),
987            pagination(options.offset, options.limit, returned, total),
988        ));
989    };
990    let all_tests = line
991        .tests
992        .iter()
993        .filter(|id| selected_includes(id))
994        .map(|id| {
995            let test = tests_by_id.get(id);
996            CoverageCoveringTest {
997                id: id.clone(),
998                name: test.map_or_else(|| id.clone(), |test| test.name.clone()),
999                provenance: test
1000                    .map_or_else(TestProvenance::default, |test| test.provenance.clone()),
1001            }
1002        })
1003        .collect::<Vec<_>>();
1004    let phases_by_id = index
1005        .phase_summaries(options.view)?
1006        .into_iter()
1007        .map(|phase| (phase.id.clone(), phase))
1008        .collect::<HashMap<_, _>>();
1009    let all_phases = line
1010        .phases
1011        .iter()
1012        .filter_map(|id| phases_by_id.get(id))
1013        .filter(|phase| selected_includes(&phase.test))
1014        .map(|phase| CoverageCoveringPhase {
1015            id: phase.id.clone(),
1016            kind: phase.kind.clone(),
1017            operation: phase.operation.clone(),
1018            source: phase.source.clone(),
1019            test: phase.test.clone(),
1020            status: phase.status.clone(),
1021            caused_by_phase_id: phase.caused_by_phase_id.clone(),
1022        })
1023        .collect::<Vec<_>>();
1024    let tests_page = all_tests
1025        .iter()
1026        .skip(options.offset)
1027        .take(options.limit)
1028        .cloned()
1029        .collect::<Vec<_>>();
1030    let phases_page = all_phases
1031        .iter()
1032        .skip(options.offset)
1033        .take(options.limit)
1034        .cloned()
1035        .collect::<Vec<_>>();
1036    let anchored_page = rendered_anchors
1037        .into_iter()
1038        .skip(options.offset)
1039        .take(options.limit)
1040        .collect::<Vec<_>>();
1041    let total = all_tests
1042        .len()
1043        .max(all_phases.len())
1044        .max(total_anchored)
1045        .max(total_limitations)
1046        .max(total_remaining);
1047    let returned = tests_page
1048        .len()
1049        .max(phases_page.len())
1050        .max(anchored_page.len())
1051        .max(limitations_page.len())
1052        .max(remaining_page.len());
1053    Ok((
1054        CoverageCoversData::Line(CoverageCoversLineData {
1055            run: options.run.into(),
1056            filters,
1057            location,
1058            source,
1059            source_origin: None,
1060            covered: line.tests.iter().any(|test| selected_includes(test)),
1061            confidence: line.confidence,
1062            total_tests: all_tests.len(),
1063            total_phases: all_phases.len(),
1064            total_anchored,
1065            covered_anchored,
1066            total_limitations,
1067            total_remaining,
1068            tests: tests_page,
1069            phases: phases_page,
1070            anchored: anchored_page,
1071            limitations: limitations_page,
1072            remaining: remaining_page,
1073            assertions: None,
1074        }),
1075        pagination(options.offset, options.limit, returned, total),
1076    ))
1077}
1078
1079#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1080pub struct CoverageTestMatch {
1081    pub id: String,
1082    pub name: String,
1083    pub outcome: String,
1084    pub provenance: TestProvenance,
1085}
1086
1087#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1088#[serde(rename_all = "camelCase")]
1089pub struct CoverageHitDetail {
1090    pub id: String,
1091    pub obligation: String,
1092    #[serde(skip_serializing_if = "Option::is_none")]
1093    pub branch_kind: Option<String>,
1094    #[serde(skip_serializing_if = "Option::is_none")]
1095    pub file: Option<String>,
1096    #[serde(skip_serializing_if = "Option::is_none")]
1097    pub line: Option<usize>,
1098    #[serde(skip_serializing_if = "Option::is_none")]
1099    pub column: Option<usize>,
1100    #[serde(skip_serializing_if = "Option::is_none")]
1101    pub label: Option<String>,
1102    #[serde(skip_serializing_if = "Option::is_none")]
1103    pub alternative: Option<String>,
1104}
1105
1106#[derive(Debug, Clone, PartialEq, Serialize)]
1107pub struct CoverageTestDecision {
1108    pub id: String,
1109    pub vectors: Vec<crate::coverage_analysis::McdcVector>,
1110    pub meta: DecisionMeta,
1111}
1112
1113#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1114#[serde(rename_all = "camelCase")]
1115pub struct CoverageTestPhase {
1116    pub id: String,
1117    pub kind: String,
1118    pub operation: String,
1119    #[serde(skip_serializing_if = "Option::is_none")]
1120    pub source: Option<String>,
1121    #[serde(skip_serializing_if = "Option::is_none")]
1122    pub status: Option<String>,
1123    #[serde(skip_serializing_if = "Option::is_none")]
1124    pub caused_by_phase_id: Option<String>,
1125    pub lines: usize,
1126    pub decisions: usize,
1127}
1128
1129#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1130pub struct CoverageTestTotals {
1131    pub lines: usize,
1132    pub hits: usize,
1133    pub decisions: usize,
1134    pub phases: usize,
1135}
1136
1137#[derive(Debug, Clone, PartialEq, Serialize)]
1138#[serde(rename_all = "camelCase")]
1139pub struct CoverageSelectedTest {
1140    pub id: String,
1141    pub name: String,
1142    #[serde(skip_serializing_if = "Option::is_none")]
1143    pub file: Option<String>,
1144    #[serde(skip_serializing_if = "Option::is_none")]
1145    pub title: Option<String>,
1146    pub retries: Vec<usize>,
1147    pub attempts: Vec<TestAttempt>,
1148    pub outcome: String,
1149    pub provenance: TestProvenance,
1150    pub role: String,
1151    /// `exact`, or `run-wide` when this test ran and nothing can say what it
1152    /// reached. Under `run-wide` the totals below are zero because the test
1153    /// has no coverage of its own, not because it reached nothing.
1154    pub attribution: String,
1155    pub hits: Vec<String>,
1156    pub decisions: Vec<CoverageTestDecision>,
1157    pub lines: Vec<SourceLine>,
1158    pub hit_details: Vec<CoverageHitDetail>,
1159    pub phases: Vec<CoverageTestPhase>,
1160    pub totals: CoverageTestTotals,
1161}
1162
1163#[derive(Debug, Clone, PartialEq, Serialize)]
1164pub struct CoverageTestMatchesData {
1165    pub run: String,
1166    pub filters: CoverageQueryFilters,
1167    pub tests: Vec<CoverageTestMatch>,
1168}
1169
1170#[derive(Debug, Clone, PartialEq, Serialize)]
1171#[serde(rename_all = "camelCase")]
1172pub struct CoverageTestDetailData {
1173    pub run: String,
1174    pub filters: CoverageQueryFilters,
1175    pub pagination_applies_to: String,
1176    pub tests: Vec<CoverageSelectedTest>,
1177}
1178
1179#[derive(Debug, Clone, PartialEq, Serialize)]
1180#[serde(untagged)]
1181pub enum CoverageTestData {
1182    Matches(CoverageTestMatchesData),
1183    Detail(CoverageTestDetailData),
1184}
1185
1186#[derive(Debug, Clone, Copy)]
1187pub struct CoverageTestQueryOptions<'a> {
1188    pub run: &'a str,
1189    pub view: CoverageViewId,
1190    pub kind: Option<&'a str>,
1191    pub runner: Option<&'a str>,
1192    pub selector: &'a str,
1193    pub offset: usize,
1194    pub limit: usize,
1195}
1196
1197fn hit_detail(id: &str, metadata: Option<&IndexedHitMetadata>) -> CoverageHitDetail {
1198    match metadata {
1199        Some(metadata) => CoverageHitDetail {
1200            id: id.into(),
1201            obligation: metadata.obligation.clone(),
1202            branch_kind: metadata.branch_kind.clone(),
1203            file: Some(metadata.file.clone()),
1204            line: Some(metadata.line),
1205            column: Some(metadata.column),
1206            label: metadata.label.clone(),
1207            alternative: metadata.alternative.clone(),
1208        },
1209        None => CoverageHitDetail {
1210            id: id.into(),
1211            obligation: "unknown".into(),
1212            branch_kind: None,
1213            file: None,
1214            line: None,
1215            column: None,
1216            label: None,
1217            alternative: None,
1218        },
1219    }
1220}
1221
1222pub fn coverage_test_query(
1223    index: &CoverageIndex<'_>,
1224    options: CoverageTestQueryOptions<'_>,
1225) -> Result<(CoverageTestData, AgentPagination), QueryError> {
1226    if options.limit == 0 {
1227        return Err(QueryError::InvalidPagination);
1228    }
1229    let tests = index.test_details(options.view)?;
1230    let summaries = tests
1231        .iter()
1232        .map(|test| test.summary.clone())
1233        .collect::<Vec<_>>();
1234    let selected = selected_test_ids(&summaries, options.kind, options.runner)?;
1235    let selector = options.selector.to_lowercase();
1236    let matches = tests
1237        .into_iter()
1238        .filter(|test| {
1239            selected
1240                .as_ref()
1241                .is_none_or(|ids| ids.contains(&test.summary.id))
1242        })
1243        .filter(|test| {
1244            test.summary.id == selector || test.summary.name.to_lowercase().contains(&selector)
1245        })
1246        .collect::<Vec<_>>();
1247    if matches.is_empty() {
1248        return Err(QueryError::TestNotFound(options.selector.into()));
1249    }
1250    let filters = query_filters(options.view, options.kind, options.runner);
1251    if matches.len() > 1 {
1252        let total = matches.len();
1253        let page = matches
1254            .into_iter()
1255            .skip(options.offset)
1256            .take(options.limit)
1257            .map(|test| CoverageTestMatch {
1258                id: test.summary.id,
1259                name: test.summary.name,
1260                outcome: test.summary.outcome,
1261                provenance: test.summary.provenance,
1262            })
1263            .collect::<Vec<_>>();
1264        let returned = page.len();
1265        return Ok((
1266            CoverageTestData::Matches(CoverageTestMatchesData {
1267                run: options.run.into(),
1268                filters,
1269                tests: page,
1270            }),
1271            pagination(options.offset, options.limit, returned, total),
1272        ));
1273    }
1274    let test = matches.into_iter().next().expect("one test match");
1275    let metadata = index
1276        .hit_metadata(options.view)?
1277        .into_iter()
1278        .map(|metadata| (metadata.id.clone(), metadata))
1279        .collect::<HashMap<_, _>>();
1280    let decisions = index
1281        .decision_metadata(options.view)?
1282        .into_iter()
1283        .map(|decision| (decision.id.clone(), decision))
1284        .collect::<HashMap<_, _>>();
1285    let all_phases = index
1286        .phase_summaries(options.view)?
1287        .into_iter()
1288        .filter(|phase| phase.test == test.summary.id)
1289        .map(|phase| CoverageTestPhase {
1290            id: phase.id,
1291            kind: phase.kind,
1292            operation: phase.operation,
1293            source: phase.source,
1294            status: phase.status,
1295            caused_by_phase_id: phase.caused_by_phase_id,
1296            lines: phase.lines,
1297            decisions: phase.decisions,
1298        })
1299        .collect::<Vec<_>>();
1300    let totals = CoverageTestTotals {
1301        lines: test.lines.len(),
1302        hits: test.hits.len(),
1303        decisions: test.decisions.len(),
1304        phases: all_phases.len(),
1305    };
1306    let total = totals
1307        .lines
1308        .max(totals.hits)
1309        .max(totals.decisions)
1310        .max(totals.phases);
1311    let lines = test
1312        .lines
1313        .iter()
1314        .skip(options.offset)
1315        .take(options.limit)
1316        .cloned()
1317        .collect::<Vec<_>>();
1318    let hits = test
1319        .hits
1320        .iter()
1321        .skip(options.offset)
1322        .take(options.limit)
1323        .cloned()
1324        .collect::<Vec<_>>();
1325    let hit_details = test
1326        .hits
1327        .iter()
1328        .skip(options.offset)
1329        .take(options.limit)
1330        .map(|id| hit_detail(id, metadata.get(id)))
1331        .collect::<Vec<_>>();
1332    let test_decisions = test
1333        .decisions
1334        .iter()
1335        .skip(options.offset)
1336        .take(options.limit)
1337        .map(|decision| {
1338            Ok(CoverageTestDecision {
1339                id: decision.id.clone(),
1340                vectors: decision.vectors.clone(),
1341                meta: decisions
1342                    .get(&decision.id)
1343                    .cloned()
1344                    .ok_or(QueryError::InvalidRecordSelection)?,
1345            })
1346        })
1347        .collect::<Result<Vec<_>, QueryError>>()?;
1348    let phases = all_phases
1349        .into_iter()
1350        .skip(options.offset)
1351        .take(options.limit)
1352        .collect::<Vec<_>>();
1353    let returned = lines
1354        .len()
1355        .max(hits.len())
1356        .max(test_decisions.len())
1357        .max(phases.len());
1358    Ok((
1359        CoverageTestData::Detail(CoverageTestDetailData {
1360            run: options.run.into(),
1361            filters,
1362            pagination_applies_to:
1363                "lines, hits/hitDetails, decisions, and phases independently within the test".into(),
1364            tests: vec![CoverageSelectedTest {
1365                id: test.summary.id,
1366                name: test.summary.name,
1367                file: test.summary.file,
1368                title: test.summary.title,
1369                retries: test.retries,
1370                attempts: test.attempts,
1371                outcome: test.summary.outcome,
1372                provenance: test.summary.provenance,
1373                role: test.summary.role,
1374                attribution: test.summary.attribution,
1375                hits,
1376                decisions: test_decisions,
1377                lines,
1378                hit_details,
1379                phases,
1380                totals,
1381            }],
1382        }),
1383        pagination(options.offset, options.limit, returned, total),
1384    ))
1385}
1386
1387#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1388pub struct CoverageDecisionMatch {
1389    pub id: String,
1390    pub file: String,
1391    pub line: usize,
1392    pub column: usize,
1393    pub source: String,
1394}
1395
1396#[derive(Debug, Clone, PartialEq, Serialize)]
1397#[serde(rename_all = "camelCase")]
1398pub struct CoverageDecisionCondition {
1399    pub index: usize,
1400    pub source: String,
1401    pub covered: bool,
1402    #[serde(skip_serializing_if = "Option::is_none")]
1403    pub assertion_covered: Option<bool>,
1404    #[serde(skip_serializing_if = "Option::is_none")]
1405    pub witness: Option<[crate::coverage_analysis::McdcVector; 2]>,
1406    #[serde(skip_serializing_if = "Option::is_none")]
1407    pub witness_tests: Option<[Vec<Id>; 2]>,
1408}
1409
1410#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1411#[serde(rename_all = "camelCase")]
1412pub struct CoverageDecisionTotals {
1413    pub conditions: usize,
1414    pub vector_observations: usize,
1415    pub tests: usize,
1416}
1417
1418#[derive(Debug, Clone, PartialEq, Serialize)]
1419#[serde(rename_all = "camelCase")]
1420pub struct CoverageSelectedDecision {
1421    pub meta: DecisionMeta,
1422    pub executed: bool,
1423    pub covered: bool,
1424    pub vectors: Vec<crate::coverage_analysis::McdcVector>,
1425    pub vector_observations: Vec<crate::coverage_report::VectorObservation>,
1426    pub conditions: Vec<CoverageDecisionCondition>,
1427    pub tests: Vec<Id>,
1428    pub confidence: CoverageConfidence,
1429    pub totals: CoverageDecisionTotals,
1430}
1431
1432#[derive(Debug, Clone, PartialEq, Serialize)]
1433pub struct CoverageDecisionMatchesData {
1434    pub run: String,
1435    pub filters: CoverageQueryFilters,
1436    pub decisions: Vec<CoverageDecisionMatch>,
1437}
1438
1439#[derive(Debug, Clone, PartialEq, Serialize)]
1440#[serde(rename_all = "camelCase")]
1441pub struct CoverageDecisionDetailData {
1442    pub run: String,
1443    pub filters: CoverageQueryFilters,
1444    pub pagination_applies_to: String,
1445    pub decisions: Vec<CoverageSelectedDecision>,
1446}
1447
1448#[derive(Debug, Clone, PartialEq, Serialize)]
1449#[serde(untagged)]
1450pub enum CoverageDecisionData {
1451    Matches(CoverageDecisionMatchesData),
1452    Detail(CoverageDecisionDetailData),
1453}
1454
1455#[derive(Debug, Clone, Copy)]
1456pub struct CoverageDecisionQueryOptions<'a> {
1457    pub run: &'a str,
1458    pub view: CoverageViewId,
1459    pub kind: Option<&'a str>,
1460    pub runner: Option<&'a str>,
1461    pub selector: &'a str,
1462    pub offset: usize,
1463    pub limit: usize,
1464}
1465
1466fn selector_location(selector: &str) -> Option<(&str, usize)> {
1467    let (prefix, last) = selector.rsplit_once(':')?;
1468    let last = last.parse::<usize>().ok()?;
1469    if let Some((file, possible_line)) = prefix.rsplit_once(':')
1470        && let Ok(line) = possible_line.parse::<usize>()
1471    {
1472        return Some((file, line));
1473    }
1474    Some((prefix, last))
1475}
1476
1477fn selected_decision(
1478    decision: crate::coverage_report::DecisionResult,
1479    selected: Option<&BTreeSet<String>>,
1480) -> crate::coverage_report::DecisionResult {
1481    let Some(selected) = selected else {
1482        return decision;
1483    };
1484    let vector_observations = decision
1485        .vector_observations
1486        .into_iter()
1487        .filter_map(|mut observation| {
1488            observation
1489                .tests
1490                .retain(|test| selected.contains(test.as_str()));
1491            (!observation.tests.is_empty()).then_some(observation)
1492        })
1493        .collect::<Vec<_>>();
1494    let vectors = vector_observations
1495        .iter()
1496        .map(|observation| observation.vector.clone())
1497        .collect::<Vec<_>>();
1498    let conditions = decision
1499        .meta
1500        .conditions
1501        .iter()
1502        .enumerate()
1503        .map(|(index, source)| {
1504            let mut witness = None;
1505            let mut witness_tests = None;
1506            'pairs: for left in 0..vector_observations.len() {
1507                for right in (left + 1)..vector_observations.len() {
1508                    let first = &vector_observations[left];
1509                    let second = &vector_observations[right];
1510                    if is_independence_pair(&first.vector, &second.vector, index) {
1511                        witness = Some([first.vector.clone(), second.vector.clone()]);
1512                        witness_tests = Some([first.tests.clone(), second.tests.clone()]);
1513                        break 'pairs;
1514                    }
1515                }
1516            }
1517            crate::coverage_report::ConditionResult {
1518                index,
1519                source: source.clone(),
1520                covered: witness.is_some(),
1521                assertion_covered: false,
1522                witness,
1523                witness_tests,
1524            }
1525        })
1526        .collect::<Vec<_>>();
1527    crate::coverage_report::DecisionResult {
1528        meta: decision.meta,
1529        executed: !vectors.is_empty(),
1530        covered: conditions.iter().all(|condition| condition.covered),
1531        vectors,
1532        vector_observations,
1533        conditions,
1534        tests: decision
1535            .tests
1536            .into_iter()
1537            .filter(|test| selected.contains(test.as_str()))
1538            .collect(),
1539        confidence: decision.confidence,
1540    }
1541}
1542
1543pub fn coverage_decision_query(
1544    index: &CoverageIndex<'_>,
1545    options: CoverageDecisionQueryOptions<'_>,
1546) -> Result<(CoverageDecisionData, AgentPagination), QueryError> {
1547    if options.limit == 0 {
1548        return Err(QueryError::InvalidPagination);
1549    }
1550    let tests = index.test_summaries(options.view)?;
1551    let selected = selected_test_ids(&tests, options.kind, options.runner)?;
1552    let decisions = index.decision_details(options.view)?;
1553    let mut matches = decisions
1554        .into_iter()
1555        .filter(|decision| decision.meta.id == options.selector)
1556        .collect::<Vec<_>>();
1557    if matches.is_empty()
1558        && let Some((file, line)) = selector_location(options.selector)
1559    {
1560        matches = index
1561            .decision_details(options.view)?
1562            .into_iter()
1563            .filter(|decision| decision.meta.file == file && decision.meta.line == line)
1564            .collect();
1565    }
1566    if matches.is_empty() {
1567        return Err(QueryError::DecisionNotFound(options.selector.into()));
1568    }
1569    let filters = query_filters(options.view, options.kind, options.runner);
1570    if matches.len() > 1 {
1571        let total = matches.len();
1572        let page = matches
1573            .into_iter()
1574            .skip(options.offset)
1575            .take(options.limit)
1576            .map(|decision| CoverageDecisionMatch {
1577                id: decision.meta.id,
1578                file: decision.meta.file,
1579                line: decision.meta.line,
1580                column: decision.meta.column,
1581                source: decision.meta.source,
1582            })
1583            .collect::<Vec<_>>();
1584        let returned = page.len();
1585        return Ok((
1586            CoverageDecisionData::Matches(CoverageDecisionMatchesData {
1587                run: options.run.into(),
1588                filters,
1589                decisions: page,
1590            }),
1591            pagination(options.offset, options.limit, returned, total),
1592        ));
1593    }
1594    let filtered = selected_decision(
1595        matches.into_iter().next().expect("one decision match"),
1596        selected.as_ref(),
1597    );
1598    let totals = CoverageDecisionTotals {
1599        conditions: filtered.conditions.len(),
1600        vector_observations: filtered.vector_observations.len(),
1601        tests: filtered.tests.len(),
1602    };
1603    let total = totals
1604        .conditions
1605        .max(totals.vector_observations)
1606        .max(totals.tests);
1607    let vector_observations = filtered
1608        .vector_observations
1609        .iter()
1610        .skip(options.offset)
1611        .take(options.limit)
1612        .cloned()
1613        .collect::<Vec<_>>();
1614    let vectors = vector_observations
1615        .iter()
1616        .map(|observation| observation.vector.clone())
1617        .collect::<Vec<_>>();
1618    let conditions = filtered
1619        .conditions
1620        .iter()
1621        .skip(options.offset)
1622        .take(options.limit)
1623        .map(|condition| CoverageDecisionCondition {
1624            index: condition.index,
1625            source: condition.source.clone(),
1626            covered: condition.covered,
1627            assertion_covered: selected.is_none().then_some(condition.assertion_covered),
1628            witness: condition.witness.clone(),
1629            witness_tests: condition.witness_tests.clone(),
1630        })
1631        .collect::<Vec<_>>();
1632    let tests = filtered
1633        .tests
1634        .iter()
1635        .skip(options.offset)
1636        .take(options.limit)
1637        .cloned()
1638        .collect::<Vec<_>>();
1639    let returned = vector_observations
1640        .len()
1641        .max(conditions.len())
1642        .max(tests.len());
1643    Ok((
1644        CoverageDecisionData::Detail(CoverageDecisionDetailData {
1645            run: options.run.into(),
1646            filters,
1647            pagination_applies_to:
1648                "conditions, vectorObservations, and tests independently within each decision"
1649                    .into(),
1650            decisions: vec![CoverageSelectedDecision {
1651                meta: filtered.meta,
1652                executed: filtered.executed,
1653                covered: filtered.covered,
1654                vectors,
1655                vector_observations,
1656                conditions,
1657                tests,
1658                confidence: filtered.confidence,
1659                totals,
1660            }],
1661        }),
1662        pagination(options.offset, options.limit, returned, total),
1663    ))
1664}
1665
1666#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1667pub struct CoverageOtherTest {
1668    pub id: String,
1669    pub name: String,
1670}
1671
1672#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1673#[serde(rename_all = "camelCase")]
1674pub struct CoverageOtherCoverage {
1675    pub covered_elsewhere: bool,
1676    pub kinds: Vec<String>,
1677    pub runners: Vec<String>,
1678    pub tests: Vec<CoverageOtherTest>,
1679}
1680
1681#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1682#[serde(rename_all = "camelCase")]
1683pub struct CoverageLineObligation {
1684    pub kind: String,
1685    pub id: String,
1686    pub line: usize,
1687    pub other_coverage: CoverageOtherCoverage,
1688}
1689
1690#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1691#[serde(rename_all = "camelCase")]
1692pub struct CoveragePointObligation {
1693    pub kind: String,
1694    pub id: String,
1695    pub line: usize,
1696    pub column: usize,
1697    pub source: String,
1698    pub other_coverage: CoverageOtherCoverage,
1699}
1700
1701#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1702#[serde(rename_all = "camelCase")]
1703pub struct CoverageBranchObligation {
1704    pub kind: String,
1705    pub id: String,
1706    pub line: usize,
1707    pub column: usize,
1708    pub source: String,
1709    pub missing: String,
1710    pub other_coverage: CoverageOtherCoverage,
1711}
1712
1713#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1714#[serde(rename_all = "camelCase")]
1715pub struct CoverageMcdcObligation {
1716    pub kind: String,
1717    pub id: String,
1718    pub line: usize,
1719    pub column: usize,
1720    pub decision: String,
1721    pub missing_condition: String,
1722    #[serde(skip)]
1723    pub condition_index: usize,
1724    pub observed_vectors: Vec<String>,
1725    pub other_coverage: CoverageOtherCoverage,
1726}
1727
1728#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1729#[serde(untagged)]
1730pub enum CoverageFileObligation {
1731    Line(CoverageLineObligation),
1732    Point(CoveragePointObligation),
1733    Branch(CoverageBranchObligation),
1734    Mcdc(CoverageMcdcObligation),
1735}
1736
1737impl CoverageFileObligation {
1738    fn line(&self) -> usize {
1739        match self {
1740            Self::Line(value) => value.line,
1741            Self::Point(value) => value.line,
1742            Self::Branch(value) => value.line,
1743            Self::Mcdc(value) => value.line,
1744        }
1745    }
1746
1747    fn kind(&self) -> &str {
1748        match self {
1749            Self::Line(value) => &value.kind,
1750            Self::Point(value) => &value.kind,
1751            Self::Branch(value) => &value.kind,
1752            Self::Mcdc(value) => &value.kind,
1753        }
1754    }
1755}
1756
1757#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1758pub struct CoverageFileLimitation {
1759    pub id: String,
1760    pub kind: String,
1761    pub file: String,
1762    pub line: usize,
1763    pub column: usize,
1764    pub source: String,
1765    pub reason: String,
1766    pub blocking: bool,
1767    pub effect: String,
1768}
1769
1770#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1771#[serde(rename_all = "camelCase")]
1772pub struct CoverageFileCounts {
1773    /// Measured lines in the file, and how many the selected tests covered:
1774    /// the file's own line coverage, comparable with other tools'.
1775    pub total_lines: usize,
1776    pub covered_lines: usize,
1777    pub uncovered_lines: usize,
1778    pub uncovered_statements: usize,
1779    pub uncovered_functions: usize,
1780    pub missing_branches: usize,
1781    pub missing_mcdc_conditions: usize,
1782    pub measurement_limitations: usize,
1783}
1784
1785#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1786#[serde(rename_all = "camelCase")]
1787pub struct CoverageFileGapLine {
1788    pub line: usize,
1789    pub state: String,
1790    #[serde(skip_serializing_if = "Option::is_none")]
1791    pub source: Option<String>,
1792    pub obligations: Vec<CoverageFileObligation>,
1793    pub limitations: Vec<CoverageFileLimitation>,
1794}
1795
1796#[derive(Debug, Clone, PartialEq, Serialize)]
1797#[serde(rename_all = "camelCase")]
1798pub struct CoverageFileDetailData {
1799    pub run: String,
1800    pub filters: CoverageQueryFilters,
1801    pub file: String,
1802    pub metric: MinimizeMetric,
1803    pub counts: CoverageFileCounts,
1804    pub total_tests: usize,
1805    pub total_obligations: usize,
1806    pub total_gap_lines: usize,
1807    pub gap_lines: Vec<CoverageFileGapLine>,
1808    pub total_limitations: usize,
1809    /// The run's saved assertion assessment for this place, attached by the
1810    /// CLI when the run has one.
1811    #[serde(skip_serializing_if = "Option::is_none")]
1812    pub assertions: Option<serde_json::Value>,
1813}
1814
1815#[derive(Debug, Clone, Copy)]
1816pub struct CoverageFileDetailOptions<'a> {
1817    pub run: &'a str,
1818    pub view: CoverageViewId,
1819    pub kind: Option<&'a str>,
1820    pub runner: Option<&'a str>,
1821    pub selector: &'a str,
1822    pub metric: MinimizeMetric,
1823    pub offset: usize,
1824    pub limit: usize,
1825}
1826
1827fn other_coverage<T: AsRef<str>>(
1828    test_ids: &[T],
1829    selected: Option<&BTreeSet<String>>,
1830    tests: &HashMap<String, IndexedTestSummary>,
1831) -> CoverageOtherCoverage {
1832    let covered = selected.map_or_else(Vec::new, |selected| {
1833        test_ids
1834            .iter()
1835            .filter(|id| !selected.contains(id.as_ref()))
1836            .filter_map(|id| tests.get(id.as_ref()))
1837            .collect::<Vec<_>>()
1838    });
1839    CoverageOtherCoverage {
1840        covered_elsewhere: !covered.is_empty(),
1841        kinds: covered
1842            .iter()
1843            .map(|test| test.provenance.kind.clone())
1844            .collect::<BTreeSet<_>>()
1845            .into_iter()
1846            .collect(),
1847        runners: covered
1848            .iter()
1849            .map(|test| test.provenance.runner.clone())
1850            .collect::<BTreeSet<_>>()
1851            .into_iter()
1852            .collect(),
1853        tests: covered
1854            .into_iter()
1855            .map(|test| CoverageOtherTest {
1856                id: test.id.clone(),
1857                name: test.name.clone(),
1858            })
1859            .collect(),
1860    }
1861}
1862
1863fn vector_text(vector: &crate::coverage_analysis::McdcVector) -> String {
1864    let values = vector
1865        .values
1866        .iter()
1867        .map(|value| match value {
1868            None => '-',
1869            Some(false) => 'F',
1870            Some(true) => 'T',
1871        })
1872        .collect::<String>();
1873    format!("{values} -> {}", if vector.outcome { 'T' } else { 'F' })
1874}
1875
1876fn obligation_matches_metric(obligation: &CoverageFileObligation, metric: MinimizeMetric) -> bool {
1877    metric == MinimizeMetric::All
1878        || matches!(
1879            (obligation.kind(), metric),
1880            ("line", MinimizeMetric::Lines)
1881                | ("statement", MinimizeMetric::Statements)
1882                | ("function", MinimizeMetric::Functions)
1883                | ("branch", MinimizeMetric::Branches)
1884                | ("mcdc", MinimizeMetric::Mcdc)
1885        )
1886}
1887
1888fn compact_source(value: &str) -> Option<String> {
1889    let line = value.lines().find(|line| !line.trim().is_empty())?.trim();
1890    let compact = line.split_whitespace().collect::<Vec<_>>().join(" ");
1891    if compact.is_empty() {
1892        None
1893    } else if compact.chars().count() > 120 {
1894        Some(format!(
1895            "{}…",
1896            compact.chars().take(119).collect::<String>()
1897        ))
1898    } else {
1899        Some(compact)
1900    }
1901}
1902
1903fn obligation_source(obligation: &CoverageFileObligation) -> Option<String> {
1904    match obligation {
1905        CoverageFileObligation::Line(_) => None,
1906        CoverageFileObligation::Point(value) => compact_source(&value.source),
1907        CoverageFileObligation::Branch(value) => compact_source(&value.source),
1908        CoverageFileObligation::Mcdc(value) => compact_source(&value.decision),
1909    }
1910}
1911
1912/// What a file's detail is read from: the run's lines, points, decisions,
1913/// limitations and tests, read once and kept by file.
1914///
1915/// Reading them is most of what a file's detail costs, and none of it
1916/// depends on the file. The HTML report asks for every file with a gap, and
1917/// read the whole run again for each: six minutes for a merged run of 2,094
1918/// tests that is exported in five seconds.
1919pub struct FileDetailSource {
1920    view: CoverageViewId,
1921    tests: Vec<crate::coverage_index::IndexedTestDetail>,
1922    test_summaries: Vec<IndexedTestSummary>,
1923    tests_by_id: HashMap<String, IndexedTestSummary>,
1924    /// The tests with a line in each file.
1925    tests_by_file: HashMap<String, Vec<String>>,
1926    lines: Vec<crate::coverage_index::IndexedLine>,
1927    lines_by_file: BTreeMap<String, Vec<usize>>,
1928    points: Vec<crate::coverage_index::IndexedHitMetadata>,
1929    points_by_file: HashMap<String, Vec<usize>>,
1930    decisions: Vec<crate::coverage_report::DecisionResult>,
1931    decisions_by_file: HashMap<String, Vec<usize>>,
1932    limitations: BTreeMap<String, Vec<crate::coverage_index::IndexedLimitation>>,
1933}
1934
1935impl FileDetailSource {
1936    pub fn read(index: &CoverageIndex<'_>, view: CoverageViewId) -> Result<Self, QueryError> {
1937        let tests = index.test_details(view)?;
1938        let mut tests_by_file = HashMap::<String, Vec<String>>::new();
1939        for test in &tests {
1940            let mut files = test
1941                .lines
1942                .iter()
1943                .map(|line| line.file.as_str())
1944                .collect::<Vec<_>>();
1945            files.sort_unstable();
1946            files.dedup();
1947            for file in files {
1948                tests_by_file
1949                    .entry(file.to_owned())
1950                    .or_default()
1951                    .push(test.summary.id.clone());
1952            }
1953        }
1954        let test_summaries = tests
1955            .iter()
1956            .map(|test| test.summary.clone())
1957            .collect::<Vec<_>>();
1958        let tests_by_id = test_summaries
1959            .iter()
1960            .map(|test| (test.id.clone(), test.clone()))
1961            .collect();
1962        let lines = index.lines(view)?;
1963        let mut lines_by_file = BTreeMap::<String, Vec<usize>>::new();
1964        for (at, line) in lines.iter().enumerate() {
1965            lines_by_file.entry(line.file.clone()).or_default().push(at);
1966        }
1967        let points = index.hit_metadata(view)?;
1968        let mut points_by_file = HashMap::<String, Vec<usize>>::new();
1969        for (at, point) in points.iter().enumerate() {
1970            points_by_file
1971                .entry(point.file.clone())
1972                .or_default()
1973                .push(at);
1974        }
1975        let decisions = index.decision_details(view)?;
1976        let mut decisions_by_file = HashMap::<String, Vec<usize>>::new();
1977        for (at, decision) in decisions.iter().enumerate() {
1978            decisions_by_file
1979                .entry(decision.meta.file.clone())
1980                .or_default()
1981                .push(at);
1982        }
1983        let mut limitations = BTreeMap::<String, Vec<_>>::new();
1984        for limitation in index.limitations(view)? {
1985            limitations
1986                .entry(limitation.file.clone())
1987                .or_default()
1988                .push(limitation);
1989        }
1990        Ok(Self {
1991            view,
1992            tests,
1993            test_summaries,
1994            tests_by_id,
1995            tests_by_file,
1996            lines,
1997            lines_by_file,
1998            points,
1999            points_by_file,
2000            decisions,
2001            decisions_by_file,
2002            limitations,
2003        })
2004    }
2005
2006    /// Every test, line, point and decision of the run, in the index's
2007    /// order, for a reader that wants them all as well as a file's detail.
2008    pub fn tests(&self) -> &[crate::coverage_index::IndexedTestDetail] {
2009        &self.tests
2010    }
2011
2012    pub fn lines(&self) -> &[crate::coverage_index::IndexedLine] {
2013        &self.lines
2014    }
2015
2016    pub fn points(&self) -> &[crate::coverage_index::IndexedHitMetadata] {
2017        &self.points
2018    }
2019
2020    pub fn decisions(&self) -> &[crate::coverage_report::DecisionResult] {
2021        &self.decisions
2022    }
2023}
2024
2025pub fn coverage_file_detail_query(
2026    index: &CoverageIndex<'_>,
2027    options: CoverageFileDetailOptions<'_>,
2028) -> Result<(CoverageFileDetailData, AgentPagination), QueryError> {
2029    if options.limit == 0 {
2030        return Err(QueryError::InvalidPagination);
2031    }
2032    coverage_file_detail_from(&FileDetailSource::read(index, options.view)?, options)
2033}
2034
2035/// One file's detail from a run already read. `options.view` is the view the
2036/// run was read in.
2037pub fn coverage_file_detail_from(
2038    read: &FileDetailSource,
2039    options: CoverageFileDetailOptions<'_>,
2040) -> Result<(CoverageFileDetailData, AgentPagination), QueryError> {
2041    if options.limit == 0 {
2042        return Err(QueryError::InvalidPagination);
2043    }
2044    debug_assert!(read.view == options.view);
2045    let selected = selected_test_ids(&read.test_summaries, options.kind, options.runner)?;
2046    let tests_by_id = &read.tests_by_id;
2047    let files = read
2048        .lines_by_file
2049        .keys()
2050        .chain(read.limitations.keys())
2051        .map(String::as_str)
2052        .collect::<BTreeSet<_>>();
2053    let file = if files.contains(options.selector) {
2054        options.selector.to_owned()
2055    } else {
2056        let matches = files
2057            .into_iter()
2058            .filter(|file| file.contains(options.selector))
2059            .collect::<Vec<_>>();
2060        if matches.is_empty() {
2061            return Err(QueryError::SourceNotFound(options.selector.into()));
2062        }
2063        if matches.len() != 1 {
2064            return Err(QueryError::AmbiguousSelector {
2065                selector: options.selector.into(),
2066                matches: matches.into_iter().map(str::to_owned).collect(),
2067            });
2068        }
2069        matches[0].to_owned()
2070    };
2071    let lines = read
2072        .lines_by_file
2073        .get(&file)
2074        .into_iter()
2075        .flatten()
2076        .map(|at| &read.lines[*at])
2077        .collect::<Vec<_>>();
2078    let metadata = read
2079        .points_by_file
2080        .get(&file)
2081        .into_iter()
2082        .flatten()
2083        .map(|at| &read.points[*at])
2084        .collect::<Vec<_>>();
2085    let selected_includes = |tests: &[String]| {
2086        selected.as_ref().map_or(!tests.is_empty(), |selected| {
2087            tests.iter().any(|test| selected.contains(test))
2088        })
2089    };
2090    let total_lines = lines.iter().filter(|line| line.measured).count();
2091    let uncovered_lines = lines
2092        .iter()
2093        .filter(|line| line.measured && !selected_includes(&line.tests))
2094        .map(|line| {
2095            CoverageFileObligation::Line(CoverageLineObligation {
2096                kind: "line".into(),
2097                id: format!("line:{}:{}", line.file, line.line),
2098                line: line.line,
2099                other_coverage: other_coverage(&line.tests, selected.as_ref(), tests_by_id),
2100            })
2101        })
2102        .collect::<Vec<_>>();
2103    let statements = metadata
2104        .iter()
2105        .filter(|point| point.obligation == "statement" && !selected_includes(&point.tests))
2106        .map(|point| {
2107            CoverageFileObligation::Point(CoveragePointObligation {
2108                kind: "statement".into(),
2109                id: point.id.clone(),
2110                line: point.line,
2111                column: point.column,
2112                source: point.label.clone().unwrap_or_else(|| point.source.clone()),
2113                other_coverage: other_coverage(&point.tests, selected.as_ref(), tests_by_id),
2114            })
2115        })
2116        .collect::<Vec<_>>();
2117    let functions = metadata
2118        .iter()
2119        .filter(|point| point.obligation == "function" && !selected_includes(&point.tests))
2120        .map(|point| {
2121            CoverageFileObligation::Point(CoveragePointObligation {
2122                kind: "function".into(),
2123                id: point.id.clone(),
2124                line: point.line,
2125                column: point.column,
2126                source: point.label.clone().unwrap_or_else(|| point.source.clone()),
2127                other_coverage: other_coverage(&point.tests, selected.as_ref(), tests_by_id),
2128            })
2129        })
2130        .collect::<Vec<_>>();
2131    let branches = metadata
2132        .iter()
2133        .filter(|branch| branch.obligation == "branch" && !selected_includes(&branch.tests))
2134        .map(|branch| {
2135            CoverageFileObligation::Branch(CoverageBranchObligation {
2136                kind: "branch".into(),
2137                id: branch.id.clone(),
2138                line: branch.line,
2139                column: branch.column,
2140                source: branch.source.clone(),
2141                missing: branch.alternative.clone().unwrap_or_default(),
2142                other_coverage: other_coverage(&branch.tests, selected.as_ref(), tests_by_id),
2143            })
2144        })
2145        .collect::<Vec<_>>();
2146    // A line ran when anything anchored on it was observed. A line inside a
2147    // multi-line expression has no line obligation of its own, so this is
2148    // what tells one that never ran from one that ran part of the way.
2149    let observed_lines = metadata
2150        .iter()
2151        .filter(|point| selected_includes(&point.tests))
2152        .map(|point| point.line)
2153        .collect::<BTreeSet<_>>();
2154    let mut mcdc = Vec::new();
2155    for original in read
2156        .decisions_by_file
2157        .get(&file)
2158        .into_iter()
2159        .flatten()
2160        .map(|at| &read.decisions[*at])
2161    {
2162        let filtered = selected_decision(original.clone(), selected.as_ref());
2163        for condition in filtered
2164            .conditions
2165            .iter()
2166            .filter(|condition| !condition.covered)
2167        {
2168            let original_tests = original.conditions[condition.index]
2169                .witness_tests
2170                .clone()
2171                .unwrap_or_default()
2172                .into_iter()
2173                .flatten()
2174                .collect::<Vec<_>>();
2175            mcdc.push(CoverageFileObligation::Mcdc(CoverageMcdcObligation {
2176                kind: "mcdc".into(),
2177                id: original.meta.id.clone(),
2178                line: original.meta.line,
2179                column: original.meta.column,
2180                decision: original.meta.source.clone(),
2181                missing_condition: condition.source.clone(),
2182                condition_index: condition.index,
2183                observed_vectors: filtered
2184                    .vector_observations
2185                    .iter()
2186                    .map(|observation| vector_text(&observation.vector))
2187                    .collect(),
2188                other_coverage: other_coverage(&original_tests, selected.as_ref(), tests_by_id),
2189            }));
2190        }
2191    }
2192    let mut obligations = uncovered_lines
2193        .iter()
2194        .chain(statements.iter())
2195        .chain(functions.iter())
2196        .chain(branches.iter())
2197        .chain(mcdc.iter())
2198        .filter(|obligation| obligation_matches_metric(obligation, options.metric))
2199        .cloned()
2200        .collect::<Vec<_>>();
2201    obligations.sort_by(|left, right| {
2202        left.line()
2203            .cmp(&right.line())
2204            .then_with(|| left.kind().cmp(right.kind()))
2205    });
2206    let mut limitations = read
2207        .limitations
2208        .get(&file)
2209        .into_iter()
2210        .flatten()
2211        .cloned()
2212        .map(|limitation| CoverageFileLimitation {
2213            id: limitation.id,
2214            kind: limitation.kind,
2215            file: limitation.file,
2216            line: limitation.line,
2217            column: limitation.column,
2218            source: limitation.source,
2219            reason: limitation.reason,
2220            blocking: limitation.blocking,
2221            effect: "outside-measured-denominator".into(),
2222        })
2223        .collect::<Vec<_>>();
2224    limitations.sort_by(|left, right| {
2225        left.line
2226            .cmp(&right.line)
2227            .then_with(|| left.column.cmp(&right.column))
2228            .then_with(|| left.id.cmp(&right.id))
2229    });
2230    let total_tests = read
2231        .tests_by_file
2232        .get(&file)
2233        .into_iter()
2234        .flatten()
2235        .filter(|test| {
2236            selected
2237                .as_ref()
2238                .is_none_or(|selected| selected.contains(*test))
2239        })
2240        .count();
2241    let total_obligations = obligations.len();
2242    let total_limitations = limitations.len();
2243    let mut grouped =
2244        BTreeMap::<usize, (Vec<CoverageFileObligation>, Vec<CoverageFileLimitation>)>::new();
2245    for obligation in obligations {
2246        grouped
2247            .entry(obligation.line())
2248            .or_default()
2249            .0
2250            .push(obligation);
2251    }
2252    for limitation in limitations {
2253        grouped
2254            .entry(limitation.line)
2255            .or_default()
2256            .1
2257            .push(limitation);
2258    }
2259    let gap_lines = grouped
2260        .into_iter()
2261        .map(|(line, (obligations, limitations))| {
2262            let source = obligations.iter().find_map(obligation_source).or_else(|| {
2263                limitations
2264                    .iter()
2265                    .find_map(|value| compact_source(&value.source))
2266            });
2267            // An MC/DC condition can be evaluated without anything on its own
2268            // line being recorded, so only branch and point gaps can say a
2269            // line never ran.
2270            let never_ran = !observed_lines.contains(&line)
2271                && !obligations.is_empty()
2272                && obligations
2273                    .iter()
2274                    .all(|value| !matches!(value, CoverageFileObligation::Mcdc(_)));
2275            let state = if never_ran
2276                || obligations
2277                    .iter()
2278                    .any(|value| matches!(value, CoverageFileObligation::Line(_)))
2279            {
2280                "missing"
2281            } else if !obligations.is_empty() || observed_lines.contains(&line) {
2282                // A line that ran with part of it outside measurement ran part
2283                // of the way, as its line view says.
2284                "part"
2285            } else {
2286                "limited"
2287            };
2288            CoverageFileGapLine {
2289                line,
2290                state: state.into(),
2291                source,
2292                obligations,
2293                limitations,
2294            }
2295        })
2296        .collect::<Vec<_>>();
2297    let total_gap_lines = gap_lines.len();
2298    let selected_gap_lines = gap_lines
2299        .into_iter()
2300        .skip(options.offset)
2301        .take(options.limit)
2302        .collect::<Vec<_>>();
2303    let returned = selected_gap_lines.len();
2304    Ok((
2305        CoverageFileDetailData {
2306            run: options.run.into(),
2307            filters: query_filters(options.view, options.kind, options.runner),
2308            file,
2309            metric: options.metric,
2310            counts: CoverageFileCounts {
2311                total_lines,
2312                covered_lines: total_lines - uncovered_lines.len(),
2313                uncovered_lines: uncovered_lines.len(),
2314                uncovered_statements: statements.len(),
2315                uncovered_functions: functions.len(),
2316                missing_branches: branches.len(),
2317                missing_mcdc_conditions: mcdc.len(),
2318                measurement_limitations: total_limitations,
2319            },
2320            total_tests,
2321            total_obligations,
2322            total_gap_lines,
2323            gap_lines: selected_gap_lines,
2324            total_limitations,
2325            assertions: None,
2326        },
2327        pagination(options.offset, options.limit, returned, total_gap_lines),
2328    ))
2329}
2330
2331#[derive(Debug, Clone, PartialEq, Serialize)]
2332pub struct CoverageDiffDelta {
2333    #[serde(serialize_with = "crate::coverage_analysis::serialize_javascript_number")]
2334    pub lines: f64,
2335    #[serde(serialize_with = "crate::coverage_analysis::serialize_javascript_number")]
2336    pub branches: f64,
2337    #[serde(serialize_with = "crate::coverage_analysis::serialize_javascript_number")]
2338    pub mcdc: f64,
2339}
2340
2341#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
2342#[serde(rename_all = "camelCase")]
2343pub struct CoverageDiffSide {
2344    pub line_count: usize,
2345    pub branch_count: usize,
2346    pub mcdc_count: usize,
2347    pub lines: Vec<String>,
2348    pub branches: Vec<String>,
2349    pub mcdc: Vec<String>,
2350}
2351
2352/// How much of a comparison falls in one file.
2353#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
2354#[serde(rename_all = "camelCase")]
2355pub struct CoverageDiffFile {
2356    pub file: String,
2357    pub lost: CoverageDiffCounts,
2358    pub gained: CoverageDiffCounts,
2359}
2360
2361#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
2362pub struct CoverageDiffCounts {
2363    pub lines: usize,
2364    pub branches: usize,
2365    pub mcdc: usize,
2366}
2367
2368impl CoverageDiffCounts {
2369    fn total(&self) -> usize {
2370        self.lines + self.branches + self.mcdc
2371    }
2372}
2373
2374/// The files of a comparison that are listed whole; the rest are counted.
2375const DIFF_FILES: usize = 20;
2376
2377#[derive(Debug, Clone, PartialEq, Serialize)]
2378#[serde(rename_all = "camelCase")]
2379pub struct CoverageDiffData {
2380    pub filters: CoverageQueryFilters,
2381    pub older: String,
2382    pub newer: String,
2383    pub delta: CoverageDiffDelta,
2384    pub gained: CoverageDiffSide,
2385    pub lost: CoverageDiffSide,
2386    /// How many files the difference is in, and the ones with the most lost,
2387    /// whatever page of items is shown. A list of 79 rows does not say that
2388    /// 45 of its 63 lost lines are one component that did not render.
2389    pub files: usize,
2390    pub by_file: Vec<CoverageDiffFile>,
2391    /// Whether what the two runs measured differs, from their fingerprints.
2392    /// Absent where a run has none.
2393    #[serde(skip_serializing_if = "Option::is_none")]
2394    pub inputs: Option<CoverageDiffInputs>,
2395}
2396
2397/// What two runs of a comparison had in common. With the same source, a
2398/// difference between them is not a change in the code: two e2e runs of one
2399/// commit read "lost: 11 lines, 31 branches" with nothing to say so.
2400#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
2401#[serde(rename_all = "camelCase")]
2402pub struct CoverageDiffInputs {
2403    /// The measured source, its dependencies or its configuration.
2404    pub source_changed: bool,
2405    pub tests_changed: bool,
2406}
2407
2408#[derive(Debug, Clone, Copy)]
2409pub struct CoverageDiffQueryOptions<'a> {
2410    pub older_run: &'a str,
2411    pub newer_run: &'a str,
2412    pub view: CoverageViewId,
2413    pub kind: Option<&'a str>,
2414    pub runner: Option<&'a str>,
2415    pub offset: usize,
2416    pub limit: usize,
2417}
2418
2419struct DiffSnapshot {
2420    summary: CoverageSummary,
2421    lines: BTreeSet<String>,
2422    branches: HashMap<String, String>,
2423    mcdc: HashMap<String, String>,
2424}
2425
2426fn diff_snapshot(
2427    index: &CoverageIndex<'_>,
2428    view: CoverageViewId,
2429) -> Result<DiffSnapshot, QueryError> {
2430    let summary = index.summary(view)?;
2431    let lines = index
2432        .lines(view)?
2433        .into_iter()
2434        .filter(|line| line.covered)
2435        .map(|line| format!("{}:{}", line.file, line.line))
2436        .collect();
2437    let branches = index
2438        .hit_metadata(view)?
2439        .into_iter()
2440        .filter(|metadata| metadata.obligation == "branch" && !metadata.tests.is_empty())
2441        .map(|metadata| {
2442            let parent = metadata
2443                .parent_id
2444                .ok_or(QueryError::InvalidRecordSelection)?;
2445            Ok((
2446                format!("{parent}:{}", metadata.id),
2447                format!(
2448                    "{}:{} {}",
2449                    metadata.file,
2450                    metadata.line,
2451                    metadata.alternative.unwrap_or_default()
2452                ),
2453            ))
2454        })
2455        .collect::<Result<HashMap<_, _>, QueryError>>()?;
2456    let mcdc = index
2457        .decision_details(view)?
2458        .into_iter()
2459        .flat_map(|decision| {
2460            decision
2461                .conditions
2462                .into_iter()
2463                .filter(|condition| condition.covered)
2464                .map(move |condition| {
2465                    (
2466                        format!("{}:c{}", decision.meta.id, condition.index),
2467                        format!(
2468                            "{}:{} C{} {}",
2469                            decision.meta.file,
2470                            decision.meta.line,
2471                            condition.index + 1,
2472                            condition.source
2473                        ),
2474                    )
2475                })
2476        })
2477        .collect();
2478    Ok(DiffSnapshot {
2479        summary,
2480        lines,
2481        branches,
2482        mcdc,
2483    })
2484}
2485
2486fn js_string_cmp(left: &str, right: &str) -> std::cmp::Ordering {
2487    left.encode_utf16().cmp(right.encode_utf16())
2488}
2489
2490fn rounded_delta(newer: f64, older: f64) -> f64 {
2491    ((newer - older) * 100.0).round() / 100.0
2492}
2493
2494pub fn coverage_diff_query(
2495    older: &CoverageIndex<'_>,
2496    newer: &CoverageIndex<'_>,
2497    options: CoverageDiffQueryOptions<'_>,
2498) -> Result<(CoverageDiffData, AgentPagination), QueryError> {
2499    if options.limit == 0 {
2500        return Err(QueryError::InvalidPagination);
2501    }
2502    let older = diff_snapshot(older, options.view)?;
2503    let newer = diff_snapshot(newer, options.view)?;
2504    let mut gained_lines = newer
2505        .lines
2506        .difference(&older.lines)
2507        .cloned()
2508        .collect::<Vec<_>>();
2509    let mut lost_lines = older
2510        .lines
2511        .difference(&newer.lines)
2512        .cloned()
2513        .collect::<Vec<_>>();
2514    let mut gained_branches = newer
2515        .branches
2516        .iter()
2517        .filter(|(id, _)| !older.branches.contains_key(*id))
2518        .map(|(_, label)| label.clone())
2519        .collect::<Vec<_>>();
2520    let mut lost_branches = older
2521        .branches
2522        .iter()
2523        .filter(|(id, _)| !newer.branches.contains_key(*id))
2524        .map(|(_, label)| label.clone())
2525        .collect::<Vec<_>>();
2526    let mut gained_mcdc = newer
2527        .mcdc
2528        .iter()
2529        .filter(|(id, _)| !older.mcdc.contains_key(*id))
2530        .map(|(_, label)| label.clone())
2531        .collect::<Vec<_>>();
2532    let mut lost_mcdc = older
2533        .mcdc
2534        .iter()
2535        .filter(|(id, _)| !newer.mcdc.contains_key(*id))
2536        .map(|(_, label)| label.clone())
2537        .collect::<Vec<_>>();
2538    for values in [
2539        &mut gained_lines,
2540        &mut lost_lines,
2541        &mut gained_branches,
2542        &mut lost_branches,
2543        &mut gained_mcdc,
2544        &mut lost_mcdc,
2545    ] {
2546        values.sort_by(|left, right| js_string_cmp(left, right));
2547    }
2548    // Every label starts `file:line`, and a file's path holds no space.
2549    let mut by_file = BTreeMap::<&str, (CoverageDiffCounts, CoverageDiffCounts)>::new();
2550    for (values, count) in [
2551        (&lost_lines, 0),
2552        (&lost_branches, 1),
2553        (&lost_mcdc, 2),
2554        (&gained_lines, 3),
2555        (&gained_branches, 4),
2556        (&gained_mcdc, 5),
2557    ] {
2558        for label in values {
2559            let place = label.split(' ').next().unwrap_or(label);
2560            let file = place.rsplit_once(':').map_or(place, |(file, _)| file);
2561            let (lost, gained) = by_file.entry(file).or_default();
2562            let side = if count < 3 { lost } else { gained };
2563            match count % 3 {
2564                0 => side.lines += 1,
2565                1 => side.branches += 1,
2566                _ => side.mcdc += 1,
2567            }
2568        }
2569    }
2570    let files = by_file.len();
2571    let mut by_file = by_file
2572        .into_iter()
2573        .map(|(file, (lost, gained))| CoverageDiffFile {
2574            file: file.to_owned(),
2575            lost,
2576            gained,
2577        })
2578        .collect::<Vec<_>>();
2579    by_file.sort_by(|left, right| {
2580        (right.lost.total(), right.gained.total())
2581            .cmp(&(left.lost.total(), left.gained.total()))
2582            .then_with(|| js_string_cmp(&left.file, &right.file))
2583    });
2584    by_file.truncate(DIFF_FILES);
2585    let total = [
2586        gained_lines.len(),
2587        gained_branches.len(),
2588        gained_mcdc.len(),
2589        lost_lines.len(),
2590        lost_branches.len(),
2591        lost_mcdc.len(),
2592    ]
2593    .into_iter()
2594    .max()
2595    .unwrap_or(0);
2596    let page = |values: &[String]| {
2597        values
2598            .iter()
2599            .skip(options.offset)
2600            .take(options.limit)
2601            .cloned()
2602            .collect::<Vec<_>>()
2603    };
2604    let gained = CoverageDiffSide {
2605        line_count: gained_lines.len(),
2606        branch_count: gained_branches.len(),
2607        mcdc_count: gained_mcdc.len(),
2608        lines: page(&gained_lines),
2609        branches: page(&gained_branches),
2610        mcdc: page(&gained_mcdc),
2611    };
2612    let lost = CoverageDiffSide {
2613        line_count: lost_lines.len(),
2614        branch_count: lost_branches.len(),
2615        mcdc_count: lost_mcdc.len(),
2616        lines: page(&lost_lines),
2617        branches: page(&lost_branches),
2618        mcdc: page(&lost_mcdc),
2619    };
2620    let returned = [
2621        gained.lines.len(),
2622        gained.branches.len(),
2623        gained.mcdc.len(),
2624        lost.lines.len(),
2625        lost.branches.len(),
2626        lost.mcdc.len(),
2627    ]
2628    .into_iter()
2629    .max()
2630    .unwrap_or(0);
2631    Ok((
2632        CoverageDiffData {
2633            filters: query_filters(options.view, options.kind, options.runner),
2634            older: options.older_run.into(),
2635            newer: options.newer_run.into(),
2636            delta: CoverageDiffDelta {
2637                lines: rounded_delta(
2638                    newer.summary.lines.percentage,
2639                    older.summary.lines.percentage,
2640                ),
2641                branches: rounded_delta(
2642                    newer.summary.branches.percentage,
2643                    older.summary.branches.percentage,
2644                ),
2645                mcdc: rounded_delta(
2646                    newer.summary.condition_coverage_pct,
2647                    older.summary.condition_coverage_pct,
2648                ),
2649            },
2650            gained,
2651            lost,
2652            files,
2653            by_file,
2654            inputs: None,
2655        },
2656        pagination(options.offset, options.limit, returned, total),
2657    ))
2658}
2659
2660pub fn coverage_scope_query(
2661    index: &CoverageIndex<'_>,
2662    options: CoverageScopeQueryOptions<'_>,
2663) -> Result<(CoverageScopeData, AgentPagination), QueryError> {
2664    if options.limit == 0 {
2665        return Err(QueryError::InvalidPagination);
2666    }
2667    let projection = index.projection(options.view, options.kind, options.runner)?;
2668    let scope = projection
2669        .source_scope
2670        .ok_or(QueryError::ScopeUnavailable)?;
2671    let mut entries = index.scope_entries(options.view)?;
2672    entries.sort_by(|left, right| {
2673        scope_status_rank(&left.status)
2674            .cmp(&scope_status_rank(&right.status))
2675            .then_with(|| left.file.cmp(&right.file))
2676    });
2677    let groups = scope_groups(&entries);
2678    let unclassified = unclassified_source(&scope.roots, &entries);
2679    let total = entries.len();
2680    let selected = entries
2681        .into_iter()
2682        .skip(options.offset)
2683        .take(options.limit)
2684        .collect::<Vec<_>>();
2685    let returned = selected.len();
2686    Ok((
2687        CoverageScopeData {
2688            run: options.run.into(),
2689            filters: CoverageQueryFilters {
2690                outcome: match options.view {
2691                    CoverageViewId::All => "all",
2692                    CoverageViewId::Passed => "passed",
2693                    CoverageViewId::Failed => "failed",
2694                }
2695                .into(),
2696                kind: options.kind.map(str::to_owned),
2697                runner: options.runner.map(str::to_owned),
2698            },
2699            kind: scope.kind,
2700            language: scope.language,
2701            model: scope.model,
2702            mode: scope.mode,
2703            roots: scope.roots,
2704            unit: scope.unit,
2705            measurement_complete: scope.measurement_complete,
2706            counts: ScopeCounts {
2707                included: scope.included,
2708                excluded: scope.excluded,
2709                ambiguous: scope.ambiguous,
2710            },
2711            measurement: projection.measurement,
2712            groups,
2713            unclassified_source: unclassified,
2714            entries: selected,
2715        },
2716        pagination(options.offset, options.limit, returned, total),
2717    ))
2718}
2719
2720pub fn coverage_summary_query(
2721    index: &CoverageIndex<'_>,
2722    options: CoverageSummaryQueryOptions<'_>,
2723) -> Result<CoverageSummaryData, QueryError> {
2724    let projection = index.projection(options.view, options.kind, options.runner)?;
2725    let mut test_kind_sources = BTreeMap::new();
2726    for test in index.test_summaries(options.view)?.iter().filter(|t| {
2727        t.role == "test"
2728            && options.kind.is_none_or(|k| t.provenance.kind == k)
2729            && options.runner.is_none_or(|r| t.provenance.runner == r)
2730    }) {
2731        *test_kind_sources
2732            .entry(test.provenance.source.clone())
2733            .or_insert(0) += 1;
2734    }
2735    let mut diagnostics = Vec::new();
2736    let mut transport_blockers = 0usize;
2737    if projection.empty_evidence_tests > 0 {
2738        diagnostics.push(CoverageDiagnostic {
2739            code: "TEST_EVIDENCE_MISSING".into(),
2740            severity: "warning".into(),
2741            message: format!(
2742                "{} test(s) recorded assertion phases but attributed zero coverage evidence; possible causes include checks of uninstrumented data, shared setup, lost async context or missing probe transport. First: {}",
2743                projection.empty_evidence_tests,
2744                projection.first_empty_evidence_test.as_deref().unwrap_or("unknown")
2745            ),
2746        });
2747    }
2748    if let Some(transport) = &projection.transport {
2749        if transport.corrupt_records > 0 {
2750            transport_blockers += 1;
2751            diagnostics.push(CoverageDiagnostic {
2752                code: "CORRUPT_EVIDENCE_RECORDS".into(),
2753                severity: "error".into(),
2754                message: format!(
2755                    "{} malformed evidence record(s) in {} file(s) were excluded; coverage is incomplete.",
2756                    transport.corrupt_records, transport.corrupt_files
2757                ),
2758            });
2759        }
2760        if transport.remote_launches > 0
2761            && transport.scoped_server_records == 0
2762            && transport.background_server_records == 0
2763            && projection.attribution.server_explicit == 0
2764            && projection.attribution.server_fallback == 0
2765        {
2766            transport_blockers += 1;
2767            diagnostics.push(CoverageDiagnostic {
2768                code: "REMOTE_SERVER_EVIDENCE_MISSING".into(),
2769                severity: "error".into(),
2770                message: "Remote launches were supervised, but neither scoped nor background server evidence returned. Supercov refuses to describe this measurement as complete.".into(),
2771            });
2772        }
2773    }
2774    let coverage_by_kind = index.dimensions(options.view, CoverageDimension::Kind)?;
2775    let coverage_by_runner = index.dimensions(options.view, CoverageDimension::Runner)?;
2776    let coverage_by_tests = if options.kind.is_none() && options.runner.is_none() {
2777        index
2778            .dimensions(options.view, CoverageDimension::Tests)?
2779            .into_iter()
2780            .next()
2781            .filter(|tests| tests.tests + tests.setups > 0)
2782            .map(|tests| tests.summary)
2783    } else {
2784        None
2785    };
2786    let other_e2e_kinds = coverage_by_kind
2787        .iter()
2788        .filter(|dimension| dimension.tests > 0)
2789        .filter_map(|dimension| dimension.kind.clone())
2790        .filter(|kind| kind != "e2e")
2791        .collect::<Vec<_>>();
2792    let e2e_observed = coverage_by_kind
2793        .iter()
2794        .any(|dimension| dimension.tests > 0 && dimension.kind.as_deref() == Some("e2e"));
2795    let e2e_gap_context = if options.kind.is_none()
2796        && options.runner.is_none()
2797        && e2e_observed
2798        && !other_e2e_kinds.is_empty()
2799    {
2800        let mut covered_elsewhere = IndexedGapDimensions {
2801            lines: 0,
2802            statements: 0,
2803            functions: 0,
2804            branches: 0,
2805            mcdc_conditions: 0,
2806        };
2807        let mut uncovered_everywhere = covered_elsewhere.clone();
2808        for gap in index.file_gaps(options.view, Some("e2e"), None)? {
2809            covered_elsewhere.lines += gap.covered_by_other_tests.lines;
2810            covered_elsewhere.statements += gap.covered_by_other_tests.statements;
2811            covered_elsewhere.functions += gap.covered_by_other_tests.functions;
2812            covered_elsewhere.branches += gap.covered_by_other_tests.branches;
2813            covered_elsewhere.mcdc_conditions += gap.covered_by_other_tests.mcdc_conditions;
2814            uncovered_everywhere.lines += gap.uncovered_everywhere.lines;
2815            uncovered_everywhere.statements += gap.uncovered_everywhere.statements;
2816            uncovered_everywhere.functions += gap.uncovered_everywhere.functions;
2817            uncovered_everywhere.branches += gap.uncovered_everywhere.branches;
2818            uncovered_everywhere.mcdc_conditions += gap.uncovered_everywhere.mcdc_conditions;
2819        }
2820        Some(CoverageKindGapContext {
2821            kind: "e2e".into(),
2822            other_kinds: other_e2e_kinds,
2823            covered_elsewhere,
2824            uncovered_everywhere,
2825        })
2826    } else {
2827        None
2828    };
2829    let filters = CoverageQueryFilters {
2830        outcome: match options.view {
2831            CoverageViewId::All => "all",
2832            CoverageViewId::Passed => "passed",
2833            CoverageViewId::Failed => "failed",
2834        }
2835        .into(),
2836        kind: options.kind.map(str::to_owned),
2837        runner: options.runner.map(str::to_owned),
2838    };
2839    let mut measurement = projection.measurement.clone();
2840    if transport_blockers > 0 {
2841        measurement.complete = false;
2842        measurement.limitations = measurement.limitations.saturating_add(transport_blockers);
2843        measurement.blocking = measurement.blocking.saturating_add(transport_blockers);
2844    }
2845    let structurally_complete = projection.summary.coverage_complete && measurement.complete;
2846    let complete = options.view == CoverageViewId::Passed
2847        && options.valid
2848        && !options.stale
2849        && structurally_complete;
2850    Ok(CoverageSummaryData {
2851        test_kind_sources,
2852        assertion_coverage: None,
2853        run: options.run.into(),
2854        command: Vec::new(),
2855        merged_across: Vec::new(),
2856        companions: Vec::new(),
2857        hints: Vec::new(),
2858        workspace: None,
2859        filters,
2860        model: index.model()?,
2861        generated_at: projection.generated_at,
2862        valid: options.valid,
2863        test_exit_code: options.test_exit_code,
2864        stale: options.stale,
2865        stale_reasons: options.stale_reasons,
2866        structurally_complete,
2867        complete,
2868        coverage: projection.summary,
2869        measurement,
2870        coverage_by_kind,
2871        coverage_by_tests,
2872        e2e_gap_context,
2873        coverage_by_runner,
2874        attribution: projection.attribution,
2875        test_attribution: {
2876            let mut counts = TestAttributionCounts::default();
2877            for test in index
2878                .test_summaries(options.view)?
2879                .iter()
2880                .filter(|test| test.role == "test")
2881            {
2882                match test.attribution.as_str() {
2883                    crate::coverage_report::ATTRIBUTION_RUN_WIDE => counts.run_wide += 1,
2884                    crate::coverage_report::ATTRIBUTION_PARTIAL => counts.partial += 1,
2885                    crate::coverage_report::ATTRIBUTION_BY_TIME => counts.by_time += 1,
2886                    _ => counts.exact += 1,
2887                }
2888            }
2889            counts
2890        },
2891        transport: projection.transport,
2892        diagnostics,
2893        confidence: (options.kind.is_none() && options.runner.is_none())
2894            .then_some(projection.confidence),
2895        files_with_gaps: projection.files_with_gaps,
2896        files_with_coverage_gaps: projection.files_with_coverage_gaps,
2897        files_with_measurement_limitations: projection.measurement.files,
2898        tests: projection.tests,
2899        setups: projection.setups,
2900        test_outcomes: projection.test_outcomes,
2901        unclassified_source: match &projection.source_scope {
2902            Some(scope) if scope.ambiguous > 0 => {
2903                unclassified_source(&scope.roots, &index.scope_entries(options.view)?)
2904            }
2905            _ => None,
2906        },
2907        source_scope: projection.source_scope,
2908    })
2909}
2910
2911#[derive(Debug, Clone, PartialEq)]
2912pub enum CoverageDimensionQueryData {
2913    Kinds(CoverageKindsData),
2914    Runners(CoverageRunnersData),
2915}
2916
2917/// Coverage of one directory: what its files have to cover, how much of it
2918/// is covered, and how much of it each kind of test covers.
2919#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
2920#[serde(rename_all = "camelCase")]
2921pub struct CoverageArea {
2922    pub directory: String,
2923    pub files: usize,
2924    pub totals: IndexedGapDimensions,
2925    pub covered: IndexedGapDimensions,
2926    pub by_kind: Vec<CoverageAreaKind>,
2927    /// What the tests of every kind cover together. The rest of `covered`
2928    /// ran while no test was running. Absent where the query names a kind or
2929    /// a runner, and for a run stored before this was recorded.
2930    #[serde(skip_serializing_if = "Option::is_none")]
2931    pub covered_by_tests: Option<IndexedGapDimensions>,
2932}
2933
2934#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
2935#[serde(rename_all = "camelCase")]
2936pub struct CoverageAreaKind {
2937    pub kind: String,
2938    pub covered: IndexedGapDimensions,
2939}
2940
2941#[derive(Debug, Clone, PartialEq, Serialize)]
2942#[serde(rename_all = "camelCase")]
2943pub struct CoverageAreasData {
2944    pub run: String,
2945    pub filters: CoverageQueryFilters,
2946    pub metric: MinimizeMetric,
2947    pub depth: usize,
2948    /// The kinds each area is split by, in the order of `byKind`. Empty when
2949    /// the query already names one kind or runner.
2950    pub kinds: Vec<String>,
2951    pub areas: Vec<CoverageArea>,
2952}
2953
2954#[derive(Debug, Clone, Copy)]
2955pub struct CoverageAreasQueryOptions<'a> {
2956    pub run: &'a str,
2957    pub view: CoverageViewId,
2958    pub metric: MinimizeMetric,
2959    pub kind: Option<&'a str>,
2960    pub runner: Option<&'a str>,
2961    pub depth: usize,
2962    pub offset: usize,
2963    pub limit: usize,
2964}
2965
2966/// The directory a file is counted under: its first `depth` folders, or all
2967/// of them when it has fewer. A file at the top of the project is in `.`.
2968fn area_directory(file: &str, depth: usize) -> String {
2969    let mut folders = file.split('/').collect::<Vec<_>>();
2970    folders.pop();
2971    if folders.is_empty() {
2972        ".".into()
2973    } else {
2974        folders[..folders.len().min(depth)].join("/")
2975    }
2976}
2977
2978fn add_dimensions(sum: &mut IndexedGapDimensions, value: &IndexedGapDimensions) {
2979    sum.lines += value.lines;
2980    sum.statements += value.statements;
2981    sum.functions += value.functions;
2982    sum.branches += value.branches;
2983    sum.mcdc_conditions += value.mcdc_conditions;
2984}
2985
2986const NO_DIMENSIONS: IndexedGapDimensions = IndexedGapDimensions {
2987    lines: 0,
2988    statements: 0,
2989    functions: 0,
2990    branches: 0,
2991    mcdc_conditions: 0,
2992};
2993
2994/// One metric of a file's or a directory's counts; `all` reads as lines.
2995pub fn dimension_for_metric(dimensions: &IndexedGapDimensions, metric: MinimizeMetric) -> usize {
2996    match metric {
2997        MinimizeMetric::All | MinimizeMetric::Lines => dimensions.lines,
2998        MinimizeMetric::Statements => dimensions.statements,
2999        MinimizeMetric::Functions => dimensions.functions,
3000        MinimizeMetric::Branches => dimensions.branches,
3001        MinimizeMetric::Mcdc => dimensions.mcdc_conditions,
3002    }
3003}
3004
3005/// Coverage by directory, split by test kind. "Where is the coverage" was the
3006/// first question after a full run, and answering it took an LCOV export and
3007/// a script: listings named files only and carried no totals.
3008pub fn coverage_areas_query(
3009    index: &CoverageIndex<'_>,
3010    options: CoverageAreasQueryOptions<'_>,
3011) -> Result<(CoverageAreasData, AgentPagination), QueryError> {
3012    if options.limit == 0 || options.depth == 0 {
3013        return Err(QueryError::InvalidPagination);
3014    }
3015    let files = index.file_gaps(options.view, options.kind, options.runner)?;
3016    if (options.kind.is_some() || options.runner.is_some()) && files.is_empty() {
3017        return Err(QueryError::TestFilterEmpty {
3018            kind: options.kind.map(str::to_owned),
3019            runner: options.runner.map(str::to_owned),
3020        });
3021    }
3022    let kinds = if options.kind.is_none() && options.runner.is_none() {
3023        index
3024            .dimensions(options.view, CoverageDimension::Kind)?
3025            .into_iter()
3026            .filter(|kind| kind.tests > 0 && kind.attributed > 0)
3027            .filter_map(|kind| kind.kind)
3028            .collect::<Vec<_>>()
3029    } else {
3030        Vec::new()
3031    };
3032    let mut areas = BTreeMap::<String, CoverageArea>::new();
3033    for file in &files {
3034        let directory = area_directory(&file.file, options.depth);
3035        let area = areas
3036            .entry(directory.clone())
3037            .or_insert_with(|| CoverageArea {
3038                directory,
3039                files: 0,
3040                totals: NO_DIMENSIONS,
3041                covered: NO_DIMENSIONS,
3042                by_kind: kinds
3043                    .iter()
3044                    .map(|kind| CoverageAreaKind {
3045                        kind: kind.clone(),
3046                        covered: NO_DIMENSIONS,
3047                    })
3048                    .collect(),
3049                covered_by_tests: None,
3050            });
3051        area.files += 1;
3052        add_dimensions(&mut area.totals, &file.totals);
3053        add_dimensions(&mut area.covered, &file.covered);
3054    }
3055    for (position, kind) in kinds.iter().enumerate() {
3056        for file in index.file_gaps(options.view, Some(kind), None)? {
3057            if let Some(area) = areas.get_mut(&area_directory(&file.file, options.depth)) {
3058                add_dimensions(&mut area.by_kind[position].covered, &file.covered);
3059            }
3060        }
3061    }
3062    if !kinds.is_empty() {
3063        let tested = index.file_gaps(options.view, Some(crate::coverage_index::ALL_TESTS), None)?;
3064        if !tested.is_empty() {
3065            for area in areas.values_mut() {
3066                area.covered_by_tests = Some(NO_DIMENSIONS);
3067            }
3068            for file in tested {
3069                if let Some(sum) = areas
3070                    .get_mut(&area_directory(&file.file, options.depth))
3071                    .and_then(|area| area.covered_by_tests.as_mut())
3072                {
3073                    add_dimensions(sum, &file.covered);
3074                }
3075            }
3076        }
3077    }
3078    let mut areas = areas.into_values().collect::<Vec<_>>();
3079    areas.sort_by(|left, right| {
3080        dimension_for_metric(&right.totals, options.metric)
3081            .cmp(&dimension_for_metric(&left.totals, options.metric))
3082            .then_with(|| left.directory.cmp(&right.directory))
3083    });
3084    let total = areas.len();
3085    let page = areas
3086        .into_iter()
3087        .skip(options.offset)
3088        .take(options.limit)
3089        .collect::<Vec<_>>();
3090    let returned = page.len();
3091    Ok((
3092        CoverageAreasData {
3093            run: options.run.into(),
3094            filters: CoverageQueryFilters {
3095                outcome: match options.view {
3096                    CoverageViewId::All => "all",
3097                    CoverageViewId::Passed => "passed",
3098                    CoverageViewId::Failed => "failed",
3099                }
3100                .into(),
3101                kind: options.kind.map(str::to_owned),
3102                runner: options.runner.map(str::to_owned),
3103            },
3104            metric: options.metric,
3105            depth: options.depth,
3106            kinds,
3107            areas: page,
3108        },
3109        pagination(options.offset, options.limit, returned, total),
3110    ))
3111}
3112
3113/// A test that made assertions and has no line, hit or decision to its name.
3114#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
3115#[serde(rename_all = "camelCase")]
3116pub struct TestWithoutEvidence {
3117    pub id: String,
3118    pub name: String,
3119    #[serde(skip_serializing_if = "Option::is_none")]
3120    pub file: Option<String>,
3121    pub kind: String,
3122    pub runner: String,
3123    pub outcome: String,
3124    pub assertions: usize,
3125    /// Whether the test's file imports any measured source, where Supercov
3126    /// reads its imports and the test runs outside a browser: one that
3127    /// imports none can only have checked test code, mocks or fixtures.
3128    #[serde(skip_serializing_if = "Option::is_none")]
3129    pub imports_measured: Option<bool>,
3130}
3131
3132#[derive(Debug, Clone, PartialEq, Serialize)]
3133#[serde(rename_all = "camelCase")]
3134pub struct TestsWithoutEvidenceData {
3135    pub run: String,
3136    pub filters: CoverageQueryFilters,
3137    pub tests: Vec<TestWithoutEvidence>,
3138}
3139
3140#[derive(Debug, Clone, Copy)]
3141pub struct TestsWithoutEvidenceOptions<'a> {
3142    pub run: &'a str,
3143    pub view: CoverageViewId,
3144    pub kind: Option<&'a str>,
3145    pub runner: Option<&'a str>,
3146    pub offset: usize,
3147    pub limit: usize,
3148}
3149
3150/// Every test the run's summary counts under "made assertions, but Supercov
3151/// received no source-coverage evidence". The summary names one of them; 63
3152/// tests behind one example could not be followed up.
3153pub fn tests_without_evidence_query(
3154    index: &CoverageIndex<'_>,
3155    options: TestsWithoutEvidenceOptions<'_>,
3156) -> Result<(TestsWithoutEvidenceData, AgentPagination), QueryError> {
3157    if options.limit == 0 {
3158        return Err(QueryError::InvalidPagination);
3159    }
3160    let mut assertions = BTreeMap::<String, usize>::new();
3161    for phase in index.phase_summaries(options.view)? {
3162        *assertions.entry(phase.test).or_insert(0) += 1;
3163    }
3164    let mut tests = index
3165        .test_details(options.view)?
3166        .into_iter()
3167        .filter(|test| {
3168            test.summary.role == "test"
3169                && test.lines.is_empty()
3170                && test.hits.is_empty()
3171                && test.decisions.is_empty()
3172                && options
3173                    .kind
3174                    .is_none_or(|kind| test.summary.provenance.kind == kind)
3175                && options
3176                    .runner
3177                    .is_none_or(|runner| test.summary.provenance.runner == runner)
3178        })
3179        .filter_map(|test| {
3180            let assertions = *assertions.get(&test.summary.id)?;
3181            Some(TestWithoutEvidence {
3182                id: test.summary.id,
3183                name: test.summary.name,
3184                file: test.summary.file,
3185                kind: test.summary.provenance.kind,
3186                runner: test.summary.provenance.runner,
3187                outcome: test.summary.outcome,
3188                assertions,
3189                imports_measured: None,
3190            })
3191        })
3192        .collect::<Vec<_>>();
3193    tests.sort_by(|left, right| {
3194        (&left.file, &left.name, &left.id).cmp(&(&right.file, &right.name, &right.id))
3195    });
3196    let total = tests.len();
3197    let page = tests
3198        .into_iter()
3199        .skip(options.offset)
3200        .take(options.limit)
3201        .collect::<Vec<_>>();
3202    let returned = page.len();
3203    Ok((
3204        TestsWithoutEvidenceData {
3205            run: options.run.into(),
3206            filters: CoverageQueryFilters {
3207                outcome: match options.view {
3208                    CoverageViewId::All => "all",
3209                    CoverageViewId::Passed => "passed",
3210                    CoverageViewId::Failed => "failed",
3211                }
3212                .into(),
3213                kind: options.kind.map(str::to_owned),
3214                runner: options.runner.map(str::to_owned),
3215            },
3216            tests: page,
3217        },
3218        pagination(options.offset, options.limit, returned, total),
3219    ))
3220}
3221
3222#[derive(Debug, Clone, PartialEq)]
3223pub enum CoverageFileQueryData {
3224    Files(CoverageFilesData),
3225    Gaps(CoverageGapsData),
3226}
3227
3228#[derive(Debug, Clone, PartialEq)]
3229pub struct CoverageFileQueryResult {
3230    pub data: CoverageFileQueryData,
3231    pub pagination: AgentPagination,
3232}
3233
3234#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
3235#[serde(rename_all = "lowercase")]
3236pub enum DecisionSort {
3237    Location,
3238    Missing,
3239}
3240
3241#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
3242#[serde(rename_all = "camelCase")]
3243pub struct DecisionGapTotals {
3244    pub decisions: usize,
3245    pub decisions_with_missing_conditions: usize,
3246    pub conditions: usize,
3247    pub missing_conditions: usize,
3248}
3249
3250#[derive(Debug, Clone, PartialEq, Serialize)]
3251#[serde(rename_all = "camelCase")]
3252pub struct CoverageFileDecisionsData {
3253    pub run: String,
3254    pub filters: CoverageQueryFilters,
3255    pub file: String,
3256    pub group: String,
3257    pub sort: DecisionSort,
3258    pub totals: DecisionGapTotals,
3259    pub decisions: Vec<IndexedDecisionGap>,
3260}
3261
3262#[derive(Debug, Clone, Copy)]
3263pub struct CoverageFileDecisionsOptions<'a> {
3264    pub run: &'a str,
3265    pub view: CoverageViewId,
3266    pub kind: Option<&'a str>,
3267    pub runner: Option<&'a str>,
3268    pub file: &'a str,
3269    pub sort: DecisionSort,
3270    pub offset: usize,
3271    pub limit: usize,
3272}
3273
3274#[derive(Debug, Clone)]
3275pub struct CoverageDimensionQueryOptions<'a> {
3276    pub run: &'a str,
3277    pub view: CoverageViewId,
3278    pub dimension: CoverageDimension,
3279    pub filters: CoverageQueryFilters,
3280    pub offset: usize,
3281    pub limit: usize,
3282}
3283
3284pub fn coverage_dimension_query(
3285    index: &CoverageIndex<'_>,
3286    options: CoverageDimensionQueryOptions<'_>,
3287) -> Result<(CoverageDimensionQueryData, AgentPagination), QueryError> {
3288    let CoverageDimensionQueryOptions {
3289        run,
3290        view,
3291        dimension,
3292        filters,
3293        offset,
3294        limit,
3295    } = options;
3296    if limit == 0 {
3297        return Err(QueryError::InvalidPagination);
3298    }
3299    let values = index.dimensions(view, dimension)?;
3300    let total = values.len();
3301    let selected = values
3302        .into_iter()
3303        .skip(offset)
3304        .take(limit)
3305        .collect::<Vec<_>>();
3306    let returned = selected.len();
3307    let data = match dimension {
3308        // The tests together are one entry of the summary, never a listing.
3309        CoverageDimension::Kind | CoverageDimension::Tests => {
3310            CoverageDimensionQueryData::Kinds(CoverageKindsData {
3311                run: run.into(),
3312                filters,
3313                kinds: selected,
3314            })
3315        }
3316        CoverageDimension::Runner => CoverageDimensionQueryData::Runners(CoverageRunnersData {
3317            run: run.into(),
3318            filters,
3319            runners: selected,
3320        }),
3321    };
3322    Ok((data, pagination(offset, limit, returned, total)))
3323}
3324
3325#[derive(Debug, Clone, Copy)]
3326pub struct CoverageFileQueryOptions<'a> {
3327    pub run: &'a str,
3328    pub view: CoverageViewId,
3329    pub metric: MinimizeMetric,
3330    pub gaps_only: bool,
3331    pub kind: Option<&'a str>,
3332    pub runner: Option<&'a str>,
3333    pub offset: usize,
3334    pub limit: usize,
3335}
3336
3337fn gap_metric_value(gap: &IndexedFileGap, metric: MinimizeMetric) -> usize {
3338    match metric {
3339        MinimizeMetric::All => gap.score,
3340        MinimizeMetric::Lines => gap.uncovered_lines,
3341        MinimizeMetric::Statements => gap.uncovered_statements,
3342        MinimizeMetric::Functions => gap.uncovered_functions,
3343        MinimizeMetric::Branches => gap.missing_branches,
3344        MinimizeMetric::Mcdc => gap.missing_mcdc_conditions,
3345    }
3346}
3347
3348fn has_gap_for_metric(gap: &IndexedFileGap, metric: MinimizeMetric) -> bool {
3349    match metric {
3350        MinimizeMetric::All => {
3351            gap.uncovered_lines > 0
3352                || gap.uncovered_statements > 0
3353                || gap.uncovered_functions > 0
3354                || gap.missing_branches > 0
3355                || gap.missing_mcdc_conditions > 0
3356        }
3357        _ => gap_metric_value(gap, metric) > 0,
3358    }
3359}
3360
3361pub fn coverage_file_decisions_query(
3362    index: &CoverageIndex<'_>,
3363    options: CoverageFileDecisionsOptions<'_>,
3364) -> Result<(CoverageFileDecisionsData, AgentPagination), QueryError> {
3365    if options.limit == 0 {
3366        return Err(QueryError::InvalidPagination);
3367    }
3368    let all = index.decision_gaps(options.view, options.kind, options.runner, options.file)?;
3369    if (options.kind.is_some() || options.runner.is_some()) && all.is_empty() {
3370        // A file with no decisions is valid, so consult the file projection to
3371        // distinguish it from a nonexistent test provenance projection.
3372        if index
3373            .file_gaps(options.view, options.kind, options.runner)?
3374            .is_empty()
3375        {
3376            return Err(QueryError::TestFilterEmpty {
3377                kind: options.kind.map(str::to_owned),
3378                runner: options.runner.map(str::to_owned),
3379            });
3380        }
3381    }
3382    let totals = DecisionGapTotals {
3383        decisions: all.len(),
3384        decisions_with_missing_conditions: all
3385            .iter()
3386            .filter(|decision| decision.missing_conditions > 0)
3387            .count(),
3388        conditions: all.iter().map(|decision| decision.conditions).sum(),
3389        missing_conditions: all.iter().map(|decision| decision.missing_conditions).sum(),
3390    };
3391    let mut missing = all
3392        .into_iter()
3393        .filter(|decision| decision.missing_conditions > 0)
3394        .collect::<Vec<_>>();
3395    missing.sort_by(|left, right| match options.sort {
3396        DecisionSort::Missing => right
3397            .missing_conditions
3398            .cmp(&left.missing_conditions)
3399            .then_with(|| left.line.cmp(&right.line))
3400            .then_with(|| left.column.cmp(&right.column)),
3401        DecisionSort::Location => left
3402            .line
3403            .cmp(&right.line)
3404            .then_with(|| left.column.cmp(&right.column))
3405            .then_with(|| left.id.cmp(&right.id)),
3406    });
3407    let total = missing.len();
3408    let rows = missing
3409        .into_iter()
3410        .skip(options.offset)
3411        .take(options.limit)
3412        .collect::<Vec<_>>();
3413    let returned = rows.len();
3414    let filters = CoverageQueryFilters {
3415        outcome: match options.view {
3416            CoverageViewId::All => "all",
3417            CoverageViewId::Passed => "passed",
3418            CoverageViewId::Failed => "failed",
3419        }
3420        .into(),
3421        kind: options.kind.map(str::to_owned),
3422        runner: options.runner.map(str::to_owned),
3423    };
3424    Ok((
3425        CoverageFileDecisionsData {
3426            run: options.run.into(),
3427            filters,
3428            file: options.file.into(),
3429            group: "decision".into(),
3430            sort: options.sort,
3431            totals,
3432            decisions: rows,
3433        },
3434        pagination(options.offset, options.limit, returned, total),
3435    ))
3436}
3437
3438pub fn coverage_file_query(
3439    index: &CoverageIndex<'_>,
3440    options: CoverageFileQueryOptions<'_>,
3441) -> Result<CoverageFileQueryResult, QueryError> {
3442    let CoverageFileQueryOptions {
3443        run,
3444        view,
3445        metric,
3446        gaps_only,
3447        kind,
3448        runner,
3449        offset,
3450        limit,
3451    } = options;
3452    if limit == 0 {
3453        return Err(QueryError::InvalidPagination);
3454    }
3455    let mut files = index.file_gaps(view, kind, runner)?;
3456    if (kind.is_some() || runner.is_some()) && files.is_empty() {
3457        return Err(QueryError::TestFilterEmpty {
3458            kind: kind.map(str::to_owned),
3459            runner: runner.map(str::to_owned),
3460        });
3461    }
3462    if gaps_only {
3463        files.retain(|gap| has_gap_for_metric(gap, metric) || gap.measurement_limitations > 0);
3464    }
3465    files.sort_by(|left, right| {
3466        gap_metric_value(right, metric)
3467            .cmp(&gap_metric_value(left, metric))
3468            .then_with(|| {
3469                right
3470                    .measurement_limitations
3471                    .cmp(&left.measurement_limitations)
3472            })
3473            .then_with(|| left.file.cmp(&right.file))
3474    });
3475    let total = files.len();
3476    let page = files
3477        .into_iter()
3478        .skip(offset)
3479        .take(limit)
3480        .collect::<Vec<_>>();
3481    let page_info = pagination(offset, limit, page.len(), total);
3482    let filters = CoverageQueryFilters {
3483        outcome: match view {
3484            CoverageViewId::All => "all",
3485            CoverageViewId::Passed => "passed",
3486            CoverageViewId::Failed => "failed",
3487        }
3488        .into(),
3489        kind: kind.map(str::to_owned),
3490        runner: runner.map(str::to_owned),
3491    };
3492    let data = if gaps_only {
3493        CoverageFileQueryData::Gaps(CoverageGapsData {
3494            run: run.into(),
3495            filters,
3496            metric,
3497            gaps: page,
3498        })
3499    } else {
3500        CoverageFileQueryData::Files(CoverageFilesData {
3501            run: run.into(),
3502            filters,
3503            metric,
3504            files: page,
3505        })
3506    };
3507    Ok(CoverageFileQueryResult {
3508        data,
3509        pagination: page_info,
3510    })
3511}
3512
3513#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
3514enum ObligationMetric {
3515    Lines,
3516    Statements,
3517    Functions,
3518    Branches,
3519    Mcdc,
3520}
3521
3522impl ObligationMetric {
3523    fn selected(self, metric: MinimizeMetric) -> bool {
3524        metric == MinimizeMetric::All
3525            || matches!(
3526                (self, metric),
3527                (Self::Lines, MinimizeMetric::Lines)
3528                    | (Self::Statements, MinimizeMetric::Statements)
3529                    | (Self::Functions, MinimizeMetric::Functions)
3530                    | (Self::Branches, MinimizeMetric::Branches)
3531                    | (Self::Mcdc, MinimizeMetric::Mcdc)
3532            )
3533    }
3534
3535    fn public(self) -> MinimizeMetric {
3536        match self {
3537            Self::Lines => MinimizeMetric::Lines,
3538            Self::Statements => MinimizeMetric::Statements,
3539            Self::Functions => MinimizeMetric::Functions,
3540            Self::Branches => MinimizeMetric::Branches,
3541            Self::Mcdc => MinimizeMetric::Mcdc,
3542        }
3543    }
3544}
3545
3546#[derive(Clone)]
3547struct Obligation {
3548    id: String,
3549    metric: ObligationMetric,
3550    /// Any one complete option satisfies this obligation.
3551    options: Vec<Vec<String>>,
3552}
3553
3554struct ObligationModel {
3555    obligations: Vec<Obligation>,
3556    setup_by_file: BTreeMap<String, Vec<String>>,
3557    tests_by_file: BTreeMap<String, Vec<String>>,
3558    background: Vec<String>,
3559}
3560
3561impl ObligationModel {
3562    fn expand(&self, selected: &BTreeSet<String>) -> BTreeSet<String> {
3563        let mut expanded = selected.clone();
3564        expanded.extend(self.background.iter().cloned());
3565        for (file, setup_ids) in &self.setup_by_file {
3566            if self
3567                .tests_by_file
3568                .get(file)
3569                .is_some_and(|tests| tests.iter().any(|id| selected.contains(id)))
3570            {
3571                expanded.extend(setup_ids.iter().cloned());
3572            }
3573        }
3574        expanded
3575    }
3576}
3577
3578fn deduplicate_options(options: impl IntoIterator<Item = Vec<String>>) -> Vec<Vec<String>> {
3579    let mut unique = BTreeMap::<String, Vec<String>>::new();
3580    for mut option in options {
3581        option.sort();
3582        option.dedup();
3583        let key = option.join("\0");
3584        unique.entry(key).or_insert(option);
3585    }
3586    unique.into_values().collect()
3587}
3588
3589fn evidence_choices(
3590    ids: &[Id],
3591    tests: &HashMap<&str, &crate::coverage_report::TestCoverageResult>,
3592    candidates: &BTreeSet<String>,
3593    tests_by_file: &BTreeMap<String, Vec<String>>,
3594) -> Vec<Vec<String>> {
3595    let mut choices = Vec::new();
3596    for id in ids {
3597        let Some(test) = tests.get(id.as_str()) else {
3598            continue;
3599        };
3600        if test.role == "background" {
3601            choices.push(Vec::new());
3602        } else if test.role == "setup" {
3603            if let Some(file) = &test.file {
3604                choices.extend(
3605                    tests_by_file
3606                        .get(file)
3607                        .into_iter()
3608                        .flatten()
3609                        .map(|candidate| vec![candidate.clone()]),
3610                );
3611            }
3612        } else if candidates.contains(id.as_str()) {
3613            choices.push(vec![id.to_string()]);
3614        }
3615    }
3616    deduplicate_options(choices)
3617}
3618
3619fn build_obligations(view: &CoverageView, candidates: &BTreeSet<String>) -> ObligationModel {
3620    let tests = view
3621        .tests
3622        .iter()
3623        .map(|test| (test.id.as_str(), test))
3624        .collect::<HashMap<_, _>>();
3625    let mut tests_by_file = BTreeMap::<String, Vec<String>>::new();
3626    let mut setup_by_file = BTreeMap::<String, Vec<String>>::new();
3627    let mut background = Vec::new();
3628    for test in &view.tests {
3629        match test.role.as_str() {
3630            "test" if candidates.contains(&test.id) => {
3631                if let Some(file) = &test.file {
3632                    tests_by_file
3633                        .entry(file.clone())
3634                        .or_default()
3635                        .push(test.id.clone());
3636                }
3637            }
3638            "setup" => {
3639                if let Some(file) = &test.file {
3640                    setup_by_file
3641                        .entry(file.clone())
3642                        .or_default()
3643                        .push(test.id.clone());
3644                }
3645            }
3646            "background" => background.push(test.id.clone()),
3647            _ => {}
3648        }
3649    }
3650    let choices = |ids: &[Id]| evidence_choices(ids, &tests, candidates, &tests_by_file);
3651    let mut obligations = Vec::new();
3652    let mut unique_lines = BTreeMap::new();
3653    for line in &view.lines {
3654        unique_lines.insert((line.file.as_str(), line.line), line);
3655    }
3656    for ((file, line), result) in unique_lines {
3657        obligations.push(Obligation {
3658            id: format!("line:{file}:{line}"),
3659            metric: ObligationMetric::Lines,
3660            options: choices(&result.tests),
3661        });
3662    }
3663    for point in &view.points {
3664        let (kind, metric) = match point.meta.kind {
3665            crate::coverage_analysis::PointKind::Statement => {
3666                ("statement", ObligationMetric::Statements)
3667            }
3668            crate::coverage_analysis::PointKind::Function => {
3669                ("function", ObligationMetric::Functions)
3670            }
3671        };
3672        obligations.push(Obligation {
3673            id: format!("{kind}:{}", point.meta.id),
3674            metric,
3675            options: choices(&point.tests),
3676        });
3677    }
3678    for branch in &view.branches {
3679        for alternative in &branch.alternatives {
3680            obligations.push(Obligation {
3681                id: format!("branch:{}:{}", branch.meta.id, alternative.id),
3682                metric: ObligationMetric::Branches,
3683                options: choices(&alternative.tests),
3684            });
3685        }
3686    }
3687    for decision in &view.decisions {
3688        for condition in 0..decision.meta.conditions.len() {
3689            let mut options = Vec::new();
3690            for left in 0..decision.vector_observations.len() {
3691                for right in (left + 1)..decision.vector_observations.len() {
3692                    let first = &decision.vector_observations[left];
3693                    let second = &decision.vector_observations[right];
3694                    if !is_independence_pair(&first.vector, &second.vector, condition) {
3695                        continue;
3696                    }
3697                    for first_choice in choices(&first.tests) {
3698                        for second_choice in choices(&second.tests) {
3699                            let mut combined = first_choice.clone();
3700                            combined.extend(second_choice);
3701                            options.push(combined);
3702                        }
3703                    }
3704                }
3705            }
3706            obligations.push(Obligation {
3707                id: format!("mcdc:{}:{condition}", decision.meta.id),
3708                metric: ObligationMetric::Mcdc,
3709                options: deduplicate_options(options),
3710            });
3711        }
3712    }
3713    ObligationModel {
3714        obligations,
3715        setup_by_file,
3716        tests_by_file,
3717        background,
3718    }
3719}
3720
3721fn percentage(metric: ObligationMetric, summary: &CoverageSummary) -> f64 {
3722    match metric {
3723        ObligationMetric::Lines => summary.lines.percentage,
3724        ObligationMetric::Statements => summary.statements.percentage,
3725        ObligationMetric::Functions => summary.functions.percentage,
3726        ObligationMetric::Branches => summary.branches.percentage,
3727        ObligationMetric::Mcdc => summary.condition_coverage_pct,
3728    }
3729}
3730
3731fn obligation_satisfied(obligation: &Obligation, selected: &BTreeSet<String>) -> bool {
3732    obligation
3733        .options
3734        .iter()
3735        .any(|option| option.iter().all(|test| selected.contains(test)))
3736}
3737
3738struct Search<'a> {
3739    obligations: &'a [Obligation],
3740    skip_limits: BTreeMap<ObligationMetric, usize>,
3741    best: BTreeSet<String>,
3742    explored_states: usize,
3743    max_states: usize,
3744    seen: BTreeSet<String>,
3745    candidate_tests: usize,
3746    target: f64,
3747    metric: MinimizeMetric,
3748}
3749
3750impl Search<'_> {
3751    fn visit(
3752        &mut self,
3753        selected: BTreeSet<String>,
3754        skipped: BTreeSet<String>,
3755        skipped_by_metric: BTreeMap<ObligationMetric, usize>,
3756    ) -> Result<(), QueryError> {
3757        self.explored_states += 1;
3758        if self.explored_states > self.max_states {
3759            return Err(QueryError::ComplexityLimit {
3760                candidate_tests: self.candidate_tests,
3761                obligations: self.obligations.len(),
3762                explored_states: self.explored_states,
3763                max_states: self.max_states,
3764                target: self.target,
3765                metric: self.metric,
3766            });
3767        }
3768        if selected.len() >= self.best.len() {
3769            return Ok(());
3770        }
3771        let state_key = format!(
3772            "{}|{}",
3773            selected.iter().cloned().collect::<Vec<_>>().join(","),
3774            skipped.iter().cloned().collect::<Vec<_>>().join(",")
3775        );
3776        if !self.seen.insert(state_key) {
3777            return Ok(());
3778        }
3779        let mut unmet = self
3780            .obligations
3781            .iter()
3782            .filter(|obligation| {
3783                !skipped.contains(&obligation.id) && !obligation_satisfied(obligation, &selected)
3784            })
3785            .collect::<Vec<_>>();
3786        if unmet.is_empty() {
3787            self.best = selected;
3788            return Ok(());
3789        }
3790        unmet.sort_by(|left, right| {
3791            let feasible = |obligation: &Obligation| {
3792                obligation
3793                    .options
3794                    .iter()
3795                    .filter(|option| option.iter().any(|test| !selected.contains(test)))
3796                    .count()
3797            };
3798            feasible(left)
3799                .cmp(&feasible(right))
3800                .then_with(|| left.id.cmp(&right.id))
3801        });
3802        let obligation = unmet[0];
3803        let mut additions = deduplicate_options(obligation.options.iter().filter_map(|option| {
3804            let addition = option
3805                .iter()
3806                .filter(|test| !selected.contains(*test))
3807                .cloned()
3808                .collect::<Vec<_>>();
3809            (!addition.is_empty()).then_some(addition)
3810        }));
3811        additions.sort_by(|left, right| {
3812            left.len()
3813                .cmp(&right.len())
3814                .then_with(|| left.join("\0").cmp(&right.join("\0")))
3815        });
3816        for addition in additions {
3817            if selected.len() + addition.len() >= self.best.len() {
3818                continue;
3819            }
3820            let mut next = selected.clone();
3821            next.extend(addition);
3822            self.visit(next, skipped.clone(), skipped_by_metric.clone())?;
3823        }
3824        let skipped_count = skipped_by_metric
3825            .get(&obligation.metric)
3826            .copied()
3827            .unwrap_or(0);
3828        if skipped_count
3829            < self
3830                .skip_limits
3831                .get(&obligation.metric)
3832                .copied()
3833                .unwrap_or(0)
3834        {
3835            let mut next_skipped = skipped;
3836            next_skipped.insert(obligation.id.clone());
3837            let mut next_counts = skipped_by_metric;
3838            next_counts.insert(obligation.metric, skipped_count + 1);
3839            self.visit(selected, next_skipped, next_counts)?;
3840        }
3841        Ok(())
3842    }
3843}
3844
3845pub fn minimum_test_set(
3846    view: &CoverageView,
3847    target: f64,
3848    metric: MinimizeMetric,
3849    max_states: usize,
3850) -> Result<MinimumTestSetResult, QueryError> {
3851    use crate::coverage_report::coverage_is_complete;
3852    if !target.is_finite() || !(0.0..=100.0).contains(&target) {
3853        return Err(QueryError::InvalidTarget(target));
3854    }
3855    if view.tests.iter().any(|test| {
3856        test.role == "background" && (!test.hits.is_empty() || !test.decisions.is_empty())
3857    }) {
3858        return Err(QueryError::UnattributedEvidence);
3859    }
3860    // And a test whose own coverage nothing recorded cannot be minimized
3861    // around either. Its empty hit set would read as a test that adds nothing
3862    // to any set, so the smallest set would drop it -- and what it covers is
3863    // precisely what nothing knows. The check above catches this whenever the
3864    // run-wide record holds anything, which is nearly always; this catches the
3865    // case where those tests happened to reach nothing measured.
3866    if view
3867        .tests
3868        .iter()
3869        .any(|test| test.role == "test" && !coverage_is_complete(&test.attribution))
3870    {
3871        return Err(QueryError::UnattributedEvidence);
3872    }
3873    let candidate_tests = view
3874        .tests
3875        .iter()
3876        .filter(|test| test.role == "test")
3877        .map(|test| test.id.clone())
3878        .collect::<BTreeSet<_>>();
3879    let model = build_obligations(view, &candidate_tests);
3880    let obligations = model
3881        .obligations
3882        .iter()
3883        .filter(|obligation| obligation.metric.selected(metric))
3884        .cloned()
3885        .collect::<Vec<_>>();
3886    let mut totals = BTreeMap::<ObligationMetric, usize>::new();
3887    for obligation in &obligations {
3888        *totals.entry(obligation.metric).or_default() += 1;
3889    }
3890    let metrics = [
3891        ObligationMetric::Lines,
3892        ObligationMetric::Statements,
3893        ObligationMetric::Functions,
3894        ObligationMetric::Branches,
3895        ObligationMetric::Mcdc,
3896    ]
3897    .into_iter()
3898    .filter(|candidate| candidate.selected(metric))
3899    .collect::<Vec<_>>();
3900    let skip_limits = metrics
3901        .iter()
3902        .map(|selected_metric| {
3903            let total = totals.get(selected_metric).copied().unwrap_or(0);
3904            let required = ((total as f64 * target) / 100.0).ceil() as usize;
3905            (*selected_metric, total.saturating_sub(required))
3906        })
3907        .collect::<BTreeMap<_, _>>();
3908    let full_expanded = model.expand(&candidate_tests);
3909    let full_summary = coverage_summary_for_tests(view, &full_expanded)?;
3910    for selected_metric in &metrics {
3911        let reachable = percentage(*selected_metric, &full_summary);
3912        if reachable + 1e-9 < target {
3913            return Err(QueryError::TargetUnreachable {
3914                metric: selected_metric.public(),
3915                target,
3916                reachable,
3917            });
3918        }
3919    }
3920    let mut search = Search {
3921        obligations: &obligations,
3922        skip_limits,
3923        best: candidate_tests.clone(),
3924        explored_states: 0,
3925        max_states,
3926        seen: BTreeSet::new(),
3927        candidate_tests: candidate_tests.len(),
3928        target,
3929        metric,
3930    };
3931    search.visit(BTreeSet::new(), BTreeSet::new(), BTreeMap::new())?;
3932    let expanded = model.expand(&search.best);
3933    let summary = coverage_summary_for_tests(view, &expanded)?;
3934    Ok(MinimumTestSetResult {
3935        optimal: true,
3936        target,
3937        metric,
3938        selected: search.best.into_iter().collect(),
3939        expanded: expanded.into_iter().collect(),
3940        summary,
3941        explored_states: search.explored_states,
3942    })
3943}
3944
3945#[cfg(test)]
3946mod tests {
3947    use crate::{
3948        coverage_analysis::McdcVector,
3949        coverage_report::{
3950            CoverageManifest, CoverageReport, CoverageReportRequest, DecisionMeta, ExitCodeInput,
3951            RawTestResult, RuntimeSnapshot, TestProvenance, analyze_coverage_results,
3952        },
3953    };
3954
3955    use super::*;
3956
3957    fn scope_entry(
3958        file: &str,
3959        status: &str,
3960        reason: &str,
3961        package: Option<&str>,
3962    ) -> IndexedScopeEntry {
3963        IndexedScopeEntry {
3964            file: file.into(),
3965            status: status.into(),
3966            reason: reason.into(),
3967            package_root: package.map(str::to_owned),
3968            measurement_limitations: 0,
3969            limitation_kinds: Vec::new(),
3970        }
3971    }
3972
3973    #[test]
3974    fn a_scope_is_grouped_by_place_and_names_the_roots_that_settle_it() {
3975        let unclassified = "unclassified first-party source";
3976        let entries = [
3977            scope_entry("components/a.tsx", "ambiguous", unclassified, None),
3978            scope_entry("components/ui/b.tsx", "ambiguous", unclassified, None),
3979            scope_entry("proxy.ts", "ambiguous", unclassified, None),
3980            scope_entry(
3981                "app/page.tsx",
3982                "included",
3983                "discovered package source root",
3984                None,
3985            ),
3986            scope_entry(
3987                "tests/a.test.ts",
3988                "excluded",
3989                "test or fixture source",
3990                None,
3991            ),
3992            scope_entry(
3993                "next.config.ts",
3994                "excluded",
3995                "build/test/tool configuration",
3996                None,
3997            ),
3998            scope_entry(
3999                "packages/ui/extra/c.ts",
4000                "ambiguous",
4001                unclassified,
4002                Some("packages/ui"),
4003            ),
4004            scope_entry(
4005                "packages/ui/src/d.ts",
4006                "included",
4007                "discovered package source root",
4008                Some("packages/ui"),
4009            ),
4010        ];
4011        let groups = scope_groups(&entries)
4012            .into_iter()
4013            .map(|group| (group.files, group.status, group.directory))
4014            .collect::<Vec<_>>();
4015        assert_eq!(
4016            groups,
4017            [
4018                (2, "ambiguous".to_owned(), "components".to_owned()),
4019                (1, "ambiguous".into(), ".".into()),
4020                (1, "ambiguous".into(), "packages/ui/extra".into()),
4021                (1, "included".into(), "app".into()),
4022                (1, "included".into(), "packages/ui/src".into()),
4023                (1, "excluded".into(), ".".into()),
4024                (1, "excluded".into(), "tests".into()),
4025            ]
4026        );
4027        // The roots discovery found, then each place: a directory by name,
4028        // and a file at the top by its own, never `.`.
4029        let hint =
4030            unclassified_source(&["app".into(), "packages/ui/src".into()], &entries).unwrap();
4031        assert_eq!(
4032            hint.roots,
4033            [
4034                "app",
4035                "packages/ui/src",
4036                "components",
4037                "packages/ui/extra",
4038                "proxy.ts"
4039            ]
4040        );
4041        assert_eq!(hint.directories[0].directory, "components");
4042        assert_eq!(hint.directories[0].files, 2);
4043        assert_eq!(unclassified_source(&[], &entries[3..6]), None);
4044    }
4045
4046    #[test]
4047    fn a_file_is_counted_under_its_first_folders() {
4048        assert_eq!(area_directory("app/api/cron/route.ts", 1), "app");
4049        assert_eq!(area_directory("app/api/cron/route.ts", 2), "app/api");
4050        assert_eq!(area_directory("app/page.tsx", 2), "app");
4051        assert_eq!(area_directory("proxy.ts", 2), ".");
4052    }
4053
4054    #[test]
4055    fn all_metric_keeps_statement_only_files_in_the_gap_set() {
4056        let gap = IndexedFileGap {
4057            view: CoverageViewId::All,
4058            file: "src/statement.js".into(),
4059            uncovered_lines: 0,
4060            uncovered_statements: 1,
4061            uncovered_functions: 0,
4062            missing_branches: 0,
4063            missing_mcdc_conditions: 0,
4064            measurement_limitations: 0,
4065            limitation_kinds: Vec::new(),
4066            covered_by_other_tests: crate::coverage_index::IndexedGapDimensions {
4067                lines: 0,
4068                statements: 0,
4069                functions: 0,
4070                branches: 0,
4071                mcdc_conditions: 0,
4072            },
4073            uncovered_everywhere: crate::coverage_index::IndexedGapDimensions {
4074                lines: 0,
4075                statements: 1,
4076                functions: 0,
4077                branches: 0,
4078                mcdc_conditions: 0,
4079            },
4080            totals: crate::coverage_index::IndexedGapDimensions {
4081                lines: 1,
4082                statements: 2,
4083                functions: 0,
4084                branches: 0,
4085                mcdc_conditions: 0,
4086            },
4087            covered: crate::coverage_index::IndexedGapDimensions {
4088                lines: 1,
4089                statements: 1,
4090                functions: 0,
4091                branches: 0,
4092                mcdc_conditions: 0,
4093            },
4094            score: 0,
4095        };
4096        assert!(has_gap_for_metric(&gap, MinimizeMetric::All));
4097        assert!(has_gap_for_metric(&gap, MinimizeMetric::Statements));
4098        assert!(!has_gap_for_metric(&gap, MinimizeMetric::Lines));
4099    }
4100
4101    fn result(id: &str, vector: McdcVector) -> RawTestResult {
4102        RawTestResult {
4103            test_id: Some(id.into()),
4104            scope: None,
4105            test: id.into(),
4106            test_file: Some("tests/permission.test.js".into()),
4107            title: None,
4108            retry: Some(0),
4109            status: Some("passed".into()),
4110            expected_status: None,
4111            flaky: false,
4112            provenance: TestProvenance {
4113                runner: "node:test".into(),
4114                kind: "unit".into(),
4115                project: None,
4116                source: "runner-default".into(),
4117            },
4118            role: "test".into(),
4119            attribution: crate::coverage_report::ATTRIBUTION_EXACT.into(),
4120            phases: Vec::new(),
4121            runtime: vec![RuntimeSnapshot {
4122                decisions: vec![crate::coverage_report::DecisionSnapshot {
4123                    meta: decision(),
4124                    vectors: vec![vector],
4125                }],
4126                hits: Vec::new(),
4127                events: Vec::new(),
4128                logicals: Vec::new(),
4129                phase_id: None,
4130            }],
4131            browser: Vec::new(),
4132            server: Vec::new(),
4133        }
4134    }
4135
4136    fn decision() -> DecisionMeta {
4137        DecisionMeta {
4138            id: "decision".into(),
4139            file: "src/permission.js".into(),
4140            line: 1,
4141            column: 1,
4142            source: "admin || owner".into(),
4143            conditions: vec!["admin".into(), "owner".into()],
4144            kind: "if".into(),
4145        }
4146    }
4147
4148    fn report(mut results: Vec<RawTestResult>) -> CoverageReport {
4149        analyze_coverage_results(&CoverageReportRequest {
4150            run_id: "run".into(),
4151            manifest: CoverageManifest {
4152                unmeasured: Vec::new(),
4153                decisions: vec![decision()],
4154                points: Vec::new(),
4155                branches: Vec::new(),
4156                limitations: Vec::new(),
4157                scope: None,
4158                assertion_sites: Vec::new(),
4159            },
4160            raw_results: std::mem::take(&mut results),
4161            generated_at: "time".into(),
4162            coverage_model: None,
4163            integrity: None,
4164            test_exit_code: ExitCodeInput::Present(Some(0)),
4165        })
4166        .unwrap()
4167    }
4168
4169    #[test]
4170    fn recomputes_mcdc_witnesses_and_removes_a_redundant_vector() {
4171        let report = report(vec![
4172            result(
4173                "admin",
4174                McdcVector {
4175                    values: vec![Some(true), None],
4176                    outcome: true,
4177                },
4178            ),
4179            result(
4180                "owner",
4181                McdcVector {
4182                    values: vec![Some(false), Some(true)],
4183                    outcome: true,
4184                },
4185            ),
4186            result(
4187                "both",
4188                McdcVector {
4189                    values: vec![Some(true), None],
4190                    outcome: true,
4191                },
4192            ),
4193            result(
4194                "neither",
4195                McdcVector {
4196                    values: vec![Some(false), Some(false)],
4197                    outcome: false,
4198                },
4199            ),
4200        ]);
4201        let minimized = minimum_test_set(&report.view, 100.0, MinimizeMetric::Mcdc, 5_000).unwrap();
4202        assert_eq!(minimized.selected.len(), 3);
4203        assert!(minimized.selected.contains(&"owner".into()));
4204        assert!(minimized.selected.contains(&"neither".into()));
4205        assert_eq!(minimized.summary.condition_coverage_pct, 100.0);
4206    }
4207
4208    #[test]
4209    fn a_test_that_belongs_to_no_test_is_not_minimized_away() {
4210        // The first guard catches this whenever the run-wide record holds
4211        // anything, which is nearly always -- so it is the rare run, where
4212        // those tests happened to reach nothing measured, that reaches this
4213        // one. Without it the smallest set would quietly drop a test whose
4214        // coverage is unknown rather than absent, and report the drop as a
4215        // saving.
4216        let mut parallel = result(
4217            "parallel",
4218            McdcVector {
4219                values: vec![Some(false), Some(false)],
4220                outcome: false,
4221            },
4222        );
4223        parallel.attribution = crate::coverage_report::ATTRIBUTION_RUN_WIDE.into();
4224        assert!(matches!(
4225            minimum_test_set(
4226                &report(vec![parallel]).view,
4227                100.0,
4228                MinimizeMetric::Mcdc,
4229                5_000,
4230            ),
4231            Err(QueryError::UnattributedEvidence)
4232        ));
4233    }
4234
4235    #[test]
4236    fn the_summary_counts_each_test_under_the_attribution_it_carries() {
4237        // These three numbers are what the `Attribution` line reads out, and
4238        // nothing -- here or in the integration scripts -- had ever checked
4239        // that a test lands in the bucket it was written with.
4240        let vector = || McdcVector {
4241            values: vec![Some(false), Some(false)],
4242            outcome: false,
4243        };
4244        let mut serial = result("serial", vector());
4245        serial.attribution = crate::coverage_report::ATTRIBUTION_EXACT.into();
4246        let mut parallel = result("parallel", vector());
4247        parallel.attribution = crate::coverage_report::ATTRIBUTION_RUN_WIDE.into();
4248        let mut ruby = result("ruby", vector());
4249        ruby.attribution = crate::coverage_report::ATTRIBUTION_PARTIAL.into();
4250        let mut also_parallel = result("also-parallel", vector());
4251        also_parallel.attribution = crate::coverage_report::ATTRIBUTION_RUN_WIDE.into();
4252        let report = report(vec![serial, parallel, ruby, also_parallel]);
4253
4254        let nonce = std::time::SystemTime::now()
4255            .duration_since(std::time::UNIX_EPOCH)
4256            .unwrap()
4257            .as_nanos();
4258        let root = std::env::temp_dir().join(format!(
4259            "supercov-attribution-counts-{}-{nonce}",
4260            std::process::id()
4261        ));
4262        std::fs::create_dir(&root).unwrap();
4263        let path = root.join("query-index.v1.bin");
4264        let identity = crate::query_index::QueryIndexIdentity {
4265            evidence_sha256: [1; 32],
4266            evidence_bytes: 100,
4267            analysis_sha256: [2; 32],
4268            producer_sha256: [3; 32],
4269            archive_schema_version: 2,
4270        };
4271        crate::query_index::write_query_index(
4272            &crate::coverage_index::coverage_index_sections(&report).unwrap(),
4273            &identity,
4274            &path,
4275        )
4276        .unwrap();
4277        let container = crate::query_index::QueryIndex::open(&path, &identity).unwrap();
4278        let index = crate::coverage_index::CoverageIndex::new(&container).unwrap();
4279
4280        let summary = coverage_summary_query(
4281            &index,
4282            CoverageSummaryQueryOptions {
4283                run: "run",
4284                view: CoverageViewId::All,
4285                kind: None,
4286                runner: None,
4287                valid: true,
4288                test_exit_code: Some(0),
4289                stale: false,
4290                stale_reasons: Vec::new(),
4291            },
4292        )
4293        .unwrap();
4294        assert_eq!(summary.test_attribution.exact, 1);
4295        assert_eq!(summary.test_attribution.run_wide, 2);
4296        assert_eq!(summary.test_attribution.partial, 1);
4297        assert_eq!(summary.test_attribution.total(), 4);
4298        std::fs::remove_dir_all(&root).ok();
4299    }
4300
4301    #[test]
4302    fn refuses_background_evidence() {
4303        let mut aggregate = result(
4304            "aggregate",
4305            McdcVector {
4306                values: vec![Some(false), Some(false)],
4307                outcome: false,
4308            },
4309        );
4310        aggregate.role = "background".into();
4311        assert!(matches!(
4312            minimum_test_set(
4313                &report(vec![aggregate]).view,
4314                100.0,
4315                MinimizeMetric::Mcdc,
4316                5_000,
4317            ),
4318            Err(QueryError::UnattributedEvidence)
4319        ));
4320    }
4321
4322    #[test]
4323    fn bounds_the_exact_search() {
4324        let report = report(vec![
4325            result(
4326                "admin",
4327                McdcVector {
4328                    values: vec![Some(true), None],
4329                    outcome: true,
4330                },
4331            ),
4332            result(
4333                "owner",
4334                McdcVector {
4335                    values: vec![Some(false), Some(true)],
4336                    outcome: true,
4337                },
4338            ),
4339            result(
4340                "neither",
4341                McdcVector {
4342                    values: vec![Some(false), Some(false)],
4343                    outcome: false,
4344                },
4345            ),
4346        ]);
4347        assert!(matches!(
4348            minimum_test_set(&report.view, 100.0, MinimizeMetric::Mcdc, 1),
4349            Err(QueryError::ComplexityLimit { .. })
4350        ));
4351    }
4352}