Files
jobboard/tests/test_remote.py
T

555 lines
25 KiB
Python

"""Where a remote role may be worked from: the `remote:` tokens, a remote
role's area in prep, the chips and gates that read it, the model's answer and
migration 27. The place texts are the live boards'."""
from __future__ import annotations
import json
import sqlite3
from collections.abc import Iterator
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
import pytest
from desk.models import Item
from app import places, prep
from app.jobs import extract, reach
from app.models import Agent, Gate
from app.places import Gazetteer, label, known, parse, seed, suggest
from app.prep import words
from app.storage import Store
#: The require gate on country of the owner's agent: the EU, the EEA, the
#: UK, Switzerland and `EU`.
EUROPE = ["AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", "DE", "GR", "HU", "IE",
"IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE", "IS",
"LI", "NO", "GB", "CH", "EU"]
def gazetteer() -> Gazetteer:
"""The seeded gazetteer, as a fresh file holds it."""
seeds, includes = seed()
ids = {(s.kind, s.key): n for n, s in enumerate(seeds, 1)}
return Gazetteer([(ids[(s.kind, s.key)], s.kind, s.key, s.name,
ids[s.parent] if s.parent else None) for s in seeds],
[(a, ids[(s.kind, s.key)]) for s in seeds for a in s.aliases],
[(ids[w], ids[p]) for w, p in includes])
GAZ = gazetteer()
def names(ids: Any) -> list[str]:
return [GAZ.places[p].name for p in ids]
# ------------------------------------------------------------------ tokens
@pytest.mark.parametrize(("token", "shown", "text"), [
("remote:country:DK", "Remote · Denmark", "remote:country:DK"),
("remote:country:dk", "Remote · Denmark", "remote:country:DK"),
("remote:country:EU", "Remote · Europe", "remote:country:EU"),
("remote:region:oresund", "Remote · Øresund", "remote:region:oresund"),
("remote:place:berlin", "Remote · Berlin", "remote:place:berlin"),
("remote:anywhere", "Remote · anywhere", "remote:anywhere"),
("remote:worldwide", "Remote · anywhere", "remote:anywhere"),
("remote:any", "Remote", "remote:any"),
])
def test_a_remote_token_reads_its_place_token(token: str, shown: str, text: str) -> None:
assert label(token) == shown
assert str(parse(token)) == text
assert known(token)
def test_a_remote_token_of_nothing_is_not_known() -> None:
for token in ("remote:", "remote:country:ZZ", "remote:region:atlantis",
"remote:remote:country:DK"):
assert not known(token), token
def test_a_remote_token_is_answered_by_the_areas_only() -> None:
assert places.match_sql("remote:country:DK") == ("0", [])
assert not places.matches("remote denmark", "DK", "remote:country:DK")
@pytest.mark.parametrize(("text", "expected"), [
("Sweden (Remote)", ["Sweden"]), # Grafana
("Remote - US", ["United States"]),
("Home Based - EMEA", ["Europe"]),
("Home Based - Worldwide", ["Anywhere"]), # Canonical
("Remote, Global", ["Anywhere"]),
("Distributed, Global", ["Anywhere"]),
("Cardiff, London or Remote (UK)", ["United Kingdom"]),
("Anywhere in France, Belgium, Spain", ["France", "Belgium", "Spain"]),
("Remote", []),
("Global HQ, Berlin", []),
("Berlin", []),
("Hybrid, Berlin", []),
])
def test_the_parts_that_say_remote_name_the_area(text: str, expected: list[str]) -> None:
assert names(GAZ.remote_places(text)) == expected
def test_anywhere_is_no_place_of_a_role() -> None:
assert GAZ.resolve("Worldwide") == () and GAZ.resolve("Anywhere") == ()
assert names(GAZ.resolve("Global HQ, Berlin")) == ["Berlin"]
assert GAZ.under((GAZ.anywhere,)) == {GAZ.anywhere}
def test_denmarks_overlap_is_what_lies_in_it_what_it_lies_in_and_anywhere() -> None:
denmark = GAZ.by_key[("country", "DK")]
found = GAZ.overlap((denmark,))
for key in (("continent", "europe"), ("region", "oresund"), ("region", "hovedstaden"),
("city", "copenhagen"), places.ANYWHERE):
assert GAZ.by_key[key] in found, key
for key in (("country", "US"), ("country", "SE"), ("region", "skane")):
assert GAZ.by_key[key] not in found, key
def test_a_remote_line_is_no_town() -> None:
lines = [("USA (Remote)", "US", 70), ("Remote US", "US", 60), ("Germany (Remote)", "DE", 61),
("Remote Germany", "DE", 14), ("Remote - Berlin", "DE", 3), ("Berlin", "DE", 80)]
for q in ("usa", "remote", "germ", "berl"):
assert [s.label for s in suggest(q, lines) if s.kind == "place"] \
== (["Berlin"] if q == "berl" else []), q
assert places.towns("USA (Remote)") == frozenset()
# ---------------------------------------------------------------- the words
def test_remote_sensing_in_a_title_is_no_remote_work() -> None:
for title in ("Senior Sea Ice Scientist with Experience on Remote Sensing",
"Remote Control Engineer", "Remote-controlled Systems Lead",
"Remote Monitoring Specialist"):
assert words.remote("jobindex", None, (), "København Ø", title=title) == "", title
assert words.remote("jobindex", None, (), "Aarhus", title="Developer - Remote") == "remote"
# ------------------------------------------------------------------ the area
@pytest.fixture
def store(tmp_path: Path) -> Iterator[Store]:
yield Store(tmp_path / "roledesk.db")
def posting(source_id: str, place: str = "", *, board: str = "greenhouse",
**raw: Any) -> Item:
item = Item(source_id=f"{board}:{source_id}", location=f"https://boards.example/{source_id}",
title="Backend Engineer", body="", published_at=datetime(2026, 9, 20, tzinfo=UTC),
raw={"board": board, "company": "Acme"} | ({"place": place} if place else {})
| raw)
item.source = "ats"
return item
def add(store: Store, source_id: str, place: str = "", **raw: Any) -> str:
item = posting(source_id, place, **raw)
store.upsert_role(item)
return item.uid
#: One role per case of the plan, by what it stands for.
LINES: dict[str, tuple[str, dict[str, Any]]] = {
"grafana": ("Sweden (Remote)", {}),
"canonical": ("Home Based - EMEA; Home Based - Worldwide", {}),
"databricks": ("Denmark", {"remote": True}),
"airtame": ("København, Hovedstaden, Denmark; Remote", {}),
"europe": ("Remote - Europe", {}),
"germany": ("Germany (Remote)", {}),
"us": ("Remote US", {}),
"plain": ("Remote", {}),
"berlin": ("Global HQ, Berlin", {"remote": True}),
"hybrid": ("Hybrid, Copenhagen", {}),
"office": ("Copenhagen", {}),
}
@pytest.fixture
def roles(store: Store) -> dict[str, str]:
found = {key: add(store, key, line, **raw) for key, (line, raw) in LINES.items()}
prep.run(store, dupes=False)
return found
def area(store: Store, uid: str) -> tuple[str, str]:
role = store.role(uid)
assert role is not None
return role.remote, role.remote_where
def test_each_remote_role_gets_the_area_its_places_say(store: Store,
roles: dict[str, str]) -> None:
assert {key: area(store, uid) for key, uid in roles.items()} == {
"grafana": ("remote", "Sweden"),
"canonical": ("remote", "anywhere"), # EMEA and worldwide is worldwide
"databricks": ("remote", "Denmark"), # the host's flag and the place
"airtame": ("remote", "Denmark"), # an office town, read as its country
"europe": ("remote", "Europe"),
"germany": ("remote", "Germany"),
"us": ("remote", "United States"),
"plain": ("remote", ""), # remote, area not stated
"berlin": ("remote", "Germany"),
"hybrid": ("hybrid", ""),
"office": ("", ""),
}
role = store.role(roles["berlin"])
assert role is not None and [p.name for p in role.places] == ["Berlin"]
def test_the_rows_carry_the_area(store: Store, roles: dict[str, str]) -> None:
rows = {r.uid: r.remote_where for r in store.roles_page(limit=100)}
assert rows[roles["canonical"]] == "anywhere" and rows[roles["plain"]] == ""
listed = {r.uid: r.remote_where for r in store.roles(limit=100, by_score=False)}
assert listed == rows
def test_many_places_read_as_their_countries(store: Store) -> None:
uid = add(store, "komma", "Bayern (Remote); Hessen (Remote); Sachsen (Remote); "
"Saarland (Remote); Dänemark (Remote)")
prep.run(store, dupes=False)
assert area(store, uid) == ("remote", "Germany, Denmark")
def uids(store: Store, *tokens: str, agent: int | None = None) -> set[str]:
return {r.uid for r in store.roles(limit=1000, places=list(tokens), agent=agent,
by_score=False)}
def pick(roles: dict[str, str], *keys: str) -> set[str]:
return {roles[k] for k in keys}
def test_remote_denmark_is_every_remote_role_open_to_someone_in_denmark(
store: Store, roles: dict[str, str]) -> None:
assert uids(store, "remote:country:DK") == pick(
roles, "canonical", "databricks", "airtame", "europe")
assert uids(store, "remote:region:hovedstaden") == uids(store, "remote:country:DK")
def test_remote_europe_is_every_remote_role_in_europe(store: Store,
roles: dict[str, str]) -> None:
assert uids(store, "remote:country:EU") == pick(
roles, "grafana", "canonical", "databricks", "airtame", "europe", "germany", "berlin")
assert uids(store, "remote:country:SE") == pick(
roles, "grafana", "canonical", "europe") # Sweden, anywhere, Europe
def test_remote_anywhere_is_only_anywhere(store: Store, roles: dict[str, str]) -> None:
assert uids(store, "remote:anywhere") == pick(roles, "canonical")
assert uids(store, "remote:country:US") == pick(roles, "canonical", "us")
def test_remote_chips_or_with_the_place_chips(store: Store, roles: dict[str, str]) -> None:
assert uids(store, "remote:anywhere", "place:copenhagen") == pick(
roles, "canonical", "airtame", "hybrid", "office")
def test_remote_any_keeps_the_agents_scope(store: Store, roles: dict[str, str]) -> None:
remote = pick(roles, "grafana", "canonical", "databricks", "airtame", "europe", "germany",
"us", "plain", "berlin")
assert uids(store, "remote:any") == remote
agent = store.save_agent("Europe", "python backend", [
Gate(field="country", values=EUROPE, mode="require")])
assert uids(store, "remote:any", agent=agent.id) == remote - pick(roles, "us")
wide = store.save_agent("Anywhere", "python backend", [
Gate(field="country", values=["DK", "SE"], mode="any")])
assert uids(store, "remote:any", agent=wide.id) == remote
def states(store: Store, *gates: Gate) -> dict[str, list[int]]:
agent = store.save_agent(f"gates {len(store.agents())}", "python backend", list(gates))
return {c.uid: c.gates for c in store.score_candidates(agent.id, list(gates))}
def test_remote_from_is_met_as_the_chip_is_and_empty_without_an_area(
store: Store, roles: dict[str, str]) -> None:
found = states(store, Gate(field="remote_from", values=["country:DK"], mode="any"),
Gate(field="remote_from", values=["anywhere"], mode="any"))
by_key = {key: found[uid] for key, uid in roles.items()}
assert by_key == {
"grafana": [0, 0], "canonical": [1, 1], "databricks": [1, 0], "airtame": [1, 0],
"europe": [1, 0], "germany": [0, 0], "us": [0, 0],
"plain": [-1, -1], # remote, area not stated: neither
"berlin": [0, 0],
"hybrid": [0, 0], # evidence: not remote
"office": [-1, -1], # remote unknown
}
def test_a_require_gate_on_europe_reads_a_remote_role_through_its_area(
store: Store, roles: dict[str, str]) -> None:
found = states(store, Gate(field="country", values=EUROPE, mode="require"))
by_key = {key: found[uid][0] for key, uid in roles.items()}
assert by_key["canonical"] == 1 # Home Based - Worldwide
assert by_key["germany"] == 1
assert by_key["europe"] == 1
assert by_key["plain"] == -1 # "Remote" alone: kept, extract reads it
assert by_key["us"] == 0 # Remote US
assert by_key["office"] == 1 and by_key["hybrid"] == 1
def test_anywhere_aliased_by_hand_is_no_place_for_the_country_gate(store: Store) -> None:
uid = add(store, "ww", "Hybrid, Worldwide")
anywhere = store.gazetteer().anywhere
assert anywhere is not None
store.add_place_alias("Worldwide", anywhere)
prep.run(store, dupes=False)
assert [p.name for p in store.role_places(uid)] == ["Anywhere"]
assert area(store, uid) == ("hybrid", "")
found = states(store, Gate(field="country", values=EUROPE, mode="require"))
assert found[uid] == [-1]
def test_the_owners_agent_keeps_remote_roles_open_to_europe_and_drops_remote_us(
store: Store, roles: dict[str, str]) -> None:
from app.jobs.score import failed_gates
gates = [Gate(field="country", values=EUROPE, mode="require"),
Gate(field="country", values=["DK", "SE"], mode="any"),
Gate(field="remote", values=["remote"], mode="any")]
agent = store.save_agent("Owner", "python backend", gates)
kept = {c.uid for c in store.score_candidates(agent.id, gates) if not failed_gates(c, gates)}
assert pick(roles, "canonical", "germany", "europe", "plain") <= kept
assert roles["us"] not in kept
# ------------------------------------------------------------ the model's say
def test_the_models_remote_wins_and_its_names_are_the_area(store: Store) -> None:
# Black Forest Labs: Ashby says OnSite, the text offers remote work with
# an office week a month.
uid = add(store, "bfl", "Freiburg, Germany", board="ashby", remote="OnSite")
prep.run(store, dupes=False)
assert area(store, uid) == ("onsite", "")
store.set_extracted(uid, {"remote": "remote", "remote_where": []})
prep.run_roles(store, [uid])
assert area(store, uid) == ("remote", "Germany")
store.set_extracted(uid, {"remote": "remote", "remote_where": ["Europe"]})
prep.run_roles(store, [uid])
assert area(store, uid) == ("remote", "Europe")
# A later pull leaves the model's answer standing.
store.upsert_role(posting("bfl", "Freiburg, Germany", board="ashby", remote="OnSite"))
prep.run(store, dupes=False)
assert area(store, uid) == ("remote", "Europe")
def test_set_extracted_writes_the_remote_answer_outright(store: Store) -> None:
uid = add(store, "1", "Berlin")
prep.run(store, dupes=False)
store.set_extracted(uid, {"remote": "remote", "remote_where": ["anywhere"]})
row = store._row("SELECT remote_model, remote_where_model, prep_due FROM roles WHERE uid=?",
(uid,))
assert row is not None
assert (row["remote_model"], json.loads(row["remote_where_model"]), row["prep_due"]) == \
("remote", ["anywhere"], 1)
store.set_extracted(uid, {})
row = store._row("SELECT remote_model, remote_where_model FROM roles WHERE uid=?", (uid,))
assert row is not None and (row["remote_model"], row["remote_where_model"]) == ("", "[]")
@pytest.fixture
def config(tmp_path: Path) -> Any:
from app.config import Config
return Config(database=str(tmp_path / "roledesk.db"), extract={"enabled": True})
def test_extract_reads_the_area_once_more_and_maps_the_role_before_the_rescore(
store: Store, config: Any, monkeypatch: pytest.MonkeyPatch) -> None:
from desk import modeltask
from desk.tools import agent as desk_agent
uid = add(store, "1", "Freiburg, Germany", board="ashby", remote="OnSite")
prep.run(store, dupes=False)
agent = store.save_agent("Backend", "python backend")
store.replace_scores(agent.id, [(uid, 1.0, "", "", None, 1.0, "fit")])
with store.transaction() as conn:
conn.execute("UPDATE roles SET extracted_at = '2026-09-22T00:00:00+00:00', "
"seniority_model = 'senior' WHERE uid = ?", (uid,))
assert [r.uid for r in store.extract_waiting(10)] == [uid] # never asked where
def reply(settings: Any, **kwargs: Any) -> desk_agent.Reply:
[one] = kwargs["question"]["roles"]
return desk_agent.Reply(data={"roles": [{
"uid": one["uid"], "place": "", "country": "", "remote": "remote",
"remote_where": ["Denmark", "Sweden"], "seniority": "senior", "salary_min": None,
"salary_max": None, "currency": "", "function": "", "permit": "",
"languages_required": []}]}, backend="api")
monkeypatch.setattr(desk_agent, "backend", lambda settings: "api")
monkeypatch.setattr(desk_agent, "ask_json", reply)
modeltask.run(extract.TASK, config, store)
assert area(store, uid) == ("remote", "Denmark, Sweden")
assert store.extract_waiting(10) == []
def test_the_prompt_asks_where_a_remote_hire_may_live() -> None:
props = extract.SCHEMA["properties"]["roles"]["items"]
assert "remote_where" in props["required"]
assert props["properties"]["remote_where"]["type"] == "array"
assert "office week" in extract.SYSTEM and "remote_where" in extract.SYSTEM
got = extract.fields({"remote": "remote", "remote_where": [" Europe ", "", "Europe", 3]})
assert (got["remote"], got["remote_where"]) == ("remote", ["Europe", "3"])
# ------------------------------------------------------------- suggestions
def test_remote_is_suggested_after_remote_any_and_before_the_regions(
store: Store, roles: dict[str, str]) -> None:
gaz = store.gazetteer()
areas = store.open_remote_areas()
def shown(q: str) -> list[tuple[str, str, str, int]]:
return [(s.kind, s.token, s.label, s.count)
for s in suggest(q, store.open_places(), 20, remote=areas, gaz=gaz,
remote_any=lambda: 9)]
found = shown("remote")
assert found[0] == ("remote", "remote:any", "Remote", 9)
tokens = [t for _, t, _, _ in found]
assert "remote:country:EU" in tokens and "remote:country:US" in tokens
assert "remote:anywhere" not in tokens
assert all(kind == "remote" for kind, _, _, _ in found)
assert ("remote", "remote:country:EU", "Remote · Europe", 7) in found
found = shown("denm")
assert found[:2] == [("country", "country:DK", "Denmark", 4),
("remote", "remote:country:DK", "Remote · Denmark", 4)]
assert shown("remote den")[:2] == [("remote", "remote:any", "Remote", 9),
("remote", "remote:country:DK", "Remote · Denmark", 4)]
assert shown("re") == [t for t in shown("re") if t[1] != "remote:any"]
def test_an_agent_kept_to_europe_is_offered_only_remote_tokens_in_europe(
store: Store, roles: dict[str, str]) -> None:
gaz = store.gazetteer()
europe = {gaz.by_key[("continent", "europe")]}
denmark = {gaz.by_key[("country", "DK")]}
def tokens(q: str, scope: set[int]) -> list[str]:
return [s.token for s in suggest(q, store.open_places(), 50,
remote=store.open_remote_areas(), gaz=gaz, scope=scope)
if s.kind == "remote"]
assert "remote:country:US" in tokens("remote", set())
assert "remote:country:US" not in tokens("remote", europe)
assert "remote:country:EU" in tokens("remote", europe)
assert "remote:country:DK" in tokens("remote den", europe)
# A scope of one country still offers the wider places it lies in.
assert tokens("remote", denmark) == ["remote:country:EU", "remote:country:DK"]
def test_a_suggested_remote_token_finds_the_roles_it_counted(
store: Store, roles: dict[str, str]) -> None:
gaz = store.gazetteer()
for s in suggest("remote", store.open_places(), 50, remote=store.open_remote_areas(),
gaz=gaz):
assert label(s.token) == s.label
assert len(uids(store, s.token)) == s.count, s.token
def test_the_api_suggests_labels_and_filters_the_remote_tokens(tmp_path: Path) -> None:
from fastapi.testclient import TestClient
from app.config import Config, ServerConfig
from app.main import create_app
app = create_app(Config(server=ServerConfig(host="127.0.0.1", port=0),
database=tmp_path / "roledesk.db"))
store: Store = app.state.store
for key in ("europe", "us", "plain"):
line, raw = LINES[key]
add(store, key, line, **raw)
prep.run(store, dupes=False)
agent = store.save_agent("Europe", "python backend", [
Gate(field="country", values=EUROPE, mode="require")])
with TestClient(app) as client:
found = client.get("/api/suggest/places", params={"q": "remote"}).json()
assert found[0] == {"token": "remote:any", "label": "Remote", "kind": "remote",
"count": 2}
assert {"token": "remote:country:EU", "label": "Remote · Europe", "kind": "remote",
"count": 1} in found
other = client.get("/api/suggest/places", params={"q": "remote", "agent": agent.id})
assert other.json()[0]["count"] == 2
tokens = [s["token"] for s in other.json()]
assert "remote:country:EU" in tokens and "remote:country:US" not in tokens
labels = client.get("/api/places/labels", params=[
("tokens", "remote:country:DK"), ("tokens", "remote:anywhere"),
("tokens", "remote:country:ZZ")]).json()
assert labels == [{"token": "remote:country:DK", "label": "Remote · Denmark"},
{"token": "remote:anywhere", "label": "Remote · anywhere"}]
rows = client.get("/api/roles", params={"places": "remote:country:DK"}).json()
assert [r["remote_where"] for r in rows["items"]] == ["Europe"]
assert client.get("/api/roles", params={"places": "remote:country:ZZ"}).status_code \
== 422
# ------------------------------------------------------------------ reach
def test_remote_from_is_remote_reach_and_never_widens_the_require_reach() -> None:
gaz = GAZ
near = Gate(field="remote_from", values=["country:DK"], mode="any")
assert reach.gate_reach(near, gaz) == reach.Reach(remote=True)
owner = Agent(id=2, name="Owner", created_at="2026-09-30", gates=[
Gate(field="country", values=EUROPE, mode="require"),
Gate(field="country", values=["DK", "SE"], mode="any"), near,
Gate(field="remote_from", values=["country:DK"], mode="require")])
assert reach.any_reach(owner, gaz) == reach.Reach(frozenset({"DK", "SE"}), remote=True)
wide = reach.require_reach(owner, gaz)
assert not wide.everywhere and not wide.remote and "DE" in wide.countries
# --------------------------------------------------------------- migration
def test_migration_27_keeps_an_older_readers_remote_as_the_hosts_word(tmp_path: Path) -> None:
path = tmp_path / "old.db"
store = Store(path)
board = add(store, "board", "Ireland")
search = Item(source_id="jobindex:1", location="https://jobindex.example/1",
title="Scientist with Experience on Remote Sensing",
body="", published_at=datetime(2026, 9, 20, tzinfo=UTC),
raw={"provider": "jobindex", "place": "København Ø", "remote": None})
search.source = "search"
store.upsert_role(search)
with sqlite3.connect(path) as conn:
conn.execute("UPDATE roles SET remote = 'remote', remote_host = '' WHERE uid = ?",
(board,))
conn.execute("UPDATE roles SET remote = 'remote', remote_host = 'remote' "
"WHERE uid = ?", (search.uid,))
conn.execute("DROP INDEX idx_role_remote_place")
conn.execute("DROP TABLE role_remote")
conn.execute("ALTER TABLE roles DROP COLUMN remote_model")
conn.execute("ALTER TABLE roles DROP COLUMN remote_where_model")
conn.execute("DELETE FROM places WHERE kind = 'continent' AND key = 'world'")
conn.execute("PRAGMA user_version = 26")
migrated = Store(path)
assert migrated.migrations_applied == [27, 28, 29, 30, 31, 32, 33]
assert migrated.gazetteer().anywhere is not None
row = migrated._row("SELECT remote_host, remote_model, remote_where_model FROM roles "
"WHERE uid = ?", (board,))
assert row is not None and tuple(row) == ("remote", "", None)
prep.run(migrated, dupes=False)
assert area(migrated, board) == ("remote", "Ireland")
assert area(migrated, search.uid) == ("", "")
with sqlite3.connect(path) as conn:
conn.execute("PRAGMA user_version = 26")
assert Store(path).migrations_applied == [27, 28, 29, 30, 31, 32, 33]
def test_a_role_waits_for_extract_until_it_was_asked_where(store: Store) -> None:
uid = add(store, "1", "Berlin")
prep.run(store, dupes=False)
with store.transaction() as conn:
conn.execute("UPDATE roles SET extracted_at = '2026-09-22', seniority_model = 'mid' "
"WHERE uid = ?", (uid,))
assert [r.uid for r in store.extract_waiting(5, [uid])] == [uid]
store.set_extracted(uid, {"seniority": "mid"})
assert store.extract_waiting(5, [uid]) == []