tokmd-analysis 1.10.0

Analysis logic and enrichers for tokmd receipts.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//! Extended identity & security tests for license radar scanning.
//!
//! Covers gaps not exercised by existing unit/bdd/edge/property suites:
//! - Additional license text variants (LICENSE.md, LICENSE-APACHE)
//! - SPDX compound expressions in metadata
//! - Metadata absence in pyproject.toml
//! - Near-miss text below min_hits threshold
//! - Whitespace-only license files
//! - package.json license array (deprecated format)
//! - Cargo.toml license-file in subdirectory

use crate::license::build_license_report;
use std::fs;
use std::path::PathBuf;
use tempfile::tempdir;
use tokmd_analysis_types::AnalysisLimits;
use tokmd_analysis_types::LicenseSourceKind;

fn default_limits() -> AnalysisLimits {
    AnalysisLimits::default()
}

// ===========================================================================
// 1. LICENSE.md variant is recognized as license text
// ===========================================================================

#[test]
fn license_md_variant_is_recognized() {
    let dir = tempdir().unwrap();
    fs::write(
        dir.path().join("LICENSE.md"),
        "Permission is hereby granted, free of charge, to any person.\n\
         The software is provided \"as is\".",
    )
    .unwrap();

    let files = vec![PathBuf::from("LICENSE.md")];
    let report = build_license_report(dir.path(), &files, &default_limits()).unwrap();

    assert!(
        report.findings.iter().any(|f| f.spdx == "MIT"),
        "LICENSE.md should be scanned for license text: {:?}",
        report.findings
    );
}

// ===========================================================================
// 2. SPDX compound expression round-trips through metadata
// ===========================================================================

#[test]
fn spdx_compound_and_expression_round_trips() {
    let dir = tempdir().unwrap();
    fs::write(
        dir.path().join("Cargo.toml"),
        "[package]\nname = \"x\"\nlicense = \"MIT AND Apache-2.0\"\n",
    )
    .unwrap();

    let files = vec![PathBuf::from("Cargo.toml")];
    let report = build_license_report(dir.path(), &files, &default_limits()).unwrap();

    assert_eq!(report.findings.len(), 1);
    assert_eq!(report.findings[0].spdx, "MIT AND Apache-2.0");
    assert_eq!(report.effective.as_deref(), Some("MIT AND Apache-2.0"));
}

#[test]
fn spdx_with_exception_round_trips() {
    let dir = tempdir().unwrap();
    fs::write(
        dir.path().join("Cargo.toml"),
        "[package]\nname = \"x\"\nlicense = \"Apache-2.0 WITH LLVM-exception\"\n",
    )
    .unwrap();

    let files = vec![PathBuf::from("Cargo.toml")];
    let report = build_license_report(dir.path(), &files, &default_limits()).unwrap();

    assert_eq!(report.findings[0].spdx, "Apache-2.0 WITH LLVM-exception");
}

// ===========================================================================
// 3. pyproject.toml without license field yields no finding
// ===========================================================================

#[test]
fn pyproject_toml_without_license_field_yields_no_metadata() {
    let dir = tempdir().unwrap();
    fs::write(
        dir.path().join("pyproject.toml"),
        "[project]\nname = \"x\"\nversion = \"0.1.0\"\n",
    )
    .unwrap();

    let files = vec![PathBuf::from("pyproject.toml")];
    let report = build_license_report(dir.path(), &files, &default_limits()).unwrap();

    assert!(
        report.findings.is_empty(),
        "pyproject.toml without license should produce no findings"
    );
}

// ===========================================================================
// 4. Near-miss license text below min_hits threshold
// ===========================================================================

#[test]
fn apache_text_with_only_one_phrase_below_threshold() {
    let dir = tempdir().unwrap();
    // Apache-2.0 requires min_hits=2, so a single phrase should not match
    fs::write(
        dir.path().join("LICENSE"),
        "This software is under the Apache License. That's all.",
    )
    .unwrap();

    let files = vec![PathBuf::from("LICENSE")];
    let report = build_license_report(dir.path(), &files, &default_limits()).unwrap();

    // Should not detect Apache-2.0 with only one of the required phrases
    assert!(
        !report.findings.iter().any(|f| f.spdx == "Apache-2.0"),
        "single Apache phrase should not meet min_hits threshold"
    );
}

#[test]
fn gpl_text_with_only_one_phrase_below_threshold() {
    let dir = tempdir().unwrap();
    // GPL-3.0 requires min_hits=2
    fs::write(
        dir.path().join("LICENSE"),
        "GNU General Public License. Some unrelated text.",
    )
    .unwrap();

    let files = vec![PathBuf::from("LICENSE")];
    let report = build_license_report(dir.path(), &files, &default_limits()).unwrap();

    assert!(
        !report.findings.iter().any(|f| f.spdx == "GPL-3.0-or-later"),
        "single GPL phrase should not meet min_hits threshold"
    );
}

