| 1 | """Story terms: the run's own stories, fixed before the jobs lane starts. |
| 2 | |
| 3 | The jobs lane searches each board for a few terms drawn from the run's own |
| 4 | stories. The host plan sends them as ``jobs_queries``; when it sends none the |
| 5 | engine derives them from the plan's subqueries, leaving out the company, the |
| 6 | people, the other side of a comparison and generic words. |
| 7 | """ |
| 8 | |
| 9 | from unittest.mock import patch |
| 10 | |
| 11 | from lib import jobs_story, pipeline |
| 12 | |
| 13 | |
| 14 | def _plan(*search_queries, jobs_queries=None): |
| 15 | plan = { |
| 16 | "intent": "opinion", |
| 17 | "freshness_mode": "balanced_recent", |
| 18 | "cluster_mode": "debate", |
| 19 | "subqueries": [ |
| 20 | {"label": f"q{i}", "search_query": query, "ranking_query": query, |
| 21 | "sources": ["reddit", "jobs"], "weight": 1.0} |
| 22 | for i, query in enumerate(search_queries) |
| 23 | ], |
| 24 | } |
| 25 | if jobs_queries is not None: |
| 26 | plan["jobs_queries"] = jobs_queries |
| 27 | return plan |
| 28 | |
| 29 | |
| 30 | def _jobs_terms(topic, plan, config=None): |
| 31 | """The story terms the jobs lane was handed, and the report.""" |
| 32 | with patch.object(pipeline, "_retrieve_stream", return_value=([], {})) as retrieve: |
| 33 | report = pipeline.run( |
| 34 | topic=topic, config=dict(config or {}), mock=True, web_backend="none", |
| 35 | depth="default", external_plan=plan, |
| 36 | ) |
| 37 | jobs_calls = [c for c in retrieve.call_args_list if c.kwargs["source"] == "jobs"] |
| 38 | assert jobs_calls, "the jobs lane did not run" |
| 39 | terms = {tuple(c.kwargs["config"].get("_jobs_story_terms") or ()) for c in jobs_calls} |
| 40 | assert len(terms) == 1, "every jobs stream sees the same terms" |
| 41 | return list(terms.pop()), report |
| 42 | |
| 43 | |
| 44 | def test_host_story_terms_are_used_as_sent(capsys): |
| 45 | terms, report = _jobs_terms( |
| 46 | "Amazon", |
| 47 | _plan("Amazon agentic payments news", jobs_queries=["agentic payments", "humanoid"]), |
| 48 | ) |
| 49 | assert terms == ["agentic payments", "humanoid"] |
| 50 | assert report.query_plan.jobs_queries == ["agentic payments", "humanoid"] |
| 51 | err = capsys.readouterr().err |
| 52 | assert err.count("story terms") == 1 |
| 53 | assert "agentic payments, humanoid" in err |
| 54 | |
| 55 | |
| 56 | def test_host_story_terms_keep_the_first_four(): |
| 57 | sent = ["agentic payments", "humanoid", "Kuiper", "Zoox", "Alexa", "AWS Trainium", "Prime Air"] |
| 58 | terms, _ = _jobs_terms("Amazon", _plan("Amazon news", jobs_queries=sent)) |
| 59 | assert terms == ["agentic payments", "humanoid", "Kuiper", "Zoox"] |
| 60 | |
| 61 | |
| 62 | def test_host_story_terms_are_trimmed_deduped_and_length_capped(): |
| 63 | sent = [" humanoid ", "Humanoid", "", "x" * 41, "agentic payments"] |
| 64 | terms, _ = _jobs_terms("Amazon", _plan("Amazon news", jobs_queries=sent)) |
| 65 | assert terms == ["humanoid", "agentic payments"] |
| 66 | |
| 67 | |
| 68 | def test_engine_terms_leave_out_company_people_comparison_entity_and_generic_words(): |
| 69 | plan = _plan( |
| 70 | "Amazon Andy Jassy agentic payments", |
| 71 | "Walmart vs Amazon grocery delivery news", |
| 72 | "latest Amazon humanoid robots review", |
| 73 | ) |
| 74 | terms, report = _jobs_terms( |
| 75 | "Amazon", |
| 76 | plan, |
| 77 | config={ |
| 78 | "_company_signal": "the company Amazon.com", |
| 79 | "_company": {"company": "Amazon.com"}, |
| 80 | "_comparison_entities": ["Amazon", "Walmart"], |
| 81 | }, |
| 82 | ) |
| 83 | assert report.query_plan.jobs_queries == [] |
| 84 | assert terms == ["agentic payments", "grocery delivery", "humanoid robots"] |
| 85 | |
| 86 | |
| 87 | def test_engine_terms_leave_out_the_person_the_topic_names(): |
| 88 | terms, _ = _jobs_terms( |
| 89 | "Palmer Luckey", |
| 90 | _plan("Palmer Luckey drone interceptors", "Luckey hypersonics update"), |
| 91 | config={"_company": {"company": "Anduril Industries", "company_via_person": True}}, |
| 92 | ) |
| 93 | assert terms == ["drone interceptors", "hypersonics"] |
| 94 | |
| 95 | |
| 96 | def test_engine_terms_leave_out_both_sides_of_a_comparison_topic(): |
| 97 | terms, _ = _jobs_terms( |
| 98 | "Amazon vs Walmart", |
| 99 | _plan("Amazon vs Walmart drone delivery", "Walmart Amazon pharmacy 2026"), |
| 100 | config={"_company_signal": "--official-site"}, |
| 101 | ) |
| 102 | assert terms == ["drone delivery", "pharmacy"] |
| 103 | |
| 104 | |
| 105 | def test_engine_terms_keep_the_first_four(): |
| 106 | terms, _ = _jobs_terms( |
| 107 | "Amazon", |
| 108 | _plan("Amazon Kuiper satellites", "Amazon Zoox robotaxi", |
| 109 | "Amazon Trainium chips", "Amazon pharmacy", "Amazon grocery"), |
| 110 | config={"_company": {"company": "Amazon"}}, |
| 111 | ) |
| 112 | assert terms == ["Kuiper satellites", "Zoox robotaxi", "Trainium chips", "pharmacy"] |
| 113 | |
| 114 | |
| 115 | def test_short_term_matches_whole_words_only(): |
| 116 | assert jobs_story.matches("AI", "Senior Engineer, AI Native Products") |
| 117 | assert not jobs_story.matches("AI", "Maintenance Technician") |
| 118 | assert not jobs_story.matches("AWS", "Laws and Policy Counsel") |
| 119 | assert jobs_story.matches("aws", "AWS Solutions Architect") |
| 120 | |
| 121 | |
| 122 | def test_longer_term_matches_anywhere_ignoring_case(): |
| 123 | assert jobs_story.matches("humanoid", "Software Engineer, Humanoids") |
| 124 | assert jobs_story.matches("agentic payments", "Lead PM - Agentic Payments") |
| 125 | assert not jobs_story.matches("humanoid", "Warehouse Associate") |
| 126 | |
| 127 | |
| 128 | def test_every_comparison_subrun_knows_every_entity(monkeypatch, tmp_path): |
| 129 | from tests.competitor_cli_audit_helpers import run_competitor_cli |
| 130 | |
| 131 | observed = run_competitor_cli(monkeypatch, tmp_path) |
| 132 | |
| 133 | for entity in ("MainBrand", "PeerOne", "PeerTwo"): |
| 134 | assert observed.calls[entity]["config"]["_comparison_entities"] == [ |
| 135 | "MainBrand", "PeerOne", "PeerTwo", |
| 136 | ] |
| 137 |