返回 last30days-skill
competitor_cli_audit_helpers.py
根目录 / tests / competitor_cli_audit_helpers.py
1 """Run real CLI/fanout wiring while collecting the research boundary inputs."""
2
3 import copy
4 import sys
5 from types import SimpleNamespace
6 from unittest import mock
7
8 import last30days as cli
9 from lib import resolve, schema
10
11
12 def run_competitor_cli(
13 monkeypatch, tmp_path, *, args=(), config=None, has_backend=False, resolutions=None,
14 topic="MainBrand", competitors_list="PeerOne,PeerTwo", companies=None,
15 expected_topics=frozenset({"MainBrand", "PeerOne", "PeerTwo"}), market_cli=None,
16 ):
17 config = {} if config is None else config
18 calls = {}
19 reports = {}
20
21 def research(**kwargs):
22 topic = kwargs["topic"]
23 calls[topic] = kwargs
24 report = schema.Report(
25 topic=topic, range_from="2026-06-01", range_to="2026-06-30",
26 generated_at="2026-06-30T12:00:00Z",
27 provider_runtime=schema.ProviderRuntime("local", "none", "none"),
28 query_plan=schema.QueryPlan("general", "recent", "none", topic, [], {}),
29 clusters=[], ranked_candidates=[], items_by_source={}, errors_by_source={},
30 )
31 reports[topic] = report
32 return report
33
34 if isinstance(resolutions, Exception):
35 resolver = mock.Mock(side_effect=resolutions)
36 else:
37 resolver = mock.Mock(side_effect=lambda topic, _config: (resolutions or {}).get(topic, {}))
38 monkeypatch.setattr(cli.env, "get_config", lambda **_kwargs: config)
39 monkeypatch.setattr(cli.env, "CONFIG_DIR", tmp_path / "config")
40 monkeypatch.setattr(cli.env, "CONFIG_FILE", tmp_path / "config" / ".env")
41 monkeypatch.setattr(cli.pipeline, "diagnose", lambda *_args, **_kwargs: {"available_sources": ["reddit"]})
42 monkeypatch.setattr(cli.pipeline, "run", research)
43 monkeypatch.setattr(resolve, "_has_backend", lambda _config: has_backend)
44 monkeypatch.setattr(resolve, "auto_resolve", resolver)
45 # copy.copy keeps the type of a failed lookup, which dict() would drop.
46 company_resolver = mock.Mock(
47 side_effect=lambda topic, **_kwargs: copy.copy((companies or {}).get(topic, {}))
48 )
49 monkeypatch.setattr(cli.company_resolve, "resolve_company", company_resolver)
50 # The price CLI is absent unless a test hands in a stand-in for it.
51 monkeypatch.setattr(cli.yahoo_finance, "_is_available", lambda: market_cli is not None)
52 monkeypatch.setattr(cli.yahoo_finance.subproc, "run_with_timeout", market_cli)
53 monkeypatch.setenv("LAST30DAYS_SKIP_PREFLIGHT", "1")
54 monkeypatch.setenv("LAST30DAYS_MEMORY_DIR", "")
55 monkeypatch.setenv("LAST30DAYS_STORE", "")
56 monkeypatch.delenv("LAST30DAYS_API_BASE", raising=False)
57 monkeypatch.delenv("LAST30DAYS_API_KEY", raising=False)
58 argv = ["last30days.py", topic]
59 if competitors_list is not None:
60 argv.extend(["--competitors-list", competitors_list])
61 argv.extend(["--emit=json", "--json-profile=raw", "--save-dir=", *args])
62 monkeypatch.setattr(sys, "argv", argv)
63
64 assert cli.main() == 0
65 assert set(calls) == set(expected_topics)
66 return SimpleNamespace(
67 calls=calls, reports=reports, resolver=resolver, company_resolver=company_resolver,
68 )
69
69 lines PYTHON