From bd4121be912d8c766a435c88739eba55f952759e Mon Sep 17 00:00:00 2001 From: Wolf Date: Tue, 29 Sep 2026 20:08:35 +0200 Subject: [PATCH] Read Jibe career sites from their jobs API Co-Authored-By: Claude Sonnet 5.5 --- plugins/atshosts/jibe.py | 132 ++++++++++++ tests/fixtures/ats/jibe.json | 403 +++++++++++++++++++++++++++++++++++ tests/test_ats_jibe.py | 173 +++++++++++++++ 3 files changed, 708 insertions(+) create mode 100644 plugins/atshosts/jibe.py create mode 100644 tests/fixtures/ats/jibe.json create mode 100644 tests/test_ats_jibe.py diff --git a/plugins/atshosts/jibe.py b/plugins/atshosts/jibe.py new file mode 100644 index 0000000..b285e6a --- /dev/null +++ b/plugins/atshosts/jibe.py @@ -0,0 +1,132 @@ +"""Jibe (iCIMS Jibe): `jibe:`, the career site's own host name, for +example `jibe:jobs.booking.com`. A JSON API on that host, paged by page number +at 100 roles a page, with the total stated. + +Written from Booking.com's site on 2026-09-29 (145 roles, two pages), the only +Jibe site read. Unverified: that another Jibe site serves `/api/jobs` with the +same parameters and answer, and what one that does not gives. A host that is +not a Jibe site fails as a board that is not a listing. + +`GET /api/jobs?limit=100&page=N` answers `{"jobs": [{"data": {...}}], "totalCount"}` +with each role's full description, so the roles need no `detail`; `limit=200` is +a 422. The findings used `brand=Booking.com`, which is not needed: without it +the site's whole board is read, 145 roles, of which the filter keeps the 97 of +brand "Booking.com" and drops the 48 of brand "Booking Holdings", the parent's +own roles (finance, legal and data teams in Bucharest, listed and applied for +on the same site). The board is read whole, as the site shows it, and `brand` +is not in the target. + +A role's page is `/jobs/` on the host, which redirects to the site's own +path (`/booking/jobs/` on Booking's), so no site's path prefix is built +in. The site's robots.txt asks for `crawl-delay: 5`, which the shared reader +does not honour (`ats.PAGE_DELAY` is 0.5 s); a board is two requests. + +Discovery can name Jibe (a site loads its assets from `jibecdn.com` or applies +on `jibeapply.com`) but not the host, since a Jibe site's own links look like +any other site's, so the host is given by hand. +""" + +from __future__ import annotations + +import html +import json +import re +from collections.abc import Callable +from typing import Any + +from desk.sources import strip_html + +from ._shared import Get, Page, Row, only, parse_dt + +NAME = "jibe" +LABEL = "Jibe" +PATTERNS = ( + # No slug: the host is the company's own, which no URL on the page names. + r"jibeapply\.com|jibecdn\.com/prod", +) + +#: The page size asked for, which is also the most the host serves (200 is a 422). +PAGE_SIZE = 100 + +#: A whole host name (two labels or more, the last one letters), so neither an +#: address (`127.0.0.1`), a bare word (`localhost`) nor a port names a site. +#: `ats._get` refuses every host but this one, redirects included, and a local +#: name or an address in any case. +_HOST = re.compile(r"[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*\.[a-z]{2,}") + + +#: Names a private network uses, never a company's career site; `ats._get` +#: refuses them too, and a slug that names one is not a board at all. +_LOCAL = (".localhost", ".local", ".internal", ".lan", ".home.arpa") + + +def slug_ok(slug: str) -> bool: + host = slug.lower() + return bool(_HOST.fullmatch(host)) and not host.endswith(_LOCAL) + + +def allowed(slug: str) -> Callable[[str], bool]: + return only(slug) + + +def careers_url(slug: str) -> str: + return f"https://{slug.lower()}/jobs" + + +def robots_page(slug: str) -> str: + return careers_url(slug) + + +def page(get: Get, slug: str, cursor: Any) -> Page: + """One page of `/api/jobs`; the next is the following number, and an empty + page ends the read.""" + number = int(cursor or 1) + data = json.loads(get(f"https://{slug.lower()}/api/jobs?limit={PAGE_SIZE}&page={number}")) + jobs = data.get("jobs") if isinstance(data, dict) else None + if not isinstance(jobs, list): + raise ValueError("not a Jibe listing: no jobs list") + total = data.get("totalCount") + results = [{"id": (j.get("data") or {}).get("slug") or ""} for j in jobs] + return data, results, total if isinstance(total, int) else None, \ + number + 1 if jobs else None + + +def parse(body: bytes, slug: str) -> list[Row]: + return rows(json.loads(body), slug) + + +def _plain(markup: Any) -> str: + return " ".join(html.unescape(strip_html(str(markup or ""))).split()) + + +def rows(data: dict[str, Any], slug: str) -> list[Row]: + """One page: each `jobs[].data` has `slug` (equal to `req_id`), `title`, + the HTML `description` and, on some, `qualifications`, `full_location` + ("Amsterdam, Netherlands; Manchester, United Kingdom" for several), + `city` and `country`, `categories`, `posted_date` and `apply_url`, on the + iCIMS site. The `salary_*` fields were 0 on every role and there is no + currency, so no pay is read; nothing says remote (`location_type` is a + kind of coordinates).""" + out = [] + for job in data.get("jobs") or []: + j = job.get("data") or {} + title = (j.get("title") or "").strip() + role = str(j.get("slug") or j.get("req_id") or "") + if not title or not role: + continue + categories = j.get("categories") or [] + text = "\n\n".join(p for p in (_plain(j.get("description")), + _plain(j.get("qualifications"))) if p) + out.append({ + "source_id": role, + "title": title, + "location": (j.get("full_location") + or ", ".join(p for p in (j.get("city"), j.get("country")) if p)).strip(), + "url": f"https://{slug.lower()}/jobs/{role}", + "posted": parse_dt(j.get("posted_date")), + "department": str((categories[0] if categories else {}).get("name") or "").strip(), + "description": text, + "remote": None, + "salary_text": "", + }) + return out diff --git a/tests/fixtures/ats/jibe.json b/tests/fixtures/ats/jibe.json new file mode 100644 index 0000000..d6dbae1 --- /dev/null +++ b/tests/fixtures/ats/jibe.json @@ -0,0 +1,403 @@ +{ + "jobs": [ + { + "data": { + "slug": "30413", + "language": "en-us", + "languages": [ + "en-us" + ], + "client_code": "workingatbooking", + "req_id": "30413", + "title": "Booking Holdings Romania - Team Leader Regulatory Data Releases (2 years contract - maternity cover)", + "description": "

Booking Holdings Romania is a Center of Excellence based in Bucharest, Romania and was created to support the increasing business demands of the Booking Holdings Brands. The Center of Excellence provides access to specialized and highly skilled talent, leading industry best practices, and collaboration opportunities across all of our Brands.

As part of our Booking Holdings Romania team, you will have the opportunity to be a part of the world’s leading provider of online travel, with a mission of making it easier for everyone to experience the world through five-primary consumer facing brands: Booking.com, Priceline, Agoda, KAYAK and OpenTable.

 

 

This role provides a hybrid way of working with an onsite presence of 2 days/week.

 

 

Role description

 

The Team Lead - DSR proactively identifies, manages, and mitigates privacy risks across the organization. This role supports and develops their team to drive operational excellence, innovation, and adaptability in a constantly evolving regulatory landscape, while elevating the business's overall privacy maturity to the next level.

 

", + "location_name": "Bucharest - BH", + "street_address": "U-Center (3rd floor) 206 Calea Șerban Vodă, district 4,", + "city": "Bucharest", + "state": "Bucureşti", + "country": "Romania", + "country_code": "RO", + "postal_code": "040215", + "location_type": "LAT_LNG", + "latitude": 44.43225, + "longitude": 26.10626, + "categories": [ + { + "name": "Legal & Public Affairs" + } + ], + "tags1": [ + "Booking Holdings COE Company Hierarchy" + ], + "brand": "Booking Holdings", + "department": "", + "recruiter_gateway_id": "104", + "salary_value": 0, + "salary_min_value": 0, + "salary_max_value": 0, + "employment_type": "FULL_TIME", + "hiring_organization": "Booking Holdings", + "hiring_organization_logo": "https://www.bookingholdings.com/wp-content/themes/booking-group/img/svg/bkng_global-desktop-logowhite.svg", + "source": "Booking Holdings", + "posted_date": "2026-09-29T12:55:00+0000", + "apply_url": "https://careers-holdings-workingatbooking.icims.com/jobs/30413/login", + "internal": false, + "external": false, + "searchable": true, + "applyable": true, + "li_easy_applyable": true, + "ats_code": "icims", + "hiring_flow_name": "iCIMS ATS Hiring Flow", + "isFrontLineAIJob": false, + "isApplyFlowJob": false, + "update_date": "2026-09-29T13:10:15+0000", + "create_date": "2026-09-29T12:57:13+0000", + "category": [ + " Legal & Public Affairs" + ], + "full_location": "Bucharest, Romania", + "short_location": "Bucharest, Romania", + "multipleLocations": false + } + }, + { + "data": { + "slug": "30262", + "language": "en-us", + "languages": [ + "en-us" + ], + "client_code": "workingatbooking", + "req_id": "30262", + "title": "Treasury Risk Specialist", + "description": "

About Booking Holdings 

Booking Holdings (NASDAQ:BKNG) is the world's leading provider of online travel and related services, provided to consumers and local partners in more than 220 countries and territories through five primary consumer-facing brands: Booking.com, Priceline, Agoda, KAYAK and OpenTable. The mission of Booking Holdings is to make it easier for everyone to experience the world. For more information, visit BookingHoldings.com and follow us on X (formerly known as Twitter) @BookingHoldings. 

 

The Role: Booking Holdings BV’s global treasury team is looking for a Treasury risk management professional to join our growing team. The Treasury Risk Specialist is responsible for performing financial analysis and operating processes relating to the identification, quantification, management and reporting of credit, counterparty, liquidity and other Treasury risk typologies that attach to Booking’s evolving business models, strategic initiatives and third party relationships.  

", + "location_name": "Amsterdam BH", + "street_address": "Oosterdokskade 163", + "city": "Amsterdam", + "state": "North Holland", + "country": "Netherlands", + "country_code": "NL", + "postal_code": "1011 DL", + "location_type": "LAT_LNG", + "latitude": 52.37403, + "longitude": 4.88969, + "categories": [ + { + "name": "Finance" + } + ], + "tags1": [ + "Booking Holdings Company Hierarchy" + ], + "brand": "Booking.com", + "department": "", + "recruiter_gateway_id": "103", + "salary_value": 0, + "salary_min_value": 0, + "salary_max_value": 0, + "employment_type": "FULL_TIME", + "hiring_organization": "Booking.com", + "hiring_organization_logo": "https://www.google.com/imgres?imgurl=https%3A%2F%2Fcdn.worldvectorlogo.com%2Flogos%2Fbookingcom-1.svg&tbnid=aYVSXTru4-zj2M&vet=12ahUKEwjpxpKWs7CCAxXtKt4AHa19CdkQMygEegQIARBw..i&imgrefurl=https%3A%2F%2Fworldvectorlogo.com%2Flogo%2Fbookingcom-1&docid=8rsjzthSju2GyM&w=2500&h=2445&q=booking.com%20logo&ved=2ahUKEwjpxpKWs7CCAxXtKt4AHa19CdkQMygEegQIARBw", + "source": "Booking.com", + "posted_date": "2026-09-29T12:50:00+0000", + "apply_url": "https://external-workingatbooking.icims.com/jobs/30262/login", + "internal": false, + "external": false, + "searchable": true, + "applyable": true, + "li_easy_applyable": true, + "ats_code": "icims", + "hiring_flow_name": "iCIMS ATS Hiring Flow", + "isFrontLineAIJob": false, + "isApplyFlowJob": false, + "update_date": "2026-09-29T12:54:14+0000", + "create_date": "2026-09-29T12:51:12+0000", + "category": [ + " Finance" + ], + "full_location": "Amsterdam, Netherlands", + "short_location": "Amsterdam, Netherlands", + "multipleLocations": false + } + }, + { + "data": { + "slug": "30320", + "language": "en-us", + "languages": [ + "en-us" + ], + "client_code": "workingatbooking", + "req_id": "30320", + "title": "Data Analytics Engineer I - Treasury Data Science & Analytics", + "description": "

About Booking Holdings 

Booking Holdings (NASDAQ:BKNG) is the world's leading provider of online travel and related services, provided to consumers and local partners in more than 220 countries and territories through five primary consumer-facing brands: Booking.com, Priceline, Agoda, KAYAK and OpenTable. The mission of Booking Holdings is to make it easier for everyone to experience the world. For more information, visit BookingHoldings.com and follow us on X (formerly known as Twitter) @BookingHoldings. 

About the team:

The Treasury Data Science & Analytics team develops trusted, scalable and reusable analytical products that enable cash flow forecasting, liquidity optimization, risk modelling, reporting and self-service analytics for the Booking Holdings Group Treasury team. 

", + "location_name": "Amsterdam BH", + "street_address": "Oosterdokskade 163", + "city": "Amsterdam", + "state": "North Holland", + "country": "Netherlands", + "country_code": "NL", + "postal_code": "1011 DL", + "location_type": "LAT_LNG", + "latitude": 52.37403, + "longitude": 4.88969, + "categories": [ + { + "name": "Finance" + } + ], + "tags1": [ + "Booking Holdings Company Hierarchy" + ], + "brand": "Booking.com", + "department": "", + "recruiter_gateway_id": "103", + "salary_value": 0, + "salary_min_value": 0, + "salary_max_value": 0, + "employment_type": "FULL_TIME", + "hiring_organization": "Booking.com", + "hiring_organization_logo": "https://www.google.com/imgres?imgurl=https%3A%2F%2Fcdn.worldvectorlogo.com%2Flogos%2Fbookingcom-1.svg&tbnid=aYVSXTru4-zj2M&vet=12ahUKEwjpxpKWs7CCAxXtKt4AHa19CdkQMygEegQIARBw..i&imgrefurl=https%3A%2F%2Fworldvectorlogo.com%2Flogo%2Fbookingcom-1&docid=8rsjzthSju2GyM&w=2500&h=2445&q=booking.com%20logo&ved=2ahUKEwjpxpKWs7CCAxXtKt4AHa19CdkQMygEegQIARBw", + "source": "Booking.com", + "posted_date": "2026-09-29T12:39:00+0000", + "apply_url": "https://external-workingatbooking.icims.com/jobs/30320/login", + "internal": false, + "external": false, + "searchable": true, + "applyable": true, + "li_easy_applyable": true, + "ats_code": "icims", + "hiring_flow_name": "iCIMS ATS Hiring Flow", + "isFrontLineAIJob": false, + "isApplyFlowJob": false, + "update_date": "2026-09-29T13:00:13+0000", + "create_date": "2026-09-29T12:40:11+0000", + "category": [ + " Finance" + ], + "full_location": "Amsterdam, Netherlands", + "short_location": "Amsterdam, Netherlands", + "multipleLocations": false + } + }, + { + "data": { + "slug": "30204", + "language": "en-us", + "languages": [ + "en-us" + ], + "client_code": "workingatbooking", + "req_id": "30204", + "title": "Senior Director Engineering - Connected Trip Experience & Data", + "description": "

About Marketplace

Booking.com's Marketplace unit turns travel goals into travel realities. Our purpose? To create frictionless experiences at each and every stage of the trip. From browsing to booking and beyond, our focus is on building the connected experiences which make journeys smoother, faster and more enjoyable. Think of us as travel matchmakers, ensuring seamless integration across our product verticals to effortlessly and intuitively connect traveler preferences and actions.

 

", + "location_name": "Amsterdam Campus", + "street_address": "Oosterdokskade 163,", + "city": "Amsterdam", + "state": "North Holland", + "country": "Netherlands", + "country_code": "NL", + "postal_code": "1011 DL", + "location_type": "LAT_LNG", + "latitude": 52.37403, + "longitude": 4.88969, + "additional_locations": [ + { + "location_name": "Manchester Campus (non-Transport)", + "street_address": "The Goods Yard Building 6 Goods Yard Street", + "city": "Manchester", + "state": "Greater Manchester", + "country": "United Kingdom", + "country_code": "GB", + "postal_code": "M3 3BG", + "latitude": 53.48095, + "longitude": -2.23743 + } + ], + "categories": [ + { + "name": "Leadership" + } + ], + "tags1": [ + "Booking.com Company Hierarchy" + ], + "brand": "Booking.com", + "department": "", + "recruiter_gateway_id": "103", + "salary_value": 0, + "salary_min_value": 0, + "salary_max_value": 0, + "employment_type": "FULL_TIME", + "hiring_organization": "Booking.com", + "hiring_organization_logo": "https://www.google.com/imgres?imgurl=https%3A%2F%2Fcdn.worldvectorlogo.com%2Flogos%2Fbookingcom-1.svg&tbnid=aYVSXTru4-zj2M&vet=12ahUKEwjpxpKWs7CCAxXtKt4AHa19CdkQMygEegQIARBw..i&imgrefurl=https%3A%2F%2Fworldvectorlogo.com%2Flogo%2Fbookingcom-1&docid=8rsjzthSju2GyM&w=2500&h=2445&q=booking.com%20logo&ved=2ahUKEwjpxpKWs7CCAxXtKt4AHa19CdkQMygEegQIARBw", + "source": "Booking.com", + "posted_date": "2026-09-10T13:46:00+0000", + "apply_url": "https://external-workingatbooking.icims.com/jobs/30204/login", + "internal": false, + "external": false, + "searchable": true, + "applyable": true, + "li_easy_applyable": true, + "ats_code": "icims", + "hiring_flow_name": "iCIMS ATS Hiring Flow", + "isFrontLineAIJob": false, + "isApplyFlowJob": false, + "update_date": "2026-09-21T18:17:45+0000", + "create_date": "2026-09-10T13:49:45+0000", + "category": [ + " Leadership" + ], + "full_location": "Amsterdam, Netherlands; Manchester, United Kingdom", + "short_location": "Amsterdam, Netherlands", + "multipleLocations": true + } + }, + { + "data": { + "slug": "30222", + "language": "en-us", + "languages": [ + "en-us" + ], + "client_code": "workingatbooking", + "req_id": "30222", + "title": "Key Account Manager", + "description": "

About Us: At Booking.com, data drives our decisions. Technology is at our core. And innovation is everywhere. But our company is more than datasets, lines of code or A/B tests. We’re the thrill of the first night in a new place. The excitement of the next morning. The friends you encounter. The journeys you take. The sights you see. And the memories you make. Through our products, partners and people, we make it easier for everyone to experience the world.

 

Role Description:

", + "location_name": "Osaka", + "street_address": "WeWork LINKS UMEDA Office number 08-103 1-1 Ofukacho Kita-Ku", + "city": "Osaka", + "state": "Osaka", + "country": "Japan", + "country_code": "JP", + "postal_code": "530-0011", + "location_type": "LAT_LNG", + "latitude": 34.7043, + "longitude": 135.4955, + "additional_locations": [ + { + "location_name": "Tokyo", + "street_address": "Shibuya Scramble Square 38F, 2-24-12 Shibuya", + "city": "Tokyo", + "state": "Tokyo", + "country": "Japan", + "country_code": "JP", + "postal_code": "150-6138", + "latitude": 35.68113, + "longitude": 139.76706 + } + ], + "categories": [ + { + "name": "Business Development & Support" + } + ], + "tags1": [ + "Booking.com Company Hierarchy" + ], + "brand": "Booking.com", + "department": "", + "recruiter_gateway_id": "103", + "salary_value": 0, + "salary_min_value": 0, + "salary_max_value": 0, + "employment_type": "FULL_TIME", + "hiring_organization": "Booking.com", + "hiring_organization_logo": "https://www.google.com/imgres?imgurl=https%3A%2F%2Fcdn.worldvectorlogo.com%2Flogos%2Fbookingcom-1.svg&tbnid=aYVSXTru4-zj2M&vet=12ahUKEwjpxpKWs7CCAxXtKt4AHa19CdkQMygEegQIARBw..i&imgrefurl=https%3A%2F%2Fworldvectorlogo.com%2Flogo%2Fbookingcom-1&docid=8rsjzthSju2GyM&w=2500&h=2445&q=booking.com%20logo&ved=2ahUKEwjpxpKWs7CCAxXtKt4AHa19CdkQMygEegQIARBw", + "source": "Booking.com", + "posted_date": "2026-09-10T03:32:00+0000", + "posting_expiry_date": "2026-10-30T16:00:00+0000", + "apply_url": "https://external-workingatbooking.icims.com/jobs/30222/login", + "internal": false, + "external": false, + "searchable": true, + "applyable": true, + "li_easy_applyable": true, + "ats_code": "icims", + "hiring_flow_name": "iCIMS ATS Hiring Flow", + "isFrontLineAIJob": false, + "isApplyFlowJob": false, + "update_date": "2026-09-11T13:03:47+0000", + "create_date": "2026-09-10T03:38:45+0000", + "category": [ + " Business Development & Support" + ], + "full_location": "Osaka, Japan; Tokyo, Japan", + "short_location": "Osaka, Japan", + "multipleLocations": true + } + }, + { + "data": { + "slug": "13402", + "language": "en-us", + "languages": [ + "en-us" + ], + "client_code": "workingatbooking", + "req_id": "13402", + "title": "Full Stack Software Engineer II", + "description": "

At Booking.com, data drives our decisions. Technology is at our core. And innovation is everywhere. But our company is more than datasets, lines of code or A/B tests. We’re the thrill of the first night in a new place. The excitement of the next morning. The friends you make. The journeys you take. The sights you see. And the food you sample. Through our products, partners and people, we can empower everyone to experience the world.

We’re a truly global e-commerce company, with business operations in nearly every country and city on the planet. And we want to make it easy for everyone, anywhere in the world, to pay for their travel or do business with our platform – whenever and however it’s convenient for them.

", + "location_name": "Amsterdam Campus", + "street_address": "Oosterdokskade 163,", + "city": "Amsterdam", + "state": "North Holland", + "country": "Netherlands", + "country_code": "NL", + "postal_code": "1011 DL", + "location_type": "LAT_LNG", + "latitude": 52.37403, + "longitude": 4.88969, + "categories": [ + { + "name": "Engineering" + } + ], + "tags1": [ + "Booking.com Company Hierarchy" + ], + "brand": "Booking.com", + "department": "", + "recruiter_gateway_id": "103", + "salary_value": 0, + "salary_min_value": 0, + "salary_max_value": 0, + "employment_type": "FULL_TIME", + "qualifications": "

Java 

Python

Javascript

React
ReactJS

", + "hiring_organization": "Booking.com", + "hiring_organization_logo": "https://www.google.com/imgres?imgurl=https%3A%2F%2Fcdn.worldvectorlogo.com%2Flogos%2Fbookingcom-1.svg&tbnid=aYVSXTru4-zj2M&vet=12ahUKEwjpxpKWs7CCAxXtKt4AHa19CdkQMygEegQIARBw..i&imgrefurl=https%3A%2F%2Fworldvectorlogo.com%2Flogo%2Fbookingcom-1&docid=8rsjzthSju2GyM&w=2500&h=2445&q=booking.com%20logo&ved=2ahUKEwjpxpKWs7CCAxXtKt4AHa19CdkQMygEegQIARBw", + "source": "Booking.com", + "posted_date": "2026-09-23T14:16:00+0000", + "apply_url": "https://external-workingatbooking.icims.com/jobs/13402/login", + "internal": false, + "external": false, + "searchable": true, + "applyable": true, + "li_easy_applyable": true, + "ats_code": "icims", + "hiring_flow_name": "iCIMS ATS Hiring Flow", + "isFrontLineAIJob": false, + "isApplyFlowJob": false, + "update_date": "2026-09-24T10:08:45+0000", + "create_date": "2026-09-23T14:27:43+0000", + "category": [ + " Engineering" + ], + "full_location": "Amsterdam, Netherlands", + "short_location": "Amsterdam, Netherlands", + "multipleLocations": false + } + } + ], + "locations": [], + "totalCount": 145, + "request_id": false, + "meta_data": false, + "count": 145, + "languageCounts": { + "en-us": { + "displayName": "English", + "count": 145 + } + } +} \ No newline at end of file diff --git a/tests/test_ats_jibe.py b/tests/test_ats_jibe.py new file mode 100644 index 0000000..0d91086 --- /dev/null +++ b/tests/test_ats_jibe.py @@ -0,0 +1,173 @@ +"""The Jibe host module against a saved page of Booking.com's listing (six of +its 145 roles, descriptions cut). No network.""" + +from __future__ import annotations + +import json +import sys +from datetime import UTC, datetime +from pathlib import Path +from typing import Any + +import httpx +import pytest + +ROOT = Path(__file__).resolve().parents[1] +if str(ROOT) not in sys.path: + sys.path.insert(0, str(ROOT)) + +from app.jobs import discover # noqa: E402 +from plugins import ats, atshosts # noqa: E402 +from plugins.atshosts import jibe # noqa: E402 + +FIXTURES = ROOT / "tests" / "fixtures" / "ats" +SITE = "jobs.booking.com" + + +def _saved() -> dict[str, Any]: + return json.loads((FIXTURES / "jibe.json").read_text()) + + +def _board(total: int) -> dict[int, dict[str, Any]]: + """`total` roles as the site pages them: 100 to a page, the saved roles + repeated under new slugs.""" + template = _saved()["jobs"] + jobs = [] + for n in range(total): + data = dict(template[n % len(template)]["data"]) | {"slug": str(1000 + n)} + jobs.append({"data": data}) + return {page + 1: {"jobs": jobs[start:start + 100], "totalCount": total} + for page, start in enumerate(range(0, total, 100))} + + +def test_a_page_becomes_rows() -> None: + rows = jibe.rows(_saved(), SITE) + assert [r["source_id"] for r in rows] == ["30413", "30262", "30320", "30204", "30222", + "13402"] + first, several = rows[0], rows[3] + assert first["title"].startswith("Booking Holdings Romania - Team Leader") + assert first["url"] == "https://jobs.booking.com/jobs/30413" + assert first["location"] == "Bucharest, Romania" + assert first["department"] == "Legal & Public Affairs" + assert first["posted"] == datetime(2026, 9, 29, 12, 55, tzinfo=UTC) + assert (first["remote"], first["salary_text"]) == (None, "") + assert several["location"] == "Amsterdam, Netherlands; Manchester, United Kingdom" + assert all(r["description"] and "<" not in r["description"] and " " not in r["description"] + for r in rows) + + +def test_qualifications_follow_the_description() -> None: + text = jibe.rows(_saved(), SITE)[-1]["description"] + assert "\n\n" in text and text.split("\n\n")[-1].startswith("Java") + + +def test_the_location_falls_back_to_city_and_country() -> None: + role = {"slug": "1", "title": "Role", "city": "Riga", "country": "Latvia"} + assert jibe.rows({"jobs": [{"data": role}]}, SITE)[0]["location"] == "Riga, Latvia" + + +def test_a_role_without_a_title_or_a_slug_is_left_out() -> None: + jobs = [{"data": {"slug": "1", "title": " "}}, {"data": {"title": "Role"}}, {}] + assert jibe.rows({"jobs": jobs}, SITE) == [] + + +def test_a_page_states_its_total_and_the_cursor_is_the_next_number() -> None: + asked: list[str] = [] + board = _board(145) + + def get(url: str, **_: Any) -> bytes: + asked.append(url) + return json.dumps(board[int(url.rsplit("=", 1)[1])]).encode() + + decoded, results, total, cursor = jibe.page(get, SITE, None) + assert asked == ["https://jobs.booking.com/api/jobs?limit=100&page=1"] + assert (len(results), total, cursor) == (100, 145, 2) + assert len(jibe.rows(decoded, SITE)) == 100 + assert jibe.page(get, SITE, 2)[2:] == (145, 3) + assert asked[-1].endswith("&page=2") + + +def test_the_board_is_read_to_its_total_through_the_app(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(ats, "PAGE_DELAY", 0) + board = _board(145) + seen: list[str] = [] + + def handler(request: httpx.Request) -> httpx.Response: + seen.append(str(request.url)) + return httpx.Response(200, json=board[int(request.url.params["page"])]) + + with httpx.Client(transport=httpx.MockTransport(handler)) as client: + rows, complete = ats._fetch_rows(client, f"jibe:{SITE}") + assert (len(rows), complete) == (145, True) + assert seen == [f"https://jobs.booking.com/api/jobs?limit=100&page={n}" for n in (1, 2)] + + +def test_a_board_that_ends_short_of_its_total_is_incomplete( + monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(ats, "PAGE_DELAY", 0) + board = _board(145) + board[2]["totalCount"] = board[1]["totalCount"] = 150 # five roles the site never sends + board[3] = {"jobs": [], "totalCount": 150} + + def handler(request: httpx.Request) -> httpx.Response: + return httpx.Response(200, json=board[int(request.url.params["page"])]) + + with httpx.Client(transport=httpx.MockTransport(handler)) as client: + rows, complete = ats._fetch_rows(client, f"jibe:{SITE}") + assert (len(rows), complete) == (145, False) + + +def test_an_empty_board_is_complete_at_zero() -> None: + body = {"jobs": [], "totalCount": 0} + with httpx.Client(transport=httpx.MockTransport( + lambda request: httpx.Response(200, json=body))) as client: + assert ats._fetch_rows(client, f"jibe:{SITE}") == ([], True) + + +@pytest.mark.parametrize("body", [b"Not found", b"[]", b'{"jobs": null}', + b'{"error": "no"}']) +def test_an_answer_that_is_not_a_listing_raises(body: bytes) -> None: + with pytest.raises(ValueError): + jibe.page(lambda url, **_: body, SITE, None) + + +def test_a_422_and_a_429_reach_the_shared_code_as_status_errors() -> None: + """A module lets them through: 429 after the first page ends the read.""" + request = httpx.Request("GET", f"https://{SITE}/api/jobs") + + def get(url: str, **_: Any) -> bytes: + raise httpx.HTTPStatusError("422", request=request, + response=httpx.Response(422, request=request)) + + with pytest.raises(httpx.HTTPStatusError): + jibe.page(get, SITE, None) + + +def test_the_slug_the_urls_and_the_allow_list() -> None: + for slug in (SITE, "Jobs.Booking.com", "careers.acme.co.uk", "acme.com", "a-b.c-d.example"): + assert jibe.slug_ok(slug) and ats.is_known_target(f"jibe:{slug}") + assert jibe.careers_url("Jobs.Booking.com") == "https://jobs.booking.com/jobs" + assert jibe.robots_page(SITE) == "https://jobs.booking.com/jobs" + allow = jibe.allowed("Jobs.Booking.com") + assert allow("jobs.booking.com") and allow("JOBS.BOOKING.COM") + assert not allow("booking.com") and not allow("evil.example") + assert not allow("jobs.booking.com.evil.example") and not allow("localhost") + assert atshosts.HOSTS["jibe"] is jibe and ats.HOSTS["jibe"] == "Jibe" + + +@pytest.mark.parametrize("slug", [ + "", "booking", "localhost", "a@b.com", "a.com#x", "a.com?x", "..", "a.com/x", "a.com%2fx", + "a.com:443", "a.com:8443", " a.com", "a.com ", "a.com\n", "a.com\r\n", "127.0.0.1", + "2130706433", "[::1]", "0x7f.0x0.0x0.0x1", "a.b", "-a.com", "a-.com", "a..com", "a.com.", + "app.localhost", "printer.local", "meta.internal", "nas.home.arpa", "router.lan", + "user@jobs.booking.com", "a_b.com", "évil.com"]) +def test_a_hostile_slug_is_refused(slug: str) -> None: + assert not jibe.slug_ok(slug) + assert not ats.is_known_target(f"jibe:{slug}") + + +def test_discovery_names_jibe_from_its_assets_but_gives_no_host() -> None: + page = ('' + 'apply') + assert [(h, s) for h, s, _ in discover.boards_in(page)] == [("jibe", "")] + assert "jibe" not in {h for h, _ in discover._UNREADABLE}