// ===========================================================================
// 5. Whitespace-only license file yields no findings
// ===========================================================================

#[test]
fn whitespace_only_license_file_no_findings() {
    let dir = tempdir().unwrap();
    fs::write(dir.path().join("LICENSE"), "   \n\n  \t  \n").unwrap();

    let files = vec![PathBuf::from("LICENSE")];
    let report = build_license_report(dir.path(), &files, &default_limits()).unwrap();

    // The text has no recognizable license phrases
    assert!(
        report.findings.is_empty(),
        "whitespace-only license file should produce no findings"
    );
}

// ===========================================================================
// 6. package.json deprecated array license format
// ===========================================================================

#[test]
fn package_json_array_license_yields_no_finding() {
    let dir = tempdir().unwrap();
    // Deprecated format: "licenses": [{"type": "MIT"}]
    // The code checks "license" (singular), so array under "licenses" is ignored
    fs::write(
        dir.path().join("package.json"),
        r#"{"name": "x", "licenses": [{"type": "MIT"}]}"#,
    )
    .unwrap();

    let files = vec![PathBuf::from("package.json")];
    let report = build_license_report(dir.path(), &files, &default_limits()).unwrap();

    // "licenses" (plural) is not recognized
    assert!(report.findings.is_empty());
}

#[test]
fn package_json_license_as_number_yields_no_finding() {
    let dir = tempdir().unwrap();
    fs::write(
        dir.path().join("package.json"),
        r#"{"name": "x", "license": 42}"#,
    )
    .unwrap();

    let files = vec![PathBuf::from("package.json")];
    let report = build_license_report(dir.path(), &files, &default_limits()).unwrap();

    assert!(
        report.findings.is_empty(),
        "numeric license value should not produce a finding"
    );
}

// ===========================================================================
// 7. Cargo.toml license-file in subdirectory resolves correctly
// ===========================================================================

#[test]
fn cargo_toml_license_file_in_subdirectory() {
    let dir = tempdir().unwrap();
    let sub = dir.path().join("licenses");
    fs::create_dir_all(&sub).unwrap();
    fs::write(
        dir.path().join("Cargo.toml"),
        "[package]\nname = \"x\"\nlicense-file = \"licenses/CUSTOM\"\n",
    )
    .unwrap();
    fs::write(
        sub.join("CUSTOM"),
        "Permission is hereby granted, free of charge.\n\
         The software is provided \"as is\".",
    )
    .unwrap();

    let files = vec![
        PathBuf::from("Cargo.toml"),
        PathBuf::from("licenses").join("CUSTOM"),
    ];
    let report = build_license_report(dir.path(), &files, &default_limits()).unwrap();

    assert!(
        report.findings.iter().any(|f| f.spdx == "MIT"
            && f.source_kind == LicenseSourceKind::Text
            && f.source_path == "licenses/CUSTOM"),
        "license-file in subdirectory should be scanned: {:?}",
        report.findings
    );
}

// ===========================================================================
// 8. Multiple LICENSE text files produce distinct source_path values
// ===========================================================================

#[test]
fn multiple_license_files_have_distinct_source_paths() {
    let dir = tempdir().unwrap();
    fs::write(
        dir.path().join("LICENSE-MIT"),
        "Permission is hereby granted, free of charge.\n\
         The software is provided \"as is\".",
    )
    .unwrap();
    fs::write(
        dir.path().join("LICENSE-APACHE"),
        "Apache License\nVersion 2.0\n\
         http://www.apache.org/licenses/\n\
         limitations under the License.",
    )
    .unwrap();

    let files = vec![
        PathBuf::from("LICENSE-MIT"),
        PathBuf::from("LICENSE-APACHE"),
    ];
    let report = build_license_report(dir.path(), &files, &default_limits()).unwrap();

    let paths: Vec<&str> = report
        .findings
        .iter()
        .map(|f| f.source_path.as_str())
        .collect();
    // All source_path values should be unique
    let mut unique = paths.clone();
    unique.sort();
    unique.dedup();
    assert_eq!(
        paths.len(),
        unique.len(),
        "each finding should have a distinct source_path"
    );
}

// ===========================================================================
// 9. Cargo.toml license field in non-[package] section is ignored
// ===========================================================================

#[test]
fn cargo_toml_license_in_dependencies_section_ignored() {
    let dir = tempdir().unwrap();
    fs::write(
        dir.path().join("Cargo.toml"),
        "[package]\nname = \"x\"\nversion = \"0.1.0\"\n\n[dependencies]\nlicense = \"MIT\"\n",
    )
    .unwrap();

    let files = vec![PathBuf::from("Cargo.toml")];
    let report = build_license_report(dir.path(), &files, &default_limits()).unwrap();

    assert!(
        report.findings.is_empty(),
        "license field outside [package] section should be ignored"
    );
}

// ===========================================================================
// 10. MPL-2.0 with only two of three phrases detected
// ===========================================================================

