File size: 2,117 Bytes
f190f3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for agent/utils.py — pure functions, no LLM needed."""

import pytest
from agent.utils import strip_fences, parse_json, job_institution, job_description


class TestStripFences:
    def test_no_fence(self):
        assert strip_fences('{"a": 1}') == '{"a": 1}'

    def test_json_fence(self):
        assert strip_fences('```json\n{"a": 1}\n```') == '{"a": 1}'

    def test_plain_fence(self):
        assert strip_fences('```\n{"a": 1}\n```') == '{"a": 1}'

    def test_leading_trailing_whitespace(self):
        assert strip_fences('  ```json\n{"a": 1}\n```  ') == '{"a": 1}'


class TestParseJson:
    def test_valid_json(self):
        assert parse_json('{"score": 80}') == {"score": 80}

    def test_json_inside_fence(self):
        assert parse_json('```json\n{"score": 80}\n```') == {"score": 80}

    def test_json_embedded_in_text(self):
        raw = 'Here is the result: {"score": 75} done.'
        assert parse_json(raw) == {"score": 75}

    def test_invalid_json_returns_none(self):
        assert parse_json("not json at all") is None

    def test_empty_string_returns_none(self):
        assert parse_json("") is None

    def test_nested_json(self):
        raw = '{"match_score": 85, "areas": ["ML", "NLP"]}'
        result = parse_json(raw)
        assert result["match_score"] == 85
        assert result["areas"] == ["ML", "NLP"]


class TestJobInstitution:
    def test_institution_field(self):
        assert job_institution({"institution": "MIT"}) == "MIT"

    def test_missing_returns_empty(self):
        assert job_institution({}) == ""

    def test_none_returns_empty(self):
        assert job_institution({"institution": None}) == ""


class TestJobDescription:
    def test_returns_description(self):
        job = {"description": "PhD in ML"}
        assert job_description(job) == "PhD in ML"

    def test_missing_returns_placeholder(self):
        assert job_description({}) == "No description provided."

    def test_truncation(self):
        job = {"description": "x" * 5000}
        result = job_description(job, max_chars=100)
        assert len(result) == 100