1use 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#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
273#[serde(rename_all = "camelCase")]
274pub struct TestAttributionCounts {
275 pub exact: usize,
277 pub by_time: usize,
280 pub partial: usize,
282 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 #[serde(default, skip_serializing_if = "Vec::is_empty")]
312 pub merged_across: Vec<String>,
313 #[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 #[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 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 #[serde(skip_serializing_if = "Option::is_none")]
365 pub unclassified_source: Option<UnclassifiedSource>,
366}
367
368#[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#[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
396fn 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
422pub 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 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 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 #[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 #[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
731#[derive(Default)]
736pub struct CoversCache {
737 tests: Vec<IndexedTestSummary>,
738 phases: HashMap<String, crate::coverage_index::IndexedPhaseSummary>,
739 limitations: Vec<crate::coverage_index::IndexedLimitation>,
740 read: bool,
741 files: HashMap<String, CoversFile>,
742}
743
744struct CoversFile {
745 metadata: HashMap<String, IndexedHitMetadata>,
746 decisions: HashMap<String, crate::coverage_report::DecisionResult>,
747 gap_lines: Vec<CoverageFileGapLine>,
748}
749
750impl CoversCache {
751 fn fill(
752 &mut self,
753 index: &CoverageIndex<'_>,
754 options: &CoverageCoversQueryOptions<'_>,
755 ) -> Result<(), QueryError> {
756 if !self.read {
757 self.tests = index.test_summaries(options.view)?;
758 self.phases = index
759 .phase_summaries(options.view)?
760 .into_iter()
761 .map(|phase| (phase.id.clone(), phase))
762 .collect();
763 self.limitations = index.limitations(options.view)?;
764 self.read = true;
765 }
766 if self.files.contains_key(options.file) {
767 return Ok(());
768 }
769 let metadata = index
771 .hit_metadata_in_file(options.view, options.file)?
772 .into_iter()
773 .map(|value| (value.id.clone(), value))
774 .collect();
775 let decisions = index
776 .decision_details_in_file(options.view, options.file)?
777 .into_iter()
778 .map(|value| (value.meta.id.clone(), value))
779 .collect();
780 let (detail, _) = coverage_file_detail_query(
781 index,
782 CoverageFileDetailOptions {
783 run: options.run,
784 view: options.view,
785 kind: options.kind,
786 runner: options.runner,
787 selector: options.file,
788 metric: MinimizeMetric::All,
789 offset: 0,
790 limit: usize::MAX,
791 },
792 )?;
793 self.files.insert(
794 options.file.into(),
795 CoversFile {
796 metadata,
797 decisions,
798 gap_lines: detail.gap_lines,
799 },
800 );
801 Ok(())
802 }
803}
804
805pub fn coverage_covers_query(
806 index: &CoverageIndex<'_>,
807 options: CoverageCoversQueryOptions<'_>,
808) -> Result<(CoverageCoversData, AgentPagination), QueryError> {
809 coverage_covers_query_cached(index, options, &mut CoversCache::default())
810}
811
812pub fn coverage_covers_query_cached(
815 index: &CoverageIndex<'_>,
816 options: CoverageCoversQueryOptions<'_>,
817 cache: &mut CoversCache,
818) -> Result<(CoverageCoversData, AgentPagination), QueryError> {
819 if options.limit == 0 {
820 return Err(QueryError::InvalidPagination);
821 }
822 cache.fill(index, &options)?;
823 let cache = &*cache;
824 let tests = &cache.tests;
825 let selected = selected_test_ids(tests, options.kind, options.runner)?;
826 let selected_includes = |id: &str| selected.as_ref().is_none_or(|ids| ids.contains(id));
827 let filters = query_filters(options.view, options.kind, options.runner);
828 let location = CoverageLocation {
829 file: options.file.into(),
830 line: options.line,
831 };
832 let file = &cache.files[options.file];
833 let (metadata, decisions) = (&file.metadata, &file.decisions);
834 let anchors = index.anchors(options.view, options.file, options.line)?;
835 let tests_by_id = tests
836 .iter()
837 .map(|test| (test.id.as_str(), test))
838 .collect::<HashMap<_, _>>();
839 let mut anchor_test_ids = anchors
840 .iter()
841 .flat_map(|anchor| anchor.tests.iter())
842 .filter(|id| selected_includes(id))
843 .cloned()
844 .collect::<BTreeSet<_>>();
845 for anchor in anchors.iter().filter(|anchor| anchor.kind == "branch") {
846 anchor_test_ids.extend(
847 metadata
848 .values()
849 .filter(|detail| detail.parent_id.as_deref() == Some(anchor.id.as_str()))
850 .flat_map(|detail| detail.tests.iter())
851 .filter(|id| selected_includes(id))
852 .cloned(),
853 );
854 }
855 let all_anchor_tests = anchor_test_ids
856 .iter()
857 .map(|id| {
858 let test = tests_by_id.get(id.as_str());
859 CoverageCoveringTest {
860 id: id.clone(),
861 name: test.map_or_else(|| id.clone(), |test| test.name.clone()),
862 provenance: test
863 .map_or_else(TestProvenance::default, |test| test.provenance.clone()),
864 }
865 })
866 .collect::<Vec<_>>();
867 let total_anchor_tests = all_anchor_tests.len();
868 let anchor_tests_page = all_anchor_tests
869 .iter()
870 .skip(options.offset)
871 .take(options.limit)
872 .cloned()
873 .collect::<Vec<_>>();
874 let render_anchor = |anchor: crate::coverage_index::IndexedAnchor| {
875 let branch_alternatives = if anchor.kind == "branch" {
876 metadata
877 .values()
878 .filter(|detail| {
879 detail.obligation == "branch"
880 && detail.parent_id.as_deref() == Some(anchor.id.as_str())
881 })
882 .collect::<Vec<_>>()
883 } else {
884 Vec::new()
885 };
886 let branch_tests = branch_alternatives
887 .iter()
888 .flat_map(|detail| detail.tests.iter())
889 .filter(|test| selected_includes(test))
890 .cloned()
891 .collect::<BTreeSet<_>>();
892 let covering_tests = if anchor.kind == "branch" {
893 branch_tests.len()
894 } else {
895 anchor
896 .tests
897 .iter()
898 .filter(|test| selected_includes(test))
899 .count()
900 };
901 let detail = metadata.get(&anchor.id);
902 let decision = decisions
903 .get(&anchor.id)
904 .cloned()
905 .map(|decision| selected_decision(decision, selected.as_ref()));
906 let conditions = decision.as_ref().map_or(anchor.conditions, |decision| {
907 Some(decision.conditions.len())
908 });
909 let covered_conditions = decision
910 .as_ref()
911 .map_or(anchor.covered_conditions, |decision| {
912 Some(
913 decision
914 .conditions
915 .iter()
916 .filter(|condition| condition.covered)
917 .count(),
918 )
919 });
920 let covered = match anchor.kind.as_str() {
921 "decision" => conditions == covered_conditions,
922 "branch" if !branch_alternatives.is_empty() => branch_alternatives
923 .iter()
924 .all(|detail| detail.tests.iter().any(|test| selected_includes(test))),
925 "branch" => anchor.covered,
926 _ => covering_tests > 0,
927 };
928 let branch_source = branch_alternatives
929 .first()
930 .map(|detail| detail.source.clone());
931 let missing_branch_alternatives = branch_alternatives
932 .iter()
933 .filter(|detail| !detail.tests.iter().any(|test| selected_includes(test)))
934 .filter_map(|detail| detail.alternative.clone())
935 .collect::<Vec<_>>();
936 CoverageAnchor {
937 kind: anchor.kind,
938 id: anchor.id,
939 column: anchor.column,
940 covered,
941 source: decision
942 .as_ref()
943 .map(|decision| decision.meta.source.clone())
944 .or(branch_source)
945 .or_else(|| {
946 detail.map(|detail| {
947 detail
948 .label
949 .clone()
950 .unwrap_or_else(|| detail.source.clone())
951 })
952 })
953 .and_then(|source| compact_source(&source)),
954 missing: if missing_branch_alternatives.is_empty() {
955 detail.and_then(|detail| detail.alternative.clone())
956 } else {
957 Some(missing_branch_alternatives.join("; "))
958 },
959 covering_tests,
960 covered_conditions,
961 conditions,
962 }
963 };
964 let rendered_anchors = anchors.into_iter().map(render_anchor).collect::<Vec<_>>();
965 let total_anchored = rendered_anchors.len();
966 let covered_anchored = rendered_anchors
967 .iter()
968 .filter(|anchor| anchor.covered)
969 .count();
970 let all_limitations = cache
971 .limitations
972 .iter()
973 .filter(|limitation| limitation.file == options.file && limitation.line == options.line)
974 .cloned()
975 .map(|limitation| CoverageFileLimitation {
976 id: limitation.id,
977 kind: limitation.kind,
978 file: limitation.file,
979 line: limitation.line,
980 column: limitation.column,
981 source: limitation.source,
982 reason: limitation.reason,
983 blocking: limitation.blocking,
984 effect: "outside-measured-denominator".into(),
985 })
986 .collect::<Vec<_>>();
987 let total_limitations = all_limitations.len();
988 let limitations_page = all_limitations
989 .iter()
990 .skip(options.offset)
991 .take(options.limit)
992 .cloned()
993 .collect::<Vec<_>>();
994 let gap_line = file
995 .gap_lines
996 .iter()
997 .find(|gap| gap.line == options.line)
998 .cloned();
999 let source = gap_line
1005 .as_ref()
1006 .and_then(|gap| gap.source.clone())
1007 .or_else(|| {
1008 rendered_anchors
1009 .iter()
1010 .filter_map(|anchor| anchor.source.clone())
1011 .min_by_key(|source| source.len())
1012 });
1013 let all_remaining = gap_line.map_or_else(Vec::new, |gap| gap.obligations);
1014 let total_remaining = all_remaining.len();
1015 let remaining_page = all_remaining
1016 .iter()
1017 .skip(options.offset)
1018 .take(options.limit)
1019 .cloned()
1020 .collect::<Vec<_>>();
1021 let Some(line) = index.line(options.view, options.file, options.line)? else {
1022 let anchored = rendered_anchors
1023 .iter()
1024 .skip(options.offset)
1025 .take(options.limit)
1026 .cloned()
1027 .collect::<Vec<_>>();
1028 let total = total_anchored.max(total_limitations).max(total_remaining);
1029 let total = total.max(total_anchor_tests);
1030 let returned = anchored
1031 .len()
1032 .max(limitations_page.len())
1033 .max(remaining_page.len())
1034 .max(anchor_tests_page.len());
1035 return Ok((
1036 CoverageCoversData::Anchors(CoverageCoversAnchorsData {
1037 run: options.run.into(),
1038 filters,
1039 location,
1040 source,
1041 source_origin: None,
1042 line_obligation: false,
1043 anchored,
1044 total_anchored,
1045 covered_anchored,
1046 total_limitations,
1047 limitations: limitations_page,
1048 total_remaining,
1049 remaining: remaining_page,
1050 total_tests: total_anchor_tests,
1051 tests: anchor_tests_page,
1052 assertions: None,
1053 }),
1054 pagination(options.offset, options.limit, returned, total),
1055 ));
1056 };
1057 let all_tests = line
1058 .tests
1059 .iter()
1060 .filter(|id| selected_includes(id))
1061 .map(|id| {
1062 let test = tests_by_id.get(id.as_str());
1063 CoverageCoveringTest {
1064 id: id.clone(),
1065 name: test.map_or_else(|| id.clone(), |test| test.name.clone()),
1066 provenance: test
1067 .map_or_else(TestProvenance::default, |test| test.provenance.clone()),
1068 }
1069 })
1070 .collect::<Vec<_>>();
1071 let phases_by_id = &cache.phases;
1072 let all_phases = line
1073 .phases
1074 .iter()
1075 .filter_map(|id| phases_by_id.get(id))
1076 .filter(|phase| selected_includes(&phase.test))
1077 .map(|phase| CoverageCoveringPhase {
1078 id: phase.id.clone(),
1079 kind: phase.kind.clone(),
1080 operation: phase.operation.clone(),
1081 source: phase.source.clone(),
1082 test: phase.test.clone(),
1083 status: phase.status.clone(),
1084 caused_by_phase_id: phase.caused_by_phase_id.clone(),
1085 })
1086 .collect::<Vec<_>>();
1087 let tests_page = all_tests
1088 .iter()
1089 .skip(options.offset)
1090 .take(options.limit)
1091 .cloned()
1092 .collect::<Vec<_>>();
1093 let phases_page = all_phases
1094 .iter()
1095 .skip(options.offset)
1096 .take(options.limit)
1097 .cloned()
1098 .collect::<Vec<_>>();
1099 let anchored_page = rendered_anchors
1100 .into_iter()
1101 .skip(options.offset)
1102 .take(options.limit)
1103 .collect::<Vec<_>>();
1104 let total = all_tests
1105 .len()
1106 .max(all_phases.len())
1107 .max(total_anchored)
1108 .max(total_limitations)
1109 .max(total_remaining);
1110 let returned = tests_page
1111 .len()
1112 .max(phases_page.len())
1113 .max(anchored_page.len())
1114 .max(limitations_page.len())
1115 .max(remaining_page.len());
1116 Ok((
1117 CoverageCoversData::Line(CoverageCoversLineData {
1118 run: options.run.into(),
1119 filters,
1120 location,
1121 source,
1122 source_origin: None,
1123 covered: line.tests.iter().any(|test| selected_includes(test)),
1124 confidence: line.confidence,
1125 total_tests: all_tests.len(),
1126 total_phases: all_phases.len(),
1127 total_anchored,
1128 covered_anchored,
1129 total_limitations,
1130 total_remaining,
1131 tests: tests_page,
1132 phases: phases_page,
1133 anchored: anchored_page,
1134 limitations: limitations_page,
1135 remaining: remaining_page,
1136 assertions: None,
1137 }),
1138 pagination(options.offset, options.limit, returned, total),
1139 ))
1140}
1141
1142#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1143pub struct CoverageTestMatch {
1144 pub id: String,
1145 pub name: String,
1146 pub outcome: String,
1147 pub provenance: TestProvenance,
1148}
1149
1150#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1151#[serde(rename_all = "camelCase")]
1152pub struct CoverageHitDetail {
1153 pub id: String,
1154 pub obligation: String,
1155 #[serde(skip_serializing_if = "Option::is_none")]
1156 pub branch_kind: Option<String>,
1157 #[serde(skip_serializing_if = "Option::is_none")]
1158 pub file: Option<String>,
1159 #[serde(skip_serializing_if = "Option::is_none")]
1160 pub line: Option<usize>,
1161 #[serde(skip_serializing_if = "Option::is_none")]
1162 pub column: Option<usize>,
1163 #[serde(skip_serializing_if = "Option::is_none")]
1164 pub label: Option<String>,
1165 #[serde(skip_serializing_if = "Option::is_none")]
1166 pub alternative: Option<String>,
1167}
1168
1169#[derive(Debug, Clone, PartialEq, Serialize)]
1170pub struct CoverageTestDecision {
1171 pub id: String,
1172 pub vectors: Vec<crate::coverage_analysis::McdcVector>,
1173 pub meta: DecisionMeta,
1174}
1175
1176#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1177#[serde(rename_all = "camelCase")]
1178pub struct CoverageTestPhase {
1179 pub id: String,
1180 pub kind: String,
1181 pub operation: String,
1182 #[serde(skip_serializing_if = "Option::is_none")]
1183 pub source: Option<String>,
1184 #[serde(skip_serializing_if = "Option::is_none")]
1185 pub status: Option<String>,
1186 #[serde(skip_serializing_if = "Option::is_none")]
1187 pub caused_by_phase_id: Option<String>,
1188 pub lines: usize,
1189 pub decisions: usize,
1190}
1191
1192#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1193pub struct CoverageTestTotals {
1194 pub lines: usize,
1195 pub hits: usize,
1196 pub decisions: usize,
1197 pub phases: usize,
1198}
1199
1200#[derive(Debug, Clone, PartialEq, Serialize)]
1201#[serde(rename_all = "camelCase")]
1202pub struct CoverageSelectedTest {
1203 pub id: String,
1204 pub name: String,
1205 #[serde(skip_serializing_if = "Option::is_none")]
1206 pub file: Option<String>,
1207 #[serde(skip_serializing_if = "Option::is_none")]
1208 pub title: Option<String>,
1209 pub retries: Vec<usize>,
1210 pub attempts: Vec<TestAttempt>,
1211 pub outcome: String,
1212 pub provenance: TestProvenance,
1213 pub role: String,
1214 pub attribution: String,
1218 pub hits: Vec<String>,
1219 pub decisions: Vec<CoverageTestDecision>,
1220 pub lines: Vec<SourceLine>,
1221 pub hit_details: Vec<CoverageHitDetail>,
1222 pub phases: Vec<CoverageTestPhase>,
1223 pub totals: CoverageTestTotals,
1224}
1225
1226#[derive(Debug, Clone, PartialEq, Serialize)]
1227pub struct CoverageTestMatchesData {
1228 pub run: String,
1229 pub filters: CoverageQueryFilters,
1230 pub tests: Vec<CoverageTestMatch>,
1231}
1232
1233#[derive(Debug, Clone, PartialEq, Serialize)]
1234#[serde(rename_all = "camelCase")]
1235pub struct CoverageTestDetailData {
1236 pub run: String,
1237 pub filters: CoverageQueryFilters,
1238 pub pagination_applies_to: String,
1239 pub tests: Vec<CoverageSelectedTest>,
1240}
1241
1242#[derive(Debug, Clone, PartialEq, Serialize)]
1243#[serde(untagged)]
1244pub enum CoverageTestData {
1245 Matches(CoverageTestMatchesData),
1246 Detail(CoverageTestDetailData),
1247}
1248
1249#[derive(Debug, Clone, Copy)]
1250pub struct CoverageTestQueryOptions<'a> {
1251 pub run: &'a str,
1252 pub view: CoverageViewId,
1253 pub kind: Option<&'a str>,
1254 pub runner: Option<&'a str>,
1255 pub selector: &'a str,
1256 pub offset: usize,
1257 pub limit: usize,
1258}
1259
1260fn hit_detail(id: &str, metadata: Option<&IndexedHitMetadata>) -> CoverageHitDetail {
1261 match metadata {
1262 Some(metadata) => CoverageHitDetail {
1263 id: id.into(),
1264 obligation: metadata.obligation.clone(),
1265 branch_kind: metadata.branch_kind.clone(),
1266 file: Some(metadata.file.clone()),
1267 line: Some(metadata.line),
1268 column: Some(metadata.column),
1269 label: metadata.label.clone(),
1270 alternative: metadata.alternative.clone(),
1271 },
1272 None => CoverageHitDetail {
1273 id: id.into(),
1274 obligation: "unknown".into(),
1275 branch_kind: None,
1276 file: None,
1277 line: None,
1278 column: None,
1279 label: None,
1280 alternative: None,
1281 },
1282 }
1283}
1284
1285pub fn coverage_test_query(
1286 index: &CoverageIndex<'_>,
1287 options: CoverageTestQueryOptions<'_>,
1288) -> Result<(CoverageTestData, AgentPagination), QueryError> {
1289 if options.limit == 0 {
1290 return Err(QueryError::InvalidPagination);
1291 }
1292 let tests = index.test_details(options.view)?;
1293 let summaries = tests
1294 .iter()
1295 .map(|test| test.summary.clone())
1296 .collect::<Vec<_>>();
1297 let selected = selected_test_ids(&summaries, options.kind, options.runner)?;
1298 let selector = options.selector.to_lowercase();
1299 let matches = tests
1300 .into_iter()
1301 .filter(|test| {
1302 selected
1303 .as_ref()
1304 .is_none_or(|ids| ids.contains(&test.summary.id))
1305 })
1306 .filter(|test| {
1307 test.summary.id == selector || test.summary.name.to_lowercase().contains(&selector)
1308 })
1309 .collect::<Vec<_>>();
1310 if matches.is_empty() {
1311 return Err(QueryError::TestNotFound(options.selector.into()));
1312 }
1313 let filters = query_filters(options.view, options.kind, options.runner);
1314 if matches.len() > 1 {
1315 let total = matches.len();
1316 let page = matches
1317 .into_iter()
1318 .skip(options.offset)
1319 .take(options.limit)
1320 .map(|test| CoverageTestMatch {
1321 id: test.summary.id,
1322 name: test.summary.name,
1323 outcome: test.summary.outcome,
1324 provenance: test.summary.provenance,
1325 })
1326 .collect::<Vec<_>>();
1327 let returned = page.len();
1328 return Ok((
1329 CoverageTestData::Matches(CoverageTestMatchesData {
1330 run: options.run.into(),
1331 filters,
1332 tests: page,
1333 }),
1334 pagination(options.offset, options.limit, returned, total),
1335 ));
1336 }
1337 let test = matches.into_iter().next().expect("one test match");
1338 let metadata = index
1339 .hit_metadata(options.view)?
1340 .into_iter()
1341 .map(|metadata| (metadata.id.clone(), metadata))
1342 .collect::<HashMap<_, _>>();
1343 let decisions = index
1344 .decision_metadata(options.view)?
1345 .into_iter()
1346 .map(|decision| (decision.id.clone(), decision))
1347 .collect::<HashMap<_, _>>();
1348 let all_phases = index
1349 .phase_summaries(options.view)?
1350 .into_iter()
1351 .filter(|phase| phase.test == test.summary.id)
1352 .map(|phase| CoverageTestPhase {
1353 id: phase.id,
1354 kind: phase.kind,
1355 operation: phase.operation,
1356 source: phase.source,
1357 status: phase.status,
1358 caused_by_phase_id: phase.caused_by_phase_id,
1359 lines: phase.lines,
1360 decisions: phase.decisions,
1361 })
1362 .collect::<Vec<_>>();
1363 let totals = CoverageTestTotals {
1364 lines: test.lines.len(),
1365 hits: test.hits.len(),
1366 decisions: test.decisions.len(),
1367 phases: all_phases.len(),
1368 };
1369 let total = totals
1370 .lines
1371 .max(totals.hits)
1372 .max(totals.decisions)
1373 .max(totals.phases);
1374 let lines = test
1375 .lines
1376 .iter()
1377 .skip(options.offset)
1378 .take(options.limit)
1379 .cloned()
1380 .collect::<Vec<_>>();
1381 let hits = test
1382 .hits
1383 .iter()
1384 .skip(options.offset)
1385 .take(options.limit)
1386 .cloned()
1387 .collect::<Vec<_>>();
1388 let hit_details = test
1389 .hits
1390 .iter()
1391 .skip(options.offset)
1392 .take(options.limit)
1393 .map(|id| hit_detail(id, metadata.get(id)))
1394 .collect::<Vec<_>>();
1395 let test_decisions = test
1396 .decisions
1397 .iter()
1398 .skip(options.offset)
1399 .take(options.limit)
1400 .map(|decision| {
1401 Ok(CoverageTestDecision {
1402 id: decision.id.clone(),
1403 vectors: decision.vectors.clone(),
1404 meta: decisions
1405 .get(&decision.id)
1406 .cloned()
1407 .ok_or(QueryError::InvalidRecordSelection)?,
1408 })
1409 })
1410 .collect::<Result<Vec<_>, QueryError>>()?;
1411 let phases = all_phases
1412 .into_iter()
1413 .skip(options.offset)
1414 .take(options.limit)
1415 .collect::<Vec<_>>();
1416 let returned = lines
1417 .len()
1418 .max(hits.len())
1419 .max(test_decisions.len())
1420 .max(phases.len());
1421 Ok((
1422 CoverageTestData::Detail(CoverageTestDetailData {
1423 run: options.run.into(),
1424 filters,
1425 pagination_applies_to:
1426 "lines, hits/hitDetails, decisions, and phases independently within the test".into(),
1427 tests: vec![CoverageSelectedTest {
1428 id: test.summary.id,
1429 name: test.summary.name,
1430 file: test.summary.file,
1431 title: test.summary.title,
1432 retries: test.retries,
1433 attempts: test.attempts,
1434 outcome: test.summary.outcome,
1435 provenance: test.summary.provenance,
1436 role: test.summary.role,
1437 attribution: test.summary.attribution,
1438 hits,
1439 decisions: test_decisions,
1440 lines,
1441 hit_details,
1442 phases,
1443 totals,
1444 }],
1445 }),
1446 pagination(options.offset, options.limit, returned, total),
1447 ))
1448}
1449
1450#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1451pub struct CoverageDecisionMatch {
1452 pub id: String,
1453 pub file: String,
1454 pub line: usize,
1455 pub column: usize,
1456 pub source: String,
1457}
1458
1459#[derive(Debug, Clone, PartialEq, Serialize)]
1460#[serde(rename_all = "camelCase")]
1461pub struct CoverageDecisionCondition {
1462 pub index: usize,
1463 pub source: String,
1464 pub covered: bool,
1465 #[serde(skip_serializing_if = "Option::is_none")]
1466 pub assertion_covered: Option<bool>,
1467 #[serde(skip_serializing_if = "Option::is_none")]
1468 pub witness: Option<[crate::coverage_analysis::McdcVector; 2]>,
1469 #[serde(skip_serializing_if = "Option::is_none")]
1470 pub witness_tests: Option<[Vec<Id>; 2]>,
1471 #[serde(skip_serializing_if = "Option::is_none")]
1473 pub missing_outcomes: Option<Vec<String>>,
1474}
1475
1476#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1477#[serde(rename_all = "camelCase")]
1478pub struct CoverageDecisionTotals {
1479 pub conditions: usize,
1480 pub vector_observations: usize,
1481 pub tests: usize,
1482}
1483
1484#[derive(Debug, Clone, PartialEq, Serialize)]
1485#[serde(rename_all = "camelCase")]
1486pub struct CoverageSelectedDecision {
1487 pub meta: DecisionMeta,
1488 pub executed: bool,
1489 pub covered: bool,
1490 pub vectors: Vec<crate::coverage_analysis::McdcVector>,
1491 pub vector_observations: Vec<crate::coverage_report::VectorObservation>,
1492 pub conditions: Vec<CoverageDecisionCondition>,
1493 pub tests: Vec<Id>,
1494 pub confidence: CoverageConfidence,
1495 pub totals: CoverageDecisionTotals,
1496}
1497
1498#[derive(Debug, Clone, PartialEq, Serialize)]
1499pub struct CoverageDecisionMatchesData {
1500 pub run: String,
1501 pub filters: CoverageQueryFilters,
1502 pub decisions: Vec<CoverageDecisionMatch>,
1503}
1504
1505#[derive(Debug, Clone, PartialEq, Serialize)]
1506#[serde(rename_all = "camelCase")]
1507pub struct CoverageDecisionDetailData {
1508 pub run: String,
1509 pub filters: CoverageQueryFilters,
1510 pub pagination_applies_to: String,
1511 pub decisions: Vec<CoverageSelectedDecision>,
1512}
1513
1514#[derive(Debug, Clone, PartialEq, Serialize)]
1515#[serde(untagged)]
1516pub enum CoverageDecisionData {
1517 Matches(CoverageDecisionMatchesData),
1518 Detail(CoverageDecisionDetailData),
1519}
1520
1521#[derive(Debug, Clone, Copy)]
1522pub struct CoverageDecisionQueryOptions<'a> {
1523 pub run: &'a str,
1524 pub view: CoverageViewId,
1525 pub kind: Option<&'a str>,
1526 pub runner: Option<&'a str>,
1527 pub selector: &'a str,
1528 pub offset: usize,
1529 pub limit: usize,
1530}
1531
1532fn selector_location(selector: &str) -> Option<(&str, usize, Option<usize>)> {
1535 let (prefix, last) = selector.rsplit_once(':')?;
1536 let last = last.parse::<usize>().ok()?;
1537 if let Some((file, possible_line)) = prefix.rsplit_once(':')
1538 && let Ok(line) = possible_line.parse::<usize>()
1539 {
1540 return Some((file, line, Some(last)));
1541 }
1542 Some((prefix, last, None))
1543}
1544
1545fn selected_decision(
1546 decision: crate::coverage_report::DecisionResult,
1547 selected: Option<&BTreeSet<String>>,
1548) -> crate::coverage_report::DecisionResult {
1549 let Some(selected) = selected else {
1550 return decision;
1551 };
1552 let vector_observations = decision
1553 .vector_observations
1554 .into_iter()
1555 .filter_map(|mut observation| {
1556 observation
1557 .tests
1558 .retain(|test| selected.contains(test.as_str()));
1559 (!observation.tests.is_empty()).then_some(observation)
1560 })
1561 .collect::<Vec<_>>();
1562 let vectors = vector_observations
1563 .iter()
1564 .map(|observation| observation.vector.clone())
1565 .collect::<Vec<_>>();
1566 let conditions = decision
1567 .meta
1568 .conditions
1569 .iter()
1570 .enumerate()
1571 .map(|(index, source)| {
1572 let mut witness = None;
1573 let mut witness_tests = None;
1574 'pairs: for left in 0..vector_observations.len() {
1575 for right in (left + 1)..vector_observations.len() {
1576 let first = &vector_observations[left];
1577 let second = &vector_observations[right];
1578 if is_independence_pair(&first.vector, &second.vector, index) {
1579 witness = Some([first.vector.clone(), second.vector.clone()]);
1580 witness_tests = Some([first.tests.clone(), second.tests.clone()]);
1581 break 'pairs;
1582 }
1583 }
1584 }
1585 crate::coverage_report::ConditionResult {
1586 index,
1587 source: source.clone(),
1588 covered: witness.is_some(),
1589 assertion_covered: false,
1590 witness,
1591 witness_tests,
1592 }
1593 })
1594 .collect::<Vec<_>>();
1595 crate::coverage_report::DecisionResult {
1596 meta: decision.meta,
1597 executed: !vectors.is_empty(),
1598 covered: conditions.iter().all(|condition| condition.covered),
1599 vectors,
1600 vector_observations,
1601 conditions,
1602 tests: decision
1603 .tests
1604 .into_iter()
1605 .filter(|test| selected.contains(test.as_str()))
1606 .collect(),
1607 confidence: decision.confidence,
1608 }
1609}
1610
1611pub fn coverage_decision_query(
1612 index: &CoverageIndex<'_>,
1613 options: CoverageDecisionQueryOptions<'_>,
1614) -> Result<(CoverageDecisionData, AgentPagination), QueryError> {
1615 if options.limit == 0 {
1616 return Err(QueryError::InvalidPagination);
1617 }
1618 let tests = index.test_summaries(options.view)?;
1619 let selected = selected_test_ids(&tests, options.kind, options.runner)?;
1620 let decisions = index.decision_details(options.view)?;
1621 let mut matches = decisions
1622 .into_iter()
1623 .filter(|decision| decision.meta.id == options.selector)
1624 .collect::<Vec<_>>();
1625 if matches.is_empty()
1626 && let Some((file, line, column)) = selector_location(options.selector)
1627 {
1628 matches = index
1629 .decision_details(options.view)?
1630 .into_iter()
1631 .filter(|decision| {
1632 decision.meta.file == file
1633 && decision.meta.line == line
1634 && column.is_none_or(|column| decision.meta.column == column)
1635 })
1636 .collect();
1637 }
1638 if matches.is_empty() {
1639 return Err(QueryError::DecisionNotFound(options.selector.into()));
1640 }
1641 let filters = query_filters(options.view, options.kind, options.runner);
1642 if matches.len() > 1 {
1643 let total = matches.len();
1644 let page = matches
1645 .into_iter()
1646 .skip(options.offset)
1647 .take(options.limit)
1648 .map(|decision| CoverageDecisionMatch {
1649 id: decision.meta.id,
1650 file: decision.meta.file,
1651 line: decision.meta.line,
1652 column: decision.meta.column,
1653 source: decision.meta.source,
1654 })
1655 .collect::<Vec<_>>();
1656 let returned = page.len();
1657 return Ok((
1658 CoverageDecisionData::Matches(CoverageDecisionMatchesData {
1659 run: options.run.into(),
1660 filters,
1661 decisions: page,
1662 }),
1663 pagination(options.offset, options.limit, returned, total),
1664 ));
1665 }
1666 let filtered = selected_decision(
1667 matches.into_iter().next().expect("one decision match"),
1668 selected.as_ref(),
1669 );
1670 let totals = CoverageDecisionTotals {
1671 conditions: filtered.conditions.len(),
1672 vector_observations: filtered.vector_observations.len(),
1673 tests: filtered.tests.len(),
1674 };
1675 let total = totals
1676 .conditions
1677 .max(totals.vector_observations)
1678 .max(totals.tests);
1679 let vector_observations = filtered
1680 .vector_observations
1681 .iter()
1682 .skip(options.offset)
1683 .take(options.limit)
1684 .cloned()
1685 .collect::<Vec<_>>();
1686 let vectors = vector_observations
1687 .iter()
1688 .map(|observation| observation.vector.clone())
1689 .collect::<Vec<_>>();
1690 let observed = filtered
1691 .vector_observations
1692 .iter()
1693 .map(|observation| &observation.vector)
1694 .collect::<Vec<_>>();
1695 let conditions = filtered
1696 .conditions
1697 .iter()
1698 .skip(options.offset)
1699 .take(options.limit)
1700 .map(|condition| CoverageDecisionCondition {
1701 index: condition.index,
1702 source: condition.source.clone(),
1703 covered: condition.covered,
1704 assertion_covered: selected.is_none().then_some(condition.assertion_covered),
1705 witness: condition.witness.clone(),
1706 witness_tests: condition.witness_tests.clone(),
1707 missing_outcomes: (!condition.covered)
1708 .then(|| missing_outcomes(condition.index, &observed)),
1709 })
1710 .collect::<Vec<_>>();
1711 let tests = filtered
1712 .tests
1713 .iter()
1714 .skip(options.offset)
1715 .take(options.limit)
1716 .cloned()
1717 .collect::<Vec<_>>();
1718 let returned = vector_observations
1719 .len()
1720 .max(conditions.len())
1721 .max(tests.len());
1722 Ok((
1723 CoverageDecisionData::Detail(CoverageDecisionDetailData {
1724 run: options.run.into(),
1725 filters,
1726 pagination_applies_to:
1727 "conditions, vectorObservations, and tests independently within each decision"
1728 .into(),
1729 decisions: vec![CoverageSelectedDecision {
1730 meta: filtered.meta,
1731 executed: filtered.executed,
1732 covered: filtered.covered,
1733 vectors,
1734 vector_observations,
1735 conditions,
1736 tests,
1737 confidence: filtered.confidence,
1738 totals,
1739 }],
1740 }),
1741 pagination(options.offset, options.limit, returned, total),
1742 ))
1743}
1744
1745#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1746pub struct CoverageOtherTest {
1747 pub id: String,
1748 pub name: String,
1749}
1750
1751#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1752#[serde(rename_all = "camelCase")]
1753pub struct CoverageOtherCoverage {
1754 pub covered_elsewhere: bool,
1755 pub kinds: Vec<String>,
1756 pub runners: Vec<String>,
1757 pub tests: Vec<CoverageOtherTest>,
1758}
1759
1760#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1761#[serde(rename_all = "camelCase")]
1762pub struct CoverageLineObligation {
1763 pub kind: String,
1764 pub id: String,
1765 pub line: usize,
1766 pub other_coverage: CoverageOtherCoverage,
1767}
1768
1769#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1770#[serde(rename_all = "camelCase")]
1771pub struct CoveragePointObligation {
1772 pub kind: String,
1773 pub id: String,
1774 pub line: usize,
1775 pub column: usize,
1776 pub source: String,
1777 pub other_coverage: CoverageOtherCoverage,
1778}
1779
1780#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1781#[serde(rename_all = "camelCase")]
1782pub struct CoverageBranchObligation {
1783 pub kind: String,
1784 pub id: String,
1785 pub line: usize,
1786 pub column: usize,
1787 pub source: String,
1788 pub missing: String,
1789 pub other_coverage: CoverageOtherCoverage,
1790}
1791
1792#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1793#[serde(rename_all = "camelCase")]
1794pub struct CoverageMcdcObligation {
1795 pub kind: String,
1796 pub id: String,
1797 pub line: usize,
1798 pub column: usize,
1799 pub decision: String,
1800 pub missing_condition: String,
1801 #[serde(skip)]
1802 pub condition_index: usize,
1803 pub observed_vectors: Vec<String>,
1804 pub missing_outcomes: Vec<String>,
1807 pub needed_vector: Option<String>,
1812 pub needs: String,
1814 pub other_coverage: CoverageOtherCoverage,
1815}
1816
1817#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1818#[serde(untagged)]
1819pub enum CoverageFileObligation {
1820 Line(CoverageLineObligation),
1821 Point(CoveragePointObligation),
1822 Branch(CoverageBranchObligation),
1823 Mcdc(CoverageMcdcObligation),
1824}
1825
1826impl CoverageFileObligation {
1827 fn line(&self) -> usize {
1828 match self {
1829 Self::Line(value) => value.line,
1830 Self::Point(value) => value.line,
1831 Self::Branch(value) => value.line,
1832 Self::Mcdc(value) => value.line,
1833 }
1834 }
1835
1836 fn kind(&self) -> &str {
1837 match self {
1838 Self::Line(value) => &value.kind,
1839 Self::Point(value) => &value.kind,
1840 Self::Branch(value) => &value.kind,
1841 Self::Mcdc(value) => &value.kind,
1842 }
1843 }
1844}
1845
1846#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1847pub struct CoverageFileLimitation {
1848 pub id: String,
1849 pub kind: String,
1850 pub file: String,
1851 pub line: usize,
1852 pub column: usize,
1853 pub source: String,
1854 pub reason: String,
1855 pub blocking: bool,
1856 pub effect: String,
1857}
1858
1859#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1860#[serde(rename_all = "camelCase")]
1861pub struct CoverageFileCounts {
1862 pub total_lines: usize,
1865 pub covered_lines: usize,
1866 pub uncovered_lines: usize,
1867 pub uncovered_statements: usize,
1868 pub uncovered_functions: usize,
1869 pub missing_branches: usize,
1870 pub missing_mcdc_conditions: usize,
1871 pub measurement_limitations: usize,
1872}
1873
1874#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1875#[serde(rename_all = "camelCase")]
1876pub struct CoverageFileGapLine {
1877 pub line: usize,
1878 pub state: String,
1879 #[serde(skip_serializing_if = "Option::is_none")]
1880 pub source: Option<String>,
1881 pub obligations: Vec<CoverageFileObligation>,
1882 pub limitations: Vec<CoverageFileLimitation>,
1883}
1884
1885#[derive(Debug, Clone, PartialEq, Serialize)]
1886#[serde(rename_all = "camelCase")]
1887pub struct CoverageFileDetailData {
1888 pub run: String,
1889 pub filters: CoverageQueryFilters,
1890 pub file: String,
1891 pub metric: MinimizeMetric,
1892 pub counts: CoverageFileCounts,
1893 pub total_tests: usize,
1894 pub total_obligations: usize,
1895 pub total_gap_lines: usize,
1896 pub gap_lines: Vec<CoverageFileGapLine>,
1897 pub total_limitations: usize,
1898 #[serde(skip_serializing_if = "Option::is_none")]
1901 pub assertions: Option<serde_json::Value>,
1902}
1903
1904#[derive(Debug, Clone, Copy)]
1905pub struct CoverageFileDetailOptions<'a> {
1906 pub run: &'a str,
1907 pub view: CoverageViewId,
1908 pub kind: Option<&'a str>,
1909 pub runner: Option<&'a str>,
1910 pub selector: &'a str,
1911 pub metric: MinimizeMetric,
1912 pub offset: usize,
1913 pub limit: usize,
1914}
1915
1916fn other_coverage<T: AsRef<str>>(
1917 test_ids: &[T],
1918 selected: Option<&BTreeSet<String>>,
1919 tests: &HashMap<String, IndexedTestSummary>,
1920) -> CoverageOtherCoverage {
1921 let covered = selected.map_or_else(Vec::new, |selected| {
1922 test_ids
1923 .iter()
1924 .filter(|id| !selected.contains(id.as_ref()))
1925 .filter_map(|id| tests.get(id.as_ref()))
1926 .collect::<Vec<_>>()
1927 });
1928 CoverageOtherCoverage {
1929 covered_elsewhere: !covered.is_empty(),
1930 kinds: covered
1931 .iter()
1932 .map(|test| test.provenance.kind.clone())
1933 .collect::<BTreeSet<_>>()
1934 .into_iter()
1935 .collect(),
1936 runners: covered
1937 .iter()
1938 .map(|test| test.provenance.runner.clone())
1939 .collect::<BTreeSet<_>>()
1940 .into_iter()
1941 .collect(),
1942 tests: covered
1943 .into_iter()
1944 .map(|test| CoverageOtherTest {
1945 id: test.id.clone(),
1946 name: test.name.clone(),
1947 })
1948 .collect(),
1949 }
1950}
1951
1952fn missing_outcomes(
1956 index: usize,
1957 observed: &[&crate::coverage_analysis::McdcVector],
1958) -> Vec<String> {
1959 let seen = |value: bool| {
1960 observed
1961 .iter()
1962 .any(|vector| vector.values.get(index).copied().flatten() == Some(value))
1963 };
1964 [true, false]
1965 .into_iter()
1966 .filter(|value| !seen(*value))
1967 .map(|value| value.to_string())
1968 .collect()
1969}
1970
1971pub fn missing_outcome_words(condition: &str, missing: &[String]) -> String {
1974 match missing {
1975 [] => format!("`{condition}` was seen both ways but never deciding the result alone"),
1976 [value] => format!("`{condition}` was never {value}"),
1977 _ => format!("`{condition}` was never evaluated"),
1978 }
1979}
1980
1981fn needed(
1985 index: usize,
1986 observed: &[&crate::coverage_analysis::McdcVector],
1987 sources: &[&str],
1988) -> (Option<String>, String) {
1989 let condition = sources.get(index).copied().unwrap_or("the condition");
1990 let Some(seen) = observed
1991 .iter()
1992 .find(|vector| vector.values.get(index).copied().flatten().is_some())
1993 else {
1994 return (None, format!("no test evaluated `{condition}`"));
1995 };
1996 let mut values = String::new();
1997 let mut words = Vec::new();
1998 for (position, value) in seen.values.iter().enumerate() {
1999 let value = match position.cmp(&index) {
2000 std::cmp::Ordering::Less => *value,
2001 std::cmp::Ordering::Equal => value.map(|value| !value),
2002 std::cmp::Ordering::Greater => None,
2003 };
2004 values.push(match value {
2005 None => '-',
2006 Some(false) => 'F',
2007 Some(true) => 'T',
2008 });
2009 if let (Some(value), Some(source)) = (value, sources.get(position)) {
2010 words.push(format!("`{source}` {value}"));
2011 }
2012 }
2013 let outcome = !seen.outcome;
2014 words.push(format!("decision {outcome}"));
2015 (
2016 Some(format!("{values} -> {}", if outcome { 'T' } else { 'F' })),
2017 words.join(", "),
2018 )
2019}
2020
2021fn vector_text(vector: &crate::coverage_analysis::McdcVector) -> String {
2022 let values = vector
2023 .values
2024 .iter()
2025 .map(|value| match value {
2026 None => '-',
2027 Some(false) => 'F',
2028 Some(true) => 'T',
2029 })
2030 .collect::<String>();
2031 format!("{values} -> {}", if vector.outcome { 'T' } else { 'F' })
2032}
2033
2034fn obligation_matches_metric(obligation: &CoverageFileObligation, metric: MinimizeMetric) -> bool {
2035 metric == MinimizeMetric::All
2036 || matches!(
2037 (obligation.kind(), metric),
2038 ("line", MinimizeMetric::Lines)
2039 | ("statement", MinimizeMetric::Statements)
2040 | ("function", MinimizeMetric::Functions)
2041 | ("branch", MinimizeMetric::Branches)
2042 | ("mcdc", MinimizeMetric::Mcdc)
2043 )
2044}
2045
2046fn compact_source(value: &str) -> Option<String> {
2047 let line = value.lines().find(|line| !line.trim().is_empty())?.trim();
2048 let compact = line.split_whitespace().collect::<Vec<_>>().join(" ");
2049 if compact.is_empty() {
2050 None
2051 } else if compact.chars().count() > 120 {
2052 Some(format!(
2053 "{}…",
2054 compact.chars().take(119).collect::<String>()
2055 ))
2056 } else {
2057 Some(compact)
2058 }
2059}
2060
2061fn obligation_source(obligation: &CoverageFileObligation) -> Option<String> {
2062 match obligation {
2063 CoverageFileObligation::Line(_) => None,
2064 CoverageFileObligation::Point(value) => compact_source(&value.source),
2065 CoverageFileObligation::Branch(value) => compact_source(&value.source),
2066 CoverageFileObligation::Mcdc(value) => compact_source(&value.decision),
2067 }
2068}
2069
2070pub struct FileDetailSource {
2078 view: CoverageViewId,
2079 tests: Vec<crate::coverage_index::IndexedTestDetail>,
2080 test_summaries: Vec<IndexedTestSummary>,
2081 tests_by_id: HashMap<String, IndexedTestSummary>,
2082 tests_by_file: HashMap<String, Vec<String>>,
2084 lines: Vec<crate::coverage_index::IndexedLine>,
2085 lines_by_file: BTreeMap<String, Vec<usize>>,
2086 points: Vec<crate::coverage_index::IndexedHitMetadata>,
2087 points_by_file: HashMap<String, Vec<usize>>,
2088 decisions: Vec<crate::coverage_report::DecisionResult>,
2089 decisions_by_file: HashMap<String, Vec<usize>>,
2090 limitations: BTreeMap<String, Vec<crate::coverage_index::IndexedLimitation>>,
2091}
2092
2093impl FileDetailSource {
2094 pub fn read_file(
2098 index: &CoverageIndex<'_>,
2099 view: CoverageViewId,
2100 file: &str,
2101 ) -> Result<Self, QueryError> {
2102 let test_summaries = index.test_summaries(view)?;
2103 let tests_by_id = test_summaries
2104 .iter()
2105 .map(|test| (test.id.clone(), test.clone()))
2106 .collect();
2107 let lines = index.lines_in_file(view, file)?;
2108 let points = index.hit_metadata_in_file(view, file)?;
2109 let decisions = index.decision_details_in_file(view, file)?;
2110 let limitations = index.limitations_in_file(view, file)?;
2111 let mut source = Self {
2112 view,
2113 tests: Vec::new(),
2114 test_summaries,
2115 tests_by_id,
2116 tests_by_file: HashMap::new(),
2117 lines_by_file: BTreeMap::new(),
2118 points_by_file: HashMap::new(),
2119 decisions_by_file: HashMap::new(),
2120 limitations: BTreeMap::new(),
2121 lines,
2122 points,
2123 decisions,
2124 };
2125 if !source.lines.is_empty() {
2126 source
2127 .lines_by_file
2128 .insert(file.to_owned(), (0..source.lines.len()).collect());
2129 let mut tests = source
2132 .lines
2133 .iter()
2134 .flat_map(|line| line.tests.iter().cloned())
2135 .collect::<Vec<_>>();
2136 tests.sort_unstable();
2137 tests.dedup();
2138 source.tests_by_file.insert(file.to_owned(), tests);
2139 }
2140 if !source.points.is_empty() {
2141 source
2142 .points_by_file
2143 .insert(file.to_owned(), (0..source.points.len()).collect());
2144 }
2145 if !source.decisions.is_empty() {
2146 source
2147 .decisions_by_file
2148 .insert(file.to_owned(), (0..source.decisions.len()).collect());
2149 }
2150 if !limitations.is_empty() {
2151 source.limitations.insert(file.to_owned(), limitations);
2152 }
2153 Ok(source)
2154 }
2155
2156 pub fn read(index: &CoverageIndex<'_>, view: CoverageViewId) -> Result<Self, QueryError> {
2157 let tests = index.test_details(view)?;
2158 let mut tests_by_file = HashMap::<String, Vec<String>>::new();
2159 for test in &tests {
2160 let mut files = test
2161 .lines
2162 .iter()
2163 .map(|line| line.file.as_str())
2164 .collect::<Vec<_>>();
2165 files.sort_unstable();
2166 files.dedup();
2167 for file in files {
2168 tests_by_file
2169 .entry(file.to_owned())
2170 .or_default()
2171 .push(test.summary.id.clone());
2172 }
2173 }
2174 let test_summaries = tests
2175 .iter()
2176 .map(|test| test.summary.clone())
2177 .collect::<Vec<_>>();
2178 let tests_by_id = test_summaries
2179 .iter()
2180 .map(|test| (test.id.clone(), test.clone()))
2181 .collect();
2182 let lines = index.lines(view)?;
2183 let mut lines_by_file = BTreeMap::<String, Vec<usize>>::new();
2184 for (at, line) in lines.iter().enumerate() {
2185 lines_by_file.entry(line.file.clone()).or_default().push(at);
2186 }
2187 let points = index.hit_metadata(view)?;
2188 let mut points_by_file = HashMap::<String, Vec<usize>>::new();
2189 for (at, point) in points.iter().enumerate() {
2190 points_by_file
2191 .entry(point.file.clone())
2192 .or_default()
2193 .push(at);
2194 }
2195 let decisions = index.decision_details(view)?;
2196 let mut decisions_by_file = HashMap::<String, Vec<usize>>::new();
2197 for (at, decision) in decisions.iter().enumerate() {
2198 decisions_by_file
2199 .entry(decision.meta.file.clone())
2200 .or_default()
2201 .push(at);
2202 }
2203 let mut limitations = BTreeMap::<String, Vec<_>>::new();
2204 for limitation in index.limitations(view)? {
2205 limitations
2206 .entry(limitation.file.clone())
2207 .or_default()
2208 .push(limitation);
2209 }
2210 Ok(Self {
2211 view,
2212 tests,
2213 test_summaries,
2214 tests_by_id,
2215 tests_by_file,
2216 lines,
2217 lines_by_file,
2218 points,
2219 points_by_file,
2220 decisions,
2221 decisions_by_file,
2222 limitations,
2223 })
2224 }
2225
2226 pub fn tests(&self) -> &[crate::coverage_index::IndexedTestDetail] {
2229 &self.tests
2230 }
2231
2232 pub fn lines(&self) -> &[crate::coverage_index::IndexedLine] {
2233 &self.lines
2234 }
2235
2236 pub fn points(&self) -> &[crate::coverage_index::IndexedHitMetadata] {
2237 &self.points
2238 }
2239
2240 pub fn decisions(&self) -> &[crate::coverage_report::DecisionResult] {
2241 &self.decisions
2242 }
2243}
2244
2245pub fn coverage_file_detail_query(
2246 index: &CoverageIndex<'_>,
2247 options: CoverageFileDetailOptions<'_>,
2248) -> Result<(CoverageFileDetailData, AgentPagination), QueryError> {
2249 if options.limit == 0 {
2250 return Err(QueryError::InvalidPagination);
2251 }
2252 let scoped = FileDetailSource::read_file(index, options.view, options.selector)?;
2255 if !scoped.lines.is_empty() || !scoped.limitations.is_empty() {
2256 return coverage_file_detail_from(&scoped, options);
2257 }
2258 coverage_file_detail_from(&FileDetailSource::read(index, options.view)?, options)
2259}
2260
2261pub fn coverage_file_detail_from(
2264 read: &FileDetailSource,
2265 options: CoverageFileDetailOptions<'_>,
2266) -> Result<(CoverageFileDetailData, AgentPagination), QueryError> {
2267 if options.limit == 0 {
2268 return Err(QueryError::InvalidPagination);
2269 }
2270 debug_assert!(read.view == options.view);
2271 let selected = selected_test_ids(&read.test_summaries, options.kind, options.runner)?;
2272 let tests_by_id = &read.tests_by_id;
2273 let files = read
2274 .lines_by_file
2275 .keys()
2276 .chain(read.limitations.keys())
2277 .map(String::as_str)
2278 .collect::<BTreeSet<_>>();
2279 let file = if files.contains(options.selector) {
2280 options.selector.to_owned()
2281 } else {
2282 let matches = files
2283 .into_iter()
2284 .filter(|file| file.contains(options.selector))
2285 .collect::<Vec<_>>();
2286 if matches.is_empty() {
2287 return Err(QueryError::SourceNotFound(options.selector.into()));
2288 }
2289 if matches.len() != 1 {
2290 return Err(QueryError::AmbiguousSelector {
2291 selector: options.selector.into(),
2292 matches: matches.into_iter().map(str::to_owned).collect(),
2293 });
2294 }
2295 matches[0].to_owned()
2296 };
2297 let lines = read
2298 .lines_by_file
2299 .get(&file)
2300 .into_iter()
2301 .flatten()
2302 .map(|at| &read.lines[*at])
2303 .collect::<Vec<_>>();
2304 let metadata = read
2305 .points_by_file
2306 .get(&file)
2307 .into_iter()
2308 .flatten()
2309 .map(|at| &read.points[*at])
2310 .collect::<Vec<_>>();
2311 let selected_includes = |tests: &[String]| {
2312 selected.as_ref().map_or(!tests.is_empty(), |selected| {
2313 tests.iter().any(|test| selected.contains(test))
2314 })
2315 };
2316 let total_lines = lines.iter().filter(|line| line.measured).count();
2317 let uncovered_lines = lines
2318 .iter()
2319 .filter(|line| line.measured && !selected_includes(&line.tests))
2320 .map(|line| {
2321 CoverageFileObligation::Line(CoverageLineObligation {
2322 kind: "line".into(),
2323 id: format!("line:{}:{}", line.file, line.line),
2324 line: line.line,
2325 other_coverage: other_coverage(&line.tests, selected.as_ref(), tests_by_id),
2326 })
2327 })
2328 .collect::<Vec<_>>();
2329 let statements = metadata
2330 .iter()
2331 .filter(|point| point.obligation == "statement" && !selected_includes(&point.tests))
2332 .map(|point| {
2333 CoverageFileObligation::Point(CoveragePointObligation {
2334 kind: "statement".into(),
2335 id: point.id.clone(),
2336 line: point.line,
2337 column: point.column,
2338 source: point.label.clone().unwrap_or_else(|| point.source.clone()),
2339 other_coverage: other_coverage(&point.tests, selected.as_ref(), tests_by_id),
2340 })
2341 })
2342 .collect::<Vec<_>>();
2343 let functions = metadata
2344 .iter()
2345 .filter(|point| point.obligation == "function" && !selected_includes(&point.tests))
2346 .map(|point| {
2347 CoverageFileObligation::Point(CoveragePointObligation {
2348 kind: "function".into(),
2349 id: point.id.clone(),
2350 line: point.line,
2351 column: point.column,
2352 source: point.label.clone().unwrap_or_else(|| point.source.clone()),
2353 other_coverage: other_coverage(&point.tests, selected.as_ref(), tests_by_id),
2354 })
2355 })
2356 .collect::<Vec<_>>();
2357 let branches = metadata
2358 .iter()
2359 .filter(|branch| branch.obligation == "branch" && !selected_includes(&branch.tests))
2360 .map(|branch| {
2361 CoverageFileObligation::Branch(CoverageBranchObligation {
2362 kind: "branch".into(),
2363 id: branch.id.clone(),
2364 line: branch.line,
2365 column: branch.column,
2366 source: branch.source.clone(),
2367 missing: branch.alternative.clone().unwrap_or_default(),
2368 other_coverage: other_coverage(&branch.tests, selected.as_ref(), tests_by_id),
2369 })
2370 })
2371 .collect::<Vec<_>>();
2372 let observed_lines = metadata
2376 .iter()
2377 .filter(|point| selected_includes(&point.tests))
2378 .map(|point| point.line)
2379 .collect::<BTreeSet<_>>();
2380 let mut mcdc = Vec::new();
2381 for original in read
2382 .decisions_by_file
2383 .get(&file)
2384 .into_iter()
2385 .flatten()
2386 .map(|at| &read.decisions[*at])
2387 {
2388 let filtered = selected_decision(original.clone(), selected.as_ref());
2389 for condition in filtered
2390 .conditions
2391 .iter()
2392 .filter(|condition| !condition.covered)
2393 {
2394 let original_tests = original.conditions[condition.index]
2395 .witness_tests
2396 .clone()
2397 .unwrap_or_default()
2398 .into_iter()
2399 .flatten()
2400 .collect::<Vec<_>>();
2401 let observed = filtered
2402 .vector_observations
2403 .iter()
2404 .map(|observation| &observation.vector)
2405 .collect::<Vec<_>>();
2406 let sources = filtered
2407 .conditions
2408 .iter()
2409 .map(|condition| condition.source.as_str())
2410 .collect::<Vec<_>>();
2411 let (needed_vector, needs) = needed(condition.index, &observed, &sources);
2412 mcdc.push(CoverageFileObligation::Mcdc(CoverageMcdcObligation {
2413 kind: "mcdc".into(),
2414 id: original.meta.id.clone(),
2415 line: original.meta.line,
2416 column: original.meta.column,
2417 decision: original.meta.source.clone(),
2418 missing_condition: condition.source.clone(),
2419 condition_index: condition.index,
2420 observed_vectors: observed.iter().map(|vector| vector_text(vector)).collect(),
2421 missing_outcomes: missing_outcomes(condition.index, &observed),
2422 needed_vector,
2423 needs,
2424 other_coverage: other_coverage(&original_tests, selected.as_ref(), tests_by_id),
2425 }));
2426 }
2427 }
2428 let mut obligations = uncovered_lines
2429 .iter()
2430 .chain(statements.iter())
2431 .chain(functions.iter())
2432 .chain(branches.iter())
2433 .chain(mcdc.iter())
2434 .filter(|obligation| obligation_matches_metric(obligation, options.metric))
2435 .cloned()
2436 .collect::<Vec<_>>();
2437 obligations.sort_by(|left, right| {
2438 left.line()
2439 .cmp(&right.line())
2440 .then_with(|| left.kind().cmp(right.kind()))
2441 });
2442 let mut limitations = read
2443 .limitations
2444 .get(&file)
2445 .into_iter()
2446 .flatten()
2447 .cloned()
2448 .map(|limitation| CoverageFileLimitation {
2449 id: limitation.id,
2450 kind: limitation.kind,
2451 file: limitation.file,
2452 line: limitation.line,
2453 column: limitation.column,
2454 source: limitation.source,
2455 reason: limitation.reason,
2456 blocking: limitation.blocking,
2457 effect: "outside-measured-denominator".into(),
2458 })
2459 .collect::<Vec<_>>();
2460 limitations.sort_by(|left, right| {
2461 left.line
2462 .cmp(&right.line)
2463 .then_with(|| left.column.cmp(&right.column))
2464 .then_with(|| left.id.cmp(&right.id))
2465 });
2466 let total_tests = read
2467 .tests_by_file
2468 .get(&file)
2469 .into_iter()
2470 .flatten()
2471 .filter(|test| {
2472 selected
2473 .as_ref()
2474 .is_none_or(|selected| selected.contains(*test))
2475 })
2476 .count();
2477 let total_obligations = obligations.len();
2478 let total_limitations = limitations.len();
2479 let mut grouped =
2480 BTreeMap::<usize, (Vec<CoverageFileObligation>, Vec<CoverageFileLimitation>)>::new();
2481 for obligation in obligations {
2482 grouped
2483 .entry(obligation.line())
2484 .or_default()
2485 .0
2486 .push(obligation);
2487 }
2488 for limitation in limitations {
2489 grouped
2490 .entry(limitation.line)
2491 .or_default()
2492 .1
2493 .push(limitation);
2494 }
2495 let gap_lines = grouped
2496 .into_iter()
2497 .map(|(line, (obligations, limitations))| {
2498 let source = obligations.iter().find_map(obligation_source).or_else(|| {
2499 limitations
2500 .iter()
2501 .find_map(|value| compact_source(&value.source))
2502 });
2503 let never_ran = !observed_lines.contains(&line)
2507 && !obligations.is_empty()
2508 && obligations
2509 .iter()
2510 .all(|value| !matches!(value, CoverageFileObligation::Mcdc(_)));
2511 let state = if never_ran
2512 || obligations
2513 .iter()
2514 .any(|value| matches!(value, CoverageFileObligation::Line(_)))
2515 {
2516 "missing"
2517 } else if !obligations.is_empty() || observed_lines.contains(&line) {
2518 "part"
2521 } else {
2522 "limited"
2523 };
2524 CoverageFileGapLine {
2525 line,
2526 state: state.into(),
2527 source,
2528 obligations,
2529 limitations,
2530 }
2531 })
2532 .collect::<Vec<_>>();
2533 let total_gap_lines = gap_lines.len();
2534 let selected_gap_lines = gap_lines
2535 .into_iter()
2536 .skip(options.offset)
2537 .take(options.limit)
2538 .collect::<Vec<_>>();
2539 let returned = selected_gap_lines.len();
2540 Ok((
2541 CoverageFileDetailData {
2542 run: options.run.into(),
2543 filters: query_filters(options.view, options.kind, options.runner),
2544 file,
2545 metric: options.metric,
2546 counts: CoverageFileCounts {
2547 total_lines,
2548 covered_lines: total_lines - uncovered_lines.len(),
2549 uncovered_lines: uncovered_lines.len(),
2550 uncovered_statements: statements.len(),
2551 uncovered_functions: functions.len(),
2552 missing_branches: branches.len(),
2553 missing_mcdc_conditions: mcdc.len(),
2554 measurement_limitations: total_limitations,
2555 },
2556 total_tests,
2557 total_obligations,
2558 total_gap_lines,
2559 gap_lines: selected_gap_lines,
2560 total_limitations,
2561 assertions: None,
2562 },
2563 pagination(options.offset, options.limit, returned, total_gap_lines),
2564 ))
2565}
2566
2567#[derive(Debug, Clone, PartialEq, Serialize)]
2568pub struct CoverageDiffDelta {
2569 #[serde(serialize_with = "crate::coverage_analysis::serialize_javascript_number")]
2570 pub lines: f64,
2571 #[serde(serialize_with = "crate::coverage_analysis::serialize_javascript_number")]
2572 pub branches: f64,
2573 #[serde(serialize_with = "crate::coverage_analysis::serialize_javascript_number")]
2574 pub mcdc: f64,
2575}
2576
2577#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
2578#[serde(rename_all = "camelCase")]
2579pub struct CoverageDiffSide {
2580 pub line_count: usize,
2581 pub branch_count: usize,
2582 pub mcdc_count: usize,
2583 pub lines: Vec<String>,
2584 pub branches: Vec<String>,
2585 pub mcdc: Vec<String>,
2586}
2587
2588#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
2590#[serde(rename_all = "camelCase")]
2591pub struct CoverageDiffFile {
2592 pub file: String,
2593 pub lost: CoverageDiffCounts,
2594 pub gained: CoverageDiffCounts,
2595}
2596
2597#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
2598pub struct CoverageDiffCounts {
2599 pub lines: usize,
2600 pub branches: usize,
2601 pub mcdc: usize,
2602}
2603
2604impl CoverageDiffCounts {
2605 fn total(&self) -> usize {
2606 self.lines + self.branches + self.mcdc
2607 }
2608}
2609
2610const DIFF_FILES: usize = 20;
2612
2613#[derive(Debug, Clone, PartialEq, Serialize)]
2614#[serde(rename_all = "camelCase")]
2615pub struct CoverageDiffData {
2616 pub filters: CoverageQueryFilters,
2617 pub older: String,
2618 pub newer: String,
2619 pub delta: CoverageDiffDelta,
2620 pub gained: CoverageDiffSide,
2621 pub lost: CoverageDiffSide,
2622 pub files: usize,
2626 pub by_file: Vec<CoverageDiffFile>,
2627 #[serde(skip_serializing_if = "Option::is_none")]
2630 pub inputs: Option<CoverageDiffInputs>,
2631 #[serde(skip_serializing_if = "std::ops::Not::not")]
2633 pub gained_only: bool,
2634}
2635
2636#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
2640#[serde(rename_all = "camelCase")]
2641pub struct CoverageDiffInputs {
2642 pub source_changed: bool,
2644 pub tests_changed: bool,
2645}
2646
2647#[derive(Debug, Clone, Copy)]
2648pub struct CoverageDiffQueryOptions<'a> {
2649 pub older_run: &'a str,
2650 pub newer_run: &'a str,
2651 pub view: CoverageViewId,
2652 pub kind: Option<&'a str>,
2653 pub runner: Option<&'a str>,
2654 pub offset: usize,
2655 pub limit: usize,
2656 pub gained_only: bool,
2660}
2661
2662struct DiffSnapshot {
2663 summary: CoverageSummary,
2664 lines: BTreeSet<String>,
2665 branches: HashMap<String, String>,
2666 mcdc: HashMap<String, String>,
2667}
2668
2669fn diff_snapshot(
2670 index: &CoverageIndex<'_>,
2671 view: CoverageViewId,
2672) -> Result<DiffSnapshot, QueryError> {
2673 let summary = index.summary(view)?;
2674 let lines = index
2675 .lines(view)?
2676 .into_iter()
2677 .filter(|line| line.covered)
2678 .map(|line| format!("{}:{}", line.file, line.line))
2679 .collect();
2680 let branches = index
2681 .hit_metadata(view)?
2682 .into_iter()
2683 .filter(|metadata| metadata.obligation == "branch" && !metadata.tests.is_empty())
2684 .map(|metadata| {
2685 let parent = metadata
2686 .parent_id
2687 .ok_or(QueryError::InvalidRecordSelection)?;
2688 Ok((
2689 format!("{parent}:{}", metadata.id),
2690 format!(
2691 "{}:{} {}",
2692 metadata.file,
2693 metadata.line,
2694 metadata.alternative.unwrap_or_default()
2695 ),
2696 ))
2697 })
2698 .collect::<Result<HashMap<_, _>, QueryError>>()?;
2699 let mcdc = index
2700 .decision_details(view)?
2701 .into_iter()
2702 .flat_map(|decision| {
2703 decision
2704 .conditions
2705 .into_iter()
2706 .filter(|condition| condition.covered)
2707 .map(move |condition| {
2708 (
2709 format!("{}:c{}", decision.meta.id, condition.index),
2710 format!(
2711 "{}:{} C{} {}",
2712 decision.meta.file,
2713 decision.meta.line,
2714 condition.index + 1,
2715 condition.source
2716 ),
2717 )
2718 })
2719 })
2720 .collect();
2721 Ok(DiffSnapshot {
2722 summary,
2723 lines,
2724 branches,
2725 mcdc,
2726 })
2727}
2728
2729fn js_string_cmp(left: &str, right: &str) -> std::cmp::Ordering {
2730 left.encode_utf16().cmp(right.encode_utf16())
2731}
2732
2733fn rounded_delta(newer: f64, older: f64) -> f64 {
2734 ((newer - older) * 100.0).round() / 100.0
2735}
2736
2737pub fn coverage_diff_query(
2738 older: &CoverageIndex<'_>,
2739 newer: &CoverageIndex<'_>,
2740 options: CoverageDiffQueryOptions<'_>,
2741) -> Result<(CoverageDiffData, AgentPagination), QueryError> {
2742 if options.limit == 0 {
2743 return Err(QueryError::InvalidPagination);
2744 }
2745 let older = diff_snapshot(older, options.view)?;
2746 let newer = diff_snapshot(newer, options.view)?;
2747 let mut gained_lines = newer
2748 .lines
2749 .difference(&older.lines)
2750 .cloned()
2751 .collect::<Vec<_>>();
2752 let mut lost_lines = older
2753 .lines
2754 .difference(&newer.lines)
2755 .cloned()
2756 .collect::<Vec<_>>();
2757 let mut gained_branches = newer
2758 .branches
2759 .iter()
2760 .filter(|(id, _)| !older.branches.contains_key(*id))
2761 .map(|(_, label)| label.clone())
2762 .collect::<Vec<_>>();
2763 let mut lost_branches = older
2764 .branches
2765 .iter()
2766 .filter(|(id, _)| !newer.branches.contains_key(*id))
2767 .map(|(_, label)| label.clone())
2768 .collect::<Vec<_>>();
2769 let mut gained_mcdc = newer
2770 .mcdc
2771 .iter()
2772 .filter(|(id, _)| !older.mcdc.contains_key(*id))
2773 .map(|(_, label)| label.clone())
2774 .collect::<Vec<_>>();
2775 let mut lost_mcdc = older
2776 .mcdc
2777 .iter()
2778 .filter(|(id, _)| !newer.mcdc.contains_key(*id))
2779 .map(|(_, label)| label.clone())
2780 .collect::<Vec<_>>();
2781 if options.gained_only {
2782 lost_lines.clear();
2783 lost_branches.clear();
2784 lost_mcdc.clear();
2785 }
2786 for values in [
2787 &mut gained_lines,
2788 &mut lost_lines,
2789 &mut gained_branches,
2790 &mut lost_branches,
2791 &mut gained_mcdc,
2792 &mut lost_mcdc,
2793 ] {
2794 values.sort_by(|left, right| js_string_cmp(left, right));
2795 }
2796 let mut by_file = BTreeMap::<&str, (CoverageDiffCounts, CoverageDiffCounts)>::new();
2798 for (values, count) in [
2799 (&lost_lines, 0),
2800 (&lost_branches, 1),
2801 (&lost_mcdc, 2),
2802 (&gained_lines, 3),
2803 (&gained_branches, 4),
2804 (&gained_mcdc, 5),
2805 ] {
2806 for label in values {
2807 let place = label.split(' ').next().unwrap_or(label);
2808 let file = place.rsplit_once(':').map_or(place, |(file, _)| file);
2809 let (lost, gained) = by_file.entry(file).or_default();
2810 let side = if count < 3 { lost } else { gained };
2811 match count % 3 {
2812 0 => side.lines += 1,
2813 1 => side.branches += 1,
2814 _ => side.mcdc += 1,
2815 }
2816 }
2817 }
2818 let files = by_file.len();
2819 let mut by_file = by_file
2820 .into_iter()
2821 .map(|(file, (lost, gained))| CoverageDiffFile {
2822 file: file.to_owned(),
2823 lost,
2824 gained,
2825 })
2826 .collect::<Vec<_>>();
2827 by_file.sort_by(|left, right| {
2828 (right.lost.total(), right.gained.total())
2829 .cmp(&(left.lost.total(), left.gained.total()))
2830 .then_with(|| js_string_cmp(&left.file, &right.file))
2831 });
2832 by_file.truncate(DIFF_FILES);
2833 let total = [
2834 gained_lines.len(),
2835 gained_branches.len(),
2836 gained_mcdc.len(),
2837 lost_lines.len(),
2838 lost_branches.len(),
2839 lost_mcdc.len(),
2840 ]
2841 .into_iter()
2842 .max()
2843 .unwrap_or(0);
2844 let page = |values: &[String]| {
2845 values
2846 .iter()
2847 .skip(options.offset)
2848 .take(options.limit)
2849 .cloned()
2850 .collect::<Vec<_>>()
2851 };
2852 let gained = CoverageDiffSide {
2853 line_count: gained_lines.len(),
2854 branch_count: gained_branches.len(),
2855 mcdc_count: gained_mcdc.len(),
2856 lines: page(&gained_lines),
2857 branches: page(&gained_branches),
2858 mcdc: page(&gained_mcdc),
2859 };
2860 let lost = CoverageDiffSide {
2861 line_count: lost_lines.len(),
2862 branch_count: lost_branches.len(),
2863 mcdc_count: lost_mcdc.len(),
2864 lines: page(&lost_lines),
2865 branches: page(&lost_branches),
2866 mcdc: page(&lost_mcdc),
2867 };
2868 let returned = [
2869 gained.lines.len(),
2870 gained.branches.len(),
2871 gained.mcdc.len(),
2872 lost.lines.len(),
2873 lost.branches.len(),
2874 lost.mcdc.len(),
2875 ]
2876 .into_iter()
2877 .max()
2878 .unwrap_or(0);
2879 Ok((
2880 CoverageDiffData {
2881 filters: query_filters(options.view, options.kind, options.runner),
2882 older: options.older_run.into(),
2883 newer: options.newer_run.into(),
2884 delta: CoverageDiffDelta {
2885 lines: rounded_delta(
2886 newer.summary.lines.percentage,
2887 older.summary.lines.percentage,
2888 ),
2889 branches: rounded_delta(
2890 newer.summary.branches.percentage,
2891 older.summary.branches.percentage,
2892 ),
2893 mcdc: rounded_delta(
2894 newer.summary.condition_coverage_pct,
2895 older.summary.condition_coverage_pct,
2896 ),
2897 },
2898 gained,
2899 lost,
2900 files,
2901 by_file,
2902 inputs: None,
2903 gained_only: options.gained_only,
2904 },
2905 pagination(options.offset, options.limit, returned, total),
2906 ))
2907}
2908
2909pub fn coverage_scope_query(
2910 index: &CoverageIndex<'_>,
2911 options: CoverageScopeQueryOptions<'_>,
2912) -> Result<(CoverageScopeData, AgentPagination), QueryError> {
2913 if options.limit == 0 {
2914 return Err(QueryError::InvalidPagination);
2915 }
2916 let projection = index.projection(options.view, options.kind, options.runner)?;
2917 let scope = projection
2918 .source_scope
2919 .ok_or(QueryError::ScopeUnavailable)?;
2920 let mut entries = index.scope_entries(options.view)?;
2921 entries.sort_by(|left, right| {
2922 scope_status_rank(&left.status)
2923 .cmp(&scope_status_rank(&right.status))
2924 .then_with(|| left.file.cmp(&right.file))
2925 });
2926 let groups = scope_groups(&entries);
2927 let unclassified = unclassified_source(&scope.roots, &entries);
2928 let total = entries.len();
2929 let selected = entries
2930 .into_iter()
2931 .skip(options.offset)
2932 .take(options.limit)
2933 .collect::<Vec<_>>();
2934 let returned = selected.len();
2935 Ok((
2936 CoverageScopeData {
2937 run: options.run.into(),
2938 filters: CoverageQueryFilters {
2939 outcome: match options.view {
2940 CoverageViewId::All => "all",
2941 CoverageViewId::Passed => "passed",
2942 CoverageViewId::Failed => "failed",
2943 }
2944 .into(),
2945 kind: options.kind.map(str::to_owned),
2946 runner: options.runner.map(str::to_owned),
2947 },
2948 kind: scope.kind,
2949 language: scope.language,
2950 model: scope.model,
2951 mode: scope.mode,
2952 roots: scope.roots,
2953 unit: scope.unit,
2954 measurement_complete: scope.measurement_complete,
2955 counts: ScopeCounts {
2956 included: scope.included,
2957 excluded: scope.excluded,
2958 ambiguous: scope.ambiguous,
2959 },
2960 measurement: projection.measurement,
2961 groups,
2962 unclassified_source: unclassified,
2963 entries: selected,
2964 },
2965 pagination(options.offset, options.limit, returned, total),
2966 ))
2967}
2968
2969pub fn coverage_summary_query(
2970 index: &CoverageIndex<'_>,
2971 options: CoverageSummaryQueryOptions<'_>,
2972) -> Result<CoverageSummaryData, QueryError> {
2973 let projection = index.projection(options.view, options.kind, options.runner)?;
2974 let mut test_kind_sources = BTreeMap::new();
2975 for test in index.test_summaries(options.view)?.iter().filter(|t| {
2976 t.role == "test"
2977 && options.kind.is_none_or(|k| t.provenance.kind == k)
2978 && options.runner.is_none_or(|r| t.provenance.runner == r)
2979 }) {
2980 *test_kind_sources
2981 .entry(test.provenance.source.clone())
2982 .or_insert(0) += 1;
2983 }
2984 let mut diagnostics = Vec::new();
2985 let mut transport_blockers = 0usize;
2986 if projection.empty_evidence_tests > 0 {
2987 diagnostics.push(CoverageDiagnostic {
2988 code: "TEST_EVIDENCE_MISSING".into(),
2989 severity: "warning".into(),
2990 message: format!(
2991 "{} 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: {}",
2992 projection.empty_evidence_tests,
2993 projection.first_empty_evidence_test.as_deref().unwrap_or("unknown")
2994 ),
2995 });
2996 }
2997 if let Some(transport) = &projection.transport {
2998 if transport.corrupt_records > 0 {
2999 transport_blockers += 1;
3000 diagnostics.push(CoverageDiagnostic {
3001 code: "CORRUPT_EVIDENCE_RECORDS".into(),
3002 severity: "error".into(),
3003 message: format!(
3004 "{} malformed evidence record(s) in {} file(s) were excluded; coverage is incomplete.",
3005 transport.corrupt_records, transport.corrupt_files
3006 ),
3007 });
3008 }
3009 if transport.remote_launches > 0
3010 && transport.scoped_server_records == 0
3011 && transport.background_server_records == 0
3012 && projection.attribution.server_explicit == 0
3013 && projection.attribution.server_fallback == 0
3014 {
3015 transport_blockers += 1;
3016 diagnostics.push(CoverageDiagnostic {
3017 code: "REMOTE_SERVER_EVIDENCE_MISSING".into(),
3018 severity: "error".into(),
3019 message: "Remote launches were supervised, but neither scoped nor background server evidence returned. Supercov refuses to describe this measurement as complete.".into(),
3020 });
3021 }
3022 }
3023 let coverage_by_kind = index.dimensions(options.view, CoverageDimension::Kind)?;
3024 let coverage_by_runner = index.dimensions(options.view, CoverageDimension::Runner)?;
3025 let coverage_by_tests = if options.kind.is_none() && options.runner.is_none() {
3026 index
3027 .dimensions(options.view, CoverageDimension::Tests)?
3028 .into_iter()
3029 .next()
3030 .filter(|tests| tests.tests + tests.setups > 0)
3031 .map(|tests| tests.summary)
3032 } else {
3033 None
3034 };
3035 let other_e2e_kinds = coverage_by_kind
3036 .iter()
3037 .filter(|dimension| dimension.tests > 0)
3038 .filter_map(|dimension| dimension.kind.clone())
3039 .filter(|kind| kind != "e2e")
3040 .collect::<Vec<_>>();
3041 let e2e_observed = coverage_by_kind
3042 .iter()
3043 .any(|dimension| dimension.tests > 0 && dimension.kind.as_deref() == Some("e2e"));
3044 let e2e_gap_context = if options.kind.is_none()
3045 && options.runner.is_none()
3046 && e2e_observed
3047 && !other_e2e_kinds.is_empty()
3048 {
3049 let mut covered_elsewhere = IndexedGapDimensions {
3050 lines: 0,
3051 statements: 0,
3052 functions: 0,
3053 branches: 0,
3054 mcdc_conditions: 0,
3055 };
3056 let mut uncovered_everywhere = covered_elsewhere.clone();
3057 for gap in index.file_gaps(options.view, Some("e2e"), None)? {
3058 covered_elsewhere.lines += gap.covered_by_other_tests.lines;
3059 covered_elsewhere.statements += gap.covered_by_other_tests.statements;
3060 covered_elsewhere.functions += gap.covered_by_other_tests.functions;
3061 covered_elsewhere.branches += gap.covered_by_other_tests.branches;
3062 covered_elsewhere.mcdc_conditions += gap.covered_by_other_tests.mcdc_conditions;
3063 uncovered_everywhere.lines += gap.uncovered_everywhere.lines;
3064 uncovered_everywhere.statements += gap.uncovered_everywhere.statements;
3065 uncovered_everywhere.functions += gap.uncovered_everywhere.functions;
3066 uncovered_everywhere.branches += gap.uncovered_everywhere.branches;
3067 uncovered_everywhere.mcdc_conditions += gap.uncovered_everywhere.mcdc_conditions;
3068 }
3069 Some(CoverageKindGapContext {
3070 kind: "e2e".into(),
3071 other_kinds: other_e2e_kinds,
3072 covered_elsewhere,
3073 uncovered_everywhere,
3074 })
3075 } else {
3076 None
3077 };
3078 let filters = CoverageQueryFilters {
3079 outcome: match options.view {
3080 CoverageViewId::All => "all",
3081 CoverageViewId::Passed => "passed",
3082 CoverageViewId::Failed => "failed",
3083 }
3084 .into(),
3085 kind: options.kind.map(str::to_owned),
3086 runner: options.runner.map(str::to_owned),
3087 };
3088 let mut measurement = projection.measurement.clone();
3089 if transport_blockers > 0 {
3090 measurement.complete = false;
3091 measurement.limitations = measurement.limitations.saturating_add(transport_blockers);
3092 measurement.blocking = measurement.blocking.saturating_add(transport_blockers);
3093 }
3094 let structurally_complete = projection.summary.coverage_complete && measurement.complete;
3095 let complete = options.view == CoverageViewId::Passed
3096 && options.valid
3097 && !options.stale
3098 && structurally_complete;
3099 Ok(CoverageSummaryData {
3100 test_kind_sources,
3101 assertion_coverage: None,
3102 run: options.run.into(),
3103 command: Vec::new(),
3104 merged_across: Vec::new(),
3105 companions: Vec::new(),
3106 hints: Vec::new(),
3107 workspace: None,
3108 filters,
3109 model: index.model()?,
3110 generated_at: projection.generated_at,
3111 valid: options.valid,
3112 test_exit_code: options.test_exit_code,
3113 stale: options.stale,
3114 stale_reasons: options.stale_reasons,
3115 structurally_complete,
3116 complete,
3117 coverage: projection.summary,
3118 measurement,
3119 coverage_by_kind,
3120 coverage_by_tests,
3121 e2e_gap_context,
3122 coverage_by_runner,
3123 attribution: projection.attribution,
3124 test_attribution: {
3125 let mut counts = TestAttributionCounts::default();
3126 for test in index
3127 .test_summaries(options.view)?
3128 .iter()
3129 .filter(|test| test.role == "test")
3130 {
3131 match test.attribution.as_str() {
3132 crate::coverage_report::ATTRIBUTION_RUN_WIDE => counts.run_wide += 1,
3133 crate::coverage_report::ATTRIBUTION_PARTIAL => counts.partial += 1,
3134 crate::coverage_report::ATTRIBUTION_BY_TIME => counts.by_time += 1,
3135 _ => counts.exact += 1,
3136 }
3137 }
3138 counts
3139 },
3140 transport: projection.transport,
3141 diagnostics,
3142 confidence: (options.kind.is_none() && options.runner.is_none())
3143 .then_some(projection.confidence),
3144 files_with_gaps: projection.files_with_gaps,
3145 files_with_coverage_gaps: projection.files_with_coverage_gaps,
3146 files_with_measurement_limitations: projection.measurement.files,
3147 tests: projection.tests,
3148 setups: projection.setups,
3149 test_outcomes: projection.test_outcomes,
3150 unclassified_source: match &projection.source_scope {
3151 Some(scope) if scope.ambiguous > 0 => {
3152 unclassified_source(&scope.roots, &index.scope_entries(options.view)?)
3153 }
3154 _ => None,
3155 },
3156 source_scope: projection.source_scope,
3157 })
3158}
3159
3160#[derive(Debug, Clone, PartialEq)]
3161pub enum CoverageDimensionQueryData {
3162 Kinds(CoverageKindsData),
3163 Runners(CoverageRunnersData),
3164}
3165
3166#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
3169#[serde(rename_all = "camelCase")]
3170pub struct CoverageArea {
3171 pub directory: String,
3172 pub files: usize,
3173 pub totals: IndexedGapDimensions,
3174 pub covered: IndexedGapDimensions,
3175 pub by_kind: Vec<CoverageAreaKind>,
3176 #[serde(skip_serializing_if = "Option::is_none")]
3180 pub covered_by_tests: Option<IndexedGapDimensions>,
3181}
3182
3183#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
3184#[serde(rename_all = "camelCase")]
3185pub struct CoverageAreaKind {
3186 pub kind: String,
3187 pub covered: IndexedGapDimensions,
3188}
3189
3190#[derive(Debug, Clone, PartialEq, Serialize)]
3191#[serde(rename_all = "camelCase")]
3192pub struct CoverageAreasData {
3193 pub run: String,
3194 pub filters: CoverageQueryFilters,
3195 pub metric: MinimizeMetric,
3196 pub depth: usize,
3197 pub kinds: Vec<String>,
3200 pub areas: Vec<CoverageArea>,
3201}
3202
3203#[derive(Debug, Clone, Copy)]
3204pub struct CoverageAreasQueryOptions<'a> {
3205 pub run: &'a str,
3206 pub view: CoverageViewId,
3207 pub metric: MinimizeMetric,
3208 pub kind: Option<&'a str>,
3209 pub runner: Option<&'a str>,
3210 pub depth: usize,
3211 pub offset: usize,
3212 pub limit: usize,
3213}
3214
3215fn area_directory(file: &str, depth: usize) -> String {
3218 let mut folders = file.split('/').collect::<Vec<_>>();
3219 folders.pop();
3220 if folders.is_empty() {
3221 ".".into()
3222 } else {
3223 folders[..folders.len().min(depth)].join("/")
3224 }
3225}
3226
3227fn add_dimensions(sum: &mut IndexedGapDimensions, value: &IndexedGapDimensions) {
3228 sum.lines += value.lines;
3229 sum.statements += value.statements;
3230 sum.functions += value.functions;
3231 sum.branches += value.branches;
3232 sum.mcdc_conditions += value.mcdc_conditions;
3233}
3234
3235const NO_DIMENSIONS: IndexedGapDimensions = IndexedGapDimensions {
3236 lines: 0,
3237 statements: 0,
3238 functions: 0,
3239 branches: 0,
3240 mcdc_conditions: 0,
3241};
3242
3243pub fn dimension_for_metric(dimensions: &IndexedGapDimensions, metric: MinimizeMetric) -> usize {
3245 match metric {
3246 MinimizeMetric::All | MinimizeMetric::Lines => dimensions.lines,
3247 MinimizeMetric::Statements => dimensions.statements,
3248 MinimizeMetric::Functions => dimensions.functions,
3249 MinimizeMetric::Branches => dimensions.branches,
3250 MinimizeMetric::Mcdc => dimensions.mcdc_conditions,
3251 }
3252}
3253
3254pub fn coverage_areas_query(
3258 index: &CoverageIndex<'_>,
3259 options: CoverageAreasQueryOptions<'_>,
3260) -> Result<(CoverageAreasData, AgentPagination), QueryError> {
3261 if options.limit == 0 || options.depth == 0 {
3262 return Err(QueryError::InvalidPagination);
3263 }
3264 let files = index.file_gaps(options.view, options.kind, options.runner)?;
3265 if (options.kind.is_some() || options.runner.is_some()) && files.is_empty() {
3266 return Err(QueryError::TestFilterEmpty {
3267 kind: options.kind.map(str::to_owned),
3268 runner: options.runner.map(str::to_owned),
3269 });
3270 }
3271 let kinds = if options.kind.is_none() && options.runner.is_none() {
3272 index
3273 .dimensions(options.view, CoverageDimension::Kind)?
3274 .into_iter()
3275 .filter(|kind| kind.tests > 0 && kind.attributed > 0)
3276 .filter_map(|kind| kind.kind)
3277 .collect::<Vec<_>>()
3278 } else {
3279 Vec::new()
3280 };
3281 let mut areas = BTreeMap::<String, CoverageArea>::new();
3282 for file in &files {
3283 let directory = area_directory(&file.file, options.depth);
3284 let area = areas
3285 .entry(directory.clone())
3286 .or_insert_with(|| CoverageArea {
3287 directory,
3288 files: 0,
3289 totals: NO_DIMENSIONS,
3290 covered: NO_DIMENSIONS,
3291 by_kind: kinds
3292 .iter()
3293 .map(|kind| CoverageAreaKind {
3294 kind: kind.clone(),
3295 covered: NO_DIMENSIONS,
3296 })
3297 .collect(),
3298 covered_by_tests: None,
3299 });
3300 area.files += 1;
3301 add_dimensions(&mut area.totals, &file.totals);
3302 add_dimensions(&mut area.covered, &file.covered);
3303 }
3304 for (position, kind) in kinds.iter().enumerate() {
3305 for file in index.file_gaps(options.view, Some(kind), None)? {
3306 if let Some(area) = areas.get_mut(&area_directory(&file.file, options.depth)) {
3307 add_dimensions(&mut area.by_kind[position].covered, &file.covered);
3308 }
3309 }
3310 }
3311 if !kinds.is_empty() {
3312 let tested = index.file_gaps(options.view, Some(crate::coverage_index::ALL_TESTS), None)?;
3313 if !tested.is_empty() {
3314 for area in areas.values_mut() {
3315 area.covered_by_tests = Some(NO_DIMENSIONS);
3316 }
3317 for file in tested {
3318 if let Some(sum) = areas
3319 .get_mut(&area_directory(&file.file, options.depth))
3320 .and_then(|area| area.covered_by_tests.as_mut())
3321 {
3322 add_dimensions(sum, &file.covered);
3323 }
3324 }
3325 }
3326 }
3327 let mut areas = areas.into_values().collect::<Vec<_>>();
3328 areas.sort_by(|left, right| {
3329 dimension_for_metric(&right.totals, options.metric)
3330 .cmp(&dimension_for_metric(&left.totals, options.metric))
3331 .then_with(|| left.directory.cmp(&right.directory))
3332 });
3333 let total = areas.len();
3334 let page = areas
3335 .into_iter()
3336 .skip(options.offset)
3337 .take(options.limit)
3338 .collect::<Vec<_>>();
3339 let returned = page.len();
3340 Ok((
3341 CoverageAreasData {
3342 run: options.run.into(),
3343 filters: CoverageQueryFilters {
3344 outcome: match options.view {
3345 CoverageViewId::All => "all",
3346 CoverageViewId::Passed => "passed",
3347 CoverageViewId::Failed => "failed",
3348 }
3349 .into(),
3350 kind: options.kind.map(str::to_owned),
3351 runner: options.runner.map(str::to_owned),
3352 },
3353 metric: options.metric,
3354 depth: options.depth,
3355 kinds,
3356 areas: page,
3357 },
3358 pagination(options.offset, options.limit, returned, total),
3359 ))
3360}
3361
3362#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
3364#[serde(rename_all = "camelCase")]
3365pub struct TestWithoutEvidence {
3366 pub id: String,
3367 pub name: String,
3368 #[serde(skip_serializing_if = "Option::is_none")]
3369 pub file: Option<String>,
3370 pub kind: String,
3371 pub runner: String,
3372 pub outcome: String,
3373 pub assertions: usize,
3374 #[serde(skip_serializing_if = "Option::is_none")]
3378 pub imports_measured: Option<bool>,
3379}
3380
3381#[derive(Debug, Clone, PartialEq, Serialize)]
3382#[serde(rename_all = "camelCase")]
3383pub struct TestsWithoutEvidenceData {
3384 pub run: String,
3385 pub filters: CoverageQueryFilters,
3386 pub tests: Vec<TestWithoutEvidence>,
3387}
3388
3389#[derive(Debug, Clone, Copy)]
3390pub struct TestsWithoutEvidenceOptions<'a> {
3391 pub run: &'a str,
3392 pub view: CoverageViewId,
3393 pub kind: Option<&'a str>,
3394 pub runner: Option<&'a str>,
3395 pub offset: usize,
3396 pub limit: usize,
3397}
3398
3399pub fn tests_without_evidence_query(
3403 index: &CoverageIndex<'_>,
3404 options: TestsWithoutEvidenceOptions<'_>,
3405) -> Result<(TestsWithoutEvidenceData, AgentPagination), QueryError> {
3406 if options.limit == 0 {
3407 return Err(QueryError::InvalidPagination);
3408 }
3409 let mut assertions = BTreeMap::<String, usize>::new();
3410 for phase in index.phase_summaries(options.view)? {
3411 *assertions.entry(phase.test).or_insert(0) += 1;
3412 }
3413 let mut tests = index
3414 .test_details(options.view)?
3415 .into_iter()
3416 .filter(|test| {
3417 test.summary.role == "test"
3418 && test.lines.is_empty()
3419 && test.hits.is_empty()
3420 && test.decisions.is_empty()
3421 && options
3422 .kind
3423 .is_none_or(|kind| test.summary.provenance.kind == kind)
3424 && options
3425 .runner
3426 .is_none_or(|runner| test.summary.provenance.runner == runner)
3427 })
3428 .filter_map(|test| {
3429 let assertions = *assertions.get(&test.summary.id)?;
3430 Some(TestWithoutEvidence {
3431 id: test.summary.id,
3432 name: test.summary.name,
3433 file: test.summary.file,
3434 kind: test.summary.provenance.kind,
3435 runner: test.summary.provenance.runner,
3436 outcome: test.summary.outcome,
3437 assertions,
3438 imports_measured: None,
3439 })
3440 })
3441 .collect::<Vec<_>>();
3442 tests.sort_by(|left, right| {
3443 (&left.file, &left.name, &left.id).cmp(&(&right.file, &right.name, &right.id))
3444 });
3445 let total = tests.len();
3446 let page = tests
3447 .into_iter()
3448 .skip(options.offset)
3449 .take(options.limit)
3450 .collect::<Vec<_>>();
3451 let returned = page.len();
3452 Ok((
3453 TestsWithoutEvidenceData {
3454 run: options.run.into(),
3455 filters: CoverageQueryFilters {
3456 outcome: match options.view {
3457 CoverageViewId::All => "all",
3458 CoverageViewId::Passed => "passed",
3459 CoverageViewId::Failed => "failed",
3460 }
3461 .into(),
3462 kind: options.kind.map(str::to_owned),
3463 runner: options.runner.map(str::to_owned),
3464 },
3465 tests: page,
3466 },
3467 pagination(options.offset, options.limit, returned, total),
3468 ))
3469}
3470
3471#[derive(Debug, Clone, PartialEq)]
3472pub enum CoverageFileQueryData {
3473 Files(CoverageFilesData),
3474 Gaps(CoverageGapsData),
3475}
3476
3477#[derive(Debug, Clone, PartialEq)]
3478pub struct CoverageFileQueryResult {
3479 pub data: CoverageFileQueryData,
3480 pub pagination: AgentPagination,
3481}
3482
3483#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
3484#[serde(rename_all = "lowercase")]
3485pub enum DecisionSort {
3486 Location,
3487 Missing,
3488}
3489
3490#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
3491#[serde(rename_all = "camelCase")]
3492pub struct DecisionGapTotals {
3493 pub decisions: usize,
3494 pub decisions_with_missing_conditions: usize,
3495 pub conditions: usize,
3496 pub missing_conditions: usize,
3497}
3498
3499#[derive(Debug, Clone, PartialEq, Serialize)]
3500#[serde(rename_all = "camelCase")]
3501pub struct CoverageFileDecisionsData {
3502 pub run: String,
3503 pub filters: CoverageQueryFilters,
3504 pub file: String,
3505 pub group: String,
3506 pub sort: DecisionSort,
3507 pub totals: DecisionGapTotals,
3508 pub decisions: Vec<CoverageFileDecisionRow>,
3509}
3510
3511#[derive(Debug, Clone, PartialEq, Serialize)]
3513#[serde(rename_all = "camelCase")]
3514pub struct CoverageFileDecisionRow {
3515 #[serde(flatten)]
3516 pub gap: IndexedDecisionGap,
3517 pub missing: Vec<CoverageMissingCondition>,
3518}
3519
3520#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
3521#[serde(rename_all = "camelCase")]
3522pub struct CoverageMissingCondition {
3523 pub index: usize,
3524 pub condition: String,
3525 pub missing_outcomes: Vec<String>,
3528 pub needed_vector: Option<String>,
3529 pub needs: String,
3530}
3531
3532#[derive(Debug, Clone, Copy)]
3533pub struct CoverageFileDecisionsOptions<'a> {
3534 pub run: &'a str,
3535 pub view: CoverageViewId,
3536 pub kind: Option<&'a str>,
3537 pub runner: Option<&'a str>,
3538 pub file: &'a str,
3539 pub sort: DecisionSort,
3540 pub offset: usize,
3541 pub limit: usize,
3542}
3543
3544#[derive(Debug, Clone)]
3545pub struct CoverageDimensionQueryOptions<'a> {
3546 pub run: &'a str,
3547 pub view: CoverageViewId,
3548 pub dimension: CoverageDimension,
3549 pub filters: CoverageQueryFilters,
3550 pub offset: usize,
3551 pub limit: usize,
3552}
3553
3554pub fn coverage_dimension_query(
3555 index: &CoverageIndex<'_>,
3556 options: CoverageDimensionQueryOptions<'_>,
3557) -> Result<(CoverageDimensionQueryData, AgentPagination), QueryError> {
3558 let CoverageDimensionQueryOptions {
3559 run,
3560 view,
3561 dimension,
3562 filters,
3563 offset,
3564 limit,
3565 } = options;
3566 if limit == 0 {
3567 return Err(QueryError::InvalidPagination);
3568 }
3569 let values = index.dimensions(view, dimension)?;
3570 let total = values.len();
3571 let selected = values
3572 .into_iter()
3573 .skip(offset)
3574 .take(limit)
3575 .collect::<Vec<_>>();
3576 let returned = selected.len();
3577 let data = match dimension {
3578 CoverageDimension::Kind | CoverageDimension::Tests => {
3580 CoverageDimensionQueryData::Kinds(CoverageKindsData {
3581 run: run.into(),
3582 filters,
3583 kinds: selected,
3584 })
3585 }
3586 CoverageDimension::Runner => CoverageDimensionQueryData::Runners(CoverageRunnersData {
3587 run: run.into(),
3588 filters,
3589 runners: selected,
3590 }),
3591 };
3592 Ok((data, pagination(offset, limit, returned, total)))
3593}
3594
3595#[derive(Debug, Clone, Copy)]
3596pub struct CoverageFileQueryOptions<'a> {
3597 pub run: &'a str,
3598 pub view: CoverageViewId,
3599 pub metric: MinimizeMetric,
3600 pub gaps_only: bool,
3601 pub kind: Option<&'a str>,
3602 pub runner: Option<&'a str>,
3603 pub offset: usize,
3604 pub limit: usize,
3605}
3606
3607fn gap_metric_value(gap: &IndexedFileGap, metric: MinimizeMetric) -> usize {
3608 match metric {
3609 MinimizeMetric::All => gap.score,
3610 MinimizeMetric::Lines => gap.uncovered_lines,
3611 MinimizeMetric::Statements => gap.uncovered_statements,
3612 MinimizeMetric::Functions => gap.uncovered_functions,
3613 MinimizeMetric::Branches => gap.missing_branches,
3614 MinimizeMetric::Mcdc => gap.missing_mcdc_conditions,
3615 }
3616}
3617
3618fn has_gap_for_metric(gap: &IndexedFileGap, metric: MinimizeMetric) -> bool {
3619 match metric {
3620 MinimizeMetric::All => {
3621 gap.uncovered_lines > 0
3622 || gap.uncovered_statements > 0
3623 || gap.uncovered_functions > 0
3624 || gap.missing_branches > 0
3625 || gap.missing_mcdc_conditions > 0
3626 }
3627 _ => gap_metric_value(gap, metric) > 0,
3628 }
3629}
3630
3631pub fn coverage_file_decisions_query(
3632 index: &CoverageIndex<'_>,
3633 options: CoverageFileDecisionsOptions<'_>,
3634) -> Result<(CoverageFileDecisionsData, AgentPagination), QueryError> {
3635 if options.limit == 0 {
3636 return Err(QueryError::InvalidPagination);
3637 }
3638 let all = index.decision_gaps(options.view, options.kind, options.runner, options.file)?;
3639 if (options.kind.is_some() || options.runner.is_some()) && all.is_empty() {
3640 if index
3643 .file_gaps(options.view, options.kind, options.runner)?
3644 .is_empty()
3645 {
3646 return Err(QueryError::TestFilterEmpty {
3647 kind: options.kind.map(str::to_owned),
3648 runner: options.runner.map(str::to_owned),
3649 });
3650 }
3651 }
3652 let totals = DecisionGapTotals {
3653 decisions: all.len(),
3654 decisions_with_missing_conditions: all
3655 .iter()
3656 .filter(|decision| decision.missing_conditions > 0)
3657 .count(),
3658 conditions: all.iter().map(|decision| decision.conditions).sum(),
3659 missing_conditions: all.iter().map(|decision| decision.missing_conditions).sum(),
3660 };
3661 let mut missing = all
3662 .into_iter()
3663 .filter(|decision| decision.missing_conditions > 0)
3664 .collect::<Vec<_>>();
3665 missing.sort_by(|left, right| match options.sort {
3666 DecisionSort::Missing => right
3667 .missing_conditions
3668 .cmp(&left.missing_conditions)
3669 .then_with(|| left.line.cmp(&right.line))
3670 .then_with(|| left.column.cmp(&right.column)),
3671 DecisionSort::Location => left
3672 .line
3673 .cmp(&right.line)
3674 .then_with(|| left.column.cmp(&right.column))
3675 .then_with(|| left.id.cmp(&right.id)),
3676 });
3677 let total = missing.len();
3678 let rows = missing
3679 .into_iter()
3680 .skip(options.offset)
3681 .take(options.limit)
3682 .collect::<Vec<_>>();
3683 let selected = if options.kind.is_some() || options.runner.is_some() {
3687 selected_test_ids(
3688 &index.test_summaries(options.view)?,
3689 options.kind,
3690 options.runner,
3691 )?
3692 } else {
3693 None
3694 };
3695 let details = index
3696 .decision_details_in_file(options.view, options.file)?
3697 .into_iter()
3698 .map(|decision| (decision.meta.id.clone(), decision))
3699 .collect::<HashMap<_, _>>();
3700 let rows = rows
3701 .into_iter()
3702 .map(|gap| {
3703 let missing = details
3704 .get(&gap.id)
3705 .map(|decision| {
3706 let filtered = selected_decision(decision.clone(), selected.as_ref());
3707 let observed = filtered
3708 .vector_observations
3709 .iter()
3710 .map(|observation| &observation.vector)
3711 .collect::<Vec<_>>();
3712 let sources = filtered
3713 .conditions
3714 .iter()
3715 .map(|condition| condition.source.as_str())
3716 .collect::<Vec<_>>();
3717 filtered
3718 .conditions
3719 .iter()
3720 .filter(|condition| !condition.covered)
3721 .map(|condition| {
3722 let (needed_vector, needs) =
3723 needed(condition.index, &observed, &sources);
3724 CoverageMissingCondition {
3725 index: condition.index,
3726 condition: condition.source.clone(),
3727 missing_outcomes: missing_outcomes(condition.index, &observed),
3728 needed_vector,
3729 needs,
3730 }
3731 })
3732 .collect()
3733 })
3734 .unwrap_or_default();
3735 CoverageFileDecisionRow { gap, missing }
3736 })
3737 .collect::<Vec<_>>();
3738 let returned = rows.len();
3739 let filters = CoverageQueryFilters {
3740 outcome: match options.view {
3741 CoverageViewId::All => "all",
3742 CoverageViewId::Passed => "passed",
3743 CoverageViewId::Failed => "failed",
3744 }
3745 .into(),
3746 kind: options.kind.map(str::to_owned),
3747 runner: options.runner.map(str::to_owned),
3748 };
3749 Ok((
3750 CoverageFileDecisionsData {
3751 run: options.run.into(),
3752 filters,
3753 file: options.file.into(),
3754 group: "decision".into(),
3755 sort: options.sort,
3756 totals,
3757 decisions: rows,
3758 },
3759 pagination(options.offset, options.limit, returned, total),
3760 ))
3761}
3762
3763pub fn coverage_file_query(
3764 index: &CoverageIndex<'_>,
3765 options: CoverageFileQueryOptions<'_>,
3766) -> Result<CoverageFileQueryResult, QueryError> {
3767 let CoverageFileQueryOptions {
3768 run,
3769 view,
3770 metric,
3771 gaps_only,
3772 kind,
3773 runner,
3774 offset,
3775 limit,
3776 } = options;
3777 if limit == 0 {
3778 return Err(QueryError::InvalidPagination);
3779 }
3780 let mut files = index.file_gaps(view, kind, runner)?;
3781 if (kind.is_some() || runner.is_some()) && files.is_empty() {
3782 return Err(QueryError::TestFilterEmpty {
3783 kind: kind.map(str::to_owned),
3784 runner: runner.map(str::to_owned),
3785 });
3786 }
3787 if gaps_only {
3788 files.retain(|gap| has_gap_for_metric(gap, metric) || gap.measurement_limitations > 0);
3789 }
3790 files.sort_by(|left, right| {
3791 gap_metric_value(right, metric)
3792 .cmp(&gap_metric_value(left, metric))
3793 .then_with(|| {
3794 right
3795 .measurement_limitations
3796 .cmp(&left.measurement_limitations)
3797 })
3798 .then_with(|| left.file.cmp(&right.file))
3799 });
3800 let total = files.len();
3801 let page = files
3802 .into_iter()
3803 .skip(offset)
3804 .take(limit)
3805 .collect::<Vec<_>>();
3806 let page_info = pagination(offset, limit, page.len(), total);
3807 let filters = CoverageQueryFilters {
3808 outcome: match view {
3809 CoverageViewId::All => "all",
3810 CoverageViewId::Passed => "passed",
3811 CoverageViewId::Failed => "failed",
3812 }
3813 .into(),
3814 kind: kind.map(str::to_owned),
3815 runner: runner.map(str::to_owned),
3816 };
3817 let data = if gaps_only {
3818 CoverageFileQueryData::Gaps(CoverageGapsData {
3819 run: run.into(),
3820 filters,
3821 metric,
3822 gaps: page,
3823 })
3824 } else {
3825 CoverageFileQueryData::Files(CoverageFilesData {
3826 run: run.into(),
3827 filters,
3828 metric,
3829 files: page,
3830 })
3831 };
3832 Ok(CoverageFileQueryResult {
3833 data,
3834 pagination: page_info,
3835 })
3836}
3837
3838#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
3839enum ObligationMetric {
3840 Lines,
3841 Statements,
3842 Functions,
3843 Branches,
3844 Mcdc,
3845}
3846
3847impl ObligationMetric {
3848 fn selected(self, metric: MinimizeMetric) -> bool {
3849 metric == MinimizeMetric::All
3850 || matches!(
3851 (self, metric),
3852 (Self::Lines, MinimizeMetric::Lines)
3853 | (Self::Statements, MinimizeMetric::Statements)
3854 | (Self::Functions, MinimizeMetric::Functions)
3855 | (Self::Branches, MinimizeMetric::Branches)
3856 | (Self::Mcdc, MinimizeMetric::Mcdc)
3857 )
3858 }
3859
3860 fn public(self) -> MinimizeMetric {
3861 match self {
3862 Self::Lines => MinimizeMetric::Lines,
3863 Self::Statements => MinimizeMetric::Statements,
3864 Self::Functions => MinimizeMetric::Functions,
3865 Self::Branches => MinimizeMetric::Branches,
3866 Self::Mcdc => MinimizeMetric::Mcdc,
3867 }
3868 }
3869}
3870
3871#[derive(Clone)]
3872struct Obligation {
3873 id: String,
3874 metric: ObligationMetric,
3875 options: Vec<Vec<String>>,
3877}
3878
3879struct ObligationModel {
3880 obligations: Vec<Obligation>,
3881 setup_by_file: BTreeMap<String, Vec<String>>,
3882 tests_by_file: BTreeMap<String, Vec<String>>,
3883 background: Vec<String>,
3884}
3885
3886impl ObligationModel {
3887 fn expand(&self, selected: &BTreeSet<String>) -> BTreeSet<String> {
3888 let mut expanded = selected.clone();
3889 expanded.extend(self.background.iter().cloned());
3890 for (file, setup_ids) in &self.setup_by_file {
3891 if self
3892 .tests_by_file
3893 .get(file)
3894 .is_some_and(|tests| tests.iter().any(|id| selected.contains(id)))
3895 {
3896 expanded.extend(setup_ids.iter().cloned());
3897 }
3898 }
3899 expanded
3900 }
3901}
3902
3903fn deduplicate_options(options: impl IntoIterator<Item = Vec<String>>) -> Vec<Vec<String>> {
3904 let mut unique = BTreeMap::<String, Vec<String>>::new();
3905 for mut option in options {
3906 option.sort();
3907 option.dedup();
3908 let key = option.join("\0");
3909 unique.entry(key).or_insert(option);
3910 }
3911 unique.into_values().collect()
3912}
3913
3914fn evidence_choices(
3915 ids: &[Id],
3916 tests: &HashMap<&str, &crate::coverage_report::TestCoverageResult>,
3917 candidates: &BTreeSet<String>,
3918 tests_by_file: &BTreeMap<String, Vec<String>>,
3919) -> Vec<Vec<String>> {
3920 let mut choices = Vec::new();
3921 for id in ids {
3922 let Some(test) = tests.get(id.as_str()) else {
3923 continue;
3924 };
3925 if test.role == "background" {
3926 choices.push(Vec::new());
3927 } else if test.role == "setup" {
3928 if let Some(file) = &test.file {
3929 choices.extend(
3930 tests_by_file
3931 .get(file)
3932 .into_iter()
3933 .flatten()
3934 .map(|candidate| vec![candidate.clone()]),
3935 );
3936 }
3937 } else if candidates.contains(id.as_str()) {
3938 choices.push(vec![id.to_string()]);
3939 }
3940 }
3941 deduplicate_options(choices)
3942}
3943
3944fn build_obligations(view: &CoverageView, candidates: &BTreeSet<String>) -> ObligationModel {
3945 let tests = view
3946 .tests
3947 .iter()
3948 .map(|test| (test.id.as_str(), test))
3949 .collect::<HashMap<_, _>>();
3950 let mut tests_by_file = BTreeMap::<String, Vec<String>>::new();
3951 let mut setup_by_file = BTreeMap::<String, Vec<String>>::new();
3952 let mut background = Vec::new();
3953 for test in &view.tests {
3954 match test.role.as_str() {
3955 "test" if candidates.contains(&test.id) => {
3956 if let Some(file) = &test.file {
3957 tests_by_file
3958 .entry(file.clone())
3959 .or_default()
3960 .push(test.id.clone());
3961 }
3962 }
3963 "setup" => {
3964 if let Some(file) = &test.file {
3965 setup_by_file
3966 .entry(file.clone())
3967 .or_default()
3968 .push(test.id.clone());
3969 }
3970 }
3971 "background" => background.push(test.id.clone()),
3972 _ => {}
3973 }
3974 }
3975 let choices = |ids: &[Id]| evidence_choices(ids, &tests, candidates, &tests_by_file);
3976 let mut obligations = Vec::new();
3977 let mut unique_lines = BTreeMap::new();
3978 for line in &view.lines {
3979 unique_lines.insert((line.file.as_str(), line.line), line);
3980 }
3981 for ((file, line), result) in unique_lines {
3982 obligations.push(Obligation {
3983 id: format!("line:{file}:{line}"),
3984 metric: ObligationMetric::Lines,
3985 options: choices(&result.tests),
3986 });
3987 }
3988 for point in &view.points {
3989 let (kind, metric) = match point.meta.kind {
3990 crate::coverage_analysis::PointKind::Statement => {
3991 ("statement", ObligationMetric::Statements)
3992 }
3993 crate::coverage_analysis::PointKind::Function => {
3994 ("function", ObligationMetric::Functions)
3995 }
3996 };
3997 obligations.push(Obligation {
3998 id: format!("{kind}:{}", point.meta.id),
3999 metric,
4000 options: choices(&point.tests),
4001 });
4002 }
4003 for branch in &view.branches {
4004 for alternative in &branch.alternatives {
4005 obligations.push(Obligation {
4006 id: format!("branch:{}:{}", branch.meta.id, alternative.id),
4007 metric: ObligationMetric::Branches,
4008 options: choices(&alternative.tests),
4009 });
4010 }
4011 }
4012 for decision in &view.decisions {
4013 for condition in 0..decision.meta.conditions.len() {
4014 let mut options = Vec::new();
4015 for left in 0..decision.vector_observations.len() {
4016 for right in (left + 1)..decision.vector_observations.len() {
4017 let first = &decision.vector_observations[left];
4018 let second = &decision.vector_observations[right];
4019 if !is_independence_pair(&first.vector, &second.vector, condition) {
4020 continue;
4021 }
4022 for first_choice in choices(&first.tests) {
4023 for second_choice in choices(&second.tests) {
4024 let mut combined = first_choice.clone();
4025 combined.extend(second_choice);
4026 options.push(combined);
4027 }
4028 }
4029 }
4030 }
4031 obligations.push(Obligation {
4032 id: format!("mcdc:{}:{condition}", decision.meta.id),
4033 metric: ObligationMetric::Mcdc,
4034 options: deduplicate_options(options),
4035 });
4036 }
4037 }
4038 ObligationModel {
4039 obligations,
4040 setup_by_file,
4041 tests_by_file,
4042 background,
4043 }
4044}
4045
4046fn percentage(metric: ObligationMetric, summary: &CoverageSummary) -> f64 {
4047 match metric {
4048 ObligationMetric::Lines => summary.lines.percentage,
4049 ObligationMetric::Statements => summary.statements.percentage,
4050 ObligationMetric::Functions => summary.functions.percentage,
4051 ObligationMetric::Branches => summary.branches.percentage,
4052 ObligationMetric::Mcdc => summary.condition_coverage_pct,
4053 }
4054}
4055
4056fn obligation_satisfied(obligation: &Obligation, selected: &BTreeSet<String>) -> bool {
4057 obligation
4058 .options
4059 .iter()
4060 .any(|option| option.iter().all(|test| selected.contains(test)))
4061}
4062
4063struct Search<'a> {
4064 obligations: &'a [Obligation],
4065 skip_limits: BTreeMap<ObligationMetric, usize>,
4066 best: BTreeSet<String>,
4067 explored_states: usize,
4068 max_states: usize,
4069 seen: BTreeSet<String>,
4070 candidate_tests: usize,
4071 target: f64,
4072 metric: MinimizeMetric,
4073}
4074
4075impl Search<'_> {
4076 fn visit(
4077 &mut self,
4078 selected: BTreeSet<String>,
4079 skipped: BTreeSet<String>,
4080 skipped_by_metric: BTreeMap<ObligationMetric, usize>,
4081 ) -> Result<(), QueryError> {
4082 self.explored_states += 1;
4083 if self.explored_states > self.max_states {
4084 return Err(QueryError::ComplexityLimit {
4085 candidate_tests: self.candidate_tests,
4086 obligations: self.obligations.len(),
4087 explored_states: self.explored_states,
4088 max_states: self.max_states,
4089 target: self.target,
4090 metric: self.metric,
4091 });
4092 }
4093 if selected.len() >= self.best.len() {
4094 return Ok(());
4095 }
4096 let state_key = format!(
4097 "{}|{}",
4098 selected.iter().cloned().collect::<Vec<_>>().join(","),
4099 skipped.iter().cloned().collect::<Vec<_>>().join(",")
4100 );
4101 if !self.seen.insert(state_key) {
4102 return Ok(());
4103 }
4104 let mut unmet = self
4105 .obligations
4106 .iter()
4107 .filter(|obligation| {
4108 !skipped.contains(&obligation.id) && !obligation_satisfied(obligation, &selected)
4109 })
4110 .collect::<Vec<_>>();
4111 if unmet.is_empty() {
4112 self.best = selected;
4113 return Ok(());
4114 }
4115 unmet.sort_by(|left, right| {
4116 let feasible = |obligation: &Obligation| {
4117 obligation
4118 .options
4119 .iter()
4120 .filter(|option| option.iter().any(|test| !selected.contains(test)))
4121 .count()
4122 };
4123 feasible(left)
4124 .cmp(&feasible(right))
4125 .then_with(|| left.id.cmp(&right.id))
4126 });
4127 let obligation = unmet[0];
4128 let mut additions = deduplicate_options(obligation.options.iter().filter_map(|option| {
4129 let addition = option
4130 .iter()
4131 .filter(|test| !selected.contains(*test))
4132 .cloned()
4133 .collect::<Vec<_>>();
4134 (!addition.is_empty()).then_some(addition)
4135 }));
4136 additions.sort_by(|left, right| {
4137 left.len()
4138 .cmp(&right.len())
4139 .then_with(|| left.join("\0").cmp(&right.join("\0")))
4140 });
4141 for addition in additions {
4142 if selected.len() + addition.len() >= self.best.len() {
4143 continue;
4144 }
4145 let mut next = selected.clone();
4146 next.extend(addition);
4147 self.visit(next, skipped.clone(), skipped_by_metric.clone())?;
4148 }
4149 let skipped_count = skipped_by_metric
4150 .get(&obligation.metric)
4151 .copied()
4152 .unwrap_or(0);
4153 if skipped_count
4154 < self
4155 .skip_limits
4156 .get(&obligation.metric)
4157 .copied()
4158 .unwrap_or(0)
4159 {
4160 let mut next_skipped = skipped;
4161 next_skipped.insert(obligation.id.clone());
4162 let mut next_counts = skipped_by_metric;
4163 next_counts.insert(obligation.metric, skipped_count + 1);
4164 self.visit(selected, next_skipped, next_counts)?;
4165 }
4166 Ok(())
4167 }
4168}
4169
4170pub fn minimum_test_set(
4171 view: &CoverageView,
4172 target: f64,
4173 metric: MinimizeMetric,
4174 max_states: usize,
4175) -> Result<MinimumTestSetResult, QueryError> {
4176 use crate::coverage_report::coverage_is_complete;
4177 if !target.is_finite() || !(0.0..=100.0).contains(&target) {
4178 return Err(QueryError::InvalidTarget(target));
4179 }
4180 if view.tests.iter().any(|test| {
4181 test.role == "background" && (!test.hits.is_empty() || !test.decisions.is_empty())
4182 }) {
4183 return Err(QueryError::UnattributedEvidence);
4184 }
4185 if view
4192 .tests
4193 .iter()
4194 .any(|test| test.role == "test" && !coverage_is_complete(&test.attribution))
4195 {
4196 return Err(QueryError::UnattributedEvidence);
4197 }
4198 let candidate_tests = view
4199 .tests
4200 .iter()
4201 .filter(|test| test.role == "test")
4202 .map(|test| test.id.clone())
4203 .collect::<BTreeSet<_>>();
4204 let model = build_obligations(view, &candidate_tests);
4205 let obligations = model
4206 .obligations
4207 .iter()
4208 .filter(|obligation| obligation.metric.selected(metric))
4209 .cloned()
4210 .collect::<Vec<_>>();
4211 let mut totals = BTreeMap::<ObligationMetric, usize>::new();
4212 for obligation in &obligations {
4213 *totals.entry(obligation.metric).or_default() += 1;
4214 }
4215 let metrics = [
4216 ObligationMetric::Lines,
4217 ObligationMetric::Statements,
4218 ObligationMetric::Functions,
4219 ObligationMetric::Branches,
4220 ObligationMetric::Mcdc,
4221 ]
4222 .into_iter()
4223 .filter(|candidate| candidate.selected(metric))
4224 .collect::<Vec<_>>();
4225 let skip_limits = metrics
4226 .iter()
4227 .map(|selected_metric| {
4228 let total = totals.get(selected_metric).copied().unwrap_or(0);
4229 let required = ((total as f64 * target) / 100.0).ceil() as usize;
4230 (*selected_metric, total.saturating_sub(required))
4231 })
4232 .collect::<BTreeMap<_, _>>();
4233 let full_expanded = model.expand(&candidate_tests);
4234 let full_summary = coverage_summary_for_tests(view, &full_expanded)?;
4235 for selected_metric in &metrics {
4236 let reachable = percentage(*selected_metric, &full_summary);
4237 if reachable + 1e-9 < target {
4238 return Err(QueryError::TargetUnreachable {
4239 metric: selected_metric.public(),
4240 target,
4241 reachable,
4242 });
4243 }
4244 }
4245 let mut search = Search {
4246 obligations: &obligations,
4247 skip_limits,
4248 best: candidate_tests.clone(),
4249 explored_states: 0,
4250 max_states,
4251 seen: BTreeSet::new(),
4252 candidate_tests: candidate_tests.len(),
4253 target,
4254 metric,
4255 };
4256 search.visit(BTreeSet::new(), BTreeSet::new(), BTreeMap::new())?;
4257 let expanded = model.expand(&search.best);
4258 let summary = coverage_summary_for_tests(view, &expanded)?;
4259 Ok(MinimumTestSetResult {
4260 optimal: true,
4261 target,
4262 metric,
4263 selected: search.best.into_iter().collect(),
4264 expanded: expanded.into_iter().collect(),
4265 summary,
4266 explored_states: search.explored_states,
4267 })
4268}
4269
4270#[cfg(test)]
4271mod tests {
4272 use crate::{
4273 coverage_analysis::McdcVector,
4274 coverage_report::{
4275 CoverageManifest, CoverageReport, CoverageReportRequest, DecisionMeta, ExitCodeInput,
4276 RawTestResult, RuntimeSnapshot, TestProvenance, analyze_coverage_results,
4277 },
4278 };
4279
4280 use super::*;
4281
4282 fn scope_entry(
4283 file: &str,
4284 status: &str,
4285 reason: &str,
4286 package: Option<&str>,
4287 ) -> IndexedScopeEntry {
4288 IndexedScopeEntry {
4289 file: file.into(),
4290 status: status.into(),
4291 reason: reason.into(),
4292 package_root: package.map(str::to_owned),
4293 measurement_limitations: 0,
4294 limitation_kinds: Vec::new(),
4295 }
4296 }
4297
4298 #[test]
4299 fn a_scope_is_grouped_by_place_and_names_the_roots_that_settle_it() {
4300 let unclassified = "unclassified first-party source";
4301 let entries = [
4302 scope_entry("components/a.tsx", "ambiguous", unclassified, None),
4303 scope_entry("components/ui/b.tsx", "ambiguous", unclassified, None),
4304 scope_entry("proxy.ts", "ambiguous", unclassified, None),
4305 scope_entry(
4306 "app/page.tsx",
4307 "included",
4308 "discovered package source root",
4309 None,
4310 ),
4311 scope_entry(
4312 "tests/a.test.ts",
4313 "excluded",
4314 "test or fixture source",
4315 None,
4316 ),
4317 scope_entry(
4318 "next.config.ts",
4319 "excluded",
4320 "build/test/tool configuration",
4321 None,
4322 ),
4323 scope_entry(
4324 "packages/ui/extra/c.ts",
4325 "ambiguous",
4326 unclassified,
4327 Some("packages/ui"),
4328 ),
4329 scope_entry(
4330 "packages/ui/src/d.ts",
4331 "included",
4332 "discovered package source root",
4333 Some("packages/ui"),
4334 ),
4335 ];
4336 let groups = scope_groups(&entries)
4337 .into_iter()
4338 .map(|group| (group.files, group.status, group.directory))
4339 .collect::<Vec<_>>();
4340 assert_eq!(
4341 groups,
4342 [
4343 (2, "ambiguous".to_owned(), "components".to_owned()),
4344 (1, "ambiguous".into(), ".".into()),
4345 (1, "ambiguous".into(), "packages/ui/extra".into()),
4346 (1, "included".into(), "app".into()),
4347 (1, "included".into(), "packages/ui/src".into()),
4348 (1, "excluded".into(), ".".into()),
4349 (1, "excluded".into(), "tests".into()),
4350 ]
4351 );
4352 let hint =
4355 unclassified_source(&["app".into(), "packages/ui/src".into()], &entries).unwrap();
4356 assert_eq!(
4357 hint.roots,
4358 [
4359 "app",
4360 "packages/ui/src",
4361 "components",
4362 "packages/ui/extra",
4363 "proxy.ts"
4364 ]
4365 );
4366 assert_eq!(hint.directories[0].directory, "components");
4367 assert_eq!(hint.directories[0].files, 2);
4368 assert_eq!(unclassified_source(&[], &entries[3..6]), None);
4369 }
4370
4371 #[test]
4372 fn a_file_is_counted_under_its_first_folders() {
4373 assert_eq!(area_directory("app/api/cron/route.ts", 1), "app");
4374 assert_eq!(area_directory("app/api/cron/route.ts", 2), "app/api");
4375 assert_eq!(area_directory("app/page.tsx", 2), "app");
4376 assert_eq!(area_directory("proxy.ts", 2), ".");
4377 }
4378
4379 #[test]
4380 fn all_metric_keeps_statement_only_files_in_the_gap_set() {
4381 let gap = IndexedFileGap {
4382 view: CoverageViewId::All,
4383 file: "src/statement.js".into(),
4384 uncovered_lines: 0,
4385 uncovered_statements: 1,
4386 uncovered_functions: 0,
4387 missing_branches: 0,
4388 missing_mcdc_conditions: 0,
4389 measurement_limitations: 0,
4390 limitation_kinds: Vec::new(),
4391 covered_by_other_tests: crate::coverage_index::IndexedGapDimensions {
4392 lines: 0,
4393 statements: 0,
4394 functions: 0,
4395 branches: 0,
4396 mcdc_conditions: 0,
4397 },
4398 uncovered_everywhere: crate::coverage_index::IndexedGapDimensions {
4399 lines: 0,
4400 statements: 1,
4401 functions: 0,
4402 branches: 0,
4403 mcdc_conditions: 0,
4404 },
4405 totals: crate::coverage_index::IndexedGapDimensions {
4406 lines: 1,
4407 statements: 2,
4408 functions: 0,
4409 branches: 0,
4410 mcdc_conditions: 0,
4411 },
4412 covered: crate::coverage_index::IndexedGapDimensions {
4413 lines: 1,
4414 statements: 1,
4415 functions: 0,
4416 branches: 0,
4417 mcdc_conditions: 0,
4418 },
4419 score: 0,
4420 };
4421 assert!(has_gap_for_metric(&gap, MinimizeMetric::All));
4422 assert!(has_gap_for_metric(&gap, MinimizeMetric::Statements));
4423 assert!(!has_gap_for_metric(&gap, MinimizeMetric::Lines));
4424 }
4425
4426 fn result(id: &str, vector: McdcVector) -> RawTestResult {
4427 RawTestResult {
4428 test_id: Some(id.into()),
4429 scope: None,
4430 test: id.into(),
4431 test_file: Some("tests/permission.test.js".into()),
4432 title: None,
4433 retry: Some(0),
4434 status: Some("passed".into()),
4435 expected_status: None,
4436 flaky: false,
4437 provenance: TestProvenance {
4438 runner: "node:test".into(),
4439 kind: "unit".into(),
4440 project: None,
4441 source: "runner-default".into(),
4442 tags: Vec::new(),
4443 },
4444 role: "test".into(),
4445 attribution: crate::coverage_report::ATTRIBUTION_EXACT.into(),
4446 phases: Vec::new(),
4447 runtime: vec![RuntimeSnapshot {
4448 decisions: vec![crate::coverage_report::DecisionSnapshot {
4449 meta: decision(),
4450 vectors: vec![vector],
4451 }],
4452 hits: Vec::new(),
4453 events: Vec::new(),
4454 logicals: Vec::new(),
4455 phase_id: None,
4456 }],
4457 browser: Vec::new(),
4458 server: Vec::new(),
4459 }
4460 }
4461
4462 fn decision() -> DecisionMeta {
4463 DecisionMeta {
4464 id: "decision".into(),
4465 file: "src/permission.js".into(),
4466 line: 1,
4467 column: 1,
4468 source: "admin || owner".into(),
4469 conditions: vec!["admin".into(), "owner".into()],
4470 kind: "if".into(),
4471 }
4472 }
4473
4474 fn report(mut results: Vec<RawTestResult>) -> CoverageReport {
4475 analyze_coverage_results(&CoverageReportRequest {
4476 run_id: "run".into(),
4477 manifest: CoverageManifest {
4478 unmeasured: Vec::new(),
4479 decisions: vec![decision()],
4480 points: Vec::new(),
4481 branches: Vec::new(),
4482 limitations: Vec::new(),
4483 scope: None,
4484 assertion_sites: Vec::new(),
4485 },
4486 raw_results: std::mem::take(&mut results),
4487 generated_at: "time".into(),
4488 coverage_model: None,
4489 integrity: None,
4490 test_exit_code: ExitCodeInput::Present(Some(0)),
4491 })
4492 .unwrap()
4493 }
4494
4495 #[test]
4496 fn recomputes_mcdc_witnesses_and_removes_a_redundant_vector() {
4497 let report = report(vec![
4498 result(
4499 "admin",
4500 McdcVector {
4501 values: vec![Some(true), None],
4502 outcome: true,
4503 },
4504 ),
4505 result(
4506 "owner",
4507 McdcVector {
4508 values: vec![Some(false), Some(true)],
4509 outcome: true,
4510 },
4511 ),
4512 result(
4513 "both",
4514 McdcVector {
4515 values: vec![Some(true), None],
4516 outcome: true,
4517 },
4518 ),
4519 result(
4520 "neither",
4521 McdcVector {
4522 values: vec![Some(false), Some(false)],
4523 outcome: false,
4524 },
4525 ),
4526 ]);
4527 let minimized = minimum_test_set(&report.view, 100.0, MinimizeMetric::Mcdc, 5_000).unwrap();
4528 assert_eq!(minimized.selected.len(), 3);
4529 assert!(minimized.selected.contains(&"owner".into()));
4530 assert!(minimized.selected.contains(&"neither".into()));
4531 assert_eq!(minimized.summary.condition_coverage_pct, 100.0);
4532 }
4533
4534 #[test]
4535 fn a_test_that_belongs_to_no_test_is_not_minimized_away() {
4536 let mut parallel = result(
4543 "parallel",
4544 McdcVector {
4545 values: vec![Some(false), Some(false)],
4546 outcome: false,
4547 },
4548 );
4549 parallel.attribution = crate::coverage_report::ATTRIBUTION_RUN_WIDE.into();
4550 assert!(matches!(
4551 minimum_test_set(
4552 &report(vec![parallel]).view,
4553 100.0,
4554 MinimizeMetric::Mcdc,
4555 5_000,
4556 ),
4557 Err(QueryError::UnattributedEvidence)
4558 ));
4559 }
4560
4561 #[test]
4562 fn the_summary_counts_each_test_under_the_attribution_it_carries() {
4563 let vector = || McdcVector {
4567 values: vec![Some(false), Some(false)],
4568 outcome: false,
4569 };
4570 let mut serial = result("serial", vector());
4571 serial.attribution = crate::coverage_report::ATTRIBUTION_EXACT.into();
4572 let mut parallel = result("parallel", vector());
4573 parallel.attribution = crate::coverage_report::ATTRIBUTION_RUN_WIDE.into();
4574 let mut ruby = result("ruby", vector());
4575 ruby.attribution = crate::coverage_report::ATTRIBUTION_PARTIAL.into();
4576 let mut also_parallel = result("also-parallel", vector());
4577 also_parallel.attribution = crate::coverage_report::ATTRIBUTION_RUN_WIDE.into();
4578 let report = report(vec![serial, parallel, ruby, also_parallel]);
4579
4580 let nonce = std::time::SystemTime::now()
4581 .duration_since(std::time::UNIX_EPOCH)
4582 .unwrap()
4583 .as_nanos();
4584 let root = std::env::temp_dir().join(format!(
4585 "supercov-attribution-counts-{}-{nonce}",
4586 std::process::id()
4587 ));
4588 std::fs::create_dir(&root).unwrap();
4589 let path = root.join("query-index.v1.bin");
4590 let identity = crate::query_index::QueryIndexIdentity {
4591 evidence_sha256: [1; 32],
4592 evidence_bytes: 100,
4593 analysis_sha256: [2; 32],
4594 producer_sha256: [3; 32],
4595 archive_schema_version: 2,
4596 };
4597 crate::query_index::write_query_index(
4598 &crate::coverage_index::coverage_index_sections(&report).unwrap(),
4599 &identity,
4600 &path,
4601 )
4602 .unwrap();
4603 let container = crate::query_index::QueryIndex::open(&path, &identity).unwrap();
4604 let index = crate::coverage_index::CoverageIndex::new(&container).unwrap();
4605
4606 let summary = coverage_summary_query(
4607 &index,
4608 CoverageSummaryQueryOptions {
4609 run: "run",
4610 view: CoverageViewId::All,
4611 kind: None,
4612 runner: None,
4613 valid: true,
4614 test_exit_code: Some(0),
4615 stale: false,
4616 stale_reasons: Vec::new(),
4617 },
4618 )
4619 .unwrap();
4620 assert_eq!(summary.test_attribution.exact, 1);
4621 assert_eq!(summary.test_attribution.run_wide, 2);
4622 assert_eq!(summary.test_attribution.partial, 1);
4623 assert_eq!(summary.test_attribution.total(), 4);
4624 std::fs::remove_dir_all(&root).ok();
4625 }
4626
4627 #[test]
4628 fn refuses_background_evidence() {
4629 let mut aggregate = result(
4630 "aggregate",
4631 McdcVector {
4632 values: vec![Some(false), Some(false)],
4633 outcome: false,
4634 },
4635 );
4636 aggregate.role = "background".into();
4637 assert!(matches!(
4638 minimum_test_set(
4639 &report(vec![aggregate]).view,
4640 100.0,
4641 MinimizeMetric::Mcdc,
4642 5_000,
4643 ),
4644 Err(QueryError::UnattributedEvidence)
4645 ));
4646 }
4647
4648 #[test]
4649 fn bounds_the_exact_search() {
4650 let report = report(vec![
4651 result(
4652 "admin",
4653 McdcVector {
4654 values: vec![Some(true), None],
4655 outcome: true,
4656 },
4657 ),
4658 result(
4659 "owner",
4660 McdcVector {
4661 values: vec![Some(false), Some(true)],
4662 outcome: true,
4663 },
4664 ),
4665 result(
4666 "neither",
4667 McdcVector {
4668 values: vec![Some(false), Some(false)],
4669 outcome: false,
4670 },
4671 ),
4672 ]);
4673 assert!(matches!(
4674 minimum_test_set(&report.view, 100.0, MinimizeMetric::Mcdc, 1),
4675 Err(QueryError::ComplexityLimit { .. })
4676 ));
4677 }
4678}