| 1 | """Person run inputs: --employer, --linkedin, and --email. |
| 2 | |
| 3 | A person run starts only when the host passes one of these flags for a person |
| 4 | topic. A name-only run behaves exactly as before. The flags build the identity |
| 5 | anchors later stages filter on: the employer name and domain, and the email |
| 6 | domain unless it is a personal-mail provider, which identifies no one. |
| 7 | """ |
| 8 | |
| 9 | from __future__ import annotations |
| 10 | |
| 11 | import contextlib |
| 12 | import io |
| 13 | import json |
| 14 | import os |
| 15 | import sys |
| 16 | from contextlib import redirect_stderr, redirect_stdout |
| 17 | from unittest import mock |
| 18 | |
| 19 | import pytest |
| 20 | |
| 21 | import last30days as cli |
| 22 | from lib import person, schema |
| 23 | |
| 24 | NAME = "Dana Okafor" |
| 25 | |
| 26 | |
| 27 | def _cli(argv, tmp_path, *, run=None): |
| 28 | """Run ``main`` with research and config probes stubbed out.""" |
| 29 | out, err = io.StringIO(), io.StringIO() |
| 30 | cfg_dir = tmp_path / "cfg" |
| 31 | cfg_dir.mkdir(exist_ok=True) |
| 32 | with contextlib.ExitStack() as stack: |
| 33 | stack.enter_context(mock.patch.object(cli.env, "get_config", return_value={})) |
| 34 | stack.enter_context(mock.patch.object(cli.env, "CONFIG_DIR", cfg_dir)) |
| 35 | stack.enter_context(mock.patch.object(cli.env, "CONFIG_FILE", cfg_dir / ".env")) |
| 36 | stack.enter_context(mock.patch.object(cli.ui, "ProgressDisplay", return_value=mock.Mock())) |
| 37 | stack.enter_context(mock.patch.dict(os.environ, {"LAST30DAYS_SKIP_PREFLIGHT": "1"})) |
| 38 | stack.enter_context(mock.patch.object(sys, "argv", ["last30days.py", *argv])) |
| 39 | stack.enter_context(mock.patch.object( |
| 40 | cli.pipeline, "run", side_effect=run or AssertionError("pipeline.run must not run"), |
| 41 | )) |
| 42 | stack.enter_context(redirect_stdout(out)) |
| 43 | stack.enter_context(redirect_stderr(err)) |
| 44 | rc = cli.main() |
| 45 | return rc, out.getvalue(), err.getvalue() |
| 46 | |
| 47 | |
| 48 | def _capturing_run(store: dict): |
| 49 | def fake_run(**kwargs): |
| 50 | store.update(kwargs) |
| 51 | topic = kwargs["topic"] |
| 52 | return schema.Report( |
| 53 | topic=topic, range_from="2026-10-20", range_to="2026-11-19", |
| 54 | generated_at="2026-11-19T00:00:00+00:00", |
| 55 | provider_runtime=schema.ProviderRuntime( |
| 56 | reasoning_provider="local", planner_model="d", rerank_model="l", |
| 57 | ), |
| 58 | query_plan=schema.QueryPlan( |
| 59 | intent="general", freshness_mode="balanced_recent", cluster_mode="story", |
| 60 | raw_topic=topic, subqueries=[], source_weights={}, |
| 61 | ), |
| 62 | clusters=[], ranked_candidates=[], items_by_source={}, errors_by_source={}, |
| 63 | ) |
| 64 | return fake_run |
| 65 | |
| 66 | |
| 67 | class TestParser: |
| 68 | def test_person_flags_parse_as_strings(self): |
| 69 | args = cli.build_parser().parse_args([ |
| 70 | NAME, |
| 71 | "--employer", "Northwind Freight:northwind.example", |
| 72 | "--linkedin", "https://www.linkedin.com/in/dana-okafor", |
| 73 | "--email", "dana@northwind.example", |
| 74 | ]) |
| 75 | assert args.employer == "Northwind Freight:northwind.example" |
| 76 | assert args.linkedin == "https://www.linkedin.com/in/dana-okafor" |
| 77 | assert args.email == "dana@northwind.example" |
| 78 | |
| 79 | def test_name_only_run_has_no_person_inputs(self): |
| 80 | args = cli.build_parser().parse_args([NAME]) |
| 81 | assert (args.employer, args.linkedin, args.email) == (None, None, None) |
| 82 | assert person.build_person_context( |
| 83 | NAME, employer=args.employer, linkedin=args.linkedin, email=args.email, |
| 84 | ) is None |
| 85 | |
| 86 | |
| 87 | class TestAnchors: |
| 88 | def test_employer_name_and_domain_are_anchors(self): |
| 89 | ctx = person.build_person_context(NAME, employer="Northwind Freight:northwind.example") |
| 90 | assert ctx.full_name == NAME |
| 91 | assert ctx.employer_name == "Northwind Freight" |
| 92 | assert ctx.employer_domain == "northwind.example" |
| 93 | assert ctx.anchors == ["Northwind Freight", "northwind.example"] |
| 94 | assert ctx.identity_weak is False |
| 95 | |
| 96 | def test_employer_site_url_reduces_to_its_domain(self): |
| 97 | ctx = person.build_person_context( |
| 98 | NAME, employer="Northwind Freight:https://www.northwind.example/team", |
| 99 | ) |
| 100 | assert ctx.employer_domain == "northwind.example" |
| 101 | assert ctx.anchors == ["Northwind Freight", "northwind.example"] |
| 102 | |
| 103 | def test_employer_without_domain_anchors_the_name(self): |
| 104 | ctx = person.build_person_context(NAME, employer="Northwind Freight") |
| 105 | assert ctx.employer_domain == "" |
| 106 | assert ctx.anchors == ["Northwind Freight"] |
| 107 | |
| 108 | def test_work_email_adds_its_domain(self): |
| 109 | ctx = person.build_person_context(NAME, email="Dana@Northwind.example") |
| 110 | assert ctx.email_domain == "northwind.example" |
| 111 | assert ctx.anchors == ["northwind.example"] |
| 112 | assert ctx.identity_weak is False |
| 113 | |
| 114 | def test_email_domain_matching_employer_domain_is_one_anchor(self): |
| 115 | ctx = person.build_person_context( |
| 116 | NAME, employer="Northwind Freight:northwind.example", email="dana@northwind.example", |
| 117 | ) |
| 118 | assert ctx.anchors == ["Northwind Freight", "northwind.example"] |
| 119 | |
| 120 | @pytest.mark.parametrize("address", [ |
| 121 | "dana@gmail.com", "dana@googlemail.com", "dana@yahoo.co.uk", "dana@hotmail.fr", |
| 122 | "dana@outlook.com", "dana@icloud.com", "dana@proton.me", "dana@gmx.de", |
| 123 | ]) |
| 124 | def test_personal_email_adds_no_anchor_and_marks_identity_weak(self, address): |
| 125 | ctx = person.build_person_context(NAME, email=address) |
| 126 | assert ctx.anchors == [] |
| 127 | assert ctx.email_domain == "" |
| 128 | assert ctx.identity_weak is True |
| 129 | # The full address is kept for a later lookup, not discarded. |
| 130 | assert ctx.email == address |
| 131 | |
| 132 | def test_personal_email_with_employer_is_not_weak(self): |
| 133 | ctx = person.build_person_context( |
| 134 | NAME, employer="Northwind Freight", email="dana@gmail.com", |
| 135 | ) |
| 136 | assert ctx.anchors == ["Northwind Freight"] |
| 137 | assert ctx.identity_weak is False |
| 138 | |
| 139 | def test_company_domain_named_like_a_mail_provider_is_an_anchor(self): |
| 140 | ctx = person.build_person_context(NAME, email="dana@outlook-freight.example") |
| 141 | assert ctx.anchors == ["outlook-freight.example"] |
| 142 | |
| 143 | def test_linkedin_url_is_normalized_and_not_weak(self): |
| 144 | ctx = person.build_person_context( |
| 145 | NAME, linkedin="http://uk.linkedin.com/in/Dana-Okafor/details/experience?trk=x#top", |
| 146 | ) |
| 147 | assert ctx.linkedin_url == "https://www.linkedin.com/in/dana-okafor" |
| 148 | assert ctx.identity_weak is False |
| 149 | |
| 150 | |
| 151 | def test_linkedin_status_accepts_only_known_values(self): |
| 152 | ctx = person.build_person_context(NAME, employer="Northwind Freight") |
| 153 | for status in ("checked", "no key", "failed", "profile not found", "match rejected"): |
| 154 | ctx.set_linkedin_status(status) |
| 155 | assert ctx.linkedin_status == status |
| 156 | with pytest.raises(ValueError): |
| 157 | ctx.set_linkedin_status("quiet") |
| 158 | |
| 159 | def test_to_dict_never_carries_the_email_address(self): |
| 160 | for address in ("dana@northwind.example", "dana@gmail.com"): |
| 161 | ctx = person.build_person_context(NAME, email=address) |
| 162 | data = ctx.to_dict() |
| 163 | assert "email" not in data |
| 164 | assert address not in json.dumps(data) |
| 165 | assert "dana@" not in json.dumps(data) |
| 166 | assert data["full_name"] == NAME |
| 167 | assert "dana@" not in repr(ctx) |
| 168 | |
| 169 | |
| 170 | class TestShapeChecks: |
| 171 | @pytest.mark.parametrize("url", [ |
| 172 | "linkedin.com/in/dana-okafor", |
| 173 | "https://www.linkedin.com/company/northwind-freight", |
| 174 | "https://www.linkedin.com/in/", |
| 175 | "https://evil.example/in/dana-okafor", |
| 176 | "https://linkedin.com.evil.example/in/dana-okafor", |
| 177 | "ftp://www.linkedin.com/in/dana-okafor", |
| 178 | "https://www.linkedin.com/in/dana okafor", |
| 179 | ]) |
| 180 | def test_malformed_linkedin_url_is_rejected(self, url): |
| 181 | with pytest.raises(ValueError, match="--linkedin"): |
| 182 | person.build_person_context(NAME, linkedin=url) |
| 183 | |
| 184 | @pytest.mark.parametrize("address", [ |
| 185 | "dana", "dana@", "@northwind.example", "dana@northwind", "dana@@northwind.example", |
| 186 | "dana okafor@northwind.example", "", |
| 187 | ]) |
| 188 | def test_malformed_email_is_rejected_without_echoing_it(self, address): |
| 189 | with pytest.raises(ValueError, match="--email") as exc: |
| 190 | person.build_person_context(NAME, email=address) |
| 191 | if address: |
| 192 | assert address not in str(exc.value) |
| 193 | |
| 194 | @pytest.mark.parametrize("employer", ["", ":northwind.example", "Northwind Freight:not a domain"]) |
| 195 | def test_malformed_employer_is_rejected(self, employer): |
| 196 | with pytest.raises(ValueError, match="--employer"): |
| 197 | person.build_person_context(NAME, employer=employer) |
| 198 | |
| 199 | |
| 200 | class TestMain: |
| 201 | def test_malformed_linkedin_url_exits_2(self, tmp_path): |
| 202 | rc, _, err = _cli([NAME, "--linkedin", "https://www.linkedin.com/company/northwind"], tmp_path) |
| 203 | assert rc == 2 |
| 204 | assert "--linkedin" in err |
| 205 | |
| 206 | def test_malformed_email_exits_2_without_echoing_it(self, tmp_path): |
| 207 | rc, _, err = _cli([NAME, "--email", "dana-at-northwind"], tmp_path) |
| 208 | assert rc == 2 |
| 209 | assert "--email" in err and "dana-at-northwind" not in err |
| 210 | |
| 211 | @pytest.mark.parametrize("argv", [ |
| 212 | ["Dana Okafor vs Lee Park", "--employer", "Northwind Freight"], |
| 213 | [NAME, "--competitors-list", "Lee Park", "--email", "dana@northwind.example"], |
| 214 | ]) |
| 215 | def test_person_flags_in_a_comparison_run_exit_2(self, tmp_path, argv): |
| 216 | rc, _, err = _cli(argv, tmp_path) |
| 217 | assert rc == 2 |
| 218 | assert "comparison" in err |
| 219 | assert "dana@" not in err |
| 220 | |
| 221 | def test_person_flags_without_a_topic_exit_2(self, tmp_path): |
| 222 | rc, _, err = _cli(["--employer", "Northwind Freight"], tmp_path) |
| 223 | assert rc == 2 |
| 224 | assert "--employer" in err |
| 225 | |
| 226 | def test_valid_person_flags_reach_the_run(self, tmp_path): |
| 227 | captured: dict = {} |
| 228 | rc, _, err = _cli( |
| 229 | [NAME, "--mock", "--employer", "Northwind Freight:northwind.example", |
| 230 | "--linkedin", "https://www.linkedin.com/in/dana-okafor", "--email", "dana@gmail.com"], |
| 231 | tmp_path, run=_capturing_run(captured), |
| 232 | ) |
| 233 | assert rc == 0, err |
| 234 | ctx = captured["config"]["_person"] |
| 235 | assert ctx.full_name == NAME |
| 236 | assert {"Northwind Freight", "northwind.example"} <= set(ctx.anchors) |
| 237 | assert ctx.linkedin_url == "https://www.linkedin.com/in/dana-okafor" |
| 238 | assert ctx.email_domain == "" |
| 239 | assert "dana@gmail.com" not in err |
| 240 | |
| 241 | def test_name_only_run_config_carries_nothing_new(self, tmp_path): |
| 242 | captured: dict = {} |
| 243 | rc, _, err = _cli([NAME, "--mock"], tmp_path, run=_capturing_run(captured)) |
| 244 | assert rc == 0, err |
| 245 | assert not any("person" in key.lower() for key in captured) |
| 246 | assert not any("person" in str(key).lower() for key in captured["config"]) |
| 247 | |
| 248 | |
| 249 | def test_person_inputs_separate_cached_reports_and_the_email_is_only_hashed(): |
| 250 | def options(*argv): |
| 251 | return cli._cache_research_options(cli.build_parser().parse_args(["Dana Okafor", *argv])) |
| 252 | |
| 253 | first = options("--employer", "Northwind Freight") |
| 254 | assert first != options("--employer", "Contoso Logistics") |
| 255 | assert options("--linkedin", "https://www.linkedin.com/in/dana-a") != options( |
| 256 | "--linkedin", "https://www.linkedin.com/in/dana-b" |
| 257 | ) |
| 258 | by_email = options("--email", "dana@northwind.example") |
| 259 | assert by_email != options("--email", "dana@contoso.example") |
| 260 | assert "dana@northwind.example" not in json.dumps(by_email) |
| 261 |