Spaces:
Running on Zero

File size: 5,945 Bytes
ca82a2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
from app.models.resume_schema import ProcessedResume, RawResumeExtraction
from app.services.post_processor import post_process_resume
from scripts.evaluate_extractor import (
    check_hallucination,
    compute_sequence_ratio,
    compute_set_metrics,
    compute_token_f1,
    is_title_truncated,
)


# ---------------------------------------------------------------------------
# 1. Regression Test: Em-dash Email ('—') Elimination
# ---------------------------------------------------------------------------

def test_regression_em_dash_email_handling():
    """
    Guarantees that em-dashes ('—', '–', '\u2014') and placeholder strings
    in raw extractions strictly produce null in the validated schema.
    """
    bad_emails = ["—", "–", "-", "\u2014", "\u2013", "\u2212", "null", "None", "not available", "N/A", ""]
    for bad_email in bad_emails:
        raw = RawResumeExtraction(
            candidate_name="Test Candidate",
            email=bad_email,
            job_title="Software Engineer",
        )
        processed = post_process_resume(raw)
        assert processed.email is None, f"Failed for email placeholder: {repr(bad_email)}"


# ---------------------------------------------------------------------------
# 2. Regression Test: Parenthesized Title Preservation
# ---------------------------------------------------------------------------

def test_regression_parenthesized_title_preservation():
    """
    Guarantees that parenthesized tech stacks in job titles are preserved
    fully without truncation at commas, slashes, or dashes.
    """
    titles = [
        "Full-Stack Developer (TypeScript / Node.js / React)",
        "Senior Cloud Architect (AWS / Azure) - Enterprise Systems",
        "Machine Learning Engineer (NLP / PyTorch / LLMs)",
        "Backend Software Engineer (Go / Kubernetes / gRPC)",
    ]
    for title in titles:
        raw = RawResumeExtraction(
            candidate_name="Alex Developer",
            job_title=title,
            work_experience=[
                {"title": title, "company": "Tech Corp", "dates": "2021 - 2024"}
            ],
        )
        processed = post_process_resume(raw)
        assert processed.job_title == title
        assert processed.work_experience[0].title == title
        assert ")" in processed.job_title


# ---------------------------------------------------------------------------
# 3. Regression Test: Missing Sections Graceful Handling
# ---------------------------------------------------------------------------

def test_regression_missing_sections_graceful_handling():
    """
    Tests resumes with missing sections (e.g. no education, no projects, or fresher)
    to ensure 100% schema compliance without throwing exceptions.
    """
    # 1. No education, no certifications
    raw_no_edu = RawResumeExtraction(
        candidate_name="Self-Taught Coder",
        email="coder@example.com",
        job_title="Frontend Developer",
        skills=["React", "JavaScript"],
        education=[],
        work_experience=[{"title": "Web Developer", "dates": "2022 - 2024"}],
    )
    processed_no_edu = post_process_resume(raw_no_edu)
    assert processed_no_edu.education == []
    assert processed_no_edu.degrees == []
    assert processed_no_edu.skills == ["React", "JavaScript"]

    # 2. Fresher with no work experience
    raw_fresher = RawResumeExtraction(
        candidate_name="Fresher Graduate",
        email="fresher@example.com",
        job_title="Junior Engineer",
        education=[{"degree": "Bachelor of Technology", "year": "2024"}],
        work_experience=[],
    )
    processed_fresher = post_process_resume(raw_fresher)
    assert processed_fresher.work_experience == []
    assert processed_fresher.total_experience_years == 0.0


# ---------------------------------------------------------------------------
# 4. Regression Test: Mojibake Encoding Resilience
# ---------------------------------------------------------------------------

def test_regression_mojibake_encoding_resilience():
    """
    Ensures that text corrupted with unicode replacement artifacts (\ufffd, non-breaking spaces)
    is cleaned without breaking schema validation.
    """
    noisy_name = "Nicole\ufffd Mclaughlin"
    noisy_title = "Data\u00a0Engineer\u200b (Python\u00a0/\u00a0Spark)"
    noisy_skill = "React\ufffd"

    raw = RawResumeExtraction(
        candidate_name=noisy_name,
        job_title=noisy_title,
        skills=[noisy_skill, "Docker"],
    )
    processed = post_process_resume(raw)

    assert "\ufffd" not in (processed.candidate_name or "")
    assert "\u200b" not in (processed.job_title or "")
    assert processed.job_title == "Data Engineer (Python / Spark)"
    assert "Docker" in processed.skills


# ---------------------------------------------------------------------------
# 5. Metric Evaluators Correctness Tests
# ---------------------------------------------------------------------------

def test_metric_is_title_truncated():
    gold = "Full-Stack Developer (TypeScript / Node.js / React)"
    truncated_pred = "Full-Stack Developer (TypeScript"
    complete_pred = "Full-Stack Developer (TypeScript / Node.js / React)"

    assert is_title_truncated(truncated_pred, gold) is True
    assert is_title_truncated(complete_pred, gold) is False


def test_metric_compute_set_metrics():
    gold = ["React", "TypeScript", "Node.js"]
    pred = ["React", "TypeScript", "Python"]

    prec, rec, f1 = compute_set_metrics(pred, gold)
    assert round(prec, 2) == 0.67
    assert round(rec, 2) == 0.67
    assert round(f1, 2) == 0.67


def test_metric_check_hallucination():
    source_text = "Experienced with React, Node.js and TypeScript on Azure."
    grounded = "TypeScript"
    hallucinated = "Ruby on Rails Cobol Mainframe"

    assert check_hallucination(grounded, source_text) is False
    assert check_hallucination(hallucinated, source_text) is True