| 1 | """Job postings stay out of the freshness note and do not flood the saved file. |
| 2 | |
| 3 | Evidence, live runs on 2026-11-10: SpaceX printed "Recent evidence is thin: |
| 4 | only 278 of 3039 dated items are from the last 7 days." when 2,898 of those |
| 5 | items were job postings, and its saved raw file was 2.4 MB, 2.3 MB of it one |
| 6 | "### Jobs (2898 items)" section. |
| 7 | """ |
| 8 | |
| 9 | from lib import jobs_digest, render, schema |
| 10 | |
| 11 | WINDOW = ("2026-10-12", "2026-11-11") |
| 12 | BOILERPLATE = "SpaceX was founded under the belief that a future where humanity is out exploring the stars. " * 4 |
| 13 | |
| 14 | |
| 15 | def _post(index, published): |
| 16 | return schema.SourceItem( |
| 17 | item_id=f"R{index}", source="reddit", title=f"Starship thread {index}", body="", |
| 18 | url=f"https://www.reddit.com/r/spacex/comments/{index}", published_at=published, |
| 19 | ) |
| 20 | |
| 21 | |
| 22 | def _job(index, published="2026-03-01"): |
| 23 | return schema.SourceItem( |
| 24 | item_id=f"GH{index}", source="jobs", title=f"Engineer {index}", body=BOILERPLATE, |
| 25 | url=f"https://boards.greenhouse.io/spacex/jobs/{index}", published_at=published, |
| 26 | container=f"Team {index}", snippet=BOILERPLATE, |
| 27 | ) |
| 28 | |
| 29 | |
| 30 | def _candidate(item): |
| 31 | return schema.Candidate( |
| 32 | candidate_id=f"c-{item.item_id}", item_id=item.item_id, source=item.source, |
| 33 | title=item.title, url=item.url, snippet=item.snippet or "", subquery_labels=["primary"], |
| 34 | native_ranks={}, local_relevance=0.5, freshness=50, engagement=None, |
| 35 | source_quality=0.5, rrf_score=0.1, sources=[item.source], source_items=[item], |
| 36 | final_score=40.0, |
| 37 | ) |
| 38 | |
| 39 | |
| 40 | def _report(posts, jobs, *, ranked=(), board=None): |
| 41 | items_by_source = {} |
| 42 | if posts: |
| 43 | items_by_source["reddit"] = posts |
| 44 | if jobs: |
| 45 | items_by_source["jobs"] = jobs |
| 46 | return schema.Report( |
| 47 | topic="SpaceX", range_from=WINDOW[0], range_to=WINDOW[1], |
| 48 | generated_at="2026-11-11T00:00:00Z", |
| 49 | provider_runtime=schema.ProviderRuntime("mock", "mock", "mock"), |
| 50 | query_plan=schema.QueryPlan( |
| 51 | intent="company", freshness_mode="balanced_recent", cluster_mode="none", |
| 52 | raw_topic="SpaceX", subqueries=[], source_weights={}, |
| 53 | ), |
| 54 | clusters=[], ranked_candidates=[_candidate(item) for item in ranked], |
| 55 | items_by_source=items_by_source, errors_by_source={}, |
| 56 | artifacts={"jobs": board} if board else {}, |
| 57 | ) |
| 58 | |
| 59 | |
| 60 | # --- Defect 1: freshness --- |
| 61 | |
| 62 | |
| 63 | def test_old_job_postings_do_not_make_recent_evidence_look_thin(): |
| 64 | # 6 of 8 conversation items are from the last week; 200 postings are older. |
| 65 | posts = [_post(index, "2026-11-08") for index in range(6)] + [_post(8, "2026-10-15"), _post(9, "2026-10-16")] |
| 66 | report = _report(posts, [_job(index) for index in range(200)]) |
| 67 | assert render._assess_data_freshness(report) is None |
| 68 | assert "Recent evidence is thin" not in "\n".join(render._render_emoji_footer(report, None)) |
| 69 | |
| 70 | |
| 71 | def test_freshness_note_counts_only_conversation_items(): |
| 72 | posts = [_post(index, "2026-11-08") for index in range(3)] + [_post(10 + index, "2026-10-15") for index in range(5)] |
| 73 | report = _report(posts, [_job(index) for index in range(200)]) |
| 74 | assert render._assess_data_freshness(report) == ( |
| 75 | "Recent evidence is thin: only 3 of 8 dated items are from the last 7 days." |
| 76 | ) |
| 77 | |
| 78 | |
| 79 | def test_fresh_job_postings_do_not_hide_thin_recent_evidence(): |
| 80 | posts = [_post(1, "2026-11-08")] + [_post(10 + index, "2026-10-15") for index in range(9)] |
| 81 | report = _report(posts, [_job(index, "2026-11-09") for index in range(200)]) |
| 82 | assert render._assess_data_freshness(report) == ( |
| 83 | "Limited recent data: only 1 of 10 dated items are from the last 7 days." |
| 84 | ) |
| 85 | |
| 86 | |
| 87 | # --- Defect 2: the saved raw file --- |
| 88 | |
| 89 | |
| 90 | def _board(jobs): |
| 91 | raw = [ |
| 92 | { |
| 93 | "id": item.item_id, "title": item.title, "url": item.url, |
| 94 | "description": f"Own line {index}.", "description_blocks": f"Own line {index}.", |
| 95 | "date": None, "posted_date_true": False, "provider": "greenhouse", |
| 96 | "department": item.container, "location": "Hawthorne", |
| 97 | } |
| 98 | for index, item in enumerate(jobs) |
| 99 | ] |
| 100 | return { |
| 101 | "company": "SpaceX", "provider": "greenhouse", "tier": "ats", "owned": True, |
| 102 | "open_roles": len(jobs), "new_in_window": None, |
| 103 | "digest": jobs_digest.build_digest(raw, jobs_digest.select_roles(raw)), |
| 104 | } |
| 105 | |
| 106 | |
| 107 | def test_saved_file_lists_the_ranked_postings_and_counts_the_rest(): |
| 108 | jobs = [_job(index) for index in range(500)] |
| 109 | report = _report([_post(1, "2026-11-08")], jobs, ranked=[jobs[7], jobs[3]], board=_board(jobs)) |
| 110 | text = render.render_full(report) |
| 111 | section = text.split("### Jobs (500 items)", 1)[1].split("\n## ", 1)[0] |
| 112 | assert section.count("**GH") == 2 |
| 113 | assert section.index("**GH7**") < section.index("**GH3**") |
| 114 | assert ( |
| 115 | "_498 more job postings on the board are not listed here: only ranked postings are. " |
| 116 | "The Jobs Digest above holds the board's distinct roles._" |
| 117 | ) in section |
| 118 | assert len(text) < 60_000 |
| 119 | |
| 120 | |
| 121 | def test_saved_file_carries_the_jobs_digest(): |
| 122 | jobs = [_job(index) for index in range(500)] |
| 123 | report = _report([_post(1, "2026-11-08")], jobs, ranked=[jobs[7]], board=_board(jobs)) |
| 124 | text = render.render_full(report) |
| 125 | assert "## Jobs Digest" in text |
| 126 | assert "- Board: SpaceX | greenhouse | 500 open roles" in text |
| 127 | assert text.index("## Jobs Digest") < text.index("## All Items by Source") |
| 128 | |
| 129 | |
| 130 | def test_saved_file_never_lists_more_than_the_limit_of_postings(): |
| 131 | jobs = [_job(index) for index in range(300)] |
| 132 | report = _report([], jobs, ranked=jobs[:120]) |
| 133 | section = render.render_full(report).split("### Jobs (300 items)", 1)[1] |
| 134 | assert section.count("**GH") == render.FULL_DUMP_JOBS_LIMIT |
| 135 | assert f"{300 - render.FULL_DUMP_JOBS_LIMIT} more job postings" in section |
| 136 | |
| 137 | |
| 138 | def test_saved_file_with_no_ranked_posting_says_so(): |
| 139 | jobs = [_job(index) for index in range(300)] |
| 140 | section = render.render_full(_report([], jobs)).split("### Jobs (300 items)", 1)[1] |
| 141 | assert "**GH" not in section |
| 142 | assert "_No job posting was ranked, so none of the board's 300 is listed here._" in section |
| 143 | |
| 144 | |
| 145 | def test_saved_file_still_lists_every_item_of_other_sources(): |
| 146 | posts = [_post(index, "2026-11-08") for index in range(60)] |
| 147 | text = render.render_full(_report(posts, [])) |
| 148 | assert text.count("**R") == 60 |
| 149 | assert "not listed here" not in text |
| 150 |