| 1 | import unittest |
| 2 | from unittest.mock import patch |
| 3 | |
| 4 | from lib import jobs |
| 5 | |
| 6 | |
| 7 | class JobsSourceTests(unittest.TestCase): |
| 8 | def test_parse_greenhouse_response_preserves_core_fields(self): |
| 9 | payload = { |
| 10 | "jobs": [ |
| 11 | { |
| 12 | "id": 123, |
| 13 | "title": "Enterprise Security Engineer", |
| 14 | "first_published": "2026-06-01T10:00:00-05:00", |
| 15 | "updated_at": "2026-06-20T10:00:00-05:00", |
| 16 | "company_name": "Acme", |
| 17 | "absolute_url": "https://boards.greenhouse.io/acme/jobs/123", |
| 18 | "content": "<p>Build SSO and SOC 2 workflows.</p>", |
| 19 | "location": {"name": "Remote"}, |
| 20 | "departments": [{"name": "Engineering"}], |
| 21 | "offices": [{"name": "US"}], |
| 22 | } |
| 23 | ] |
| 24 | } |
| 25 | parsed = jobs.parse_greenhouse_response(payload, board_token="acme") |
| 26 | self.assertEqual(1, len(parsed)) |
| 27 | item = parsed[0] |
| 28 | self.assertEqual("Enterprise Security Engineer", item["title"]) |
| 29 | self.assertEqual("2026-06-01", item["date"]) |
| 30 | self.assertTrue(item["posted_date_true"]) |
| 31 | self.assertEqual("Acme", item["company_name"]) |
| 32 | self.assertEqual("Engineering", item["department"]) |
| 33 | self.assertIn("SSO", item["description"]) |
| 34 | self.assertEqual("greenhouse", item["provider"]) |
| 35 | |
| 36 | def test_greenhouse_role_without_first_published_has_no_date(self): |
| 37 | payload = {"jobs": [{"id": 1, "title": "Engineer", "updated_at": "2026-06-20T10:00:00-05:00"}]} |
| 38 | item = jobs.parse_greenhouse_response(payload, board_token="acme")[0] |
| 39 | self.assertIsNone(item["date"]) |
| 40 | self.assertFalse(item["posted_date_true"]) |
| 41 | |
| 42 | def test_only_real_posting_dates_count_as_true(self): |
| 43 | ashby = jobs.parse_ashby_response( |
| 44 | {"jobs": [{"id": "1", "title": "Engineer", "publishedAt": "2026-06-02T00:00:00Z"}]}, slug="acme", |
| 45 | )[0] |
| 46 | lever = jobs.parse_lever_response( |
| 47 | [{"id": "1", "text": "Engineer", "createdAt": 1780000000000}], slug="acme", |
| 48 | )[0] |
| 49 | released = jobs.parse_smartrecruiters_response( |
| 50 | {"content": [{"id": "1", "name": "Engineer", "releasedDate": "2026-06-02T00:00:00.000Z"}]}, slug="acme", |
| 51 | )[0] |
| 52 | created = jobs.parse_smartrecruiters_response( |
| 53 | {"content": [{"id": "2", "name": "Engineer", "createdOn": "2026-06-02T00:00:00.000Z"}]}, slug="acme", |
| 54 | )[0] |
| 55 | self.assertTrue(ashby["posted_date_true"]) |
| 56 | self.assertFalse(lever["posted_date_true"]) |
| 57 | self.assertTrue(released["posted_date_true"]) |
| 58 | self.assertFalse(created["posted_date_true"]) |
| 59 | |
| 60 | @patch("lib.jobs.http.get_text", return_value=None) |
| 61 | @patch("lib.jobs.http.get") |
| 62 | def test_all_ats_miss_degrades_to_empty(self, mock_get, _mock_text): |
| 63 | # No careers page (get_text None), no web backend, every ATS probe 404s. |
| 64 | mock_get.side_effect = jobs.http.HTTPError("missing", status_code=404) |
| 65 | items, artifact = jobs.search_jobs( |
| 66 | "MissingCo", |
| 67 | ("2026-05-16", "2026-06-16"), |
| 68 | {}, |
| 69 | web_backend="none", |
| 70 | ) |
| 71 | self.assertEqual([], items) |
| 72 | self.assertEqual(0, artifact["resultCount"]) |
| 73 | self.assertEqual("web", artifact["tier"]) |
| 74 | |
| 75 | def test_detect_ats_reads_provider_and_slug_from_embed(self): |
| 76 | html = '<a href="https://jobs.ashbyhq.com/listenlabs/4b17">Open roles</a>' |
| 77 | provider, slug = jobs.detect_ats(html) |
| 78 | self.assertEqual("ashby", provider) |
| 79 | self.assertEqual("listenlabs", slug) |
| 80 | |
| 81 | def test_detect_ats_handles_greenhouse_embed_query(self): |
| 82 | html = '<script src="https://boards.greenhouse.io/embed/job_board?for=acmeco"></script>' |
| 83 | provider, slug = jobs.detect_ats(html) |
| 84 | self.assertEqual("greenhouse", provider) |
| 85 | self.assertEqual("acmeco", slug) |
| 86 | |
| 87 | def test_detect_ats_returns_none_when_no_ats(self): |
| 88 | self.assertEqual((None, None), jobs.detect_ats("<html><body>About us</body></html>")) |
| 89 | |
| 90 | def test_parse_ashby_response_core_fields(self): |
| 91 | payload = { |
| 92 | "jobs": [ |
| 93 | { |
| 94 | "id": "abc", |
| 95 | "title": "Founding Research Scientist, Human Simulation", |
| 96 | "departmentName": "Research", |
| 97 | "locationName": "San Francisco", |
| 98 | "publishedDate": "2026-06-10", |
| 99 | "jobUrl": "https://jobs.ashbyhq.com/listenlabs/abc", |
| 100 | "descriptionPlain": "Build human simulation models.", |
| 101 | } |
| 102 | ] |
| 103 | } |
| 104 | parsed = jobs.parse_ashby_response(payload, slug="listenlabs") |
| 105 | self.assertEqual(1, len(parsed)) |
| 106 | item = parsed[0] |
| 107 | self.assertEqual("Founding Research Scientist, Human Simulation", item["title"]) |
| 108 | self.assertEqual("Research", item["department"]) |
| 109 | self.assertEqual("2026-06-10", item["date"]) |
| 110 | self.assertEqual("ashby", item["provider"]) |
| 111 | |
| 112 | def test_parse_lever_response_handles_list_and_epoch(self): |
| 113 | payload = [ |
| 114 | { |
| 115 | "id": "xyz", |
| 116 | "text": "Staff Engineer", |
| 117 | "hostedUrl": "https://jobs.lever.co/acme/xyz", |
| 118 | "categories": {"department": "Engineering", "location": "Remote"}, |
| 119 | "createdAt": 1749513600000, |
| 120 | "descriptionPlain": "Own the platform.", |
| 121 | } |
| 122 | ] |
| 123 | parsed = jobs.parse_lever_response(payload, slug="acme") |
| 124 | self.assertEqual(1, len(parsed)) |
| 125 | self.assertEqual("Staff Engineer", parsed[0]["title"]) |
| 126 | self.assertEqual("Engineering", parsed[0]["department"]) |
| 127 | self.assertEqual("2025-06-10", parsed[0]["date"]) |
| 128 | self.assertEqual("lever", parsed[0]["provider"]) |
| 129 | |
| 130 | def test_parse_smartrecruiters_response_core_fields(self): |
| 131 | payload = { |
| 132 | "content": [ |
| 133 | { |
| 134 | "id": "777", |
| 135 | "name": "Account Executive", |
| 136 | "department": {"label": "Sales"}, |
| 137 | "location": {"city": "New York", "country": "us"}, |
| 138 | "releasedDate": "2026-06-02T00:00:00.000Z", |
| 139 | } |
| 140 | ] |
| 141 | } |
| 142 | parsed = jobs.parse_smartrecruiters_response(payload, slug="acme") |
| 143 | self.assertEqual(1, len(parsed)) |
| 144 | self.assertEqual("Account Executive", parsed[0]["title"]) |
| 145 | self.assertEqual("Sales", parsed[0]["department"]) |
| 146 | self.assertEqual("2026-06-02", parsed[0]["date"]) |
| 147 | self.assertEqual("smartrecruiters", parsed[0]["provider"]) |
| 148 | |
| 149 | def test_parse_workable_response_reads_the_live_row_shape(self): |
| 150 | # The fields Workable's v3 jobs endpoint returns: `published`, a list |
| 151 | # of departments, and no URL or description. |
| 152 | payload = { |
| 153 | "total": 1, |
| 154 | "results": [ |
| 155 | { |
| 156 | "id": 6125026, |
| 157 | "shortcode": "AB12CD34EF", |
| 158 | "title": "Product Designer", |
| 159 | "remote": True, |
| 160 | "location": {"city": "", "country": "France", "countryCode": "FR", "region": None}, |
| 161 | "locations": [ |
| 162 | {"city": "", "country": "France", "countryCode": "FR", "hidden": False, "region": None} |
| 163 | ], |
| 164 | "state": "published", |
| 165 | "published": "2026-09-21T00:00:00.000Z", |
| 166 | "type": "full", |
| 167 | "department": ["Product", "Design"], |
| 168 | "workplace": "remote", |
| 169 | } |
| 170 | ], |
| 171 | } |
| 172 | parsed = jobs.parse_workable_response(payload, slug="acme") |
| 173 | self.assertEqual(1, len(parsed)) |
| 174 | item = parsed[0] |
| 175 | self.assertEqual("2026-09-21", item["date"]) |
| 176 | self.assertTrue(item["posted_date_true"]) |
| 177 | self.assertEqual("Product, Design", item["department"]) |
| 178 | self.assertEqual("France", item["location"]) |
| 179 | self.assertEqual("https://apply.workable.com/acme/j/AB12CD34EF/", item["url"]) |
| 180 | self.assertEqual("workable", item["provider"]) |
| 181 | self.assertNotIn("[", item["department"]) |
| 182 | |
| 183 | def test_parse_workable_response_still_reads_the_older_row_shape(self): |
| 184 | payload = { |
| 185 | "jobs": [ |
| 186 | { |
| 187 | "shortcode": "OLD1", |
| 188 | "title": "Clerk", |
| 189 | "published_on": "2026-06-02", |
| 190 | "created_at": "2026-05-01", |
| 191 | "department": "Operations", |
| 192 | "location": {"city": "Austin", "country": "United States"}, |
| 193 | "url": "https://apply.workable.com/acme/j/OLD1/", |
| 194 | }, |
| 195 | {"shortcode": "OLD2", "title": "Porter", "created_at": "2026-05-01", "department": None}, |
| 196 | ] |
| 197 | } |
| 198 | first, second = jobs.parse_workable_response(payload, slug="acme") |
| 199 | self.assertEqual("2026-06-02", first["date"]) |
| 200 | self.assertTrue(first["posted_date_true"]) |
| 201 | self.assertEqual("Operations", first["department"]) |
| 202 | self.assertEqual("Austin, United States", first["location"]) |
| 203 | self.assertEqual("2026-05-01", second["date"]) |
| 204 | self.assertFalse(second["posted_date_true"]) |
| 205 | self.assertEqual("", second["department"]) |
| 206 | |
| 207 | def test_extract_jsonld_jobs_parses_jobposting(self): |
| 208 | html = ( |
| 209 | '<script type="application/ld+json">' |
| 210 | '{"@context":"https://schema.org","@type":"JobPosting",' |
| 211 | '"title":"Product Designer","datePosted":"2026-06-05",' |
| 212 | '"description":"<p>Design things.</p>",' |
| 213 | '"url":"https://acme.com/jobs/1",' |
| 214 | '"jobLocation":{"@type":"Place","address":{"@type":"PostalAddress",' |
| 215 | '"addressLocality":"SF","addressCountry":"US"}}}' |
| 216 | '</script>' |
| 217 | ) |
| 218 | parsed = jobs.extract_jsonld_jobs(html, "https://acme.com/careers") |
| 219 | self.assertEqual(1, len(parsed)) |
| 220 | self.assertEqual("Product Designer", parsed[0]["title"]) |
| 221 | self.assertEqual("2026-06-05", parsed[0]["date"]) |
| 222 | self.assertEqual("careers-jsonld", parsed[0]["provider"]) |
| 223 | self.assertIn("SF", parsed[0]["location"]) |
| 224 | |
| 225 | def test_extract_jsonld_jobs_handles_graph_arrays(self): |
| 226 | html = ( |
| 227 | '<script type="application/ld+json">' |
| 228 | '{"@graph":[{"@type":"Organization","name":"Acme"},' |
| 229 | '{"@type":"JobPosting","title":"GTM Lead","datePosted":"2026-06-01"}]}' |
| 230 | '</script>' |
| 231 | ) |
| 232 | parsed = jobs.extract_jsonld_jobs(html, "https://acme.com/careers") |
| 233 | self.assertEqual(1, len(parsed)) |
| 234 | self.assertEqual("GTM Lead", parsed[0]["title"]) |
| 235 | |
| 236 | def test_jsonld_jobs_without_urls_survive_normalize_and_dedupe(self): |
| 237 | from lib import dedupe, normalize |
| 238 | |
| 239 | html = ( |
| 240 | '<script type="application/ld+json">' |
| 241 | '{"@graph":[' |
| 242 | '{"@type":"JobPosting","title":"Founding Designer","datePosted":"2026-06-01"},' |
| 243 | '{"@type":"JobPosting","title":"GTM Lead","datePosted":"2026-06-02"},' |
| 244 | '{"@type":"JobPosting","title":"Staff Engineer","datePosted":"2026-06-03"}' |
| 245 | ']}' |
| 246 | '</script>' |
| 247 | ) |
| 248 | parsed = jobs.extract_jsonld_jobs(html, "https://acme.com/careers") |
| 249 | self.assertEqual(3, len(parsed)) |
| 250 | self.assertTrue(all(item["url"] == "" for item in parsed)) |
| 251 | self.assertTrue(all(item["source_url"] == "https://acme.com/careers" for item in parsed)) |
| 252 | |
| 253 | normalized = normalize.normalize_source_items("jobs", parsed, "2026-05-17", "2026-06-16") |
| 254 | kept = dedupe.dedupe_items(normalized) |
| 255 | self.assertEqual( |
| 256 | ["Founding Designer", "GTM Lead", "Staff Engineer"], |
| 257 | sorted(item.title for item in kept), |
| 258 | ) |
| 259 | |
| 260 | |
| 261 | if __name__ == "__main__": |
| 262 | unittest.main() |
| 263 | |
| 264 | |
| 265 | class JobsDateAndBannerTests(unittest.TestCase): |
| 266 | def test_jobs_keep_open_roles_outside_date_window(self): |
| 267 | from lib import normalize |
| 268 | raw = [ |
| 269 | {"id": "1", "title": "Founding Research Scientist", "url": "https://x/1", |
| 270 | "date": "2026-01-10", "provider": "ashby"}, # posted months ago, still open |
| 271 | {"id": "2", "title": "Account Executive", "url": "https://x/2", |
| 272 | "date": "2026-06-10", "provider": "ashby"}, # within window |
| 273 | ] |
| 274 | items = normalize.normalize_source_items("jobs", raw, "2026-05-17", "2026-06-16") |
| 275 | titles = [i.title for i in items] |
| 276 | self.assertIn("Founding Research Scientist", titles) # not dropped by date window |
| 277 | self.assertEqual(2, len(items)) |
| 278 | |
| 279 | |
| 280 | class JobsDedupeTests(unittest.TestCase): |
| 281 | def test_jobs_dedupe_by_url_not_fuzzy_boilerplate(self): |
| 282 | from lib import dedupe, schema |
| 283 | # Distinct roles sharing heavy boilerplate but with unique URLs. |
| 284 | boiler = "TL;DR: Listen Labs is a fast-growing early-stage startup. Roll up your sleeves." |
| 285 | items = [ |
| 286 | schema.SourceItem(item_id=f"AB{i}", source="jobs", title=t, body=boiler, |
| 287 | url=f"https://jobs.ashbyhq.com/listenlabs/{i}") |
| 288 | for i, t in enumerate(["Founding Research Scientist", "Account Executive", |
| 289 | "Growth Associate", "Business Development Rep"]) |
| 290 | ] |
| 291 | kept = dedupe.dedupe_items(items) |
| 292 | self.assertEqual(4, len(kept)) # all distinct URLs survive |
| 293 | |
| 294 | def test_jobs_dedupe_collapses_same_url(self): |
| 295 | from lib import dedupe, schema |
| 296 | items = [ |
| 297 | schema.SourceItem(item_id="AB1", source="jobs", title="Role", body="x", |
| 298 | url="https://jobs.ashbyhq.com/listenlabs/1"), |
| 299 | schema.SourceItem(item_id="AB1b", source="jobs", title="Role", body="x", |
| 300 | url="https://jobs.ashbyhq.com/listenlabs/1"), |
| 301 | ] |
| 302 | self.assertEqual(1, len(dedupe.dedupe_items(items))) |
| 303 |