A search agent is a persona, and every like or pass belongs to the one being viewed. The rail now opens on Viewing as: a full-width chooser, the name alone with one agent, a note with none, remembered in the URL and localStorage. The vote buttons, the reader's affinity control, the feed, the tag counts and the reader fetch all carry that agent; before, votes sent none and every one landed on the default agent. Best first and Newest first are back as the segmented control under Order, independent of the agent. The rail selects no longer clip descenders: Chrome ignores line-height on a select, so the fixed box is gone in favour of padding and a min-height. Settings render inside a sticky rail (Search agents; Sources with Job boards, Company boards, Runs, Messages; Account), a strip under the header on a phone, so Runs is one click away however long the company list gets. Sources shows one section at a time, chosen by the fragment, so the old redirects still land. Search agents are cards, newest first: name, an On/Off switch with its word, the intent in full, the weights, Edit in place, New agent on top, and a link to Roles as that agent. Switching one on rescores it. replace_scores writes nothing for an agent deleted while score was running, instead of failing the run on the foreign key. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
753 lines
33 KiB
Python
753 lines
33 KiB
Python
"""The store: schema, role upserts, closure, companies, subscriptions, the tracker."""
|
||
|
||
from __future__ import annotations
|
||
|
||
import sqlite3
|
||
from collections.abc import Iterator
|
||
from datetime import UTC, datetime, timedelta
|
||
from pathlib import Path
|
||
|
||
import pytest
|
||
from pydantic import ValidationError
|
||
|
||
from desk.models import Item
|
||
from app.models import Application, company_key, place_key, title_key
|
||
from app.storage import LATEST, Store
|
||
|
||
|
||
class Clock:
|
||
def __init__(self) -> None:
|
||
self.at = datetime(2026, 9, 23, 9, 0, tzinfo=UTC)
|
||
|
||
def __call__(self) -> datetime:
|
||
return self.at
|
||
|
||
def tick(self, hours: float = 1) -> None:
|
||
self.at += timedelta(hours=hours)
|
||
|
||
|
||
@pytest.fixture
|
||
def clock() -> Clock:
|
||
return Clock()
|
||
|
||
|
||
@pytest.fixture
|
||
def store(tmp_path: Path, clock: Clock) -> Iterator[Store]:
|
||
s = Store(tmp_path / "roledesk.db")
|
||
s.clock = clock
|
||
yield s
|
||
|
||
|
||
def posting(source_id: str = "123", **raw: object) -> Item:
|
||
item = Item(source_id=source_id, location=f"https://boards.example/acme/{source_id}",
|
||
title="Senior Backend Engineer (m/w/d)", body="We build things.",
|
||
published_at=datetime(2026, 9, 20, tzinfo=UTC), raw=dict(raw))
|
||
item.source = "ats"
|
||
return item
|
||
|
||
|
||
# ------------------------------------------------------------------ schema
|
||
|
||
def test_schema_creates_on_an_empty_file(tmp_path: Path) -> None:
|
||
path = tmp_path / "new.db"
|
||
store = Store(path)
|
||
with sqlite3.connect(path) as conn:
|
||
tables = {r[0] for r in conn.execute(
|
||
"SELECT name FROM sqlite_master WHERE type='table'")}
|
||
assert conn.execute("PRAGMA user_version").fetchone()[0] == LATEST
|
||
assert conn.execute("PRAGMA journal_mode").fetchone()[0] == "wal"
|
||
indexes = {r[0] for r in conn.execute(
|
||
"SELECT name FROM sqlite_master WHERE type='index'")}
|
||
assert {"roles", "companies", "subscriptions", "applications",
|
||
"application_events", "agents", "scores", "state"} <= tables
|
||
assert "idx_roles_cluster" in indexes
|
||
assert store.migrations_applied == []
|
||
# A second open finds it current and applies nothing.
|
||
assert Store(path).migrations_applied == []
|
||
|
||
|
||
def test_a_newer_file_is_refused(tmp_path: Path) -> None:
|
||
path = tmp_path / "future.db"
|
||
Store(path)
|
||
with sqlite3.connect(path) as conn:
|
||
conn.execute(f"PRAGMA user_version = {LATEST + 1}")
|
||
with pytest.raises(RuntimeError, match=r"Upgrade roledesk"):
|
||
Store(path)
|
||
|
||
|
||
def _columns(path: Path, table: str) -> list[tuple[str, str, int, str]]:
|
||
with sqlite3.connect(path) as conn:
|
||
return [(r[1], r[2], r[3], r[4]) for r in conn.execute(f"PRAGMA table_info({table})")]
|
||
|
||
|
||
def test_a_v1_file_migrates_to_the_shape_a_new_file_has(tmp_path: Path) -> None:
|
||
fresh, old = tmp_path / "fresh.db", tmp_path / "old.db"
|
||
Store(fresh)
|
||
Store(old)
|
||
with sqlite3.connect(old) as conn:
|
||
for column in ("permit", "languages", "extract_tries", "summary", "summarised_at",
|
||
"article_text", "article_at", "read_at"):
|
||
conn.execute(f"ALTER TABLE roles DROP COLUMN {column}")
|
||
for column in ("intent_key", "intent_vec"):
|
||
conn.execute(f"ALTER TABLE agents DROP COLUMN {column}")
|
||
# v1 had affinity on the company; the vote columns v4 added and v6
|
||
# drops again are not in a fresh file, so there is nothing to put back.
|
||
conn.execute("ALTER TABLE companies ADD COLUMN affinity INTEGER NOT NULL DEFAULT 0")
|
||
for table in ("vectors", "role_tags", "agent_companies", "role_votes"):
|
||
conn.execute(f"DROP TABLE {table}")
|
||
conn.execute("PRAGMA user_version = 1")
|
||
|
||
assert Store(old).migrations_applied == list(range(2, LATEST + 1))
|
||
# ALTER appends, so the order differs; every read is by column name.
|
||
for table in ("roles", "agents", "vectors", "companies", "role_tags",
|
||
"agent_companies", "role_votes"):
|
||
assert sorted(_columns(old, table)) == sorted(_columns(fresh, table))
|
||
|
||
|
||
def _downgrade_to_v4(path: Path) -> None:
|
||
"""A fresh file put back to the v4 shape: votes on the role, affinity on
|
||
the company, no read state, no tags, no per-agent tables."""
|
||
with sqlite3.connect(path) as conn:
|
||
conn.execute("ALTER TABLE roles DROP COLUMN read_at")
|
||
conn.execute("DROP TABLE role_tags")
|
||
conn.execute("ALTER TABLE roles ADD COLUMN vote INTEGER CHECK (vote IN (-1, 0, 1))")
|
||
conn.execute("ALTER TABLE roles ADD COLUMN voted_at TEXT")
|
||
conn.execute("ALTER TABLE companies ADD COLUMN affinity INTEGER NOT NULL DEFAULT 0")
|
||
conn.execute("DROP TABLE agent_companies")
|
||
conn.execute("DROP TABLE role_votes")
|
||
conn.execute("PRAGMA user_version = 4")
|
||
|
||
|
||
def test_v6_moves_the_votes_and_affinities_to_the_default_agent(tmp_path: Path) -> None:
|
||
path = tmp_path / "v4.db"
|
||
store = Store(path)
|
||
store.upsert_role(posting("liked"))
|
||
store.upsert_role(posting("passed"))
|
||
store.upsert_role(posting("none"))
|
||
store.add_company("Acme", key="acme")
|
||
store.add_company("Other", key="other")
|
||
# The disabled agent has the lower id; the enabled one is the default.
|
||
second = store.save_agent("Second", enabled=False)
|
||
first = store.save_agent("First")
|
||
assert first.id > second.id and store.default_agent().id == first.id
|
||
_downgrade_to_v4(path)
|
||
with sqlite3.connect(path) as conn:
|
||
conn.execute("UPDATE roles SET vote = 1, voted_at = 'then' WHERE uid = ?",
|
||
(posting("liked").uid,))
|
||
conn.execute("UPDATE roles SET vote = -1 WHERE uid = ?", (posting("passed").uid,))
|
||
conn.execute("UPDATE companies SET affinity = -1 WHERE key = 'acme'")
|
||
|
||
migrated = Store(path)
|
||
|
||
assert migrated.migrations_applied == [5, 6]
|
||
assert [a.name for a in migrated.agents()] == ["First", "Second"] # none added
|
||
assert migrated.votes(first.id) == {posting("liked").uid: 1, posting("passed").uid: -1}
|
||
assert migrated.votes(second.id) == {}
|
||
liked = migrated.role(posting("liked").uid, first.id)
|
||
assert liked is not None and liked.voted_at == "then"
|
||
passed = migrated.role(posting("passed").uid, first.id)
|
||
assert passed is not None and passed.voted_at # stamped when v4 had none
|
||
assert [c.affinity for c in migrated.companies(first.id)] == [-1, 0]
|
||
assert [c.affinity for c in migrated.companies(second.id)] == [0, 0]
|
||
with sqlite3.connect(path) as conn:
|
||
columns = {r[1] for r in conn.execute("PRAGMA table_info(roles)")}
|
||
assert not {"vote", "voted_at"} & columns
|
||
assert "affinity" not in {r[1] for r in conn.execute("PRAGMA table_info(companies)")}
|
||
|
||
|
||
def test_v6_makes_an_agent_when_there_is_something_to_move_and_none_to_take_it(
|
||
tmp_path: Path) -> None:
|
||
path = tmp_path / "v4.db"
|
||
store = Store(path)
|
||
store.upsert_role(posting("liked"))
|
||
store.add_company("Acme", key="acme")
|
||
_downgrade_to_v4(path)
|
||
with sqlite3.connect(path) as conn:
|
||
conn.execute("UPDATE companies SET affinity = 2 WHERE key = 'acme'")
|
||
|
||
migrated = Store(path)
|
||
|
||
[agent] = migrated.agents()
|
||
assert (agent.name, agent.enabled, agent.gates) == ("My ranking", True, [])
|
||
assert agent.created_at.endswith("+00:00")
|
||
assert migrated.default_agent() == agent
|
||
assert migrated.company("acme", agent.id).affinity == 2 # type: ignore[union-attr]
|
||
|
||
|
||
def test_v6_makes_no_agent_when_there_is_nothing_to_move(tmp_path: Path) -> None:
|
||
path = tmp_path / "v4.db"
|
||
store = Store(path)
|
||
store.upsert_role(posting("plain"))
|
||
store.add_company("Acme", key="acme")
|
||
_downgrade_to_v4(path)
|
||
|
||
assert Store(path).migrations_applied == [5, 6]
|
||
assert Store(path).agents() == []
|
||
|
||
|
||
def test_a_v4_file_migrates_read_state_and_tags(tmp_path: Path) -> None:
|
||
fresh, old = tmp_path / "fresh.db", tmp_path / "old.db"
|
||
Store(fresh)
|
||
Store(old)
|
||
_downgrade_to_v4(old)
|
||
|
||
assert Store(old).migrations_applied == [5, 6]
|
||
assert sorted(_columns(old, "roles")) == sorted(_columns(fresh, "roles"))
|
||
with sqlite3.connect(old) as conn:
|
||
tables = {r[0] for r in conn.execute(
|
||
"SELECT name FROM sqlite_master WHERE type='table'")}
|
||
assert "role_tags" in tables
|
||
|
||
|
||
def test_state_is_a_mapping(store: Store) -> None:
|
||
store.state["ats:last"] = "2026-09-23"
|
||
assert store.state["ats:last"] == "2026-09-23"
|
||
assert dict(store.state) == {"ats:last": "2026-09-23"}
|
||
del store.state["ats:last"]
|
||
assert "ats:last" not in store.state
|
||
|
||
|
||
# ------------------------------------------------------------------- keys
|
||
|
||
def test_keys() -> None:
|
||
assert title_key("Senior Backend Engineer (m/w/d)") == "senior backend engineer"
|
||
assert title_key("Data Scientist (f/m/x) – Berlin") == "data scientist berlin" # noqa: RUF001
|
||
assert title_key("Product Designer m/w/d") == "product designer"
|
||
assert title_key("Engineer (all genders)") == "engineer"
|
||
assert place_key("Zürich, Switzerland") == "zurich switzerland"
|
||
assert company_key("Trade Republic Bank GmbH") == "trade-republic-bank"
|
||
assert company_key("Celonis SE") == company_key("celonis") == "celonis"
|
||
|
||
|
||
# ------------------------------------------------------------------- roles
|
||
|
||
def test_upsert_twice_keeps_first_seen_and_bumps_last_seen(
|
||
store: Store, clock: Clock) -> None:
|
||
assert store.upsert_role(posting(company="Acme GmbH", place="Berlin")) is True
|
||
first = store.role(posting().uid)
|
||
assert first is not None
|
||
clock.tick()
|
||
assert store.upsert_role(posting(company="Acme GmbH", place="Berlin")) is False
|
||
again = store.role(posting().uid)
|
||
assert again is not None
|
||
assert again.first_seen == first.first_seen == first.created_at
|
||
assert again.last_seen > first.last_seen
|
||
assert (again.company_key, again.title_key, again.place_key) == \
|
||
("acme", "senior backend engineer", "berlin")
|
||
|
||
|
||
def test_upsert_unpacks_raw_and_keeps_filled_columns(store: Store) -> None:
|
||
store.upsert_role(posting(place="Berlin", remote=True, salary_min="80000",
|
||
salary_max=95000, currency="EUR", department="Eng"))
|
||
role = store.role(posting().uid)
|
||
assert role is not None
|
||
assert (role.remote, role.salary_min, role.salary_max, role.currency) == \
|
||
("remote", 80000, 95000, "EUR")
|
||
assert role.raw["department"] == "Eng"
|
||
assert role.description == "We build things."
|
||
assert role.original_url.endswith("/123")
|
||
# A later fetch saying less does not erase what was known.
|
||
store.upsert_role(posting())
|
||
role = store.role(posting().uid)
|
||
assert role is not None
|
||
assert (role.place, role.remote, role.salary_min) == ("Berlin", "remote", 80000)
|
||
|
||
|
||
def test_company_falls_back_to_the_subscription(store: Store) -> None:
|
||
sub = store.add_subscription("ats", "greenhouse:acme", company="Acme", board="Greenhouse")
|
||
store.upsert_role(posting(), sub)
|
||
role = store.role(posting().uid)
|
||
assert role is not None
|
||
assert (role.company, role.company_key, role.subscription_id) == ("Acme", "acme", sub.id)
|
||
|
||
|
||
def test_roles_newest_first_and_filtered(store: Store, clock: Clock) -> None:
|
||
store.upsert_role(posting("1", company="Acme", place="Berlin"))
|
||
clock.tick()
|
||
store.upsert_role(posting("2", company="Acme", place="Munich"))
|
||
clock.tick()
|
||
store.upsert_role(posting("3", company="Other", place="Berlin"))
|
||
assert [r.source_id for r in store.roles()] == ["3", "2", "1"]
|
||
assert [r.source_id for r in store.roles(company="ACME")] == ["2", "1"]
|
||
assert [r.source_id for r in store.roles(place="berlin")] == ["3", "1"]
|
||
assert [r.source_id for r in store.roles(limit=1)] == ["3"]
|
||
|
||
|
||
def test_roles_filters_q_function_remote_and_days(store: Store, clock: Clock) -> None:
|
||
store.upsert_role(posting("1", company="Acme", place="Berlin",
|
||
function="engineering", remote=True))
|
||
clock.tick(hours=48)
|
||
store.upsert_role(posting("2", company="Acme", place="Munich",
|
||
function="sales"))
|
||
assert [r.source_id for r in store.roles(q="senior engineer")] == ["2", "1"]
|
||
assert [r.source_id for r in store.roles(q="backend nonsense")] == []
|
||
assert [r.source_id for r in store.roles(function="engineering")] == ["1"]
|
||
assert [r.source_id for r in store.roles(remote="remote")] == ["1"]
|
||
assert [r.source_id for r in store.roles(company="ac")] == ["2", "1"]
|
||
assert [r.source_id for r in store.roles(place="berlin")] == ["1"]
|
||
assert [r.source_id for r in store.roles(days=1)] == ["2"]
|
||
assert [r.source_id for r in store.roles(days=30)] == ["2", "1"]
|
||
|
||
|
||
def test_roles_hide_muted_excludes_before_the_limit(store: Store, clock: Clock) -> None:
|
||
agent = store.save_agent("A")
|
||
store.add_company("Acme", key="acme")
|
||
store.set_affinity(agent.id, "acme", -1)
|
||
store.upsert_role(posting("other", company="Other"))
|
||
clock.tick()
|
||
for i in range(3):
|
||
store.upsert_role(posting(f"a{i}", company="Acme"))
|
||
clock.tick()
|
||
# Acme's roles are the three newest, so a plain limit=1 shows only Acme;
|
||
# hide_muted has to reach past all three to find "other" rather than
|
||
# just dropping Acme's rows out of an already-limited page.
|
||
listing = {"agent": agent.id, "by_score": False}
|
||
assert [r.source_id for r in store.roles(limit=1, **listing)] == ["a2"]
|
||
assert [r.source_id for r in store.roles(limit=1, hide_muted=True, **listing)] == ["other"]
|
||
assert {r.source_id for r in store.roles(hide_muted=True, **listing)} == {"other"}
|
||
assert {r.source_id for r in store.roles(**listing)} == {"a0", "a1", "a2", "other"}
|
||
# Hidden is per agent: another agent, or no agent, hides nothing.
|
||
other = store.save_agent("B")
|
||
assert store.count_roles(hide_muted=True, agent=other.id, by_score=False) == 4
|
||
assert store.count_roles(hide_muted=True) == 4
|
||
|
||
|
||
def test_roles_page_by_offset_and_count_the_same_filter(
|
||
store: Store, clock: Clock) -> None:
|
||
for sid in ("1", "2", "3", "4"):
|
||
store.upsert_role(posting(sid, company="Acme" if sid != "4" else "Other"))
|
||
clock.tick()
|
||
assert [r.source_id for r in store.roles(limit=2, offset=1)] == ["3", "2"]
|
||
assert [r.source_id for r in store.roles(limit=2, offset=3)] == ["1"]
|
||
assert store.count_roles() == 4
|
||
assert store.count_roles(company="acme") == 3
|
||
agent = store.save_agent("A")
|
||
store.add_company("Other", key="other")
|
||
store.set_affinity(agent.id, "other", -1)
|
||
assert store.count_roles(hide_muted=True, agent=agent.id, by_score=False) == 3
|
||
|
||
|
||
def test_a_vote_is_stamped_cleared_and_filtered(store: Store, clock: Clock) -> None:
|
||
for sid in ("up", "down", "fine", "none"):
|
||
store.upsert_role(posting(sid))
|
||
uid = {r.source_id: r.uid for r in store.roles()}
|
||
agent = store.save_agent("A")
|
||
role = store.set_vote(agent.id, uid["up"], 1)
|
||
assert (role.vote, role.voted_at) == (1, clock().isoformat(timespec="seconds"))
|
||
store.set_vote(agent.id, uid["down"], -1)
|
||
store.set_vote(agent.id, uid["fine"], 0)
|
||
assert store.votes(agent.id) == {uid["up"]: 1, uid["down"]: -1, uid["fine"]: 0}
|
||
|
||
def listed(judged: str) -> list[str]:
|
||
return [r.source_id for r in store.roles(agent=agent.id, by_score=False,
|
||
judged=judged)]
|
||
|
||
assert listed("liked") == ["up"]
|
||
assert listed("passed") == ["down"]
|
||
assert listed("unjudged") == ["none"]
|
||
assert store.count_roles(agent=agent.id, by_score=False, judged="unjudged") == 1
|
||
with pytest.raises(ValueError, match="judged"):
|
||
store.roles(judged="maybe")
|
||
|
||
clock.tick()
|
||
changed = store.set_vote(agent.id, uid["up"], -1)
|
||
assert (changed.vote, changed.voted_at) == (-1, clock().isoformat(timespec="seconds"))
|
||
cleared = store.set_vote(agent.id, uid["up"], None)
|
||
assert (cleared.vote, cleared.voted_at) == (None, None)
|
||
assert store.set_vote(agent.id, uid["up"], None).vote is None # clearing twice is fine
|
||
with pytest.raises(ValueError):
|
||
store.set_vote(agent.id, uid["up"], 2)
|
||
with pytest.raises(ValueError):
|
||
store.set_vote(agent.id, uid["up"], True)
|
||
with pytest.raises(KeyError):
|
||
store.set_vote(agent.id, "nope", 1)
|
||
with pytest.raises(KeyError):
|
||
store.set_vote(999, uid["up"], 1)
|
||
|
||
|
||
def test_votes_are_per_agent(store: Store) -> None:
|
||
store.upsert_role(posting("1"))
|
||
uid = posting("1").uid
|
||
one = store.save_agent("One")
|
||
two = store.save_agent("Two")
|
||
store.set_vote(one.id, uid, 1)
|
||
|
||
assert store.role(uid, one.id).vote == 1 # type: ignore[union-attr]
|
||
assert store.role(uid, two.id).vote is None # type: ignore[union-attr]
|
||
assert store.role(uid).vote is None # type: ignore[union-attr]
|
||
assert store.votes(two.id) == {}
|
||
assert [r.vote for r in store.roles(agent=two.id, by_score=False)] == [None]
|
||
assert [r.vote for r in store.roles()] == [None]
|
||
assert [r.source_id for r in store.roles(agent=two.id, by_score=False, judged="liked")] == []
|
||
assert [r.source_id for r in store.roles(judged="liked")] == []
|
||
assert [r.source_id for r in store.roles(judged="unjudged")] == ["1"]
|
||
|
||
store.set_vote(two.id, uid, -1)
|
||
assert store.role(uid, one.id).vote == 1 # type: ignore[union-attr]
|
||
assert store.role(uid, two.id).vote == -1 # type: ignore[union-attr]
|
||
|
||
# Deleting the agent takes its votes with it and leaves the other's.
|
||
store.delete_agent(two.id)
|
||
assert store.votes(one.id) == {uid: 1}
|
||
with store.transaction() as conn:
|
||
assert conn.execute("SELECT COUNT(*) FROM role_votes").fetchone()[0] == 1
|
||
|
||
|
||
def test_roles_with_an_agent_but_not_by_score_are_newest_first_with_votes(
|
||
store: Store, clock: Clock) -> None:
|
||
for sid in ("1", "2", "3"):
|
||
store.upsert_role(posting(sid))
|
||
clock.tick()
|
||
uid = {r.source_id: r.uid for r in store.roles()}
|
||
agent = store.save_agent("A")
|
||
store.replace_scores(agent.id, [(uid["1"], 0.9, ""), (uid["2"], 0.1, "")]) # 3 unscored
|
||
store.set_vote(agent.id, uid["1"], 1)
|
||
|
||
newest = store.roles(agent=agent.id, by_score=False)
|
||
assert [r.source_id for r in newest] == ["3", "2", "1"]
|
||
assert [r.score for r in newest] == [None, None, None]
|
||
assert [r.vote for r in newest] == [None, None, 1]
|
||
assert store.count_roles(agent=agent.id, by_score=False) == 3
|
||
assert [r.source_id for r in store.roles(agent=agent.id)] == ["1", "2"]
|
||
|
||
|
||
def test_read_state_is_set_cleared_and_a_vote_marks_read(
|
||
store: Store, clock: Clock) -> None:
|
||
store.upsert_role(posting("1"))
|
||
uid = posting("1").uid
|
||
role = store.role(uid)
|
||
assert role is not None and role.read_at is None
|
||
|
||
read = store.set_read(uid)
|
||
assert read.read_at == clock().isoformat(timespec="seconds")
|
||
clock.tick()
|
||
again = store.set_read(uid)
|
||
assert again.read_at == read.read_at, "reading twice keeps the first stamp"
|
||
|
||
cleared = store.set_read(uid, False)
|
||
assert cleared.read_at is None
|
||
|
||
agent = store.save_agent("A")
|
||
voted = store.set_vote(agent.id, uid, 1)
|
||
assert voted.read_at is not None, "voting is reading"
|
||
stamp = voted.read_at
|
||
unvoted = store.set_vote(agent.id, uid, None)
|
||
assert unvoted.read_at == stamp, "clearing a vote is not un-reading"
|
||
|
||
with pytest.raises(KeyError):
|
||
store.set_read("nope")
|
||
|
||
|
||
def test_roles_include_read_excludes_a_read_role_before_the_limit(store: Store) -> None:
|
||
store.upsert_role(posting("1"))
|
||
store.upsert_role(posting("2"))
|
||
uid = {r.source_id: r.uid for r in store.roles()}
|
||
store.set_read(uid["1"])
|
||
|
||
assert {r.source_id for r in store.roles()} == {"1", "2"}
|
||
assert {r.source_id for r in store.roles(include_read=False)} == {"2"}
|
||
assert store.count_roles(include_read=False) == 1
|
||
|
||
|
||
def test_set_tags_normalises_dedupes_and_caps(store: Store) -> None:
|
||
store.upsert_role(posting("1"))
|
||
uid = posting("1").uid
|
||
long_tag = "x" * 50
|
||
|
||
tags = store.set_tags(uid, [" Backend Engineer ", "python", "PYTHON", "", " ",
|
||
long_tag, *[f"t{i}" for i in range(15)]])
|
||
|
||
assert tags == sorted(tags)
|
||
assert len(tags) == 12 # capped at _MAX_TAGS
|
||
assert len(set(tags)) == 12 # no repeats
|
||
assert all(len(t) <= 40 for t in tags) # cut to _MAX_TAG_LEN
|
||
assert "backend engineer" in tags and "python" in tags
|
||
role = store.role(uid)
|
||
assert role is not None and role.tags == tags # loaded sorted, with the role
|
||
|
||
with pytest.raises(KeyError):
|
||
store.set_tags("nope", ["x"])
|
||
|
||
|
||
def test_roles_filters_by_tags_any_and_all(store: Store) -> None:
|
||
store.upsert_role(posting("1"))
|
||
store.upsert_role(posting("2"))
|
||
store.upsert_role(posting("3"))
|
||
uid = {r.source_id: r.uid for r in store.roles()}
|
||
store.set_tags(uid["1"], ["python", "backend"])
|
||
store.set_tags(uid["2"], ["python", "frontend"])
|
||
store.set_tags(uid["3"], ["go"])
|
||
|
||
assert {r.source_id for r in store.roles(tags=["python"])} == {"1", "2"}
|
||
assert {r.source_id for r in store.roles(tags=["python", "go"])} == {"1", "2", "3"}
|
||
assert {r.source_id for r in
|
||
store.roles(tags=["python", "backend"], tag_mode="all")} == {"1"}
|
||
assert store.count_roles(tags=["python"]) == 2
|
||
assert store.count_roles(tags=["python", "backend"], tag_mode="all") == 1
|
||
with pytest.raises(ValueError, match="tag_mode"):
|
||
store.roles(tags=["python"], tag_mode="maybe")
|
||
|
||
|
||
def test_tag_counts_orders_by_count_then_tag_and_respects_muted(store: Store) -> None:
|
||
store.add_company("Hidden", key="hidden")
|
||
agent = store.save_agent("A")
|
||
store.set_affinity(agent.id, "hidden", -1)
|
||
store.upsert_role(posting("1", company="Acme"))
|
||
store.upsert_role(posting("2", company="Acme"))
|
||
store.upsert_role(posting("3", company="Hidden"))
|
||
uid = {r.source_id: r.uid for r in store.roles(hide_muted=False)}
|
||
store.set_tags(uid["1"], ["python", "backend"])
|
||
store.set_tags(uid["2"], ["python"])
|
||
store.set_tags(uid["3"], ["python", "backend"])
|
||
store.set_read(uid["1"])
|
||
|
||
assert store.tag_counts(agent.id) == [("python", 2, 1), ("backend", 1, 0)]
|
||
assert store.tag_counts(agent.id, show_muted=True) == [("python", 3, 2), ("backend", 2, 1)]
|
||
assert store.tag_counts() == [("python", 3, 2), ("backend", 2, 1)] # no agent, nothing hidden
|
||
|
||
|
||
def test_scores_for_an_agent_deleted_mid_run_are_dropped_not_an_error(store: Store) -> None:
|
||
store.upsert_role(posting("a"))
|
||
uid = store.roles()[0].uid
|
||
agent = store.save_agent("A")
|
||
store.delete_agent(agent.id)
|
||
assert store.replace_scores(agent.id, [(uid, 0.9, "")]) == 0
|
||
assert store.scores(agent.id) == {}
|
||
|
||
|
||
def test_a_vote_moves_the_served_score_and_the_order(store: Store) -> None:
|
||
for sid in ("a", "b", "c"):
|
||
store.upsert_role(posting(sid))
|
||
uid = {r.source_id: r.uid for r in store.roles()}
|
||
agent = store.save_agent("A")
|
||
other = store.save_agent("B")
|
||
store.replace_scores(agent.id, [(uid["a"], 0.9, ""), (uid["b"], 0.5, ""),
|
||
(uid["c"], 0.4, "")])
|
||
store.replace_scores(other.id, [(uid["a"], 0.9, ""), (uid["b"], 0.5, ""),
|
||
(uid["c"], 0.4, "")])
|
||
store.set_vote(agent.id, uid["a"], -1) # 0.45
|
||
store.set_vote(agent.id, uid["c"], 1) # 0.48
|
||
ranked = [(r.source_id, round(r.score or 0, 3)) for r in store.roles(agent=agent.id)]
|
||
assert ranked == [("b", 0.5), ("c", 0.48), ("a", 0.45)]
|
||
assert store.scores(agent.id)[uid["a"]][0] == 0.9 # the stored score is kept
|
||
store.set_vote(agent.id, uid["a"], 1)
|
||
assert store.roles(agent=agent.id, limit=1)[0].score == 1.0
|
||
# The other agent's ranking is untouched by these votes.
|
||
assert [(r.source_id, r.score) for r in store.roles(agent=other.id)] == \
|
||
[("a", 0.9), ("b", 0.5), ("c", 0.4)]
|
||
|
||
|
||
def test_mark_closed_closes_what_the_board_no_longer_lists(store: Store) -> None:
|
||
sub = store.add_subscription("ats", "greenhouse:acme")
|
||
other = store.add_subscription("ats", "greenhouse:other")
|
||
for source_id in ("1", "2", "3"):
|
||
store.upsert_role(posting(source_id), sub)
|
||
store.upsert_role(posting("9"), other)
|
||
assert store.mark_closed("ats", ["1", "3"], sub.id) == 1
|
||
assert {r.source_id for r in store.roles()} == {"1", "3", "9"}
|
||
assert {r.source_id for r in store.roles(open_only=False)} == {"1", "2", "3", "9"}
|
||
# Listed again: open again.
|
||
store.upsert_role(posting("2"), sub)
|
||
assert {r.source_id for r in store.roles()} == {"1", "2", "3", "9"}
|
||
|
||
|
||
def test_an_item_without_a_source_is_refused(store: Store) -> None:
|
||
item = posting()
|
||
item.source = ""
|
||
with pytest.raises(ValueError, match="no source"):
|
||
store.upsert_role(item)
|
||
|
||
|
||
# --------------------------------------------------------------- companies
|
||
|
||
def test_companies_crud(store: Store) -> None:
|
||
acme = store.add_company("Acme GmbH", domain="acme.example")
|
||
assert acme.key == "acme" and acme.affinity == 0 and acme.status == "researching"
|
||
with pytest.raises(ValueError, match="already exists"):
|
||
store.add_company("Acme")
|
||
updated = store.update_company("acme", status="contacted")
|
||
assert updated.status == "contacted"
|
||
with pytest.raises(ValueError, match="not a company field"):
|
||
store.update_company("acme", affinity=2)
|
||
with pytest.raises(ValueError, match="not a company field"):
|
||
store.update_company("acme", created_at="x")
|
||
assert [c.key for c in store.companies()] == ["acme"]
|
||
assert store.remove_company("acme") is True
|
||
assert store.company("acme") is None
|
||
|
||
|
||
def test_affinity_is_per_agent(store: Store) -> None:
|
||
store.add_company("Acme", key="acme")
|
||
store.add_company("Other", key="other")
|
||
one = store.save_agent("One")
|
||
two = store.save_agent("Two")
|
||
|
||
assert store.set_affinity(one.id, "acme", 2).affinity == 2
|
||
assert store.company("acme", one.id).affinity == 2 # type: ignore[union-attr]
|
||
assert store.company("acme", two.id).affinity == 0 # type: ignore[union-attr]
|
||
assert store.company("acme").affinity == 0 # type: ignore[union-attr]
|
||
assert [(c.key, c.affinity) for c in store.companies(one.id)] == [("acme", 2), ("other", 0)]
|
||
assert [c.affinity for c in store.companies(two.id)] == [0, 0]
|
||
assert [c.affinity for c in store.companies()] == [0, 0]
|
||
|
||
store.set_affinity(two.id, "acme", -1)
|
||
assert store.company("acme", one.id).affinity == 2 # type: ignore[union-attr]
|
||
assert store.company("acme", two.id).affinity == -1 # type: ignore[union-attr]
|
||
|
||
# 0 deletes the row rather than keeping a neutral one.
|
||
assert store.set_affinity(one.id, "acme", 0).affinity == 0
|
||
with store.transaction() as conn:
|
||
rows = conn.execute("SELECT agent_id, company_key FROM agent_companies").fetchall()
|
||
assert [tuple(r) for r in rows] == [(two.id, "acme")]
|
||
|
||
with pytest.raises(ValueError, match="affinity"):
|
||
store.set_affinity(one.id, "acme", 5)
|
||
with pytest.raises(ValueError, match="affinity"):
|
||
store.set_affinity(one.id, "acme", True) # type: ignore[arg-type]
|
||
with pytest.raises(KeyError):
|
||
store.set_affinity(one.id, "nope", 1)
|
||
with pytest.raises(KeyError):
|
||
store.set_affinity(999, "acme", 1)
|
||
|
||
store.delete_agent(two.id)
|
||
with store.transaction() as conn:
|
||
assert conn.execute("SELECT COUNT(*) FROM agent_companies").fetchone()[0] == 0
|
||
|
||
|
||
def test_the_default_agent_is_the_lowest_enabled_id(store: Store) -> None:
|
||
assert store.default_agent() is None
|
||
off = store.save_agent("Off", enabled=False)
|
||
assert store.default_agent() == off
|
||
on = store.save_agent("On")
|
||
assert store.default_agent() == on
|
||
later = store.save_agent("Later")
|
||
assert store.default_agent() == on
|
||
store.delete_agent(on.id)
|
||
assert store.default_agent() == later
|
||
|
||
|
||
def test_open_role_counts_and_company_boards(store: Store) -> None:
|
||
sub = store.add_subscription("ats", "greenhouse:acme", company="Acme", board="greenhouse")
|
||
store.add_company("Acme", key="acme")
|
||
store.upsert_role(posting("1", company="Acme", place="Berlin"), sub)
|
||
store.upsert_role(posting("2", company="Acme", place="Munich"), sub)
|
||
store.mark_closed("ats", ["1"], sub.id)
|
||
assert store.open_role_counts() == {"acme": 1}
|
||
assert store.company_boards() == {"acme": "greenhouse"}
|
||
|
||
|
||
# ----------------------------------------------------------- subscriptions
|
||
|
||
def test_subscriptions_crud(store: Store) -> None:
|
||
sub = store.add_subscription("ats", "greenhouse:acme", label="Acme",
|
||
company="Acme", board="Greenhouse", params={"q": 1})
|
||
assert (sub.company, sub.board, sub.params, sub.fetch) == \
|
||
("Acme", "Greenhouse", {"q": 1}, 1)
|
||
with pytest.raises(ValueError):
|
||
store.add_subscription("ats", "greenhouse:acme")
|
||
assert store.subscription_for("ats", "Acme") == sub
|
||
store.update_subscription(sub.id, fetch=False)
|
||
assert store.subscriptions(fetchable=True) == []
|
||
assert [s.id for s in store.subscriptions(source="ats")] == [sub.id]
|
||
gone = store.unsubscribe(sub.id)
|
||
assert (gone.subscribed, gone.fetch, gone.show) == (0, 0, 0)
|
||
|
||
|
||
# ------------------------------------------------------------ applications
|
||
|
||
def test_a_status_change_writes_an_event(store: Store, clock: Clock) -> None:
|
||
saved = store.save_application(Application(company_key="acme", role_title="Engineer"))
|
||
assert saved.id and saved.status == "saved"
|
||
clock.tick()
|
||
store.set_application_status(saved.id, "applied")
|
||
clock.tick()
|
||
store.save_application(saved.model_copy(update={"status": "applied", "notes": "x"}))
|
||
clock.tick()
|
||
store.set_application_status(saved.id, "screen")
|
||
events = store.application_events(saved.id)
|
||
assert [e.status for e in events] == ["saved", "applied", "screen"]
|
||
assert events[0].at < events[1].at < events[2].at
|
||
assert [a.notes for a in store.applications()] == ["x"]
|
||
|
||
|
||
def test_an_invalid_status_is_refused(store: Store) -> None:
|
||
saved = store.save_application(Application(role_title="Engineer"))
|
||
with pytest.raises(ValueError, match="not an application status"):
|
||
store.set_application_status(saved.id, "ghosted")
|
||
with pytest.raises(ValidationError):
|
||
Application(status="ghosted")
|
||
with pytest.raises(sqlite3.IntegrityError), store.transaction() as conn:
|
||
conn.execute("UPDATE applications SET status='ghosted' WHERE id=?", (saved.id,))
|
||
assert [e.status for e in store.application_events(saved.id)] == ["saved"]
|
||
|
||
|
||
def test_events_are_append_only(store: Store) -> None:
|
||
saved = store.save_application(Application(role_title="Engineer"))
|
||
with pytest.raises(sqlite3.IntegrityError, match="append-only"), \
|
||
store.transaction() as conn:
|
||
conn.execute("UPDATE application_events SET status='offer'")
|
||
assert store.application_events(saved.id)[0].status == "saved"
|
||
|
||
|
||
def test_an_application_may_point_at_a_role(store: Store) -> None:
|
||
store.upsert_role(posting())
|
||
saved = store.save_application(Application(role_uid=posting().uid, status="applied"))
|
||
assert saved.role_uid == posting().uid
|
||
assert [e.status for e in store.application_events(saved.id)] == ["applied"]
|
||
|
||
|
||
# -------------------------------------------------- summary and posting text
|
||
|
||
def test_the_posting_text_is_stored_and_stamped(store: Store) -> None:
|
||
store.upsert_role(posting())
|
||
role = store.set_article_text(posting().uid, "The whole posting.")
|
||
assert role is not None
|
||
assert role.article_text == "The whole posting." and role.article_at == store.now()
|
||
assert store.set_article_text("nope", "x") is None
|
||
|
||
|
||
def test_a_summary_is_written_once(store: Store) -> None:
|
||
store.upsert_role(posting())
|
||
uid = posting().uid
|
||
assert store.set_summary(uid, "First.") is True
|
||
assert store.set_summary(uid, "Second.") is False
|
||
role = store.role(uid)
|
||
assert role is not None and role.summary == "First." and role.summarised_at
|
||
assert store.set_summary("nope", "x") is False
|
||
|
||
|
||
def test_a_summarys_tags_land_with_it_and_not_on_a_rejected_late_answer(
|
||
store: Store) -> None:
|
||
store.upsert_role(posting())
|
||
uid = posting().uid
|
||
assert store.set_summary(uid, "First.", ["Python", "backend"]) is True
|
||
role = store.role(uid)
|
||
assert role is not None and role.tags == ["backend", "python"]
|
||
|
||
# A second write is rejected (already summarised), and does not touch tags.
|
||
assert store.set_summary(uid, "Second.", ["go"]) is False
|
||
role = store.role(uid)
|
||
assert role is not None and role.tags == ["backend", "python"]
|
||
|
||
|
||
def test_summarise_waits_on_the_top_of_each_enabled_agent(store: Store) -> None:
|
||
"""Two agents each contribute their own top one; a closed role and a
|
||
gated score are not in anybody's top. A hidden company needs no case:
|
||
`score` writes no rows for it under the agent that hid it."""
|
||
for sid in "abcd":
|
||
store.upsert_role(posting(sid))
|
||
a, b, c, d = (posting(s).uid for s in "abcd")
|
||
with store.transaction() as conn:
|
||
conn.execute("UPDATE roles SET closed_at = ? WHERE uid = ?", (store.now(), d))
|
||
one = store.save_agent("One")
|
||
two = store.save_agent("Two")
|
||
store.replace_scores(one.id, [(d, 0.99, ""), (a, 0.9, ""), (b, 0.5, "")])
|
||
store.replace_scores(two.id, [(a, 0.95, "remote"), (c, 0.7, ""), (b, 0.8, "")])
|
||
|
||
waiting = {r.uid for r in store.summarise_waiting(10, per_agent=1)}
|
||
|
||
assert waiting == {a, b}
|
||
assert {r.uid for r in store.summarise_waiting(10, per_agent=2)} == {a, b, c}
|
||
assert store.summarise_pending(per_agent=2) == 3
|
||
assert [r.uid for r in store.summarise_waiting(10, 1, uids=[d])] == [d]
|