"""The Deel host module against a saved page (trimmed from Klarna's board, 2026-09-29), and the fetch around it. No network.""" from __future__ import annotations import json import sys 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.discover import boards_in # noqa: E402 from plugins import ats, readers # noqa: E402 from plugins.readers import Ask, deel # noqa: E402 PAGE = (ROOT / "tests" / "fixtures" / "ats" / "deel.html").read_bytes() URL = "https://jobs.deel.com/klarna" def test_rows_come_from_the_payload_with_descriptions_resolved() -> None: rows = deel.parse(PAGE, "klarna") assert [r["source_id"][:8] for r in rows] == [ "21584046", "a66b42b6", "f96bb9d4", "a8be45ac", "a6649d86"] first = rows[0] assert first["title"] == "Incident Manager" assert first["url"] == ("https://jobs.deel.com/klarna/job-details/" "21584046-56d5-44c4-8b18-7e15dc659827/overview") assert first["location"] == "Stockholm" and first["department"] == "Engineering" assert first["remote"] == "onsite" assert first["description"].startswith("Klarna, briefly At Klarna") assert "<" not in first["description"] and "$" not in first["description"][:3] assert all(r["description"] for r in rows) # The day the posting was published, not the day it was drafted (12:27). assert first["posted"] is not None and first["posted"].isoformat() == \ "2026-09-07T12:33:38.484000+00:00" def test_several_locations_are_joined_and_remote_uses_the_stores_words() -> None: rows = {r["source_id"][:8]: r for r in deel.parse(PAGE, "klarna")} assert rows["a8be45ac"]["location"] == "Stockholm; London; Milan; Warsaw" assert rows["f96bb9d4"]["remote"] == "remote" assert rows["f96bb9d4"]["location"] == "Ohio" # Every place is kept, in the host's order; the work arrangement is the # places' remote flag. assert rows["a8be45ac"]["places"] == [ {"text": name, "remote": False} for name in ("Stockholm", "London", "Milan", "Warsaw")] assert rows["f96bb9d4"]["places"] == [{"text": "Ohio", "remote": True}] assert rows["f96bb9d4"]["employment"] == "contract" # "Consultant" assert rows["21584046"]["employment"] == "full_time" def test_pay_is_only_text_since_the_period_is_not_stated() -> None: rows = {r["source_id"][:8]: r for r in deel.parse(PAGE, "klarna")} assert rows["21584046"]["salary_text"] == "SEK 775,398 to 1,108,832" assert rows["a66b42b6"]["salary_text"] == "GBP from 105,197" assert rows["f96bb9d4"]["salary_text"] == "" # a role with no range assert all("salary_min" not in r for r in rows.values()) def _page_with(body: dict[str, Any]) -> bytes: """A page whose one payload row holds `body`, and a description row of multibyte text whose length is given in bytes, as the stream does.""" text = "Zo\u00eb\u2019s caf\u00e9 \u2014 100 \u20ac" # 2, 3 and 1 bytes wide row = f"5:T{len(text.encode()):x},{text}" + \ '6:["$","div",null,' + json.dumps(body, ensure_ascii=False, separators=(",", ":")) + "]\n" return f''.encode() def test_a_text_row_is_read_by_its_length_in_bytes() -> None: body = {"jobPostings": [{"id": "a", "title": "Baker", "richtextDescription": "$5", "job": {}}, {"id": "b", "title": "Cook", "richtextDescription": "
Inline & done
", "job": {}}]} rows = deel.parse(_page_with(body), "acme") assert [r["description"] for r in rows] == ["Zo\u00eb\u2019s caf\u00e9 \u2014 100 \u20ac", "Inline & done"] assert rows[0]["remote"] is None and rows[0]["location"] == "" and rows[0]["posted"] is None def test_a_board_without_roles_is_an_empty_board_but_another_page_is_not_a_board() -> None: assert deel.parse(_page_with({"jobPostings": []}), "acme") == [] with pytest.raises(ValueError, match="not a Deel job board"): deel.parse(b"