Migration 18 adds roles.dup_of with a partial index and runs the first rebuild. Store.rebuild_dupes feeds app.dupes.assign the open roles of every (company_key, title_key) with more than one, rewrites dup_of where it changed, clears it on closed roles, and hands read_at and votes to a changed group's kept role. The pull's settle and `app dupes` run it. RoleRow.copies counts a row's open copies; Role.copies lists them and Role.dup_of names a copy's kept role. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
178 lines
7.3 KiB
Python
178 lines
7.3 KiB
Python
"""`roles.dup_of`: migration 18, `Store.rebuild_dupes`, and what a copy is left out of."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sqlite3
|
|
from datetime import UTC, datetime
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from desk.models import Item
|
|
|
|
from app.storage import Store
|
|
|
|
|
|
@pytest.fixture
|
|
def store(tmp_path: Path) -> Store:
|
|
return Store(tmp_path / "roledesk.db")
|
|
|
|
|
|
def role(source_id: str, *, company: str = "SAP", title: str = "Forward Deployed Engineer",
|
|
place: str = "Garching", body: str = "The posting.", day: int = 20,
|
|
**raw: object) -> Item:
|
|
item = Item(source_id=source_id, location=f"https://jobs.example/{source_id}",
|
|
title=title, body=body, published_at=datetime(2026, 9, day, tzinfo=UTC),
|
|
raw={"company": company, "place": place, "board": "successfactors", **raw})
|
|
item.source = "ats"
|
|
return item
|
|
|
|
|
|
def dup_of(store: Store) -> dict[str, str | None]:
|
|
with sqlite3.connect(store.path) as conn:
|
|
return dict(conn.execute("SELECT uid, dup_of FROM roles"))
|
|
|
|
|
|
def test_a_board_under_several_ids_is_one_role_and_another_town_stays_apart(
|
|
store: Store) -> None:
|
|
"""SAP's repeats: three Garching rows are one kept role (the one with
|
|
the description) and two copies; New York is its own role."""
|
|
garching = [role("1", body=""), role("2"), role("3", body="")]
|
|
york = role("4", place="New York, NY, US")
|
|
for item in (*garching, york):
|
|
store.upsert_role(item)
|
|
|
|
assert store.rebuild_dupes() == (1, 2, 2)
|
|
kept = garching[1].uid
|
|
assert dup_of(store) == {garching[0].uid: kept, kept: None,
|
|
garching[2].uid: kept, york.uid: None}
|
|
assert store.rebuild_dupes().changed == 0 # stable between runs
|
|
|
|
|
|
def test_a_search_copy_of_a_board_role_is_hidden_behind_the_board(store: Store) -> None:
|
|
board = role("board", company="Trackunit", title="Backend Engineer", place="Aalborg",
|
|
board="teamtailor", body="")
|
|
search = role("search", company="Trackunit", title="Backend Engineer",
|
|
place="Aalborg, Denmark", board=None, provider="jobindex")
|
|
for item in (search, board):
|
|
store.upsert_role(item)
|
|
store.rebuild_dupes()
|
|
assert dup_of(store) == {board.uid: None, search.uid: board.uid}
|
|
|
|
|
|
def test_berlin_and_munich_stay_two_roles(store: Store) -> None:
|
|
for item in (role("b", place="Berlin"), role("m", place="Munich")):
|
|
store.upsert_role(item)
|
|
assert store.rebuild_dupes() == (0, 0, 0)
|
|
|
|
|
|
def test_closing_the_kept_role_hands_the_group_on_and_a_closed_copy_is_cleared(
|
|
store: Store) -> None:
|
|
first, second, third = role("1", day=18), role("2", day=19), role("3", day=20)
|
|
for item in (first, second, third):
|
|
store.upsert_role(item)
|
|
store.rebuild_dupes()
|
|
assert dup_of(store)[second.uid] == first.uid
|
|
|
|
with sqlite3.connect(store.path) as conn:
|
|
conn.execute("UPDATE roles SET closed_at = '2026-09-25' WHERE uid = ?", (first.uid,))
|
|
store.rebuild_dupes()
|
|
assert dup_of(store) == {first.uid: None, second.uid: None, third.uid: second.uid}
|
|
|
|
with sqlite3.connect(store.path) as conn:
|
|
conn.execute("UPDATE roles SET closed_at = '2026-09-26' WHERE uid = ?", (third.uid,))
|
|
store.rebuild_dupes()
|
|
assert set(dup_of(store).values()) == {None}
|
|
|
|
|
|
def test_read_and_votes_move_to_the_kept_role(store: Store) -> None:
|
|
"""A copy read and liked before it was known as one: the kept role
|
|
becomes read and liked. An agent that voted on the kept role keeps its
|
|
own vote, and a later unread on the kept role is not undone."""
|
|
one, two = store.save_agent("One"), store.save_agent("Two")
|
|
kept, copy = role("kept", day=18), role("copy", day=19)
|
|
store.upsert_role(copy)
|
|
store.set_vote(one.id, copy.uid, 1)
|
|
store.set_vote(two.id, copy.uid, -1)
|
|
store.upsert_role(kept)
|
|
store.set_vote(two.id, kept.uid, 0)
|
|
|
|
store.rebuild_dupes()
|
|
|
|
read = store.role(copy.uid).read_at # type: ignore[union-attr]
|
|
assert store.role(kept.uid).read_at == read # type: ignore[union-attr]
|
|
assert store.votes(one.id) == {kept.uid: 1}
|
|
assert store.votes(two.id) == {kept.uid: 0, copy.uid: -1}
|
|
|
|
store.set_read(kept.uid, False)
|
|
store.rebuild_dupes()
|
|
assert store.role(kept.uid).read_at is None # type: ignore[union-attr]
|
|
|
|
|
|
def test_a_closed_kept_role_hands_its_read_and_vote_to_the_next(store: Store) -> None:
|
|
agent = store.save_agent("One")
|
|
first, second = role("1", day=18), role("2", day=19)
|
|
for item in (first, second):
|
|
store.upsert_role(item)
|
|
store.rebuild_dupes()
|
|
store.set_vote(agent.id, first.uid, 1)
|
|
with sqlite3.connect(store.path) as conn:
|
|
conn.execute("UPDATE roles SET closed_at = '2026-09-25' WHERE uid = ?", (first.uid,))
|
|
# A third copy keeps the group alive once the first has closed.
|
|
store.upsert_role(role("3", day=20))
|
|
|
|
store.rebuild_dupes()
|
|
|
|
assert store.votes(agent.id) == {second.uid: 1}
|
|
assert store.role(second.uid).read_at is not None # type: ignore[union-attr]
|
|
|
|
|
|
def test_a_copy_is_left_out_of_the_feed_counts_and_model_stages(store: Store) -> None:
|
|
kept, copy, other = role("1", day=18), role("2", day=19), role("3", place="Berlin")
|
|
for item in (kept, copy, other):
|
|
store.upsert_role(item)
|
|
store.set_tags(item.uid, ["python"])
|
|
store.rebuild_dupes()
|
|
agent = store.save_agent("A", intent="engineering")
|
|
|
|
assert {r.uid for r in store.roles()} == {kept.uid, other.uid}
|
|
rows = {r.uid: r.copies for r in store.roles_page()}
|
|
assert rows == {kept.uid: 1, other.uid: 0}
|
|
assert store.count_roles() == 2
|
|
assert sum(n for _, n in store.role_facets()["seniority"]) == 2
|
|
assert store.tag_counts() == [("python", 2, 2)]
|
|
assert {c.uid for c in store.score_candidates(agent.id, [])} == {kept.uid, other.uid}
|
|
assert {r.uid for r in store.roles_to_embed("m", 10)} == {kept.uid, other.uid}
|
|
assert store.open_role_counts() == {"sap": 2}
|
|
assert [count for _, _, count, _ in store.open_companies()] == [2]
|
|
store.replace_scores(agent.id, [(u, 0.9, "") for u in (kept.uid, copy.uid, other.uid)])
|
|
assert {r.uid for r in store.extract_waiting(10)} == {kept.uid, other.uid}
|
|
assert {r.uid for r in store.summarise_waiting(10)} == {kept.uid, other.uid}
|
|
|
|
# The copy still opens by uid, and the kept role lists it.
|
|
assert store.role(copy.uid).dup_of == kept.uid # type: ignore[union-attr]
|
|
assert [c.uid for c in store.role(kept.uid).copies] == [copy.uid] # type: ignore[union-attr]
|
|
assert store.role(other.uid).copies == [] # type: ignore[union-attr]
|
|
|
|
|
|
def test_a_v17_file_gains_dup_of_with_its_first_rebuild(tmp_path: Path) -> None:
|
|
path = tmp_path / "v17.db"
|
|
store = Store(path)
|
|
first, second = role("1", day=18), role("2", day=19)
|
|
for item in (first, second):
|
|
store.upsert_role(item)
|
|
store.put_vectors("m", [(first.uid, np.ones(3, dtype=np.float32))])
|
|
with sqlite3.connect(path) as conn:
|
|
conn.execute("DROP INDEX idx_roles_dup_of")
|
|
conn.execute("ALTER TABLE roles DROP COLUMN dup_of")
|
|
conn.execute("PRAGMA user_version = 17")
|
|
|
|
migrated = Store(path)
|
|
|
|
assert migrated.migrations_applied == [18]
|
|
assert dup_of(migrated) == {first.uid: None, second.uid: first.uid}
|
|
with sqlite3.connect(path) as conn:
|
|
assert conn.execute("SELECT 1 FROM sqlite_master WHERE name = 'idx_roles_dup_of'"
|
|
).fetchone() is not None
|