返回 last30days-skill
test_env_v3.py
根目录 / tests / test_env_v3.py
1 import os
2 import unittest
3 from pathlib import Path
4 from unittest import mock
5
6 from lib import bird_x, env
7
8
9 class EnvV3Tests(unittest.TestCase):
10 def setUp(self):
11 self._saved_credentials = dict(bird_x._credentials)
12
13 def tearDown(self):
14 bird_x._credentials.clear()
15 bird_x._credentials.update(self._saved_credentials)
16
17 def test_x_source_prefers_xai_without_bird_probe(self):
18 with mock.patch("lib.bird_x.is_bird_authenticated", side_effect=AssertionError("should not probe bird auth")):
19 source = env.get_x_source({"XAI_API_KEY": "test"})
20 self.assertEqual("xai", source)
21
22 def test_x_source_uses_bird_with_explicit_cookies(self):
23 with mock.patch("lib.bird_x.is_bird_installed", return_value=True):
24 source = env.get_x_source({"AUTH_TOKEN": "a", "CT0": "b"})
25 self.assertEqual("bird", source)
26 self.assertEqual("a", bird_x._credentials["AUTH_TOKEN"])
27 self.assertEqual("b", bird_x._credentials["CT0"])
28
29 def test_bird_auth_never_checks_browser_cookies(self):
30 # The guarantee: is_bird_authenticated() must not spawn any child
31 # process to probe for cookies. All subprocess paths in bird_x go
32 # through subproc.run_with_timeout, so patching that covers it.
33 with mock.patch("lib.bird_x.is_bird_installed", return_value=True), mock.patch(
34 "lib.bird_x.subproc.run_with_timeout",
35 side_effect=AssertionError("browser-cookie whoami should not run"),
36 ):
37 bird_x._credentials.clear()
38 with mock.patch.dict(os.environ, {}, clear=False):
39 self.assertIsNone(bird_x.is_bird_authenticated())
40
41 def test_file_permission_check_skips_windows_posix_mode_bits(self):
42 path = mock.Mock(spec=Path)
43 with mock.patch.object(env.os, "name", "nt"), mock.patch.object(env.sys.stderr, "write") as write:
44 env._check_file_permissions(path)
45
46 path.stat.assert_not_called()
47 write.assert_not_called()
48
49 def test_get_config_includes_perplexity_knobs(self):
50 overrides = {
51 "LAST30DAYS_PERPLEXITY_MODE": "search",
52 "LAST30DAYS_PERPLEXITY_MODEL": "sonar-reasoning-pro",
53 "LAST30DAYS_PERPLEXITY_AGENT_MODEL": "perplexity/sonar",
54 "LAST30DAYS_PERPLEXITY_AGENT_PRESET": "low",
55 "LAST30DAYS_PERPLEXITY_AGENT_MAX_STEPS": "4",
56 "LAST30DAYS_PERPLEXITY_AGENT_MAX_OUTPUT_TOKENS": "8192",
57 "LAST30DAYS_PERPLEXITY_AGENT_TIMEOUT_SECONDS": "120",
58 "LAST30DAYS_PERPLEXITY_MAX_RESULTS": "3",
59 "LAST30DAYS_PERPLEXITY_SEARCH_CONTEXT_SIZE": "low",
60 "LAST30DAYS_PERPLEXITY_SEARCH_MODE": "academic",
61 "LAST30DAYS_PERPLEXITY_DOMAIN_FILTER": "example.com",
62 "LAST30DAYS_PERPLEXITY_LANGUAGE_FILTER": "en",
63 "LAST30DAYS_PERPLEXITY_COUNTRY": "US",
64 "LAST30DAYS_PERPLEXITY_RECENCY_FILTER": "week",
65 "LAST30DAYS_PERPLEXITY_REASONING_EFFORT": "high",
66 "LAST30DAYS_PERPLEXITY_DEEP_TIMEOUT_SECONDS": "600",
67 }
68 with mock.patch.object(env, "CONFIG_FILE", None), \
69 mock.patch.object(env, "_find_project_env", return_value=None), \
70 mock.patch("lib.env._load_keychain", return_value={}), \
71 mock.patch("lib.env._load_pass", return_value={}), \
72 mock.patch.dict(os.environ, overrides, clear=False):
73 config = env.get_config()
74
75 for key, value in overrides.items():
76 self.assertEqual(value, config[key])
77
78
79 class XurlSafePathGatingTests(unittest.TestCase):
80 """F1: the safe/diagnose path (probe=False — what doctor uses) never
81 runs xurl's live `whoami` network check; it keys on local evidence
82 (xurl_x.has_stored_auth) instead."""
83
84 BIRD_OFF = {
85 "installed": False,
86 "authenticated": False,
87 "username": None,
88 "can_install": True,
89 }
90
91 def _status(self, config, probe, stored_mock, live_mock):
92 with mock.patch("lib.bird_x.get_bird_status", return_value=dict(self.BIRD_OFF)), \
93 mock.patch("lib.bird_x.set_credentials", lambda *a, **k: None), \
94 mock.patch("lib.xurl_x.has_stored_auth", **stored_mock), \
95 mock.patch("lib.xurl_x.is_available", **live_mock):
96 return env.get_x_source_status(config, probe=probe)
97
98 _LIVE_FORBIDDEN = {
99 "side_effect": AssertionError(
100 "probe=False must not run the live `xurl whoami` network check"
101 )
102 }
103 _STORED_FORBIDDEN = {
104 "side_effect": AssertionError("probe=True should use the live check")
105 }
106
107 def test_probe_false_uses_local_evidence_only(self):
108 status = self._status(
109 {}, probe=False,
110 stored_mock={"return_value": True}, live_mock=self._LIVE_FORBIDDEN,
111 )
112 self.assertEqual("xurl", status["source"])
113 self.assertTrue(status["xurl_available"])
114
115 def test_probe_false_without_stored_auth_reports_unavailable(self):
116 status = self._status(
117 {}, probe=False,
118 stored_mock={"return_value": False}, live_mock=self._LIVE_FORBIDDEN,
119 )
120 self.assertIsNone(status["source"])
121 self.assertFalse(status["xurl_available"])
122
123 def test_probe_true_keeps_the_live_check(self):
124 status = self._status(
125 {}, probe=True,
126 stored_mock=self._STORED_FORBIDDEN, live_mock={"return_value": True},
127 )
128 self.assertEqual("xurl", status["source"])
129 self.assertTrue(status["xurl_available"])
130
131 def test_x_backend_chain_local_only_never_calls_live_check(self):
132 with mock.patch(
133 "lib.xurl_x.is_available",
134 side_effect=AssertionError("local_only chain must not run `xurl whoami`"),
135 ), mock.patch("lib.xurl_x.has_stored_auth", return_value=True):
136 chain = env.x_backend_chain({}, local_only=True)
137 self.assertEqual(["xurl"], chain)
138
139 def test_x_backend_chain_default_stays_live(self):
140 with mock.patch("lib.xurl_x.is_available", return_value=True), \
141 mock.patch(
142 "lib.xurl_x.has_stored_auth",
143 side_effect=AssertionError("default chain should use the live check"),
144 ):
145 chain = env.x_backend_chain({})
146 self.assertEqual(["xurl"], chain)
147
148
149 class ThreadsAvailabilityTests(unittest.TestCase):
150 """Threads is in the SC default-on family: same key, same per-call cost
151 shape as TikTok / Instagram, so the same default-on rule applies.
152 Suppression goes through EXCLUDE_SOURCES, not gated opt-in."""
153
154 def test_threads_available_with_sc_key_only(self):
155 self.assertTrue(env.is_threads_available({"SCRAPECREATORS_API_KEY": "k"}))
156
157 def test_threads_unavailable_without_sc_key(self):
158 self.assertFalse(env.is_threads_available({}))
159 self.assertFalse(env.is_threads_available({"INCLUDE_SOURCES": "threads"}))
160
161 def test_threads_availability_predicate_is_key_only(self):
162 """is_threads_available is availability-only (key present).
163
164 Scheduling is gated separately by INCLUDE_SOURCES in the pipeline's
165 available_sources (see TestScrapeCreatorsTierGating) — the predicate
166 itself only reports whether the credential exists.
167 """
168 self.assertTrue(env.is_threads_available({
169 "SCRAPECREATORS_API_KEY": "k",
170 "INCLUDE_SOURCES": "",
171 }))
172
173 if __name__ == "__main__":
174 unittest.main()
175
175 lines PYTHON