返回 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_MAX_RESULTS": "3",
54 "LAST30DAYS_PERPLEXITY_SEARCH_CONTEXT_SIZE": "low",
55 "LAST30DAYS_PERPLEXITY_SEARCH_MODE": "academic",
56 "LAST30DAYS_PERPLEXITY_DOMAIN_FILTER": "example.com",
57 "LAST30DAYS_PERPLEXITY_LANGUAGE_FILTER": "en",
58 "LAST30DAYS_PERPLEXITY_COUNTRY": "US",
59 "LAST30DAYS_PERPLEXITY_RECENCY_FILTER": "week",
60 "LAST30DAYS_PERPLEXITY_REASONING_EFFORT": "high",
61 "LAST30DAYS_PERPLEXITY_DEEP_TIMEOUT_SECONDS": "600",
62 }
63 with mock.patch.object(env, "CONFIG_FILE", None), \
64 mock.patch.object(env, "_find_project_env", return_value=None), \
65 mock.patch("lib.env._load_keychain", return_value={}), \
66 mock.patch("lib.env._load_pass", return_value={}), \
67 mock.patch.dict(os.environ, overrides, clear=False):
68 config = env.get_config()
69
70 for key, value in overrides.items():
71 self.assertEqual(value, config[key])
72
73
74 class XurlSafePathGatingTests(unittest.TestCase):
75 """F1: the safe/diagnose path (probe=False — what doctor uses) never
76 runs xurl's live `whoami` network check; it keys on local evidence
77 (xurl_x.has_stored_auth) instead."""
78
79 BIRD_OFF = {
80 "installed": False,
81 "authenticated": False,
82 "username": None,
83 "can_install": True,
84 }
85
86 def _status(self, config, probe, stored_mock, live_mock):
87 with mock.patch("lib.bird_x.get_bird_status", return_value=dict(self.BIRD_OFF)), \
88 mock.patch("lib.bird_x.set_credentials", lambda *a, **k: None), \
89 mock.patch("lib.xurl_x.has_stored_auth", **stored_mock), \
90 mock.patch("lib.xurl_x.is_available", **live_mock):
91 return env.get_x_source_status(config, probe=probe)
92
93 _LIVE_FORBIDDEN = {
94 "side_effect": AssertionError(
95 "probe=False must not run the live `xurl whoami` network check"
96 )
97 }
98 _STORED_FORBIDDEN = {
99 "side_effect": AssertionError("probe=True should use the live check")
100 }
101
102 def test_probe_false_uses_local_evidence_only(self):
103 status = self._status(
104 {}, probe=False,
105 stored_mock={"return_value": True}, live_mock=self._LIVE_FORBIDDEN,
106 )
107 self.assertEqual("xurl", status["source"])
108 self.assertTrue(status["xurl_available"])
109
110 def test_probe_false_without_stored_auth_reports_unavailable(self):
111 status = self._status(
112 {}, probe=False,
113 stored_mock={"return_value": False}, live_mock=self._LIVE_FORBIDDEN,
114 )
115 self.assertIsNone(status["source"])
116 self.assertFalse(status["xurl_available"])
117
118 def test_probe_true_keeps_the_live_check(self):
119 status = self._status(
120 {}, probe=True,
121 stored_mock=self._STORED_FORBIDDEN, live_mock={"return_value": True},
122 )
123 self.assertEqual("xurl", status["source"])
124 self.assertTrue(status["xurl_available"])
125
126 def test_x_backend_chain_local_only_never_calls_live_check(self):
127 with mock.patch(
128 "lib.xurl_x.is_available",
129 side_effect=AssertionError("local_only chain must not run `xurl whoami`"),
130 ), mock.patch("lib.xurl_x.has_stored_auth", return_value=True):
131 chain = env.x_backend_chain({}, local_only=True)
132 self.assertEqual(["xurl"], chain)
133
134 def test_x_backend_chain_default_stays_live(self):
135 with mock.patch("lib.xurl_x.is_available", return_value=True), \
136 mock.patch(
137 "lib.xurl_x.has_stored_auth",
138 side_effect=AssertionError("default chain should use the live check"),
139 ):
140 chain = env.x_backend_chain({})
141 self.assertEqual(["xurl"], chain)
142
143
144 class ThreadsAvailabilityTests(unittest.TestCase):
145 """Threads is in the SC default-on family: same key, same per-call cost
146 shape as TikTok / Instagram, so the same default-on rule applies.
147 Suppression goes through EXCLUDE_SOURCES, not gated opt-in."""
148
149 def test_threads_available_with_sc_key_only(self):
150 self.assertTrue(env.is_threads_available({"SCRAPECREATORS_API_KEY": "k"}))
151
152 def test_threads_unavailable_without_sc_key(self):
153 self.assertFalse(env.is_threads_available({}))
154 self.assertFalse(env.is_threads_available({"INCLUDE_SOURCES": "threads"}))
155
156 def test_threads_availability_predicate_is_key_only(self):
157 """is_threads_available is availability-only (key present).
158
159 Scheduling is gated separately by INCLUDE_SOURCES in the pipeline's
160 available_sources (see TestScrapeCreatorsTierGating) — the predicate
161 itself only reports whether the credential exists.
162 """
163 self.assertTrue(env.is_threads_available({
164 "SCRAPECREATORS_API_KEY": "k",
165 "INCLUDE_SOURCES": "",
166 }))
167
168 if __name__ == "__main__":
169 unittest.main()
170
170 lines PYTHON