| 1 | """Main-topic targeting must not reach another entity's pipeline invocation.""" |
| 2 | |
| 3 | import json |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | from tests.competitor_cli_audit_helpers import run_competitor_cli |
| 8 | |
| 9 | |
| 10 | @pytest.mark.parametrize( |
| 11 | ("flag", "value", "keyword", "main_value"), |
| 12 | [ |
| 13 | ("--subreddits", "MainOnly,MainNews", "subreddits", ["MainOnly", "MainNews"]), |
| 14 | ("--x-handle", "MainOnly", "x_handle", "MainOnly"), |
| 15 | ("--x-related", "MainFriend", "x_related", ["MainFriend"]), |
| 16 | ("--github-user", "MainOwner", "github_user", "mainowner"), |
| 17 | ("--github-repo", "mainowner/project", "github_repos", ["mainowner/project"]), |
| 18 | ("--tiktok-hashtags", "MainOnly", "tiktok_hashtags", ["MainOnly"]), |
| 19 | ("--tiktok-creators", "MainCreator", "tiktok_creators", ["MainCreator"]), |
| 20 | ("--ig-creators", "MainInstagram", "ig_creators", ["MainInstagram"]), |
| 21 | ], |
| 22 | ) |
| 23 | def test_main_targeting_does_not_leak_to_peer( |
| 24 | monkeypatch, tmp_path, flag, value, keyword, main_value |
| 25 | ): |
| 26 | observed = run_competitor_cli(monkeypatch, tmp_path, args=[flag, value]) |
| 27 | |
| 28 | assert observed.calls["MainBrand"][keyword] == main_value |
| 29 | for peer in ("PeerOne", "PeerTwo"): |
| 30 | assert observed.calls[peer].get(keyword) is None |
| 31 | |
| 32 | |
| 33 | def test_main_plan_is_not_reused_for_a_peer(monkeypatch, tmp_path): |
| 34 | main_plan = { |
| 35 | "intent": "general", |
| 36 | "freshness_mode": "recent", |
| 37 | "cluster_mode": "none", |
| 38 | "subqueries": [{ |
| 39 | "label": "main-only", "search_query": "MainBrand only", |
| 40 | "ranking_query": "MainBrand", "sources": ["reddit"], |
| 41 | }], |
| 42 | } |
| 43 | plan_file = tmp_path / "main-plan.json" |
| 44 | plan_file.write_text(json.dumps(main_plan), encoding="utf-8") |
| 45 | |
| 46 | observed = run_competitor_cli(monkeypatch, tmp_path, args=["--plan", str(plan_file)]) |
| 47 | |
| 48 | assert observed.calls["MainBrand"]["external_plan"] == main_plan |
| 49 | assert observed.calls["PeerOne"].get("external_plan") is None |
| 50 | assert observed.calls["PeerTwo"].get("external_plan") is None |
| 51 | |
| 52 | |
| 53 | def test_peer_context_does_not_mutate_main_or_an_unresolved_peer(monkeypatch, tmp_path): |
| 54 | config = {"BRAVE_API_KEY": "dummy-brave-key"} |
| 55 | observed = run_competitor_cli( |
| 56 | monkeypatch, tmp_path, args=["--x-handle", "MainOnly"], |
| 57 | config=config, has_backend=True, |
| 58 | resolutions={"PeerOne": {"context": "PeerOne unique context"}, "PeerTwo": {}}, |
| 59 | ) |
| 60 | |
| 61 | assert observed.calls["PeerOne"]["config"].get("_auto_resolve_context", "") == "PeerOne unique context" |
| 62 | assert observed.calls["PeerTwo"]["config"].get("_auto_resolve_context", "") == "" |
| 63 | assert observed.calls["MainBrand"]["config"].get("_auto_resolve_context", "") == "" |
| 64 | assert "_auto_resolve_context" not in config |
| 65 | |
| 66 | |
| 67 | def test_main_context_does_not_leak_to_peer_without_context(monkeypatch, tmp_path): |
| 68 | observed = run_competitor_cli( |
| 69 | monkeypatch, tmp_path, args=["--auto-resolve"], |
| 70 | config={"BRAVE_API_KEY": "dummy-brave-key"}, has_backend=True, |
| 71 | resolutions={ |
| 72 | "MainBrand": {"context": "MainBrand private launch context"}, |
| 73 | "PeerOne": {"context": "PeerOne distinct context"}, |
| 74 | "PeerTwo": {}, |
| 75 | }, |
| 76 | ) |
| 77 | |
| 78 | assert observed.calls["MainBrand"]["config"]["_auto_resolve_context"] == "MainBrand private launch context" |
| 79 | assert observed.calls["PeerOne"]["config"]["_auto_resolve_context"] == "PeerOne distinct context" |
| 80 | assert observed.calls["PeerTwo"]["config"].get("_auto_resolve_context", "") == "" |
| 81 | assert observed.reports["PeerTwo"].artifacts["resolved"]["context"] == "" |
| 82 | |
| 83 | |
| 84 | def test_vs_topic_auto_resolve_targets_each_entity_separately(monkeypatch, tmp_path): |
| 85 | observed = run_competitor_cli( |
| 86 | monkeypatch, tmp_path, topic="MainBrand vs PeerOne vs PeerTwo", |
| 87 | competitors_list=None, args=["--auto-resolve"], |
| 88 | config={"BRAVE_API_KEY": "dummy-brave-key"}, has_backend=True, |
| 89 | resolutions={ |
| 90 | "MainBrand": {"x_handle": "mainbrand", "subreddits": ["mainbrand"]}, |
| 91 | "PeerOne": {"x_handle": "peerone", "subreddits": ["peerone"]}, |
| 92 | "PeerTwo": {"x_handle": "peertwo", "subreddits": ["peertwo"]}, |
| 93 | }, |
| 94 | ) |
| 95 | |
| 96 | resolver_topics = [call.args[0] for call in observed.resolver.call_args_list] |
| 97 | assert resolver_topics[0] == "MainBrand" |
| 98 | assert sorted(resolver_topics[1:]) == ["PeerOne", "PeerTwo"] |
| 99 | assert observed.calls["MainBrand"]["x_handle"] == "mainbrand" |
| 100 | assert observed.calls["PeerOne"]["x_handle"] == "peerone" |
| 101 | assert observed.calls["PeerTwo"]["x_handle"] == "peertwo" |
| 102 |