| 1 | import re |
| 2 | import threading |
| 3 | import unittest |
| 4 | from unittest.mock import patch |
| 5 | |
| 6 | from lib import jobs, jobs_digest, render, schema |
| 7 | |
| 8 | TOKEN = "acme.wd5/AcmeCareers" |
| 9 | SITE = "https://www.acme.com" |
| 10 | CAREERS = '<a href="https://acme.wd5.myworkdayjobs.com/en-US/AcmeCareers">Jobs</a>' |
| 11 | LIST_URL = "https://acme.wd5.myworkdayjobs.com/wday/cxs/acme/AcmeCareers/jobs" |
| 12 | |
| 13 | |
| 14 | def _row(index: int) -> dict: |
| 15 | return { |
| 16 | "title": f"Role {index}", |
| 17 | "externalPath": f"/job/Remote/Role-{index}_JR{index}", |
| 18 | "locationsText": "2 Locations", |
| 19 | "postedOn": "Posted 3 Days Ago", |
| 20 | "bulletFields": [f"JR{index}"], |
| 21 | } |
| 22 | |
| 23 | |
| 24 | class FakeBoard: |
| 25 | """Stands in for a Workday board at the `http.post` / `http.get` seam.""" |
| 26 | |
| 27 | def __init__(self, size: int, *, reported_total: int | None = None, malformed: set[int] = frozenset()): |
| 28 | self.size = size |
| 29 | self.reported_total = size if reported_total is None else reported_total |
| 30 | self.malformed = set(malformed) |
| 31 | self.list_calls: list[tuple[str, dict]] = [] |
| 32 | self.detail_calls: list[str] = [] |
| 33 | self.failing_details: set[str] = set() |
| 34 | self.empty_details: set[str] = set() |
| 35 | self._lock = threading.Lock() |
| 36 | |
| 37 | def post(self, url, json_data, headers=None, **kwargs): |
| 38 | with self._lock: |
| 39 | self.list_calls.append((url, dict(json_data))) |
| 40 | offset = json_data["offset"] |
| 41 | rows = [] |
| 42 | for index in range(offset, min(offset + json_data["limit"], self.size)): |
| 43 | row = _row(index) |
| 44 | if index in self.malformed: |
| 45 | del row["title"] |
| 46 | rows.append(row) |
| 47 | # Workday reports the total on the first page only. |
| 48 | return {"total": self.reported_total if offset == 0 else 0, "jobPostings": rows, "facets": []} |
| 49 | |
| 50 | def get(self, url, headers=None, **kwargs): |
| 51 | with self._lock: |
| 52 | self.detail_calls.append(url) |
| 53 | ident = url.rsplit("_", 1)[-1] |
| 54 | if ident in self.failing_details: |
| 55 | raise jobs.http.HTTPError("server error", status_code=500) |
| 56 | if ident in self.empty_details: |
| 57 | return {"errorCode": "HTTP_404"} |
| 58 | return { |
| 59 | "jobPostingInfo": { |
| 60 | "jobDescription": f"<p>Own the {ident} roadmap.</p>", |
| 61 | "startDate": "2026-11-03", |
| 62 | "location": "Remote", |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | |
| 67 | class WorkdayDetectionTests(unittest.TestCase): |
| 68 | def test_careers_link_yields_provider_and_endpoint_parts(self): |
| 69 | html = '<a href="https://nvidia.wd5.myworkdayjobs.com/NVIDIAExternalCareerSite">Jobs</a>' |
| 70 | provider, token = jobs.detect_ats(html) |
| 71 | self.assertEqual("workday", provider) |
| 72 | self.assertEqual("nvidia.wd5/NVIDIAExternalCareerSite", token) |
| 73 | self.assertEqual( |
| 74 | "https://nvidia.wd5.myworkdayjobs.com/wday/cxs/nvidia/NVIDIAExternalCareerSite/jobs", |
| 75 | jobs.workday_list_url(token), |
| 76 | ) |
| 77 | |
| 78 | def test_locale_segment_is_not_taken_for_the_site(self): |
| 79 | html = '<a href="https://acme.wd12.myworkdayjobs.com/en-US/AcmeCareers/job/Remote/x_JR1">Role</a>' |
| 80 | provider, token = jobs.detect_ats(html) |
| 81 | self.assertEqual("workday", provider) |
| 82 | self.assertEqual("acme.wd12/AcmeCareers", token) |
| 83 | self.assertEqual( |
| 84 | "https://acme.wd12.myworkdayjobs.com/wday/cxs/acme/AcmeCareers/jobs", |
| 85 | jobs.workday_list_url(token), |
| 86 | ) |
| 87 | |
| 88 | def test_page_linking_only_a_workday_board_counts_as_a_careers_page(self): |
| 89 | html = '<a href="https://acme.wd5.myworkdayjobs.com/AcmeCareers">Search</a>' |
| 90 | self.assertTrue(jobs._looks_like_careers_html(html)) |
| 91 | |
| 92 | |
| 93 | class WorkdayListingTests(unittest.TestCase): |
| 94 | def _search(self, board, **kwargs): |
| 95 | with patch("lib.jobs.http.post", side_effect=board.post): |
| 96 | return jobs.search_workday_board(TOKEN, **kwargs) |
| 97 | |
| 98 | def test_45_row_board_is_read_across_three_list_calls(self): |
| 99 | board = FakeBoard(45) |
| 100 | items, info = self._search(board) |
| 101 | self.assertEqual(3, len(board.list_calls)) |
| 102 | self.assertEqual({LIST_URL}, {url for url, _ in board.list_calls}) |
| 103 | self.assertEqual([0, 20, 40], sorted(body["offset"] for _, body in board.list_calls)) |
| 104 | self.assertEqual([f"Role {i}" for i in range(45)], [item["title"] for item in items]) |
| 105 | self.assertEqual({"board_total": 45, "board_capped": False, "board_cut_short": False}, info) |
| 106 | |
| 107 | def test_listing_rows_carry_no_posted_date_or_description(self): |
| 108 | items, _ = self._search(FakeBoard(1)) |
| 109 | item = items[0] |
| 110 | self.assertEqual("workday", item["provider"]) |
| 111 | self.assertEqual(TOKEN, item["board_token"]) |
| 112 | self.assertEqual("WDJR0", item["id"]) |
| 113 | self.assertEqual("https://acme.wd5.myworkdayjobs.com/AcmeCareers/job/Remote/Role-0_JR0", item["url"]) |
| 114 | self.assertEqual("2 Locations", item["location"]) |
| 115 | self.assertEqual("", item["description"]) |
| 116 | self.assertIsNone(item["date"]) |
| 117 | self.assertEqual("low", item["date_confidence"]) |
| 118 | |
| 119 | def test_board_over_the_ceiling_stops_there_and_records_the_cap(self): |
| 120 | board = FakeBoard(2500) |
| 121 | items, info = self._search(board) |
| 122 | self.assertEqual(100, len(board.list_calls)) |
| 123 | self.assertEqual(2000, len(items)) |
| 124 | self.assertTrue(info["board_capped"]) |
| 125 | self.assertFalse(info["board_cut_short"]) |
| 126 | |
| 127 | def test_403_or_429_on_the_list_is_a_miss(self): |
| 128 | for status in (403, 429): |
| 129 | with patch( |
| 130 | "lib.jobs.http.post", |
| 131 | side_effect=jobs.http.HTTPError("blocked", status_code=status), |
| 132 | ): |
| 133 | items, _info = jobs.search_workday_board(TOKEN) |
| 134 | self.assertEqual([], items) |
| 135 | |
| 136 | @patch("lib.jobs.http.get") |
| 137 | @patch("lib.jobs.http.post", side_effect=jobs.http.HTTPError("blocked", status_code=403)) |
| 138 | @patch("lib.jobs.http.get_text") |
| 139 | def test_blocked_board_falls_through_to_the_normal_miss(self, mock_text, _mock_post, mock_get): |
| 140 | mock_text.return_value = '<a href="https://acme.wd5.myworkdayjobs.com/AcmeCareers">Jobs</a>' |
| 141 | items, artifact = jobs.search_jobs("Acme", ("2026-10-11", "2026-11-10"), {}, web_backend="none") |
| 142 | self.assertEqual([], items) |
| 143 | self.assertEqual("web", artifact["tier"]) |
| 144 | self.assertEqual(0, artifact["resultCount"]) |
| 145 | self.assertIn(f"careers:workday:{TOKEN}", artifact["attempted"]) |
| 146 | mock_get.assert_not_called() |
| 147 | |
| 148 | def test_rows_without_a_title_are_ignored(self): |
| 149 | board = FakeBoard(25, malformed={3, 21}) |
| 150 | items, info = self._search(board) |
| 151 | self.assertEqual(23, len(items)) |
| 152 | self.assertNotIn("", [item["title"] for item in items]) |
| 153 | self.assertFalse(info["board_cut_short"]) |
| 154 | |
| 155 | def test_stops_fetching_when_the_budget_is_spent(self): |
| 156 | board = FakeBoard(400) |
| 157 | now = [0.0] |
| 158 | |
| 159 | def slow_post(url, json_data, headers=None, **kwargs): |
| 160 | now[0] += 20.0 # every list call costs 20 s on the injected clock |
| 161 | return board.post(url, json_data, headers, **kwargs) |
| 162 | |
| 163 | with patch("lib.jobs.http.post", side_effect=slow_post): |
| 164 | items, info = jobs.search_workday_board(TOKEN, budget_seconds=30.0, clock=lambda: now[0]) |
| 165 | self.assertTrue(info["board_cut_short"]) |
| 166 | self.assertGreaterEqual(len(items), 20) |
| 167 | self.assertLess(len(items), 400) |
| 168 | # One page before the budget ran out, plus at most one in-flight call per worker. |
| 169 | self.assertLessEqual(len(board.list_calls), 1 + jobs.WORKDAY_MAX_WORKERS) |
| 170 | |
| 171 | def test_a_failed_later_page_is_recorded_as_cut_short(self): |
| 172 | board = FakeBoard(45) |
| 173 | |
| 174 | def flaky_post(url, json_data, headers=None, **kwargs): |
| 175 | if json_data["offset"] == 20: |
| 176 | raise jobs.http.HTTPError("server error", status_code=500) |
| 177 | return board.post(url, json_data, headers, **kwargs) |
| 178 | |
| 179 | with patch("lib.jobs.http.post", side_effect=flaky_post): |
| 180 | items, info = jobs.search_workday_board(TOKEN) |
| 181 | self.assertEqual(25, len(items)) |
| 182 | self.assertTrue(info["board_cut_short"]) |
| 183 | |
| 184 | |
| 185 | class WorkdayDetailTests(unittest.TestCase): |
| 186 | def _items(self, board, count): |
| 187 | with patch("lib.jobs.http.post", side_effect=board.post): |
| 188 | items, _ = jobs.search_workday_board(TOKEN) |
| 189 | return items[:count] |
| 190 | |
| 191 | def test_fills_description_and_posted_date_for_selected_roles_only(self): |
| 192 | board = FakeBoard(5) |
| 193 | items = self._items(board, 5) |
| 194 | with patch("lib.jobs.http.get", side_effect=board.get): |
| 195 | jobs.fetch_workday_details([items[1], items[3]]) |
| 196 | self.assertEqual( |
| 197 | { |
| 198 | "https://acme.wd5.myworkdayjobs.com/wday/cxs/acme/AcmeCareers/job/Remote/Role-1_JR1", |
| 199 | "https://acme.wd5.myworkdayjobs.com/wday/cxs/acme/AcmeCareers/job/Remote/Role-3_JR3", |
| 200 | }, |
| 201 | set(board.detail_calls), |
| 202 | ) |
| 203 | self.assertEqual("Own the JR1 roadmap.", items[1]["description"]) |
| 204 | self.assertEqual("2026-11-03", items[1]["date"]) |
| 205 | self.assertEqual("high", items[1]["date_confidence"]) |
| 206 | for untouched in (items[0], items[2], items[4]): |
| 207 | self.assertEqual("", untouched["description"]) |
| 208 | self.assertIsNone(untouched["date"]) |
| 209 | self.assertEqual("low", untouched["date_confidence"]) |
| 210 | |
| 211 | def test_one_failed_detail_call_skips_only_that_role(self): |
| 212 | board = FakeBoard(3) |
| 213 | board.failing_details = {"JR1"} |
| 214 | items = self._items(board, 3) |
| 215 | with patch("lib.jobs.http.get", side_effect=board.get): |
| 216 | jobs.fetch_workday_details(items) |
| 217 | self.assertEqual("", items[1]["description"]) |
| 218 | self.assertIsNone(items[1]["date"]) |
| 219 | self.assertEqual("Own the JR0 roadmap.", items[0]["description"]) |
| 220 | self.assertEqual("2026-11-03", items[2]["date"]) |
| 221 | |
| 222 | def test_no_detail_calls_once_the_budget_is_spent(self): |
| 223 | board = FakeBoard(3) |
| 224 | items = self._items(board, 3) |
| 225 | with patch("lib.jobs.http.get", side_effect=board.get): |
| 226 | jobs.fetch_workday_details(items, deadline=10.0, clock=lambda: 11.0) |
| 227 | self.assertEqual([], board.detail_calls) |
| 228 | self.assertEqual("", items[0]["description"]) |
| 229 | |
| 230 | @patch("lib.jobs.http.get_text") |
| 231 | def test_search_jobs_details_only_the_roles_the_digest_selected(self, mock_text): |
| 232 | mock_text.return_value = '<a href="https://acme.wd5.myworkdayjobs.com/en-US/AcmeCareers">Jobs</a>' |
| 233 | board = FakeBoard(45) |
| 234 | with patch("lib.jobs.http.post", side_effect=board.post), \ |
| 235 | patch("lib.jobs.http.get", side_effect=board.get): |
| 236 | items, artifact = jobs.search_jobs( |
| 237 | "Acme", ("2026-10-11", "2026-11-10"), {}, web_backend="none", site=SITE, |
| 238 | ) |
| 239 | self.assertEqual(45, len(items)) |
| 240 | self.assertEqual("ats", artifact["tier"]) |
| 241 | self.assertEqual("workday", artifact["provider"]) |
| 242 | self.assertEqual(TOKEN, artifact["board_token"]) |
| 243 | self.assertEqual(45, artifact["board_total"]) |
| 244 | self.assertFalse(artifact["board_capped"]) |
| 245 | self.assertFalse(artifact["board_cut_short"]) |
| 246 | selected = jobs_digest.DIGEST_MAX_ROLES |
| 247 | self.assertEqual(selected, len(board.detail_calls)) |
| 248 | digest = artifact["digest"] |
| 249 | self.assertEqual(selected, len(digest["roles"])) |
| 250 | self.assertTrue(all(entry["text"].startswith("Own the JR") for entry in digest["roles"])) |
| 251 | self.assertTrue(all(entry["posted"] == "2026-11-03" for entry in digest["roles"])) |
| 252 | self.assertEqual(selected, sum(1 for item in items if item["description"])) |
| 253 | self.assertNotIn("note", digest) |
| 254 | # Listing rows carry no true date, so the board has no count of new roles. |
| 255 | self.assertEqual(45, artifact["open_roles"]) |
| 256 | self.assertIsNone(artifact["new_in_window"]) |
| 257 | |
| 258 | @patch("lib.jobs.http.get_text") |
| 259 | def test_spent_budget_skips_detail_calls_and_the_digest_says_so(self, mock_text): |
| 260 | mock_text.return_value = '<a href="https://acme.wd5.myworkdayjobs.com/en-US/AcmeCareers">Jobs</a>' |
| 261 | board = FakeBoard(45) |
| 262 | real = jobs.fetch_workday_details |
| 263 | |
| 264 | def late(items, **kwargs): |
| 265 | return real(items, deadline=kwargs["deadline"], clock=lambda: kwargs["deadline"] + 1) |
| 266 | |
| 267 | with patch("lib.jobs.http.post", side_effect=board.post), \ |
| 268 | patch("lib.jobs.http.get", side_effect=board.get), \ |
| 269 | patch.object(jobs, "fetch_workday_details", side_effect=late): |
| 270 | _items, artifact = jobs.search_jobs( |
| 271 | "Acme", ("2026-10-11", "2026-11-10"), {}, web_backend="none", site=SITE, |
| 272 | ) |
| 273 | self.assertEqual([], board.detail_calls) |
| 274 | self.assertEqual( |
| 275 | "role text fetched for 0 of 40 selected roles (time budget spent before 40)", |
| 276 | artifact["digest"]["note"], |
| 277 | ) |
| 278 | self.assertTrue(all(entry["text"] == "" for entry in artifact["digest"]["roles"])) |
| 279 | |
| 280 | def _search(self, board): |
| 281 | with patch("lib.jobs.http.get_text", return_value=CAREERS), \ |
| 282 | patch("lib.jobs.http.post", side_effect=board.post), \ |
| 283 | patch("lib.jobs.http.get", side_effect=board.get): |
| 284 | return jobs.search_jobs( |
| 285 | "Acme", ("2026-10-11", "2026-11-10"), {}, web_backend="none", site=SITE, |
| 286 | ) |
| 287 | |
| 288 | def test_small_board_with_every_detail_fetched_counts_its_new_roles(self): |
| 289 | _items, artifact = self._search(FakeBoard(5)) |
| 290 | self.assertEqual(5, artifact["open_roles"]) |
| 291 | self.assertEqual(5, artifact["new_in_window"]) |
| 292 | self.assertNotIn("note", artifact["digest"]) |
| 293 | |
| 294 | def test_failed_and_empty_detail_calls_count_as_unfetched(self): |
| 295 | board = FakeBoard(5) |
| 296 | board.failing_details = {"JR1"} |
| 297 | board.empty_details = {"JR3"} |
| 298 | with patch("lib.jobs.http.post", side_effect=board.post): |
| 299 | items, _ = jobs.search_workday_board(TOKEN) |
| 300 | with patch("lib.jobs.http.get", side_effect=board.get): |
| 301 | self.assertEqual((0, 2), jobs.fetch_workday_details(items)) |
| 302 | with patch("lib.jobs.http.get", side_effect=board.get): |
| 303 | self.assertEqual( |
| 304 | (5, 0), jobs.fetch_workday_details(items, deadline=10.0, clock=lambda: 11.0), |
| 305 | ) |
| 306 | |
| 307 | _items, artifact = self._search(board) |
| 308 | self.assertEqual( |
| 309 | "role text fetched for 3 of 5 selected roles (request failed or came back empty for 2)", |
| 310 | artifact["digest"]["note"], |
| 311 | ) |
| 312 | # Two roles have no true date, so the board has no count of new roles. |
| 313 | self.assertIsNone(artifact["new_in_window"]) |
| 314 | |
| 315 | def test_two_postings_with_one_title_both_get_their_details(self): |
| 316 | board = FakeBoard(3) |
| 317 | real_post = board.post |
| 318 | |
| 319 | def post(url, json_data, headers=None, **kwargs): |
| 320 | payload = real_post(url, json_data, headers=headers, **kwargs) |
| 321 | for row in payload["jobPostings"]: |
| 322 | row["title"] = "Software Engineer" |
| 323 | return payload |
| 324 | |
| 325 | board.post = post |
| 326 | _items, artifact = self._search(board) |
| 327 | self.assertEqual(3, len(board.detail_calls)) |
| 328 | self.assertEqual( |
| 329 | {"Own the JR0 roadmap.", "Own the JR1 roadmap.", "Own the JR2 roadmap."}, |
| 330 | {entry["text"] for entry in artifact["digest"]["roles"]}, |
| 331 | ) |
| 332 | |
| 333 | def test_board_on_a_guessed_domain_gives_counts_and_no_digest(self): |
| 334 | board = FakeBoard(5) |
| 335 | with patch("lib.jobs.http.get_text", return_value=CAREERS), \ |
| 336 | patch("lib.jobs.http.post", side_effect=board.post), \ |
| 337 | patch("lib.jobs.http.get", side_effect=board.get): |
| 338 | _items, artifact = jobs.search_jobs( |
| 339 | "Acme", ("2026-10-11", "2026-11-10"), {}, web_backend="none", |
| 340 | ) |
| 341 | self.assertEqual(5, artifact["open_roles"]) |
| 342 | self.assertFalse(artifact["owned"]) |
| 343 | self.assertNotIn("digest", artifact) |
| 344 | self.assertEqual([], board.detail_calls) |
| 345 | |
| 346 | def test_capped_board_counts_as_a_floor(self): |
| 347 | page = '<a href="https://acme.wd5.myworkdayjobs.com/en-US/AcmeCareers">Jobs</a>' |
| 348 | board = FakeBoard(2000, reported_total=2000) |
| 349 | with patch("lib.jobs.http.get_text", return_value=page), \ |
| 350 | patch("lib.jobs.http.post", side_effect=board.post), \ |
| 351 | patch("lib.jobs.http.get", side_effect=board.get): |
| 352 | _items, artifact = jobs.search_jobs( |
| 353 | "Acme", ("2026-10-11", "2026-11-10"), {}, web_backend="none", |
| 354 | ) |
| 355 | self.assertEqual(2000, artifact["open_roles"]) |
| 356 | self.assertTrue(artifact["open_roles_floor"]) |
| 357 | |
| 358 | |
| 359 | def _site_row(site: str, index: int) -> dict: |
| 360 | return { |
| 361 | "title": f"{site} role {index}", |
| 362 | "externalPath": f"/job/Beaverton-Oregon/{site}-role-{index}_R-{site}{index}", |
| 363 | "locationsText": "Beaverton, Oregon", |
| 364 | "postedOn": "Posted Today", |
| 365 | "bulletFields": [f"R-{site}{index}"], |
| 366 | } |
| 367 | |
| 368 | |
| 369 | NIKE_ROBOTS = ( |
| 370 | "User-agent: *\nDisallow: /nke/\nDisallow: /nke2/\nDisallow: /nke4/\nDisallow: /refreshFacet/\n" |
| 371 | ) |
| 372 | SALESFORCE_ROBOTS = "\n".join( |
| 373 | [f"Sitemap: https://salesforce.wd12.myworkdayjobs.com/{site}/siteMap.xml" |
| 374 | for site in ("External_Career_Site", "Tableau", "Slack")] |
| 375 | + ["", "User-agent: *"] |
| 376 | + [f"Allow: /{site}/" for site in ("External_Career_Site", "Heroku")] |
| 377 | + ["Disallow: /refreshFacet/", ""] |
| 378 | ) |
| 379 | SHARED = { |
| 380 | "title": "Air Manufacturing Innovation Engineer", |
| 381 | "externalPath": "/job/Beaverton-Oregon/Air-Manufacturing-Innovation-Engineer_R-96484", |
| 382 | "locationsText": "Beaverton, Oregon", |
| 383 | "postedOn": "Posted Today", |
| 384 | "bulletFields": ["R-96484"], |
| 385 | } |
| 386 | |
| 387 | |
| 388 | class FakeTenant: |
| 389 | """Every career site of Workday tenants, its robots.txt and the careers page.""" |
| 390 | |
| 391 | def __init__(self, sites: dict[str, list[dict] | dict], *, robots: str | None, page: str): |
| 392 | # "host/site" -> rows, or a dict answered as is (not the listing shape). |
| 393 | self.sites = sites |
| 394 | self.robots = robots |
| 395 | self.page = page |
| 396 | self.list_calls: list[str] = [] |
| 397 | self._lock = threading.Lock() |
| 398 | |
| 399 | def get_text(self, url, **kwargs): |
| 400 | if url.endswith("/robots.txt"): |
| 401 | # ``get_text`` answers None for a file that is not there, and for |
| 402 | # one asked right after a burst of listing pages, which Workday |
| 403 | # answers with 429 for a while. |
| 404 | return None if self.list_calls else self.robots |
| 405 | return self.page |
| 406 | |
| 407 | def post(self, url, json_data, headers=None, **kwargs): |
| 408 | with self._lock: |
| 409 | self.list_calls.append(url) |
| 410 | match = re.fullmatch(r"https://([^/]+)/wday/cxs/[^/]+/([^/]+)/jobs", url) |
| 411 | rows = self.sites.get(f"{match.group(1)}/{match.group(2)}") if match else None |
| 412 | if rows is None: |
| 413 | raise jobs.http.HTTPError("not found", status_code=404) |
| 414 | if isinstance(rows, dict): |
| 415 | return rows |
| 416 | offset = json_data["offset"] |
| 417 | return { |
| 418 | "total": len(rows) if offset == 0 else 0, |
| 419 | "jobPostings": rows[offset:offset + json_data["limit"]], |
| 420 | } |
| 421 | |
| 422 | def get(self, url, headers=None, **kwargs): |
| 423 | return {"jobPostingInfo": {"jobDescription": "<p>Build it.</p>", "startDate": "2026-11-03"}} |
| 424 | |
| 425 | def sites_read(self) -> dict[str, int]: |
| 426 | counts: dict[str, int] = {} |
| 427 | for url in self.list_calls: |
| 428 | match = re.fullmatch(r"https://([^/]+)/wday/cxs/[^/]+/([^/]+)/jobs", url) |
| 429 | key = f"{match.group(1)}/{match.group(2)}" |
| 430 | counts[key] = counts.get(key, 0) + 1 |
| 431 | return counts |
| 432 | |
| 433 | def search(self, company: str, site: str): |
| 434 | with patch("lib.jobs.http.get_text", side_effect=self.get_text), \ |
| 435 | patch("lib.jobs.http.post", side_effect=self.post), \ |
| 436 | patch("lib.jobs.http.get", side_effect=self.get): |
| 437 | return jobs.search_jobs( |
| 438 | company, ("2026-10-11", "2026-11-10"), {}, web_backend="none", site=site, |
| 439 | ) |
| 440 | |
| 441 | |
| 442 | NIKE = "nike.wd1.myworkdayjobs.com" |
| 443 | NIKE_PAGE = '<a href="https://nike.wd1.myworkdayjobs.com/en-US/nke2">Jobs</a>' |
| 444 | |
| 445 | |
| 446 | class WorkdayTenantSitesTests(unittest.TestCase): |
| 447 | def test_every_site_robots_txt_disallows_is_read_and_a_shared_posting_counts_once(self): |
| 448 | tenant = FakeTenant( |
| 449 | { |
| 450 | f"{NIKE}/nke": [_site_row("nke", 0), _site_row("nke", 1), SHARED], |
| 451 | f"{NIKE}/nke2": [_site_row("nke2", 0), SHARED], |
| 452 | f"{NIKE}/nke4": [_site_row("nke4", 0)], |
| 453 | }, |
| 454 | robots=NIKE_ROBOTS, page=NIKE_PAGE, |
| 455 | ) |
| 456 | items, artifact = tenant.search("Nike", "https://www.nike.com") |
| 457 | self.assertEqual({f"{NIKE}/nke": 1, f"{NIKE}/nke2": 1, f"{NIKE}/nke4": 1}, tenant.sites_read()) |
| 458 | self.assertEqual(5, len(items)) |
| 459 | self.assertEqual(5, artifact["open_roles"]) |
| 460 | self.assertTrue(artifact["owned"]) |
| 461 | titles = {item["title"] for item in items} |
| 462 | self.assertIn("Air Manufacturing Innovation Engineer", titles) |
| 463 | self.assertIn("nke4 role 0", titles) |
| 464 | self.assertEqual( |
| 465 | [{"site": "nke2", "roles": 2}, {"site": "nke", "roles": 3}, {"site": "nke4", "roles": 1}], |
| 466 | artifact["board_sites"], |
| 467 | ) |
| 468 | # A role from another site links to, and is detailed on, its own site. |
| 469 | nke4 = next(item for item in items if item["title"] == "nke4 role 0") |
| 470 | self.assertTrue(nke4["url"].startswith(f"https://{NIKE}/nke4/job/")) |
| 471 | # The digest's count line says which sites the roles came from. |
| 472 | report = schema.Report( |
| 473 | topic="Nike", range_from="2026-10-11", range_to="2026-11-10", |
| 474 | generated_at="2026-11-10T00:00:00Z", |
| 475 | provider_runtime=schema.ProviderRuntime("mock", "mock", "mock"), |
| 476 | query_plan=schema.QueryPlan( |
| 477 | intent="product", freshness_mode="balanced_recent", cluster_mode="none", |
| 478 | raw_topic="Nike", subqueries=[], source_weights={}, |
| 479 | ), |
| 480 | clusters=[], ranked_candidates=[], items_by_source={}, |
| 481 | errors_by_source={}, artifacts={"jobs": artifact}, |
| 482 | ) |
| 483 | roles_line = next( |
| 484 | line for line in render._render_jobs_digest(report) if line.startswith("- Roles listed:") |
| 485 | ) |
| 486 | self.assertIn("Read from 3 Workday sites: nke2 (2 roles), nke (3 roles), nke4 (1 role).", roles_line) |
| 487 | |
| 488 | def test_sites_listed_only_in_allow_and_sitemap_lines_are_found(self): |
| 489 | host = "salesforce.wd12.myworkdayjobs.com" |
| 490 | tenant = FakeTenant( |
| 491 | { |
| 492 | f"{host}/{site}": [_site_row(site, 0)] |
| 493 | for site in ("External_Career_Site", "Tableau", "Slack", "Heroku") |
| 494 | }, |
| 495 | robots=SALESFORCE_ROBOTS, page="", |
| 496 | ) |
| 497 | items, artifact = tenant.search("Salesforce", "https://www.salesforce.com") |
| 498 | self.assertEqual( |
| 499 | {f"{host}/{site}" for site in ("External_Career_Site", "Tableau", "Slack", "Heroku")}, |
| 500 | set(tenant.sites_read()), |
| 501 | ) |
| 502 | self.assertEqual(4, artifact["open_roles"]) |
| 503 | self.assertEqual(4, len(items)) |
| 504 | |
| 505 | def test_a_candidate_that_does_not_answer_with_a_listing_is_dropped(self): |
| 506 | tenant = FakeTenant( |
| 507 | { |
| 508 | f"{NIKE}/nke2": [_site_row("nke2", 0)], |
| 509 | f"{NIKE}/nke": {"errorCode": "S21", "httpStatus": 404, "total": 40}, |
| 510 | }, |
| 511 | robots=NIKE_ROBOTS, page=NIKE_PAGE, |
| 512 | ) |
| 513 | items, artifact = tenant.search("Nike", "https://www.nike.com") |
| 514 | # One call to see the shape, none to page through it. |
| 515 | self.assertEqual(1, tenant.sites_read()[f"{NIKE}/nke"]) |
| 516 | self.assertEqual(1, artifact["open_roles"]) |
| 517 | self.assertNotIn("board_sites", artifact) |
| 518 | self.assertEqual(["nke2 role 0"], [item["title"] for item in items]) |
| 519 | |
| 520 | def test_without_robots_txt_the_sites_the_page_links_are_read(self): |
| 521 | page = NIKE_PAGE + '<a href="https://nike.wd1.myworkdayjobs.com/nke4">Students</a>' |
| 522 | tenant = FakeTenant( |
| 523 | {f"{NIKE}/nke2": [_site_row("nke2", 0)], f"{NIKE}/nke4": [_site_row("nke4", 0)]}, |
| 524 | robots=None, page=page, |
| 525 | ) |
| 526 | _items, artifact = tenant.search("Nike", "https://www.nike.com") |
| 527 | self.assertEqual({f"{NIKE}/nke2": 1, f"{NIKE}/nke4": 1}, tenant.sites_read()) |
| 528 | self.assertEqual(2, artifact["open_roles"]) |
| 529 | |
| 530 | def test_a_site_on_another_tenant_linked_from_the_page_is_not_read(self): |
| 531 | page = NIKE_PAGE + '<a href="https://converse.wd1.myworkdayjobs.com/careers">Converse</a>' |
| 532 | tenant = FakeTenant( |
| 533 | { |
| 534 | f"{NIKE}/nke2": [_site_row("nke2", 0)], |
| 535 | "converse.wd1.myworkdayjobs.com/careers": [_site_row("converse", 0)], |
| 536 | }, |
| 537 | robots=None, page=page, |
| 538 | ) |
| 539 | _items, artifact = tenant.search("Nike", "https://www.nike.com") |
| 540 | self.assertEqual({f"{NIKE}/nke2": 1}, tenant.sites_read()) |
| 541 | self.assertEqual(1, artifact["open_roles"]) |
| 542 | |
| 543 | def test_one_site_at_its_ceiling_leaves_the_others_read_and_the_total_capped(self): |
| 544 | robots = "User-agent: *\n" + "".join(f"Disallow: /s{n}/\n" for n in range(1, 5)) |
| 545 | page = '<a href="https://acme.wd5.myworkdayjobs.com/s1">Jobs</a>' |
| 546 | host = "acme.wd5.myworkdayjobs.com" |
| 547 | sites = {f"{host}/s1": [_site_row("s1", index) for index in range(2500)]} |
| 548 | sites.update({ |
| 549 | f"{host}/s{n}": [_site_row(f"s{n}", index) for index in range(2000)] for n in (2, 3, 4) |
| 550 | }) |
| 551 | tenant = FakeTenant(sites, robots=robots, page=page) |
| 552 | items, artifact = tenant.search("Acme", SITE) |
| 553 | read = tenant.sites_read() |
| 554 | self.assertEqual(100, read[f"{host}/s1"]) |
| 555 | self.assertEqual({f"{host}/s{n}" for n in range(1, 5)}, set(read)) |
| 556 | self.assertEqual(jobs.COMPANY_BOARD_ROW_CEILING, len(items)) |
| 557 | self.assertEqual(jobs.COMPANY_BOARD_ROW_CEILING, artifact["open_roles"]) |
| 558 | self.assertTrue(artifact["open_roles_floor"]) |
| 559 | |
| 560 | |
| 561 | if __name__ == "__main__": |
| 562 | unittest.main() |
| 563 |