dart-decimate 0.0.6 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,410 @@
1
+ use super::*;
2
+ use crate::output::{FindingAction, FindingEdge, NextStep, ReportCommand, ReportSummary};
3
+
4
+ #[test]
5
+ fn renders_browser_ready_report_with_escaped_evidence() {
6
+ let report = JsonReport {
7
+ schema_version: "dart-decimate.report.v1".to_owned(),
8
+ kind: "combined".to_owned(),
9
+ tool: "dart-decimate".to_owned(),
10
+ command: ReportCommand::Check,
11
+ verdict: Verdict::Fail,
12
+ summary: ReportSummary {
13
+ files: 3,
14
+ edges: 2,
15
+ findings: 1,
16
+ cycles: 1,
17
+ ..ReportSummary::default()
18
+ },
19
+ findings: vec![Finding {
20
+ rule_id: "dart-decimate/circular-dependency".to_owned(),
21
+ fingerprint: Some("cycle:<unsafe>".to_owned()),
22
+ kind: FindingKind::CircularDependency,
23
+ severity: Severity::Error,
24
+ message: "Circular dependency spans 3 Dart files".to_owned(),
25
+ path: "lib/a.dart".to_owned(),
26
+ line: 1,
27
+ column: 0,
28
+ safe_to_delete: false,
29
+ files: vec![
30
+ "lib/a.dart".to_owned(),
31
+ "lib/b.dart".to_owned(),
32
+ "lib/c.dart".to_owned(),
33
+ ],
34
+ edge: None,
35
+ actions: vec![
36
+ FindingAction::new(
37
+ "break-cycle",
38
+ "Move shared dependencies inward or invert one import edge",
39
+ false,
40
+ )
41
+ .with_dart_decimate_args([
42
+ "inspect",
43
+ "--format",
44
+ "json",
45
+ "--file",
46
+ "lib/a.dart",
47
+ ]),
48
+ ],
49
+ }],
50
+ clone_groups: Vec::new(),
51
+ complexity: Vec::new(),
52
+ file_scores: Vec::new(),
53
+ hotspots: Vec::new(),
54
+ refactoring_targets: Vec::new(),
55
+ threshold_overrides: Vec::new(),
56
+ feature_flags: Vec::new(),
57
+ security_candidates: Vec::new(),
58
+ attack_surface: Vec::new(),
59
+ runtime_coverage: None,
60
+ next_steps: Vec::new(),
61
+ };
62
+
63
+ let rendered = render_html_report(&report);
64
+
65
+ assert!(rendered.starts_with("<!doctype html>"));
66
+ assert!(rendered.contains("<h1>check report</h1>"));
67
+ assert!(rendered.contains("Architecture"));
68
+ assert!(rendered.contains("1 circular dependency"));
69
+ assert!(rendered.contains("Why"));
70
+ assert!(rendered.contains("cycle:&lt;unsafe&gt;"));
71
+ assert!(rendered.contains("lib/a.dart -&gt; lib/b.dart -&gt; lib/c.dart -&gt; lib/a.dart"));
72
+ }
73
+
74
+ #[test]
75
+ fn groups_findings_by_type_with_search_and_type_controls() {
76
+ let report = JsonReport {
77
+ schema_version: "dart-decimate.report.v1".to_owned(),
78
+ kind: "combined".to_owned(),
79
+ tool: "dart-decimate".to_owned(),
80
+ command: ReportCommand::Check,
81
+ verdict: Verdict::Fail,
82
+ summary: ReportSummary {
83
+ files: 4,
84
+ edges: 3,
85
+ findings: 3,
86
+ unresolved_dependencies: 2,
87
+ unused_widget_params: 1,
88
+ ..ReportSummary::default()
89
+ },
90
+ findings: vec![
91
+ finding(
92
+ FindingKind::UnresolvedDependency,
93
+ "dart-decimate/unresolved-dependency",
94
+ "Missing local import lib/missing_a.dart",
95
+ "lib/a.dart",
96
+ ),
97
+ finding(
98
+ FindingKind::UnresolvedDependency,
99
+ "dart-decimate/unresolved-dependency",
100
+ "Missing local import lib/missing_b.dart",
101
+ "lib/b.dart",
102
+ ),
103
+ finding(
104
+ FindingKind::UnusedWidgetParam,
105
+ "dart-decimate/unused-widget-param",
106
+ "Widget constructor parameter is never read",
107
+ "lib/widget.dart",
108
+ ),
109
+ ],
110
+ clone_groups: Vec::new(),
111
+ complexity: Vec::new(),
112
+ file_scores: Vec::new(),
113
+ hotspots: Vec::new(),
114
+ refactoring_targets: Vec::new(),
115
+ threshold_overrides: Vec::new(),
116
+ feature_flags: Vec::new(),
117
+ security_candidates: Vec::new(),
118
+ attack_surface: Vec::new(),
119
+ runtime_coverage: None,
120
+ next_steps: Vec::new(),
121
+ };
122
+
123
+ let rendered = render_html_report(&report);
124
+
125
+ assert!(rendered.contains("data-finding-search"));
126
+ assert!(!rendered.contains("data-finding-type"));
127
+ assert!(rendered.contains(
128
+ "<button class=\"type-filter\" type=\"button\" data-finding-filter data-kind=\"Unused widget parameter\" aria-pressed=\"false\">2. Unused widget parameter (1)</button>"
129
+ ));
130
+ assert!(!rendered.contains("if (query || selectedType) group.open = true;"));
131
+ assert!(rendered.contains("<details class=\"finding-group\" data-finding-group data-kind=\"Unresolved dependency\" open>"));
132
+ assert!(rendered.contains(
133
+ "<details class=\"finding-group\" data-finding-group data-kind=\"Unused widget parameter\">"
134
+ ));
135
+ assert!(rendered.contains("<span class=\"group-number\">2.</span>"));
136
+ assert!(rendered.contains("<span class=\"chevron\" aria-hidden=\"true\"></span>"));
137
+ assert!(rendered.contains("2 findings | 2 errors"));
138
+ assert!(rendered.contains("1 finding | 1 error"));
139
+ }
140
+
141
+ #[test]
142
+ fn group_headers_render_unique_rule_ids_only_when_concise() {
143
+ let report = JsonReport {
144
+ schema_version: "dart-decimate.report.v1".to_owned(),
145
+ kind: "combined".to_owned(),
146
+ tool: "dart-decimate".to_owned(),
147
+ command: ReportCommand::Check,
148
+ verdict: Verdict::Fail,
149
+ summary: ReportSummary {
150
+ files: 6,
151
+ findings: 6,
152
+ policy_violations: 2,
153
+ security_candidates: 4,
154
+ ..ReportSummary::default()
155
+ },
156
+ findings: vec![
157
+ finding(
158
+ FindingKind::PolicyViolation,
159
+ "dart-decimate/policy/mobile/no-dart-io",
160
+ "dart:io is not allowed",
161
+ "lib/io.dart",
162
+ ),
163
+ finding(
164
+ FindingKind::PolicyViolation,
165
+ "dart-decimate/policy/mobile/no-process",
166
+ "Process APIs are not allowed",
167
+ "lib/process.dart",
168
+ ),
169
+ finding(
170
+ FindingKind::SecurityCandidate,
171
+ "dart-decimate/security/hardcoded-secret",
172
+ "Hardcoded secret candidate",
173
+ "lib/secret.dart",
174
+ ),
175
+ finding(
176
+ FindingKind::SecurityCandidate,
177
+ "dart-decimate/security/tls-bypass",
178
+ "TLS bypass candidate",
179
+ "lib/tls.dart",
180
+ ),
181
+ finding(
182
+ FindingKind::SecurityCandidate,
183
+ "dart-decimate/security/raw-sql",
184
+ "Raw SQL candidate",
185
+ "lib/sql.dart",
186
+ ),
187
+ finding(
188
+ FindingKind::SecurityCandidate,
189
+ "dart-decimate/security/webview-javascript",
190
+ "WebView JavaScript bridge candidate",
191
+ "lib/webview.dart",
192
+ ),
193
+ ],
194
+ clone_groups: Vec::new(),
195
+ complexity: Vec::new(),
196
+ file_scores: Vec::new(),
197
+ hotspots: Vec::new(),
198
+ refactoring_targets: Vec::new(),
199
+ threshold_overrides: Vec::new(),
200
+ feature_flags: Vec::new(),
201
+ security_candidates: Vec::new(),
202
+ attack_surface: Vec::new(),
203
+ runtime_coverage: None,
204
+ next_steps: Vec::new(),
205
+ };
206
+
207
+ let rendered = render_html_report(&report);
208
+
209
+ assert!(rendered.contains(
210
+ "<span class=\"summary-text\"><strong>Policy violation</strong><span class=\"rule\">dart-decimate/policy/mobile/no-dart-io, dart-decimate/policy/mobile/no-process</span></span>"
211
+ ));
212
+ assert!(
213
+ rendered
214
+ .contains("<span class=\"summary-text\"><strong>Security candidate</strong></span>")
215
+ );
216
+ assert!(!rendered.contains(
217
+ "<strong>Security candidate</strong><span class=\"rule\">dart-decimate/security/hardcoded-secret</span>"
218
+ ));
219
+ }
220
+
221
+ #[test]
222
+ fn grouped_findings_preserves_first_seen_order_for_repeated_types() {
223
+ let findings = vec![
224
+ finding(
225
+ FindingKind::UnresolvedDependency,
226
+ "dart-decimate/unresolved-dependency",
227
+ "Missing local import lib/missing_a.dart",
228
+ "lib/a.dart",
229
+ ),
230
+ finding(
231
+ FindingKind::UnusedWidgetParam,
232
+ "dart-decimate/unused-widget-param",
233
+ "Widget constructor parameter is never read",
234
+ "lib/widget.dart",
235
+ ),
236
+ finding(
237
+ FindingKind::UnresolvedDependency,
238
+ "dart-decimate/unresolved-dependency",
239
+ "Missing local import lib/missing_b.dart",
240
+ "lib/b.dart",
241
+ ),
242
+ finding(
243
+ FindingKind::PolicyViolation,
244
+ "dart-decimate/policy/mobile/no-dart-io",
245
+ "dart:io is not allowed",
246
+ "lib/io.dart",
247
+ ),
248
+ finding(
249
+ FindingKind::UnusedWidgetParam,
250
+ "dart-decimate/unused-widget-param",
251
+ "Second widget constructor parameter is never read",
252
+ "lib/second_widget.dart",
253
+ ),
254
+ ];
255
+
256
+ let groups = grouped_findings(&findings);
257
+
258
+ assert_eq!(groups.len(), 3);
259
+ assert_eq!(groups[0].kind, FindingKind::UnresolvedDependency);
260
+ assert_eq!(
261
+ groups[0]
262
+ .entries
263
+ .iter()
264
+ .map(|(index, _)| *index)
265
+ .collect::<Vec<_>>(),
266
+ vec![1, 3]
267
+ );
268
+ assert_eq!(groups[1].kind, FindingKind::UnusedWidgetParam);
269
+ assert_eq!(
270
+ groups[1]
271
+ .entries
272
+ .iter()
273
+ .map(|(index, _)| *index)
274
+ .collect::<Vec<_>>(),
275
+ vec![2, 5]
276
+ );
277
+ assert_eq!(groups[2].kind, FindingKind::PolicyViolation);
278
+ assert_eq!(
279
+ groups[2]
280
+ .entries
281
+ .iter()
282
+ .map(|(index, _)| *index)
283
+ .collect::<Vec<_>>(),
284
+ vec![4]
285
+ );
286
+ }
287
+
288
+ #[test]
289
+ fn escapes_control_characters_from_user_values() {
290
+ let report = JsonReport {
291
+ schema_version: "dart-decimate.report.v1".to_owned(),
292
+ kind: "combined".to_owned(),
293
+ tool: "dart-decimate".to_owned(),
294
+ command: ReportCommand::Check,
295
+ verdict: Verdict::Fail,
296
+ summary: ReportSummary {
297
+ files: 2,
298
+ edges: 1,
299
+ findings: 1,
300
+ cycles: 1,
301
+ ..ReportSummary::default()
302
+ },
303
+ findings: vec![Finding {
304
+ rule_id: "dart-decimate/circular-dependency".to_owned(),
305
+ fingerprint: Some("cycle:\x1bunsafe".to_owned()),
306
+ kind: FindingKind::CircularDependency,
307
+ severity: Severity::Error,
308
+ message: "Circular dependency includes \x07bell".to_owned(),
309
+ path: "lib/\x1b[31mbad.dart".to_owned(),
310
+ line: 1,
311
+ column: 0,
312
+ safe_to_delete: false,
313
+ files: vec![
314
+ "lib/\x1b[31mbad.dart".to_owned(),
315
+ "lib/\x07bell.dart".to_owned(),
316
+ ],
317
+ edge: Some(FindingEdge {
318
+ from: "lib/\x1b[31mbad.dart".to_owned(),
319
+ to: "lib/\x07bell.dart".to_owned(),
320
+ specifier: "package:app/\x1bbad.dart".to_owned(),
321
+ kind: "import".to_owned(),
322
+ }),
323
+ actions: vec![
324
+ FindingAction::new("inspect-control", "Inspect \x07bell", false)
325
+ .with_dart_decimate_args([
326
+ "inspect",
327
+ "--format",
328
+ "json",
329
+ "--file",
330
+ "lib/\x1b[31mbad.dart",
331
+ ]),
332
+ ],
333
+ }],
334
+ clone_groups: Vec::new(),
335
+ complexity: Vec::new(),
336
+ file_scores: Vec::new(),
337
+ hotspots: Vec::new(),
338
+ refactoring_targets: Vec::new(),
339
+ threshold_overrides: Vec::new(),
340
+ feature_flags: Vec::new(),
341
+ security_candidates: Vec::new(),
342
+ attack_surface: Vec::new(),
343
+ runtime_coverage: None,
344
+ next_steps: vec![NextStep {
345
+ id: "inspect-control".to_owned(),
346
+ command: "dart-decimate inspect --file lib/\x1b[31mbad.dart".to_owned(),
347
+ reason: "Review \x07bell evidence".to_owned(),
348
+ }],
349
+ };
350
+
351
+ let rendered = render_html_report(&report);
352
+
353
+ assert!(!rendered.contains('\x1b'));
354
+ assert!(!rendered.contains('\x07'));
355
+ assert!(rendered.contains("lib/&#x1B;[31mbad.dart"));
356
+ assert!(rendered.contains("&#x7;bell"));
357
+ }
358
+
359
+ #[test]
360
+ fn renders_omitted_details_for_summary_only_failures() {
361
+ let mut report = JsonReport {
362
+ schema_version: "dart-decimate.report.v1".to_owned(),
363
+ kind: "combined".to_owned(),
364
+ tool: "dart-decimate".to_owned(),
365
+ command: ReportCommand::Security,
366
+ verdict: Verdict::Fail,
367
+ summary: ReportSummary {
368
+ files: 1,
369
+ findings: 2,
370
+ security_candidates: 2,
371
+ ..ReportSummary::default()
372
+ },
373
+ findings: Vec::new(),
374
+ clone_groups: Vec::new(),
375
+ complexity: Vec::new(),
376
+ file_scores: Vec::new(),
377
+ hotspots: Vec::new(),
378
+ refactoring_targets: Vec::new(),
379
+ threshold_overrides: Vec::new(),
380
+ feature_flags: Vec::new(),
381
+ security_candidates: Vec::new(),
382
+ attack_surface: Vec::new(),
383
+ runtime_coverage: None,
384
+ next_steps: Vec::new(),
385
+ };
386
+ report.findings.clear();
387
+
388
+ let rendered = render_html_report(&report);
389
+
390
+ assert!(rendered.contains("<h1>security report</h1>"));
391
+ assert!(rendered.contains("2 findings were omitted from this summary output."));
392
+ assert!(!rendered.contains("No findings. The selected Dart graph checks passed."));
393
+ }
394
+
395
+ fn finding(kind: FindingKind, rule_id: &str, message: &str, path: &str) -> Finding {
396
+ Finding {
397
+ rule_id: rule_id.to_owned(),
398
+ fingerprint: None,
399
+ kind,
400
+ severity: Severity::Error,
401
+ message: message.to_owned(),
402
+ path: path.to_owned(),
403
+ line: 1,
404
+ column: 0,
405
+ safe_to_delete: false,
406
+ files: Vec::new(),
407
+ edge: None,
408
+ actions: Vec::new(),
409
+ }
410
+ }