"""`app.dupes` against postings as boards really wrote them. Pure: no store, no network. The live cases are the 2026-09-29 database's.""" from __future__ import annotations import sys from dataclasses import replace from pathlib import Path ROOT = Path(__file__).resolve().parents[1] if str(ROOT) not in sys.path: sys.path.insert(0, str(ROOT)) import pytest # noqa: E402 from app.dupes import ( # noqa: E402 Copy, assign, fold, keep_order, place_line, same_posting, similar, ) from app.places import towns # noqa: E402 def cp(uid: str = "u", company: str = "acme", title: str = "engineer", country: str = "DE", place: str = "", host: str = "greenhouse", desc: bool = True, posted: str | None = None) -> Copy: return Copy(uid, company, title, country, place, host, desc, posted) #: `(company_key, title_key, country, host_a, place_a, host_b, place_b, kept host)`. LIVE = [ ("ericsson", "entry level 6g developer", "SE", "platsbanken", "Lund", "eightfold", "Lund, Skåne, Sweden", "eightfold"), ("gn-group", "machine learning engineer", "DK", "jobindex", "Ballerup", "workday", "Ballerup, Denmark", "workday"), ("kmd", "student worker finance", "DK", "jobindex", "Ballerup, Ballerup, Aalborg, Aalborg", "successfactors", "Ballerup, Denmark", "successfactors"), ("trackunit", "machine learning engineer", "DK", "jobindex", "Aalborg, Copenhagen, Kolding, Aarhus, Hybrid position", "teamtailor", "København, Denmark; Aarhus, Denmark; Kolding, Denmark; Aalborg, Denmark", "teamtailor"), ] @pytest.mark.parametrize("company,title,country,ha,pa,hb,pb,kept", LIVE) def test_live_cross_host_pairs(company, title, country, ha, pa, hb, pb, kept): a = cp("a", company, title, country, pa, ha) b = cp("b", company, title, country, pb, hb) assert same_posting(a, b) and same_posting(b, a) result = assign([a, b]) assert result == ({"a": "b", "b": None} if kept == hb else {"a": None, "b": "a"}) def test_sap_requisitions(): place = "Garching bei München (Munich), Germany" title = "forward deployed chief ai engineering expert" rows = [ cp("g3", "sap", title, "DE", place, "successfactors", False, "2026-09-02"), cp("g1", "sap", title, "DE", place, "successfactors", False, "2026-09-01"), cp("g2", "sap", title, "DE", place, "successfactors", False, "2026-09-01"), cp("ny", "sap", title, "US", "New York, United States", "successfactors", True, "2026-08-01"), ] # The earliest posted_at, then the smallest uid, is kept; New York is alone # although it is the only row with a description. assert assign(rows) == {"g1": None, "g2": "g1", "g3": "g1", "ny": None} def test_houston_state_code(): a = cp("a", "sap", "x", "US", "Houston, TX, United States") b = cp("b", "sap", "x", "US", "Houston, United States") assert same_posting(a, b) def test_copenhagen_district_and_remote_phrase(): a = cp("a", "acme", "x", "DK", "København K og mulighed for hjemmearbejde") b = cp("b", "acme", "x", "DK", "Copenhagen, Denmark") assert towns(a.place) == {"copenhagen"} assert same_posting(a, b) def test_different_towns_are_not_the_same(): assert not same_posting(cp("a", place="Berlin, Germany"), cp("b", place="Munich, Germany")) assert assign([cp("a", place="Berlin"), cp("b", place="Munich")]) == {"a": None, "b": None} def test_empty_place_matches(): assert same_posting(cp("a", place=""), cp("b", place="Berlin")) assert same_posting(cp("a", place=""), cp("b", place="")) assert same_posting(cp("a", place="Germany"), cp("b", place="Berlin, Germany")) def test_empty_company_never_grouped(): a, b = cp("a", company=""), cp("b", company="") assert not same_posting(a, b) assert assign([a, b]) == {"a": None, "b": None} def test_other_company_or_title_is_not_the_same(): assert not same_posting(cp("a"), cp("b", company="other")) assert not same_posting(cp("a"), cp("b", title="designer")) def test_country_rules(): assert not same_posting(cp("a", country="DE", place="Berlin"), cp("b", country="US", place="Berlin")) assert same_posting(cp("a", country="", place="Berlin"), cp("b", country="DE", place="Berlin, Germany")) assert not same_posting(cp("a", country="", place="Berlin"), cp("b", country="DE", place="Munich")) @pytest.mark.parametrize("place,country,expected", [ ("Lund, Skåne, Sweden", "SE", {"lund"}), ("Houston, TX, United States", "US", {"houston"}), ("New York, United States", "US", {"new york"}), ("Aarhus C", "DK", {"aarhus"}), ("Remote Berlin", "DE", {"berlin"}), ("Aalborg, Copenhagen, Kolding, Aarhus, Hybrid position", "DK", {"aalborg", "copenhagen", "kolding", "aarhus"}), ("Berlin und Munich", "DE", {"berlin", "munich"}), ("Mountain View, California", "US", {"mountain view"}), ("Ningbo, Zhejiang, China", "CN", {"ningbo"}), ("Germany", "DE", set()), ("Remote", "", set()), ("", "", set()), ]) def test_towns(place, country, expected): # A head `places` cannot parse, "remote berlin", may come along; it matches # nothing, so only the towns that must be there and the empty case count. found = towns(place) assert found >= expected and (expected or not found) def test_a_state_or_province_is_not_a_shared_town(): # Live false merges before `places.towns`: Databricks and Bosch. assert not same_posting(cp("a", country="US", place="Mountain View, California"), cp("b", country="US", place="San Francisco, California")) assert not same_posting(cp("a", country="CN", place="Ningbo, Zhejiang, China"), cp("b", country="CN", place="Hangzhou, Zhejiang, China")) def test_assign_does_not_chain(): # A matches B by Berlin, B matches C by Hamburg, A and C do not match, so A # cannot share C's group. a = cp("a", place="Berlin", posted="2026-09-03") b = cp("b", place="Berlin, Hamburg", posted="2026-09-02") c = cp("c", place="Hamburg", posted="2026-09-01") assert not same_posting(a, c) assert assign([a, b, c]) == {"a": None, "b": "c", "c": None} def test_assign_returns_every_uid(): rows = [cp("a"), cp("b", title="other"), cp("c", company="")] assert assign(rows) == {"a": None, "b": None, "c": None} assert assign([]) == {} def test_assign_separate_groups_in_one_partition(): rows = [cp("a", place="Berlin"), cp("b", place="Berlin"), cp("c", place="Munich"), cp("d", place="Munich")] assert assign(rows) == {"a": None, "b": "a", "c": None, "d": "c"} def test_keep_order_ranks(): board = cp("z", host="workday", desc=False, posted="2026-09-09") search = cp("a", host="jobindex", desc=True, posted="2026-01-01") assert min([search, board], key=keep_order) is board with_desc = cp("z", desc=True, posted="2026-09-09") without = cp("a", desc=False, posted="2026-01-01") assert min([without, with_desc], key=keep_order) is with_desc early = cp("z", posted="2026-01-01") late = cp("a", posted="2026-02-01") undated = cp("0", posted=None) assert sorted([undated, late, early], key=keep_order) == [early, late, undated] assert min([cp("b", posted="2026-01-01"), cp("a", posted="2026-01-01")], key=keep_order).uid == "a" def test_every_search_provider_loses_to_a_board(search_stubs): from app.dupes import is_search_host assert all(is_search_host(h) for h in ("jobindex", "adzuna", "platsbanken", "freehire", "jobnet", "jobbank")) assert not any(is_search_host(h) for h in ("greenhouse", "workday", "teamtailor", "")) board = cp("z", host="greenhouse", desc=False, posted="2026-09-09") for host in ("freehire", "jobnet", "jobbank"): search = cp("a", host=host, desc=True, posted="2026-01-01") assert min([search, board], key=keep_order) is board def test_a_row_without_a_town_joins_only_one_group(): # "Germany" matches Berlin and Munich; joining both would make them one role. rows = [cp("a", place="Berlin"), cp("b", place="Munich"), cp("c", place="Germany")] assert assign(rows) == {"a": None, "b": None, "c": None} rows = [cp("a", place="Berlin"), cp("b", place="Berlin"), cp("c", place="")] assert assign(rows) == {"a": None, "b": "a", "c": "a"} rows = [cp("a", place=""), cp("b", place="Germany")] assert assign(rows) == {"a": None, "b": "a"} def test_a_row_without_a_place_never_bridges_two_countries(): # Live: LEGO "Sales Associate", three rows with no place, one Belgium, one # Netherlands, `country` empty on all five until the place names one. rows = [cp("a", country="", place=""), cp("b", country="", place=""), cp("c", country="", place=""), cp("d", country="BE", place="Belgium"), cp("e", country="NL", place="Netherlands")] got = assign(rows) assert got["d"] is None and got["e"] is None assert got == {"a": None, "b": "a", "c": "a", "d": None, "e": None} assert not same_posting(cp("a", country="EU", place="Belgium"), cp("b", country="EU", place="Netherlands")) def pc(uid: str, place: str, text: str = "", host: str = "greenhouse", url: str = "u", title: str = "engineer") -> Copy: return Copy(uid, "acme", title, "DE", place, host, bool(text), None, text, url or "") def test_one_posting_per_place_folds_into_the_first_and_only_within_a_host() -> None: text = "We build the payment platform for our bank customers in Kotlin." got = fold([pc("b", "Berlin", text), pc("m", "Munich", text), pc("h", "Hamburg", text), pc("x", "Kiel", text, host="lever")]) assert got.dup_of == {"b": None, "m": "b", "h": "b", "x": None} assert got.placed == {"m", "h"} def test_folding_needs_the_same_title_a_link_and_texts_that_can_be_one_job() -> None: a = "Build payment services in Kotlin for our bank customers and their teams." b = "Negotiate enterprise cloud contracts with procurement and legal buyers." got = fold([pc("1", "Berlin", a), pc("2", "Munich", b), pc("3", "Hamburg", a, url=""), pc("4", "Kiel", a, title="manager")]) assert got.dup_of == {"1": None, "2": None, "3": None, "4": None} assert fold([pc("1", "Berlin", a), pc("2", "Munich", "")]).placed == {"2"} def test_a_remote_listing_is_not_folded_into_an_office_one() -> None: """The kept role takes a copy's places, not its remote, so the gate would read "Remote, Germany" as an office role in Vienna and reject it for an agent that takes remote roles. Two remote listings still fold.""" text = "We build the payment platform for our bank customers in Kotlin." office = replace(pc("v", "Vienna", text), country="AT") remote = replace(pc("r", "Remote, Germany", text), remote=True) other = replace(pc("s", "Remote, Spain", text), country="ES", remote=True) got = fold([office, remote, other]) assert got.dup_of == {"v": None, "r": None, "s": "r"} assert got.placed == {"s"} def test_a_copy_of_a_folded_row_follows_it_to_the_new_kept_role() -> None: """Berlin is kept over Munich; a second Munich row that was Munich's copy now points at Berlin, and is not a place of its own.""" text = "We build the payment platform for our bank customers in Kotlin." same = fold([pc("b", "Berlin", text), pc("m", "Munich", text), pc("m2", "Munich, Germany", text)]) assert same.dup_of == {"b": None, "m": "b", "m2": "b"} assert same.placed == {"m"} # m2 is m's own copy, not another place def test_similar_and_place_line() -> None: assert similar("", "anything at all") and similar("a job in Kotlin", "a job in Kotlin") assert not similar("sell cloud contracts to buyers", "build kotlin payment services") assert place_line(["Berlin", "Munich", "Berlin", "Hamburg"]) == "Berlin +2" assert place_line(["Berlin", " "]) == "Berlin" and place_line([]) == ""