Merge branch 'main' into wp1-byhand-api

# Conflicts:
#	app/models.py
#	app/storage/schema.py
#	app/storage/store.py
#	app/web/api.py
#	tests/test_storage.py
#	tests/test_web.py
This commit is contained in:
Wolf committed 2026-09-29 19:25:42 +02:00
commit 1a2689e08d
17 files changed
+565 -109

No files matched your search

+11
View File
@@ -85,6 +85,17 @@ def test_a_role_address_is_the_same_page_with_its_reader_open(
assert missing.status_code == 404 and "text/html" in missing.headers["content-type"]
def test_the_watchlist_is_the_third_tab_and_a_shell(client: TestClient) -> None:
page = client.get("/watchlist")
assert page.status_code == 200
assert 'href="/watchlist"\n class="active" aria-current="page">Watchlist' in page.text
assert 'id="watchlist-list"' in page.text and "/static/watchlist.js" in page.text
nav = page.text[page.text.index("<nav>"):page.text.index("</nav>")]
tabs = [t for t in ("Roles", "Applications", "Watchlist") if f">{t}</a>" in nav]
assert tabs == ["Roles", "Applications", "Watchlist"]
assert nav.count('aria-current="page"') == 1
def test_the_read_page_of_its_own_is_gone(client: TestClient) -> None:
assert client.get("/static/role.js").status_code == 404
other = client.get("/applications").text
+52 -26
View File
@@ -69,7 +69,7 @@ def test_schema_creates_on_an_empty_file(tmp_path: Path) -> None:
"application_events", "agents", "scores", "state"} <= tables
assert "idx_roles_cluster" in indexes
assert store.migrations_applied == []
assert LATEST == 15
assert LATEST == 16
# A second open finds it current and applies nothing.
assert Store(path).migrations_applied == []
@@ -88,18 +88,26 @@ def _columns(path: Path, table: str) -> list[tuple[str, str, int, str]]:
return [(r[1], r[2], r[3], r[4]) for r in conn.execute(f"PRAGMA table_info({table})")]
def _undo_v15(conn: sqlite3.Connection) -> None:
"""A fresh file put back to the v14 shape, without the by-hand columns of
a company, and then to the v13 one by `_undo_v14`. Every downgrade below
starts here, since v15 is the last migration."""
def _undo_v16(conn: sqlite3.Connection) -> None:
"""A fresh file put back to the v15 shape: no by-hand reason on a
company."""
conn.execute("ALTER TABLE companies DROP COLUMN hand_reason")
conn.execute("ALTER TABLE companies DROP COLUMN checked_at")
_undo_v14(conn)
def _undo_v15(conn: sqlite3.Connection) -> None:
"""A fresh file put back to the v14 shape: no Watchlist columns on a
company."""
_undo_v16(conn)
conn.execute("ALTER TABLE companies DROP COLUMN watch")
conn.execute("ALTER TABLE companies DROP COLUMN careers_opened_at")
def _undo_v14(conn: sqlite3.Connection) -> None:
"""A fresh file put back to the v13 shape: an agent's intent and its
cached vector on the agent row, no prompts, no prompt on a score."""
cached vector on the agent row, no prompts, no prompt on a score. Every
downgrade below starts here, after `_undo_v15`, since v16 is the last
migration."""
_undo_v15(conn)
conn.execute("DROP TABLE agent_prompts")
conn.execute("ALTER TABLE scores DROP COLUMN prompt")
conn.execute("ALTER TABLE agents ADD COLUMN intent TEXT NOT NULL DEFAULT ''")
@@ -112,7 +120,7 @@ def test_a_v1_file_migrates_to_the_shape_a_new_file_has(tmp_path: Path) -> None:
Store(fresh)
Store(old)
with sqlite3.connect(old) as conn:
_undo_v15(conn)
_undo_v14(conn)
conn.execute("DROP INDEX idx_roles_posted") # v9: indexes posted_at
for column in ("permit", "languages", "extract_tries", "summary", "summarised_at",
"article_text", "article_at", "read_at", "posted_at", "salary_text"):
@@ -135,11 +143,28 @@ def test_a_v1_file_migrates_to_the_shape_a_new_file_has(tmp_path: Path) -> None:
assert sorted(_columns(old, table)) == sorted(_columns(fresh, table))
def test_a_v14_file_gains_the_watchlist_columns_and_leaves_companies_unrated(
tmp_path: Path) -> None:
path = tmp_path / "v14.db"
Store(path).add_company("Acme", ats="teamtailor", country="DK", careers_url="https://a.example")
with sqlite3.connect(path) as conn:
_undo_v15(conn)
conn.execute("PRAGMA user_version = 14")
migrated = Store(path)
assert migrated.migrations_applied == [15, 16]
acme = migrated.company("acme")
assert acme is not None
assert (acme.ats, acme.careers_url) == ("teamtailor", "https://a.example")
assert (acme.watch, acme.careers_opened_at) == (None, "")
def test_a_v8_file_gains_ats_slug_and_keeps_its_companies(tmp_path: Path) -> None:
path = tmp_path / "v8.db"
Store(path).add_company("Acme", ats="teamtailor", country="DK")
with sqlite3.connect(path) as conn:
_undo_v15(conn)
_undo_v14(conn)
conn.execute("ALTER TABLE companies DROP COLUMN ats_slug")
conn.execute("DROP INDEX idx_roles_posted") # v10, which follows
conn.execute("ALTER TABLE roles DROP COLUMN posted_at")
@@ -158,7 +183,7 @@ 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:
_undo_v15(conn)
_undo_v14(conn)
conn.execute("ALTER TABLE roles DROP COLUMN read_at")
conn.execute("DROP INDEX idx_roles_posted") # v9: indexes posted_at
conn.execute("ALTER TABLE roles DROP COLUMN posted_at") # v9
@@ -270,7 +295,7 @@ def test_v10_backfills_posted_at_by_the_same_rule_upsert_uses(
assert before_trusted is not None and before_untrusted is not None
with sqlite3.connect(path) as conn:
_undo_v15(conn)
_undo_v14(conn)
conn.execute("DROP INDEX idx_roles_posted")
conn.execute("ALTER TABLE roles DROP COLUMN posted_at")
conn.execute("ALTER TABLE roles DROP COLUMN salary_text") # v12
@@ -298,7 +323,7 @@ def test_v13_recomputes_country_from_place_unless_the_board_or_extract_set_it(
for item in (wrong, several, given, extracted):
store.upsert_role(item)
with sqlite3.connect(path) as conn:
_undo_v15(conn)
_undo_v14(conn)
conn.execute("UPDATE roles SET country = 'US' WHERE place = 'Bogota, CO'")
conn.execute("UPDATE roles SET country = '' WHERE uid = ?", (several.uid,))
conn.execute("UPDATE roles SET extracted_at = '2026-09-23' WHERE uid = ?",
@@ -325,7 +350,7 @@ def test_v14_makes_each_intent_the_agents_one_prompt_with_its_vector(tmp_path: P
store.replace_scores(backend.id, [(uid, 0.7, "")])
vec = np.asarray([0.6, 0.8], dtype=np.float32)
with sqlite3.connect(path) as conn:
_undo_v15(conn)
_undo_v14(conn)
conn.execute("UPDATE agents SET intent = 'python backend', intent_key = 'k', "
"intent_vec = ? WHERE id = ?", (pack(vec), backend.id))
conn.execute("UPDATE agents SET intent = ' \n ' WHERE id = ?", (blank.id,))
@@ -333,7 +358,7 @@ def test_v14_makes_each_intent_the_agents_one_prompt_with_its_vector(tmp_path: P
migrated = Store(path)
assert migrated.migrations_applied == [14, 15]
assert migrated.migrations_applied == [14, 15, 16]
assert migrated.agent(backend.id).prompts == [ # type: ignore[union-attr]
Prompt(name="Backend", text="python backend")]
assert migrated.agent(blank.id).prompts == [] # type: ignore[union-attr]
@@ -347,22 +372,21 @@ def test_v14_makes_each_intent_the_agents_one_prompt_with_its_vector(tmp_path: P
assert not {"intent", "intent_key", "intent_vec"} & columns
def test_v15_gives_a_company_no_reason_and_no_check_and_keeps_its_row(tmp_path: Path) -> None:
fresh, path = tmp_path / "fresh.db", tmp_path / "v14.db"
def test_v16_gives_a_company_no_reason_and_keeps_its_row(tmp_path: Path) -> None:
fresh, path = tmp_path / "fresh.db", tmp_path / "v15.db"
Store(fresh)
Store(path).add_company("Acme", careers_url="https://acme.example/jobs", notes="met them")
with sqlite3.connect(path) as conn:
conn.execute("ALTER TABLE companies DROP COLUMN hand_reason")
conn.execute("ALTER TABLE companies DROP COLUMN checked_at")
conn.execute("PRAGMA user_version = 14")
_undo_v16(conn)
conn.execute("PRAGMA user_version = 15")
migrated = Store(path)
assert migrated.migrations_applied == [15]
assert migrated.migrations_applied == [16]
acme = migrated.company("acme")
assert acme is not None
assert (acme.careers_url, acme.notes, acme.hand_reason, acme.checked_at) == (
"https://acme.example/jobs", "met them", "", "")
assert (acme.careers_url, acme.notes, acme.hand_reason) == (
"https://acme.example/jobs", "met them", "")
assert sorted(_columns(path, "companies")) == sorted(_columns(fresh, "companies"))
@@ -695,6 +719,9 @@ def test_roles_filters_q_function_remote_and_days(store: Store, clock: Clock) ->
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")] == []
# A word may match the company instead of the title, each on its own.
assert [r.source_id for r in store.roles(q="acme engineer")] == ["2", "1"]
assert [r.source_id for r in store.roles(q="acme 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"]
@@ -1009,9 +1036,8 @@ def test_companies_crud(store: Store) -> None:
assert store.company("acme") is None
def test_a_company_takes_a_reason_from_the_vocabulary_and_a_check_date(store: Store) -> None:
acme = store.add_company("Acme", hand_reason="robots", checked_at="2026-09-01T00:00:00+00:00")
assert (acme.hand_reason, acme.checked_at) == ("robots", "2026-09-01T00:00:00+00:00")
def test_a_company_takes_a_reason_from_the_vocabulary(store: Store) -> None:
assert store.add_company("Acme", hand_reason="robots").hand_reason == "robots"
assert store.add_company("Plain").hand_reason == ""
with pytest.raises(ValueError, match="hand_reason is one of"):
store.add_company("Odd", hand_reason="lazy")
+74 -17
View File
@@ -427,12 +427,12 @@ def test_companies_lists_board_and_open_role_count(
res = client.get("/api/companies")
assert res.status_code == 200
[row] = [c for c in res.json() if c["key"] == "acme"]
assert row == {"key": "acme", "name": "Acme", "domain": "", "notes": "",
"board": "greenhouse", "subscription_id": sub.id, "on": True,
"affinity": 0, "open_roles": 1, "ats": "greenhouse", "ats_slug": "",
"careers_url": "", "country": "DK", "failing_since": "",
"failing_reason": "", "from_search": False, "hand_reason": "",
"checked_at": "", "by_hand": False}
assert row == {"key": "acme", "name": "Acme", "notes": "", "board": "greenhouse",
"subscription_id": sub.id, "on": True, "affinity": 0, "open_roles": 1,
"ats": "greenhouse", "ats_slug": "", "careers_url": "",
"country": "DK", "city": "", "domain": "", "watch": None,
"careers_opened_at": "", "failing_since": "", "failing_reason": "",
"from_search": False, "hand_reason": "", "by_hand": False}
[other] = [c for c in res.json() if c["key"] == "other"]
assert (other["board"], other["subscription_id"], other["on"], other["ats"]) == (
"", None, None, "")
@@ -555,6 +555,53 @@ def test_post_score_answers_with_the_job_to_wait_for(client: TestClient, queue:
assert queue.jobs.get(res.json()["id"]).kind == "score"
# ------------------------------------------------------------- watchlist
def test_the_watchlist_rating_is_global_and_leaves_affinity_alone(
client: TestClient, store: Store) -> None:
store.add_company("Acme", ats="workday")
agent = store.save_agent("A")
store.set_affinity(agent.id, "acme", 1)
for watch, stored in ((True, 1), (False, 0), (None, None)):
res = client.post("/api/companies/acme/watch", json={"watch": watch})
assert res.status_code == 200 and res.json()["watch"] == stored
listed = client.get("/api/companies").json()[0]
assert (listed["watch"], listed["affinity"]) == (stored, 1)
def test_the_careers_page_is_set_cleared_and_must_be_a_web_address(
client: TestClient, store: Store) -> None:
store.add_company("Acme")
res = client.post("/api/companies/acme/careers", json={"careers_url": "https://a.example/jobs"})
assert res.status_code == 200 and res.json()["careers_url"] == "https://a.example/jobs"
for bad in ("javascript:alert(1)", "a.example/jobs", "ftp://a.example"):
assert client.post("/api/companies/acme/careers",
json={"careers_url": bad}).status_code == 422
assert store.company("acme").careers_url == "https://a.example/jobs"
cleared = client.post("/api/companies/acme/careers", json={"careers_url": ""})
assert cleared.status_code == 200 and store.company("acme").careers_url == ""
def test_opening_the_careers_page_stamps_the_time(client: TestClient, store: Store) -> None:
store.add_company("Acme")
assert client.get("/api/companies").json()[0]["careers_opened_at"] == ""
res = client.post("/api/companies/acme/opened")
assert res.status_code == 200
assert res.json()["careers_opened_at"] == store.company("acme").careers_opened_at != ""
assert client.get("/api/companies").json()[0]["careers_opened_at"] != ""
def test_the_watchlist_endpoints_404_for_an_unknown_company(client: TestClient) -> None:
assert client.post("/api/companies/nope/watch", json={"watch": True}).status_code == 404
assert client.post("/api/companies/nope/careers",
json={"careers_url": "https://a.example"}).status_code == 404
assert client.post("/api/companies/nope/opened").status_code == 404
# ----------------------------------------------------------- applications
def test_save_creates_an_application_with_a_saved_event(
@@ -632,7 +679,7 @@ def test_the_applications_page_has_no_next_column(client: TestClient) -> None:
# -------------------------------------------------------------------- tabs
def test_every_page_renders_with_the_whole_nav(client: TestClient) -> None:
assert [t.label for t in TABS] == ["Roles", "Applications"]
assert [t.label for t in TABS] == ["Roles", "Applications", "Watchlist"]
assert [t.label for t in SETTINGS] == ["Search agents", "Sources", "Account"]
assert all(t.path.startswith("/settings/") for t in SETTINGS)
for tab in (*TABS, *SETTINGS):
@@ -1231,8 +1278,9 @@ def test_a_company_is_added_by_hand_with_a_reason_and_a_link(
"key": "revolut", "name": "Revolut Ltd", "domain": "", "notes": "wants a login",
"board": "", "subscription_id": None, "on": None, "affinity": 0, "open_roles": 0,
"ats": "", "ats_slug": "", "careers_url": "https://revolut.example/careers",
"country": "", "failing_since": "", "failing_reason": "", "from_search": False,
"hand_reason": "added", "checked_at": "", "by_hand": True}
"country": "", "city": "", "watch": None, "careers_opened_at": "",
"failing_since": "", "failing_reason": "", "from_search": False,
"hand_reason": "added", "by_hand": True}
assert store.company("revolut").hand_reason == "added" # type: ignore[union-attr]
other = client.post("/api/companies", json={"name": "Meta", "hand_reason": "blocked"})
@@ -1316,17 +1364,26 @@ def test_a_company_with_a_board_keeps_its_name_and_takes_the_other_edits(
assert store.company("acme").name == "Acme" # type: ignore[union-attr]
def test_checking_a_company_stamps_now(client: TestClient, store: Store) -> None:
def test_the_watchlists_careers_link_and_the_by_hand_edit_share_one_url_rule(
client: TestClient, store: Store) -> None:
store.add_company("Acme", key="acme")
for bad in ("https://", "javascript:alert(1)"):
posted = client.post("/api/companies/acme/careers", json={"careers_url": bad})
patched = client.patch("/api/companies/acme", json={"careers_url": bad})
assert posted.status_code == patched.status_code == 422
assert posted.json() == patched.json()
assert store.company("acme").careers_url == "" # type: ignore[union-attr]
def test_opening_a_by_hand_company_shows_when_it_was_last_looked_at(
client: TestClient, store: Store) -> None:
clock = Clock()
store.clock = clock
store.add_company("Acme", key="acme", hand_reason="unread")
res = client.post("/api/companies/acme/checked")
assert res.status_code == 200
assert res.json()["checked_at"] == "2026-09-23T09:00:00+00:00"
clock.tick(48)
assert client.post("/api/companies/acme/checked").json()["checked_at"] == (
"2026-09-25T09:00:00+00:00")
assert client.post("/api/companies/nope/checked").status_code == 404
assert client.post("/api/companies/acme/opened").status_code == 200
[row] = client.get("/api/companies").json()
assert (row["by_hand"], row["careers_opened_at"]) == (True, "2026-09-23T09:00:00+00:00")
assert client.post("/api/companies/acme/checked").status_code in (404, 405)
def test_a_company_is_deleted_unless_it_is_read(