From b3cb93d5aa6b9cfbfb88c1ffc96283ef87dad43d Mon Sep 17 00:00:00 2001 From: Wolf Date: Tue, 29 Sep 2026 17:02:19 +0200 Subject: [PATCH] Keep a typed careers link, seed the by-hand reason and add the by-hand commands to app companies Co-Authored-By: Claude Sonnet 5.5 --- app/cli.py | 127 +++++++++++++++++++++-- app/jobs/sources.py | 125 +++++++++++++++++++++- tests/test_sources.py | 235 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 472 insertions(+), 15 deletions(-) diff --git a/app/cli.py b/app/cli.py index 6017af6..35b3cbe 100644 --- a/app/cli.py +++ b/app/cli.py @@ -3,7 +3,7 @@ app serve [--host H] [--port P] [--reload] app job pull|extract|embed|score [--payload JSON] app worker [--workers http=8,cpu=1] [--until-idle] - app companies seed|add|set-board|detach|discover|check + app companies seed|add|edit|checked|remove|by-hand|set-board|detach|discover|check app searches add|list|remove app agents list|prompt app stages [extract|summarise on|off] @@ -44,6 +44,8 @@ if str(_ROOT) not in sys.path: def build_parser() -> argparse.ArgumentParser: + from app.models import HAND_REASONS + parser = argparse.ArgumentParser( prog="app", description="roledesk: a job-search engine on desk and the pipeline queue.") parser.add_argument("-c", "--config", default=None, @@ -71,10 +73,34 @@ def build_parser() -> argparse.ArgumentParser: seed = companies_sub.add_parser( "seed", help="import seed/companies.json, and the boards it names for rows without one") seed.add_argument("--path", default="seed/companies.json") + # One word after `add` is a company for the list checked by hand, two are + # a board and its company, which is what `add` meant before that list. add = companies_sub.add_parser( - "add", help="follow a company by its board, attaching or replacing an existing one's") - add.add_argument("board", help="host:slug, e.g. greenhouse:acme") - add.add_argument("name") + "add", help="add NAME to the list checked by hand, or with BOARD NAME follow a " + "company by its board, attaching or replacing an existing one's") + add.add_argument("first", metavar="NAME|BOARD", + help="the company, or host:slug, e.g. greenhouse:acme, before its name") + add.add_argument("second", metavar="NAME", nargs="?") + add.add_argument("--careers-url", default="", help="its careers page, http or https") + add.add_argument("--note", default="") + add.add_argument("--reason", default="added", choices=HAND_REASONS, metavar="REASON", + help=f"one of {', '.join(repr(r) for r in HAND_REASONS)}") + edit = companies_sub.add_parser("edit", help="change a company's name, link, note or reason") + edit.add_argument("key") + edit.add_argument("--name") + edit.add_argument("--careers-url") + edit.add_argument("--note") + edit.add_argument("--reason", choices=HAND_REASONS, metavar="REASON", + help=f"one of {', '.join(repr(r) for r in HAND_REASONS)}") + checked = companies_sub.add_parser( + "checked", help="record that its careers page was looked at just now") + checked.add_argument("key") + remove = companies_sub.add_parser( + "remove", help="delete a company with no board on and no open roles") + remove.add_argument("key") + companies_sub.add_parser( + "by-hand", help="the companies checked by hand, never checked first, then the " + "longest ago") set_board = companies_sub.add_parser( "set-board", help="attach a board to a company, or replace the one it has") set_board.add_argument("key", help="the company key, e.g. trade-republic") @@ -274,7 +300,15 @@ def run_companies(args: argparse.Namespace) -> int: if args.action == "seed": return seed_companies(store, args.path) if args.action == "add": - return add_company(store, args.board, args.name) + return add_company(store, args) + if args.action == "edit": + return edit_company(store, args) + if args.action == "checked": + return company_checked(store, args.key) + if args.action == "remove": + return remove_company(store, args.key) + if args.action == "by-hand": + return list_by_hand(store) if args.action == "set-board": return set_board(store, args.key, args.board) if args.action == "detach": @@ -318,7 +352,25 @@ def discover_company(store: Store, name: str, domain: str, *, add: bool) -> int: return 0 -def add_company(store: Store, board: str, name: str) -> int: +def add_company(store: Store, args: argparse.Namespace) -> int: + """`app companies add "Acme" [--careers-url URL] [--note TEXT] [--reason + REASON]` puts a company with no board on the list checked by hand; + `app companies add greenhouse:acme "Acme"` follows it by its board.""" + if args.second is not None: + return add_company_by_board(store, args.first, args.second) + from app import jobs + + try: + company = jobs.sources.add_by_hand(store, args.first, careers_url=args.careers_url, + notes=args.note, hand_reason=args.reason) + except ValueError as exc: + print(f"companies add: {exc}", file=sys.stderr) + return 1 + print(json.dumps({"key": company.key, "hand_reason": company.hand_reason})) + return 0 + + +def add_company_by_board(store: Store, board: str, name: str) -> int: """`app companies add greenhouse:acme "Acme"`: fetch the board first and refuse if it does not answer or is not a known host. A company that exists gets the board attached or replaced. Port of seedgen's `-add`.""" @@ -334,6 +386,69 @@ def add_company(store: Store, board: str, name: str) -> int: return 0 +def edit_company(store: Store, args: argparse.Namespace) -> int: + """`app companies edit acme --careers-url https://acme.example/jobs`: only + the options given change; see `jobs.sources.edit_company`.""" + from app import jobs + + given = {"name": args.name, "careers_url": args.careers_url, "notes": args.note, + "hand_reason": args.reason} + try: + company = jobs.sources.edit_company( + store, args.key, **{f: v for f, v in given.items() if v is not None}) + except KeyError: + print(f"companies edit: no company {args.key!r}", file=sys.stderr) + return 1 + except ValueError as exc: + print(f"companies edit: {exc}", file=sys.stderr) + return 1 + print(json.dumps({"key": company.key, "name": company.name, + "careers_url": company.careers_url, "notes": company.notes, + "hand_reason": company.hand_reason})) + return 0 + + +def company_checked(store: Store, key: str) -> int: + """`app companies checked acme`: its careers page was looked at just now.""" + if store.company(key) is None: + print(f"companies checked: no company {key!r}", file=sys.stderr) + return 1 + company = store.update_company(key, checked_at=store.now()) + print(json.dumps({"key": key, "checked_at": company.checked_at})) + return 0 + + +def remove_company(store: Store, key: str) -> int: + """`app companies remove acme`: refused for a company still read, see + `jobs.sources.remove_company`.""" + from app import jobs + + try: + jobs.sources.remove_company(store, key) + except KeyError: + print(f"companies remove: no company {key!r}", file=sys.stderr) + return 1 + except ValueError as exc: + print(f"companies remove: {exc}", file=sys.stderr) + return 1 + print(json.dumps({"key": key, "removed": True})) + return 0 + + +def list_by_hand(store: Store) -> int: + """`app companies by-hand`: the companies with no board shown that are not + known from a search only, one line each, never checked first and then the + longest ago, as the Sources page lists them; see `jobs.sources.by_hand`.""" + from app import jobs + + rows = jobs.sources.by_hand(store) + for c in rows: + checked = c.checked_at[:10] or "never" + print(f"{c.name:30} {c.hand_reason or '-':8} {checked:10} {c.careers_url}") + print(f"{len(rows)} by hand") + return 0 + + def set_board(store: Store, key: str, board: str) -> int: """`app companies set-board trade-republic greenhouse:traderepublic`: attach the board to an existing company or replace the one it has.""" diff --git a/app/jobs/sources.py b/app/jobs/sources.py index 4eedbea..9e566b6 100644 --- a/app/jobs/sources.py +++ b/app/jobs/sources.py @@ -15,9 +15,10 @@ import json import os from pathlib import Path from typing import TYPE_CHECKING, Any +from urllib.parse import urlsplit from app.config import Config -from app.models import Subscription, company_key +from app.models import HAND_REASONS, Company, Subscription, company_key from app.storage import Store from app.jobs.pull import FAILING, search_target @@ -150,12 +151,116 @@ def add_discovered(store: Store, name: str, domain: str, if sub is not None and sub.show: raise ValueError(f"{name} is read from {sub.target} and discovery found no " "readable board; nothing changed") + # A link somebody typed is kept: discovery's is only a guess at the page. store.update_company(key, ats=found.ats, ats_slug=found.ats_slug, - careers_url=found.careers_url or company.careers_url, + careers_url=company.careers_url or found.careers_url, **({"domain": domain} if not company.domain else {})) return key, 0 +def known_from_search_only(company: Company, ever_boarded: set[str]) -> bool: + """A row that exists only because a search listed its roles and somebody + judged it (`Store.set_affinity`): no board, not even a detached one + (`ever_boarded`, the keys of every `ats` subscription), and none of the + facts every way of following a company or listing it by hand writes.""" + return not (company.key in ever_boarded or company.ats or company.careers_url + or company.domain or company.hand_reason) + + +def by_hand(store: Store) -> list[Company]: + """The list checked by hand: companies with no board shown that are not + known from a search only. Never checked first, then the longest ago; the + name breaks a tie.""" + boards = store.company_boards() + ever_boarded = {company_key(s.company) for s in store.subscriptions(source="ats")} + rows = [c for c in store.companies() + if c.key not in boards and not known_from_search_only(c, ever_boarded)] + return sorted(rows, key=lambda c: (c.checked_at, c.name.casefold())) + + +class Conflict(ValueError): + """A change refused because of what the company already is, not because of + what was asked for: the pages answer it with 409, not 422.""" + + +def check_careers_url(url: str) -> str: + """`url` stripped; ValueError unless it is empty or an http(s) link. The + page puts it in an `href`, where `javascript:` would run.""" + url = url.strip() + if url: + parts = urlsplit(url) + if parts.scheme not in ("http", "https") or not parts.netloc: + raise ValueError("The careers link must start with http:// or https://.") + return url + + +def add_by_hand(store: Store, name: str, *, careers_url: str = "", notes: str = "", + hand_reason: str = "added") -> Company: + """A company with no board, put on the list checked by hand. Raises + Conflict when the key its name folds to is taken, ValueError for a name + with no letters, a link that is no http(s) URL or a reason outside + `HAND_REASONS`.""" + link = check_careers_url(careers_url) + key = company_key(name) + if key and store.company(key) is not None: + raise Conflict(f"{name.strip()} is on the list already as {key}.") + return store.add_company(name, key=key, careers_url=link, notes=notes, + hand_reason=hand_reason) + + +def edit_company(store: Store, key: str, **fields: str) -> Company: + """Change a company's `name`, `careers_url`, `notes` or `hand_reason`; the + key never changes. A new name is Conflict for a company with a board + subscription, shown or not, since the subscription is found by the key + its company name folds to and would be lost. Raises KeyError for an + unknown company and ValueError for a blank name, a link that is no http(s) + URL or a reason outside `HAND_REASONS`.""" + unknown = set(fields) - {"name", "careers_url", "notes", "hand_reason"} + if unknown: + raise ValueError(f"not a field to edit: {', '.join(sorted(unknown))}") + company = store.company(key) + if company is None: + raise KeyError(key) + if "careers_url" in fields: + fields["careers_url"] = check_careers_url(fields["careers_url"]) + if "name" in fields: + fields["name"] = fields["name"].strip() + if not fields["name"]: + raise ValueError("A company needs a name.") + if fields["name"] != company.name and store.company_subscription(key) is not None: + raise Conflict(f"{company.name} has a board, which is found by its name; " + "detach the board first.") + return store.update_company(key, **fields) + + +def remove_company(store: Store, key: str) -> None: + """Delete company `key` and every agent's stance on it. Raises KeyError + for an unknown company and Conflict for one still read: a company with + a board on (`show`, as `detach_board` decides) or with open roles, which + would otherwise vanish from the list with nothing said. A detached board + is no board, since nothing reads it and its roles are closed; its + subscription row stays, and follows the company if it comes back.""" + if store.company(key) is None: + raise KeyError(key) + sub = store.company_subscription(key) + if sub is not None and sub.show: + raise Conflict(f"{key} is read from {sub.target}; detach the board first") + if store.open_role_counts().get(key): + raise Conflict(f"{key} has open roles; detach its board first") + store.remove_company(key) + + +def _fill_empty(store: Store, key: str, **facts: str) -> None: + """Write each of `facts` to company `key` where the stored value is + empty: what a seed may add to a row that exists, since a person's link or + reason is never overwritten.""" + company = store.company(key) + assert company is not None + fill = {f: v for f, v in facts.items() if v and not getattr(company, f)} + if fill: + store.update_company(key, **fill) + + def seed_companies(store: Store, path: str | Path) -> dict[str, Any]: """Import `seed/companies.json`: one `companies` row per entry, and its `board` through `set_board` where the entry names one. An entry without @@ -167,8 +272,10 @@ def seed_companies(store: Store, path: str | Path) -> dict[str, Any]: existing row that never had a board gets the one the seed now names, so a seed update that gains boards reaches a database made before it. A row's board, once it has one (detached counts), and its affinity, notes and - status are never touched. The seed's boards are not fetched again: they - were read once when the seed was written, and `app companies check` + status are never touched; its `careers_url` and `hand_reason` are filled + where empty, so the seed can hand a row its link and its reason but never + overwrite the one a person set. The seed's boards are not fetched again: + they were read once when the seed was written, and `app companies check` finds the dead ones. Returns counts, and `problems`, one line per entry that could not land whole.""" from plugins.ats import is_known_target @@ -186,8 +293,15 @@ def seed_companies(store: Store, path: str | Path) -> dict[str, Any]: board = "" facts = {f: str(row.get(f) or "") for f in ("domain", "country", "city", "careers_url", "ats", "ats_slug")} + reason = str(row.get("hand_reason") or "") + if reason not in HAND_REASONS: + problems.append(f"{key}: {reason!r} is not a hand_reason; kept without it") + reason = "" exists = store.company(key) is not None + if exists: + _fill_empty(store, key, hand_reason=reason) if exists and (not board or store.company_subscription(key) is not None): + _fill_empty(store, key, careers_url=facts["careers_url"]) skipped += 1 continue if board: @@ -200,13 +314,14 @@ def seed_companies(store: Store, path: str | Path) -> dict[str, Any]: except ValueError as exc: problems.append(f"{key}: {exc}") if exists: + _fill_empty(store, key, careers_url=facts["careers_url"]) skipped += 1 continue # No board, or one that clashed: the company is still a row, and a # clashing board's host and slug are what discovery found. if board: facts["ats"], _, facts["ats_slug"] = board.partition(":") - store.add_company(name, key=key, **facts) + store.add_company(name, key=key, hand_reason=reason, **facts) added += 1 return {"added": added, "boards": boards, "skipped": skipped, "total": len(entries), "problems": problems} diff --git a/tests/test_sources.py b/tests/test_sources.py index 7665d3f..dfedad0 100644 --- a/tests/test_sources.py +++ b/tests/test_sources.py @@ -110,7 +110,7 @@ def test_a_board_that_does_not_answer_or_is_unknown_is_refused( with pytest.raises(ValueError, match="did not answer"): sources.set_board(store, "", "greenhouse:gone", name="Gone") with pytest.raises(ValueError, match="host:slug"): - sources.set_board(store, "", "workday:gone", name="Gone") + sources.set_board(store, "", "taleo:gone", name="Gone") with pytest.raises(ValueError, match="needs a name"): sources.set_board(store, "nobody", "greenhouse:acme") with pytest.raises(ValueError, match="is not the company"): @@ -161,20 +161,34 @@ def test_add_discovered_records_an_unreadable_host_with_its_slug( careers_url="https://acme.example/careers") assert sources.add_discovered(store, "Acme", "acme.example", found) == ("acme", 0) store.update_company("acme", notes="keep") - newer = Discovery(ats="workday", ats_slug="acme-inc", + newer = Discovery(ats="taleo", ats_slug="acme-inc", careers_url="https://acme.example/jobs") assert sources.add_discovered(store, "Acme", "acme.example", newer) == ("acme", 0) acme = store.company("acme") assert acme is not None + # The host and slug follow what discovery found last; the link, once + # there, is kept, since a person may have typed it. assert (acme.ats, acme.ats_slug, acme.careers_url, acme.notes) == ( - "workday", "acme-inc", "https://acme.example/jobs", "keep") + "taleo", "acme-inc", "https://acme.example/careers", "keep") + + +def test_add_discovered_fills_a_link_only_where_there_is_none( + store: Store, boards: Boards) -> None: + store.add_company("Acme", ats="teamtailor") + found = Discovery(ats="taleo", ats_slug="acme", careers_url="https://acme.example/jobs") + sources.add_discovered(store, "Acme", "acme.example", found) + assert store.company("acme").careers_url == "https://acme.example/jobs" # type: ignore[union-attr] + + store.update_company("acme", careers_url="https://typed.example/by-hand") + sources.add_discovered(store, "Acme", "acme.example", found) + assert store.company("acme").careers_url == "https://typed.example/by-hand" # type: ignore[union-attr] def test_add_discovered_keeps_a_board_when_nothing_readable_was_found( store: Store, boards: Boards) -> None: sources.set_board(store, "", "greenhouse:acme", name="Acme") with pytest.raises(ValueError, match="nothing changed"): - sources.add_discovered(store, "Acme", "acme.example", Discovery(ats="workday")) + sources.add_discovered(store, "Acme", "acme.example", Discovery(ats="taleo")) acme = store.company("acme") assert acme is not None and acme.ats == "greenhouse" @@ -240,6 +254,143 @@ def test_a_seed_board_that_clashes_still_lands_the_company( assert store.company_subscription("acme-two") is None +def test_a_seed_rerun_fills_an_empty_link_and_reason_and_never_overwrites_one( + store: Store, boards: Boards, tmp_path: Path) -> None: + store.add_company("Bare", key="bare") + store.add_company("Typed", key="typed", careers_url="https://typed.example/by-hand", + hand_reason="blocked", notes="mine") + seed = _seed(tmp_path, + {"name": "Bare", "careers_url": "https://bare.example/jobs", + "hand_reason": "unread"}, + {"name": "Typed", "careers_url": "https://seed.example/jobs", + "hand_reason": "robots"}, + {"name": "Fresh", "careers_url": "https://fresh.example/jobs", + "hand_reason": "key"}) + + result = sources.seed_companies(store, seed) + + assert (result["added"], result["skipped"], result["problems"]) == (1, 2, []) + got = {k: (c.careers_url, c.hand_reason, c.notes) for k in ("bare", "typed", "fresh") + if (c := store.company(k)) is not None} + assert got == { + "bare": ("https://bare.example/jobs", "unread", ""), + "typed": ("https://typed.example/by-hand", "blocked", "mine"), + "fresh": ("https://fresh.example/jobs", "key", "")} + # Run again, nothing moves. + assert sources.seed_companies(store, seed)["added"] == 0 + assert store.company("bare").hand_reason == "unread" # type: ignore[union-attr] + + +def test_a_seed_reason_outside_the_vocabulary_is_a_problem_not_a_row_value( + store: Store, boards: Boards, tmp_path: Path) -> None: + result = sources.seed_companies( + store, _seed(tmp_path, {"name": "Odd", "hand_reason": "lazy"})) + assert result["problems"] == ["odd: 'lazy' is not a hand_reason; kept without it"] + assert store.company("odd").hand_reason == "" # type: ignore[union-attr] + + +def test_a_seed_link_reaches_a_row_whose_board_clashes( + store: Store, boards: Boards, tmp_path: Path) -> None: + sources.set_board(store, "acme", "greenhouse:acme", name="Acme") + store.add_company("Twin", key="twin") + result = sources.seed_companies(store, _seed( + tmp_path, {"name": "Twin", "board": "greenhouse:acme", + "careers_url": "https://twin.example/jobs"})) + assert len(result["problems"]) == 1 + assert store.company("twin").careers_url == "https://twin.example/jobs" # type: ignore[union-attr] + + +# ------------------------------------------------------------- by hand + +def test_a_company_added_by_hand_has_no_board_and_a_checked_link(store: Store) -> None: + company = sources.add_by_hand(store, "Revolut Ltd", careers_url=" https://r.example/jobs ", + notes="login wall") + assert (company.key, company.hand_reason, company.careers_url, company.notes) == ( + "revolut", "added", "https://r.example/jobs", "login wall") + assert store.company_subscription("revolut") is None + with pytest.raises(sources.Conflict, match="on the list already"): + sources.add_by_hand(store, "REVOLUT") + for link in ("javascript:alert(1)", "r.example/jobs", "https://", "ftp://r.example"): + with pytest.raises(ValueError, match="http"): + sources.add_by_hand(store, "Other", careers_url=link) + with pytest.raises(ValueError, match="hand_reason"): + sources.add_by_hand(store, "Other", hand_reason="lazy") + with pytest.raises(ValueError, match="needs a name"): + sources.add_by_hand(store, " ") + assert [c.key for c in store.companies()] == ["revolut"] + + +def test_edit_company_changes_what_it_is_given_and_renames_only_without_a_board( + store: Store, boards: Boards) -> None: + sources.add_by_hand(store, "Acme", notes="old") + edited = sources.edit_company(store, "acme", name="Acme GmbH", hand_reason="gone") + assert (edited.key, edited.name, edited.notes, edited.hand_reason) == ( + "acme", "Acme GmbH", "old", "gone") + with pytest.raises(ValueError, match="needs a name"): + sources.edit_company(store, "acme", name=" ") + with pytest.raises(ValueError, match="not a field"): + sources.edit_company(store, "acme", ats="lever") + with pytest.raises(KeyError): + sources.edit_company(store, "nobody", notes="x") + + sources.set_board(store, "acme", "greenhouse:acme") + with pytest.raises(sources.Conflict, match="detach"): + sources.edit_company(store, "acme", name="Acme Labs") + # The name it has, and any other field, are no rename. + assert sources.edit_company(store, "acme", name="Acme GmbH", notes="new").notes == "new" + sources.detach_board(store, "acme") + with pytest.raises(sources.Conflict): + sources.edit_company(store, "acme", name="Acme Labs") + + +def test_remove_company_refuses_a_board_shown_and_open_roles( + store: Store, boards: Boards) -> None: + agent = store.save_agent("A") + sources.add_by_hand(store, "Hand") + store.set_affinity(agent.id, "hand", 2) + sources.remove_company(store, "hand") + assert store.company("hand") is None + assert store.company("hand", agent.id) is None + with pytest.raises(KeyError): + sources.remove_company(store, "hand") + + sub, _found, _closed = sources.set_board(store, "acme", "greenhouse:acme", name="Acme") + with pytest.raises(sources.Conflict, match="detach the board"): + sources.remove_company(store, "acme") + store.upsert_role(posting("1"), sub) + sources.detach_board(store, "acme") # closes the role: no board, none open + sources.remove_company(store, "acme") + assert store.company("acme") is None + # The detached subscription stays and is found again by a company of that name. + assert store.company_subscription("acme") is not None + + store.add_company("Open", key="open") + other = store.add_subscription("search", "jobindex:x", label="x", board="jobindex") + item = posting("2") + item.raw = {"company": "Open"} + store.upsert_role(item, other) + with pytest.raises(ValueError, match="open roles"): + sources.remove_company(store, "open") + + +def test_by_hand_lists_the_unboarded_never_checked_first( + store: Store, boards: Boards) -> None: + sources.add_by_hand(store, "Zeta") + sources.add_by_hand(store, "Beta") + sources.add_by_hand(store, "Old") + sources.add_by_hand(store, "Recent") + store.update_company("old", checked_at="2026-08-01T00:00:00+00:00") + store.update_company("recent", checked_at="2026-09-20T00:00:00+00:00") + sources.set_board(store, "read", "greenhouse:acme", name="Read") # a board: not listed + store.add_company("Pleo", key="pleo", ats="teamtailor") # unread, no reason: listed + store.add_company("Quiet", key="quiet") # from a search only + sources.set_board(store, "gone", "ashby:acme", name="Gone") + sources.detach_board(store, "gone") # detached: listed + + assert [c.key for c in sources.by_hand(store)] == [ + "beta", "gone", "pleo", "zeta", "old", "recent"] + + # -------------------------------------------------------------------- CLI def _cli(tmp_path: Path, *argv: str) -> int: @@ -289,6 +440,82 @@ def test_cli_seed_prints_counts_and_problems( assert "companies seed: odd: 'nosuch:odd' is not a known board" in captured.err +def test_cli_add_with_one_name_lists_a_company_by_hand_and_with_two_follows_a_board( + store: Store, boards: Boards, tmp_path: Path, + capsys: pytest.CaptureFixture[str]) -> None: + assert _cli(tmp_path, "add", "Revolut", "--careers-url", "https://r.example/jobs", + "--note", "login wall") == 0 + assert json.loads(capsys.readouterr().out) == {"key": "revolut", "hand_reason": "added"} + revolut = store.company("revolut") + assert revolut is not None + assert (revolut.careers_url, revolut.notes, revolut.hand_reason) == ( + "https://r.example/jobs", "login wall", "added") + assert _cli(tmp_path, "add", "Meta", "--reason", "blocked") == 0 + assert store.company("meta").hand_reason == "blocked" # type: ignore[union-attr] + capsys.readouterr() + + assert _cli(tmp_path, "add", "Revolut") == 1 + assert "on the list already" in capsys.readouterr().err + assert _cli(tmp_path, "add", "Odd", "--careers-url", "javascript:alert(1)") == 1 + assert "http" in capsys.readouterr().err + with pytest.raises(SystemExit): # argparse refuses the reason + _cli(tmp_path, "add", "Odd", "--reason", "lazy") + capsys.readouterr() + assert store.company("odd") is None + + # Two words are the board and the company, as before. + assert _cli(tmp_path, "add", "greenhouse:acme", "Acme") == 0 + assert json.loads(capsys.readouterr().out) == { + "key": "acme", "board": "greenhouse:acme", "roles": 3} + + +def test_cli_edit_checked_and_remove_a_company_by_hand( + store: Store, boards: Boards, tmp_path: Path, + capsys: pytest.CaptureFixture[str]) -> None: + store.add_company("Acme", key="acme", notes="keep", hand_reason="unread") + assert _cli(tmp_path, "edit", "acme", "--careers-url", "https://acme.example/jobs", + "--reason", "robots") == 0 + assert json.loads(capsys.readouterr().out) == { + "key": "acme", "name": "Acme", "careers_url": "https://acme.example/jobs", + "notes": "keep", "hand_reason": "robots"} + assert _cli(tmp_path, "edit", "acme", "--note", "") == 0 + assert store.company("acme").notes == "" # type: ignore[union-attr] + assert _cli(tmp_path, "edit", "acme", "--careers-url", "ftp://x") == 1 + assert "http" in capsys.readouterr().err + assert _cli(tmp_path, "edit", "nobody", "--note", "x") == 1 + assert "no company 'nobody'" in capsys.readouterr().err + + assert _cli(tmp_path, "checked", "acme") == 0 + stamp = json.loads(capsys.readouterr().out)["checked_at"] + assert stamp and store.company("acme").checked_at == stamp # type: ignore[union-attr] + assert _cli(tmp_path, "checked", "nobody") == 1 + capsys.readouterr() + + sources.set_board(store, "read", "greenhouse:acme", name="Read") + assert _cli(tmp_path, "remove", "read") == 1 + assert "detach the board first" in capsys.readouterr().err + assert _cli(tmp_path, "remove", "acme") == 0 + assert store.company("acme") is None + assert _cli(tmp_path, "remove", "acme") == 1 + + +def test_cli_by_hand_prints_never_checked_first_then_the_oldest_check( + store: Store, boards: Boards, tmp_path: Path, + capsys: pytest.CaptureFixture[str]) -> None: + store.add_company("Recent", careers_url="https://recent.example/jobs", + hand_reason="gone", checked_at="2026-09-20T08:00:00+00:00") + store.add_company("Old", hand_reason="robots", checked_at="2026-08-01T08:00:00+00:00") + store.add_company("Never", careers_url="https://never.example/jobs", hand_reason="added") + sources.set_board(store, "read", "greenhouse:acme", name="Read") + + assert _cli(tmp_path, "by-hand") == 0 + lines = capsys.readouterr().out.splitlines() + assert [ln.split()[0] for ln in lines[:-1]] == ["Never", "Old", "Recent"] + assert lines[0].split() == ["Never", "added", "never", "https://never.example/jobs"] + assert lines[1].split() == ["Old", "robots", "2026-08-01"] + assert lines[-1] == "3 by hand" + + def test_replacing_or_detaching_a_board_forgets_that_it_was_failing( store: Store, boards: Boards) -> None: from app.jobs.pull import FAILING, failing_boards