#[test]
fn mpl2_with_two_phrases_still_detected() {
    let dir = tempdir().unwrap();
    fs::write(
        dir.path().join("LICENSE"),
        "Mozilla Public License\nVersion 2.0\n\
         This Source Code Form is subject to the terms of the Mozilla Public License.",
    )
    .unwrap();

    let files = vec![PathBuf::from("LICENSE")];
    let report = build_license_report(dir.path(), &files, &default_limits()).unwrap();

    assert!(
        report.findings.iter().any(|f| f.spdx == "MPL-2.0"),
        "MPL-2.0 should be detected with 2 phrase hits"
    );
}

// ===========================================================================
// 11. Findings from both metadata and text for same license are both present
// ===========================================================================

#[test]
fn metadata_and_text_same_license_produces_two_findings() {
    let dir = tempdir().unwrap();
    fs::write(
        dir.path().join("Cargo.toml"),
        "[package]\nname = \"x\"\nlicense = \"MIT\"\n",
    )
    .unwrap();
    fs::write(
        dir.path().join("LICENSE"),
        "Permission is hereby granted, free of charge.\n\
         The software is provided \"as is\".",
    )
    .unwrap();

    let files = vec![PathBuf::from("Cargo.toml"), PathBuf::from("LICENSE")];
    let report = build_license_report(dir.path(), &files, &default_limits()).unwrap();

    let metadata_count = report
        .findings
        .iter()
        .filter(|f| f.source_kind == LicenseSourceKind::Metadata)
        .count();
    let text_count = report
        .findings
        .iter()
        .filter(|f| f.source_kind == LicenseSourceKind::Text)
        .count();

    assert!(
        metadata_count >= 1,
        "should have at least one metadata finding"
    );
    assert!(text_count >= 1, "should have at least one text finding");
    assert!(
        report.findings.len() >= 2,
        "should have both metadata and text findings"
    );
}

// ===========================================================================
// 12. Empty file list with non-empty root yields empty report
// ===========================================================================

#[test]
fn empty_file_list_with_populated_root_yields_empty_report() {
    let dir = tempdir().unwrap();
    // Create files in the directory but pass empty file list
    fs::write(dir.path().join("LICENSE"), "MIT License").unwrap();
    fs::write(
        dir.path().join("Cargo.toml"),
        "[package]\nlicense = \"MIT\"\n",
    )
    .unwrap();

    let report = build_license_report(dir.path(), &[], &default_limits()).unwrap();
    assert!(report.findings.is_empty());
    assert!(report.effective.is_none());
}

// ===========================================================================
// 13. pyproject.toml with tool.poetry license
// ===========================================================================

#[test]
fn pyproject_toml_poetry_license_detected() {
    let dir = tempdir().unwrap();
    fs::write(
        dir.path().join("pyproject.toml"),
        "[tool.poetry]\nname = \"x\"\nlicense = \"BSD-3-Clause\"\n",
    )
    .unwrap();

    let files = vec![PathBuf::from("pyproject.toml")];
    let report = build_license_report(dir.path(), &files, &default_limits()).unwrap();

    assert_eq!(report.findings.len(), 1);
    assert_eq!(report.findings[0].spdx, "BSD-3-Clause");
    assert_eq!(report.findings[0].source_kind, LicenseSourceKind::Metadata);
}

// ===========================================================================
// 14. package.json with object-style license
// ===========================================================================

#[test]
fn package_json_object_license_detected() {
    let dir = tempdir().unwrap();
    fs::write(
        dir.path().join("package.json"),
        r#"{"name": "x", "license": {"type": "ISC"}}"#,
    )
    .unwrap();

    let files = vec![PathBuf::from("package.json")];
    let report = build_license_report(dir.path(), &files, &default_limits()).unwrap();

    assert_eq!(report.findings.len(), 1);
    assert_eq!(report.findings[0].spdx, "ISC");
}

// ===========================================================================
// 15. Effective license is highest confidence finding
// ===========================================================================

#[test]
fn effective_license_is_highest_confidence() {
    let dir = tempdir().unwrap();
    // Metadata confidence is 0.95; MIT text with both phrases yields 1.0
    fs::write(
        dir.path().join("Cargo.toml"),
        "[package]\nname = \"x\"\nlicense = \"Apache-2.0\"\n",
    )
    .unwrap();
    fs::write(
        dir.path().join("LICENSE"),
        "Permission is hereby granted, free of charge.\n\
         The software is provided \"as is\".",
    )
    .unwrap();

    let files = vec![PathBuf::from("Cargo.toml"), PathBuf::from("LICENSE")];
    let report = build_license_report(dir.path(), &files, &default_limits()).unwrap();

    // MIT text matching both phrases gives confidence 1.0, beating metadata 0.95
    assert_eq!(report.effective.as_deref(), Some("MIT"));
    // First finding should be highest confidence
    assert!(report.findings[0].confidence >= report.findings.last().unwrap().confidence);
}