| 1 | """The person resolve step in ``main``: LinkedIn, bio page, and handles. |
| 2 | |
| 3 | A person run resolves who the person is before the X-handle gate, because a |
| 4 | handle found on their profile or bio page changes which lanes run. These |
| 5 | tests drive ``main`` with the LinkedIn lookup faked at ``lib.http.request`` |
| 6 | and the bio lane faked at its entry point, and read what reaches |
| 7 | ``pipeline.run``. |
| 8 | """ |
| 9 | |
| 10 | from __future__ import annotations |
| 11 | |
| 12 | import io |
| 13 | import sys |
| 14 | import threading |
| 15 | import time |
| 16 | from contextlib import redirect_stderr, redirect_stdout |
| 17 | from unittest import mock |
| 18 | |
| 19 | import last30days as cli |
| 20 | from lib import deepline, employer_site, http |
| 21 | |
| 22 | NAME = "Dana Okafor" |
| 23 | EMPLOYER = "Northwind Freight:northwind.example" |
| 24 | PROFILE_URL = "https://www.linkedin.com/in/dana-okafor" |
| 25 | DEEPLINE = "https://code.deepline.com/api/v2/integrations" |
| 26 | DIAG = { |
| 27 | "available_sources": ["reddit", "x", "hackernews", "github"], |
| 28 | "providers": {"google": False, "openai": False, "xai": False}, |
| 29 | "x_backend": None, |
| 30 | "bird_installed": False, |
| 31 | "bird_authenticated": False, |
| 32 | "bird_username": None, |
| 33 | "native_web_backend": None, |
| 34 | "permission_preflight": {}, |
| 35 | } |
| 36 | PLAN_WITH_X = ( |
| 37 | '{"intent":"general","freshness_mode":"balanced_recent","cluster_mode":"none",' |
| 38 | '"subqueries":[{"label":"primary","search_query":"Dana Okafor","ranking_query":' |
| 39 | '"What has Dana Okafor done lately?","sources":["reddit","x","hackernews"],"weight":1.0}]}' |
| 40 | ) |
| 41 | HOSTED = {"CLAUDECODE": "1"} |
| 42 | NO_BIO = {"status": "not found", "url": "", "title": "", "bio": "", "links": [], "domain": "northwind.example"} |
| 43 | |
| 44 | |
| 45 | def _profile(websites: list[str], company: str = "Northwind Freight") -> dict: |
| 46 | return {"status": "completed", "result": {"data": {"element": { |
| 47 | "firstName": "Dana", "lastName": "Okafor", |
| 48 | "headline": f"VP Operations at {company}", |
| 49 | "linkedinUrl": PROFILE_URL, |
| 50 | "currentPosition": [{"position": "VP Operations", "companyName": company, |
| 51 | "startDate": {"text": "Mar 2024"}}], |
| 52 | "experience": [], |
| 53 | "websites": websites, |
| 54 | }}}} |
| 55 | |
| 56 | |
| 57 | class _RetrievalReached(Exception): |
| 58 | pass |
| 59 | |
| 60 | |
| 61 | def _run_main(monkeypatch, argv, *, websites=(), bio=None, config=None, host_env=None, auto=None, socials=None): |
| 62 | for name in ("LAST30DAYS_API_KEY", "LAST30DAYS_API_BASE", "LAST30DAYS_MEMORY_DIR"): |
| 63 | monkeypatch.delenv(name, raising=False) |
| 64 | monkeypatch.setenv("LAST30DAYS_SKIP_PREFLIGHT", "1") |
| 65 | for name, value in (host_env or {}).items(): |
| 66 | monkeypatch.setenv(name, value) |
| 67 | monkeypatch.setattr(sys, "argv", ["last30days.py", *argv]) |
| 68 | |
| 69 | requests: list[str] = [] |
| 70 | |
| 71 | def fake_request(method, url, headers=None, json_data=None, **kwargs): |
| 72 | requests.append(url) |
| 73 | if url == f"{DEEPLINE}/harvestapi_get_profile/execute": |
| 74 | return _profile(list(websites)) |
| 75 | if url == f"{DEEPLINE}/limadata_find_person_profiles/execute": |
| 76 | return {"status": "completed", "result": {"data": {"linkedin_url": PROFILE_URL}}} |
| 77 | raise AssertionError(f"unexpected request to {url}") |
| 78 | |
| 79 | bio_lane = bio if callable(bio) else mock.Mock(return_value=dict(bio or NO_BIO)) |
| 80 | run_mock = mock.Mock(side_effect=_RetrievalReached) |
| 81 | err = io.StringIO() |
| 82 | cfg = {"OPENROUTER_API_KEY": "dummy-openrouter-key-000", "DEEPLINE_API_KEY": "dl-test-key"} |
| 83 | cfg.update(config or {}) |
| 84 | with mock.patch.object(cli.env, "get_config", return_value=cfg), \ |
| 85 | mock.patch.object(cli.pipeline, "diagnose", mock.Mock(return_value=dict(DIAG))), \ |
| 86 | mock.patch.object(cli.pipeline, "run", run_mock), \ |
| 87 | mock.patch.object(http, "request", fake_request), \ |
| 88 | mock.patch.object(employer_site, "find_bio", bio_lane), \ |
| 89 | mock.patch("lib.resolve.auto_resolve", mock.Mock(return_value=dict(auto or {}))), \ |
| 90 | mock.patch("lib.resolve.official_site_socials", mock.Mock(return_value=dict(socials or {"x_handle": "", "github_user": ""}))), \ |
| 91 | mock.patch.object(cli.ui, "ProgressDisplay", return_value=mock.Mock()), \ |
| 92 | redirect_stdout(io.StringIO()), redirect_stderr(err): |
| 93 | try: |
| 94 | rc = cli.main() |
| 95 | except _RetrievalReached: |
| 96 | rc = None |
| 97 | except SystemExit as exc: |
| 98 | rc = exc.code |
| 99 | return rc, err.getvalue(), run_mock, requests |
| 100 | |
| 101 | |
| 102 | def _person(run_mock): |
| 103 | return run_mock.call_args.kwargs["config"]["_person"] |
| 104 | |
| 105 | |
| 106 | def test_profile_x_link_feeds_the_x_lane_as_a_derived_handle(monkeypatch): |
| 107 | rc, err, run_mock, _ = _run_main( |
| 108 | monkeypatch, [NAME, "--employer", EMPLOYER, "--linkedin", PROFILE_URL, "--plan", PLAN_WITH_X], |
| 109 | websites=["https://x.com/danaokafor", "https://github.com/dokafor"], host_env=HOSTED, |
| 110 | ) |
| 111 | assert rc is None, err |
| 112 | assert run_mock.call_args.kwargs["x_handle"] == "danaokafor" |
| 113 | assert run_mock.call_args.kwargs["github_user"] == "dokafor" |
| 114 | assert _person(run_mock).derived_handles == {"x": "danaokafor", "github": "dokafor"} |
| 115 | |
| 116 | |
| 117 | def test_host_x_handle_beats_the_bio_page(monkeypatch): |
| 118 | bio = dict(NO_BIO, status="found", links=["https://x.com/dana_bio"]) |
| 119 | rc, err, run_mock, _ = _run_main( |
| 120 | monkeypatch, [NAME, "--employer", EMPLOYER, "--x-handle", "dana_host", "--plan", PLAN_WITH_X], |
| 121 | bio=bio, host_env=HOSTED, |
| 122 | ) |
| 123 | assert rc is None, err |
| 124 | assert run_mock.call_args.kwargs["x_handle"] == "dana_host" |
| 125 | assert "x" not in _person(run_mock).derived_handles |
| 126 | |
| 127 | |
| 128 | def test_bio_page_links_win_over_profile_websites(monkeypatch): |
| 129 | bio = dict(NO_BIO, status="found", links=["https://x.com/dana_bio"]) |
| 130 | rc, err, run_mock, _ = _run_main( |
| 131 | monkeypatch, [NAME, "--employer", EMPLOYER, "--linkedin", PROFILE_URL, "--plan", PLAN_WITH_X], |
| 132 | websites=["https://x.com/dana_profile"], bio=bio, host_env=HOSTED, |
| 133 | ) |
| 134 | assert rc is None, err |
| 135 | assert run_mock.call_args.kwargs["x_handle"] == "dana_bio" |
| 136 | |
| 137 | |
| 138 | def test_company_x_account_is_not_a_derived_handle(monkeypatch): |
| 139 | rc, err, run_mock, _ = _run_main( |
| 140 | monkeypatch, [NAME, "--employer", EMPLOYER, "--linkedin", PROFILE_URL, "--plan", PLAN_WITH_X], |
| 141 | websites=["https://x.com/NorthwindFreight", "https://www.northwind.example/", "https://danaokafor.example"], |
| 142 | host_env=HOSTED, |
| 143 | ) |
| 144 | assert rc is None, err |
| 145 | assert run_mock.call_args.kwargs["x_handle"] is None |
| 146 | assert _person(run_mock).derived_handles == {"site": "https://danaokafor.example"} |
| 147 | |
| 148 | |
| 149 | def test_person_run_with_no_handles_passes_the_x_handle_gate(monkeypatch): |
| 150 | rc, err, run_mock, _ = _run_main( |
| 151 | monkeypatch, [NAME, "--employer", EMPLOYER, "--plan", PLAN_WITH_X], host_env=HOSTED, |
| 152 | ) |
| 153 | assert rc is None, err |
| 154 | assert "X handle decision missing" not in err |
| 155 | assert "No X handle:" in err |
| 156 | assert run_mock.call_args.kwargs["x_handle"] is None |
| 157 | |
| 158 | |
| 159 | def test_person_run_ignores_handles_auto_resolve_picked_by_mention_count(monkeypatch): |
| 160 | rc, err, run_mock, _ = _run_main( |
| 161 | monkeypatch, [NAME, "--employer", EMPLOYER, "--auto-resolve"], |
| 162 | auto={"x_handle": "dana_namesake", "github_user": "dana-namesake"}, host_env=HOSTED, |
| 163 | ) |
| 164 | assert rc is None, err |
| 165 | assert run_mock.call_args.kwargs["x_handle"] is None |
| 166 | assert run_mock.call_args.kwargs["github_user"] is None |
| 167 | |
| 168 | |
| 169 | def test_person_run_ignores_handles_from_an_auto_resolved_official_site(monkeypatch): |
| 170 | # A host whose label matches the bare name may be a namesake's site. |
| 171 | rc, err, run_mock, _ = _run_main( |
| 172 | monkeypatch, [NAME, "--employer", EMPLOYER, "--auto-resolve"], |
| 173 | auto={"trustpilot_domain": "danaokafor.example"}, |
| 174 | socials={"x_handle": "dana_namesake", "github_user": "dana-namesake"}, host_env=HOSTED, |
| 175 | ) |
| 176 | assert rc is None, err |
| 177 | assert run_mock.call_args.kwargs["x_handle"] is None |
| 178 | assert run_mock.call_args.kwargs["github_user"] is None |
| 179 | |
| 180 | |
| 181 | def test_resolve_deadline_lets_the_run_proceed_and_records_a_timeout(monkeypatch): |
| 182 | monkeypatch.setattr(cli, "PERSON_RESOLVE_SECONDS", 0.5) |
| 183 | # The paid client will not start a call with under its usual floor left. |
| 184 | monkeypatch.setattr(deepline, "MIN_ATTEMPT_SECONDS", 0.0) |
| 185 | release = threading.Event() |
| 186 | |
| 187 | def slow_bio(*args, **kwargs): |
| 188 | release.wait(5) |
| 189 | return dict(NO_BIO) |
| 190 | |
| 191 | started = time.monotonic() |
| 192 | try: |
| 193 | rc, err, run_mock, _ = _run_main( |
| 194 | monkeypatch, [NAME, "--employer", EMPLOYER, "--linkedin", PROFILE_URL, "--plan", PLAN_WITH_X], |
| 195 | websites=["https://x.com/danaokafor"], bio=slow_bio, host_env=HOSTED, |
| 196 | ) |
| 197 | finally: |
| 198 | release.set() |
| 199 | assert time.monotonic() - started < 4 |
| 200 | assert rc is None, err |
| 201 | person = _person(run_mock) |
| 202 | assert person.timed_out is True |
| 203 | assert person.linkedin_status == "checked" |
| 204 | assert run_mock.call_args.kwargs["x_handle"] == "danaokafor" |
| 205 | |
| 206 | |
| 207 | def test_end_to_end_person_run_reaches_the_pipeline_resolved(monkeypatch): |
| 208 | bio = dict(NO_BIO, status="found", url="https://northwind.example/team/dana-okafor", |
| 209 | links=["https://github.com/dokafor"]) |
| 210 | rc, err, run_mock, requests = _run_main( |
| 211 | monkeypatch, [NAME, "--employer", EMPLOYER, "--email", "dana.okafor@northwind.example", |
| 212 | "--plan", PLAN_WITH_X], |
| 213 | websites=["https://x.com/danaokafor"], bio=bio, host_env=HOSTED, |
| 214 | ) |
| 215 | assert rc is None, err |
| 216 | assert requests == [ |
| 217 | f"{DEEPLINE}/limadata_find_person_profiles/execute", |
| 218 | f"{DEEPLINE}/harvestapi_get_profile/execute", |
| 219 | ] |
| 220 | person = _person(run_mock) |
| 221 | assert person.linkedin_url == PROFILE_URL |
| 222 | assert person.linkedin_status == "checked" |
| 223 | for anchor in ("Northwind Freight", "northwind.example", PROFILE_URL): |
| 224 | assert anchor in person.anchors |
| 225 | assert person.bio["url"] == "https://northwind.example/team/dana-okafor" |
| 226 | assert person.derived_handles == {"x": "danaokafor", "github": "dokafor"} |
| 227 | assert person.timed_out is False |
| 228 | assert run_mock.call_args.kwargs["x_handle"] == "danaokafor" |
| 229 | assert run_mock.call_args.kwargs["github_user"] == "dokafor" |
| 230 | assert "dana.okafor@northwind.example" not in err |
| 231 | |
| 232 | |
| 233 | def test_run_without_person_flags_has_no_resolve_step(monkeypatch): |
| 234 | rc, err, run_mock, requests = _run_main( |
| 235 | monkeypatch, [NAME, "--x-handle", "dana_host", "--plan", PLAN_WITH_X], host_env=HOSTED, |
| 236 | ) |
| 237 | assert rc is None, err |
| 238 | assert requests == [] |
| 239 | assert "_person" not in run_mock.call_args.kwargs["config"] |
| 240 |