Skip to main content

source_map_tauri/frontend/
tests.rs

1use std::path::Path;
2
3use regex::Regex;
4use serde_json::Value;
5
6use crate::{
7    config::{normalize_path, ResolvedConfig},
8    ids::document_id,
9    model::ArtifactDoc,
10    security::apply_artifact_security,
11};
12
13pub fn extract_frontend_tests(
14    config: &ResolvedConfig,
15    path: &Path,
16    text: &str,
17) -> Vec<ArtifactDoc> {
18    let import_re =
19        Regex::new(r#"import\s+\{?\s*([A-Za-z0-9_,\s]+)\s*\}?\s+from"#).expect("valid regex");
20    let command_re = Regex::new(r#""([^"]+)""#).expect("valid regex");
21
22    let source_path = normalize_path(&config.root, path);
23    let name = path
24        .file_name()
25        .and_then(|item| item.to_str())
26        .unwrap_or("frontend_test");
27    let mut doc = ArtifactDoc {
28        id: document_id(
29            &config.repo,
30            "frontend_test",
31            Some(&source_path),
32            Some(1),
33            Some(name),
34        ),
35        repo: config.repo.clone(),
36        kind: "frontend_test".to_owned(),
37        side: Some("test".to_owned()),
38        language: crate::frontend::language_for_path(path),
39        name: Some(name.to_owned()),
40        display_name: Some(name.to_owned()),
41        source_path: Some(source_path),
42        line_start: Some(1),
43        line_end: Some(text.lines().count() as u32),
44        column_start: None,
45        column_end: None,
46        package_name: None,
47        comments: Vec::new(),
48        tags: vec!["frontend test".to_owned()],
49        related_symbols: Vec::new(),
50        related_tests: Vec::new(),
51        risk_level: "low".to_owned(),
52        risk_reasons: Vec::new(),
53        contains_phi: false,
54        has_related_tests: false,
55        updated_at: chrono::Utc::now().to_rfc3339(),
56        data: Default::default(),
57    };
58
59    let imports = import_re
60        .captures_iter(text)
61        .flat_map(|capture| {
62            capture[1]
63                .split(',')
64                .map(|item| item.trim().to_owned())
65                .filter(|item| !item.is_empty())
66                .collect::<Vec<_>>()
67        })
68        .collect::<Vec<_>>();
69    let mocked_commands = if text.contains("mockIPC") {
70        command_re
71            .captures_iter(text)
72            .filter_map(|capture| {
73                let value = capture.get(1).expect("command").as_str();
74                if value.contains('|')
75                    || value
76                        .chars()
77                        .all(|item| item.is_ascii_alphanumeric() || item == '_' || item == ':')
78                {
79                    Some(value.to_owned())
80                } else {
81                    None
82                }
83            })
84            .collect::<Vec<_>>()
85    } else {
86        Vec::new()
87    };
88
89    doc.data.insert(
90        "imports".to_owned(),
91        Value::Array(imports.into_iter().map(Value::String).collect()),
92    );
93    doc.data.insert(
94        "mocked_commands".to_owned(),
95        Value::Array(mocked_commands.into_iter().map(Value::String).collect()),
96    );
97    doc.data.insert(
98        "command".to_owned(),
99        Value::String(format!(
100            "pnpm vitest {}",
101            normalize_path(&config.root, path)
102        )),
103    );
104
105    apply_artifact_security(&mut doc);
106    vec![doc]
107}