返回 last30days-skill
test_diagnose_browser_pending.py
根目录 / tests / test_diagnose_browser_pending.py
1 """Tests for the --diagnose / --preflight browser-auth pending signal.
2
3 `--diagnose` and `--preflight` load config in `plan_only` mode, which skips
4 browser-cookie extraction (no Keychain popup). Before this fix that made X drop
5 out of `available_sources` whenever auth came from FROM_BROWSER, even though a
6 real run authenticates X fine — a false-negative that sent a debugging session
7 down a 30-minute wrong path. `env.x_pending_browser_auth` reports the
8 "available pending browser auth" state without reading any cookie, and
9 `pipeline.diagnose` / `pipeline.available_sources` surface it.
10 """
11
12 from unittest import mock
13
14 from lib import env, pipeline
15
16
17 def _cfg(**over):
18 cfg = {"_BROWSER_COOKIE_MODE": "plan_only"}
19 cfg.update(over)
20 return cfg
21
22
23 class TestXPendingBrowserAuth:
24 """The no-read predicate in env.py."""
25
26 def test_pending_true_with_from_browser_and_bird(self):
27 cfg = _cfg(FROM_BROWSER="chrome")
28 with mock.patch("lib.env.get_x_source", return_value=None), \
29 mock.patch("lib.bird_x.is_bird_installed", return_value=True):
30 assert env.x_pending_browser_auth(cfg) is True
31
32 def test_false_when_no_browser_resolved(self):
33 # FROM_BROWSER=off -> cookie_extraction_browsers() returns [].
34 cfg = _cfg(FROM_BROWSER="off")
35 with mock.patch("lib.env.get_x_source", return_value=None), \
36 mock.patch("lib.bird_x.is_bird_installed", return_value=True):
37 assert env.x_pending_browser_auth(cfg) is False
38
39 def test_false_when_from_browser_unset(self):
40 cfg = _cfg()
41 with mock.patch("lib.env.get_x_source", return_value=None), \
42 mock.patch("lib.bird_x.is_bird_installed", return_value=True):
43 assert env.x_pending_browser_auth(cfg) is False
44
45 def test_false_when_x_available_outright(self):
46 # Static bird creds / xurl / xquik -> get_x_source truthy -> not pending.
47 cfg = _cfg(FROM_BROWSER="chrome")
48 with mock.patch("lib.env.get_x_source", return_value="bird"):
49 assert env.x_pending_browser_auth(cfg) is False
50
51 def test_false_when_xai_key_present(self):
52 # Real get_x_source path: an xAI key makes X available outright.
53 cfg = _cfg(FROM_BROWSER="chrome", XAI_API_KEY="dummy-not-real")
54 assert env.x_pending_browser_auth(cfg) is False
55
56 def test_false_when_bird_not_installed(self):
57 cfg = _cfg(FROM_BROWSER="chrome")
58 with mock.patch("lib.env.get_x_source", return_value=None), \
59 mock.patch("lib.bird_x.is_bird_installed", return_value=False):
60 assert env.x_pending_browser_auth(cfg) is False
61
62 def test_false_in_read_mode(self):
63 # A real run (mode=read) has already attempted extraction; its status
64 # must be unchanged, never "pending".
65 cfg = _cfg(FROM_BROWSER="chrome", _BROWSER_COOKIE_MODE="read")
66 with mock.patch("lib.env.get_x_source", return_value=None), \
67 mock.patch("lib.bird_x.is_bird_installed", return_value=True):
68 assert env.x_pending_browser_auth(cfg) is False
69
70 def test_false_with_explicit_cookie_denial(self):
71 cfg = _cfg(FROM_BROWSER="chrome", _BROWSER_COOKIE_MODE="off")
72 with mock.patch("lib.env.get_x_source", return_value=None), \
73 mock.patch("lib.bird_x.is_bird_installed", return_value=True):
74 diag = pipeline.diagnose(cfg, safe=True)
75 assert "x" not in diag["available_sources"]
76 assert diag["x_pending_browser_auth"] is False
77
78 def test_reads_no_cookies(self):
79 # R3: the predicate must never read cookie values or hit Keychain.
80 cfg = _cfg(FROM_BROWSER="chrome")
81 with mock.patch("lib.env.get_x_source", return_value=None), \
82 mock.patch("lib.bird_x.is_bird_installed", return_value=True), \
83 mock.patch("lib.env.extract_browser_credentials",
84 side_effect=AssertionError("must not read cookies")), \
85 mock.patch("lib.cookie_extract.extract_cookies",
86 side_effect=AssertionError("must not read cookies")):
87 assert env.x_pending_browser_auth(cfg) is True
88
89
90 class TestDiagnoseSurfacesPending:
91 """available_sources + diagnose consume the predicate."""
92
93 def test_diagnose_includes_pending_x_and_flag(self):
94 cfg = _cfg(FROM_BROWSER="chrome")
95 with mock.patch("lib.env.get_x_source", return_value=None), \
96 mock.patch("lib.bird_x.is_bird_installed", return_value=True):
97 diag = pipeline.diagnose(cfg, safe=True)
98 assert "x" in diag["available_sources"]
99 assert diag["x_pending_browser_auth"] is True
100 # The safe contract is preserved: no cookie values read.
101 assert diag["browser_cookies"]["reads_values"] is False
102
103 def test_diagnose_excludes_x_when_browser_off(self):
104 cfg = _cfg(FROM_BROWSER="off")
105 with mock.patch("lib.env.get_x_source", return_value=None), \
106 mock.patch("lib.bird_x.is_bird_installed", return_value=True):
107 diag = pipeline.diagnose(cfg, safe=True)
108 assert "x" not in diag["available_sources"]
109 assert diag["x_pending_browser_auth"] is False
110
111 def test_diagnose_x_outright_not_pending(self):
112 cfg = _cfg(FROM_BROWSER="chrome")
113 with mock.patch("lib.env.get_x_source", return_value="bird"):
114 diag = pipeline.diagnose(cfg, safe=True)
115 assert "x" in diag["available_sources"]
116 assert diag["x_pending_browser_auth"] is False
117
118 def test_available_sources_no_double_x(self):
119 # When X is available outright, the else branch must not also append a 2nd "x".
120 cfg = _cfg(FROM_BROWSER="chrome")
121 with mock.patch("lib.env.get_x_source", return_value="bird"):
122 sources = pipeline.available_sources(cfg)
123 assert sources.count("x") == 1
124
125 def test_available_sources_uses_precomputed_x_pending(self):
126 # diagnose() passes x_pending in to avoid a second predicate evaluation;
127 # when provided, available_sources must not call the predicate itself.
128 cfg = _cfg(FROM_BROWSER="chrome")
129 with mock.patch("lib.env.get_x_source", return_value=None), \
130 mock.patch("lib.env.x_pending_browser_auth",
131 side_effect=AssertionError("must use precomputed x_pending")):
132 assert "x" in pipeline.available_sources(cfg, x_pending=True)
133 assert "x" not in pipeline.available_sources(cfg, x_pending=False)
134
135 def test_diagnose_evaluates_predicate_once(self):
136 # The tidy: x_pending_browser_auth runs exactly once per diagnose() call.
137 cfg = _cfg(FROM_BROWSER="chrome")
138 with mock.patch("lib.env.get_x_source", return_value=None), \
139 mock.patch("lib.bird_x.is_bird_installed", return_value=True), \
140 mock.patch("lib.env.x_pending_browser_auth", wraps=env.x_pending_browser_auth) as spy:
141 diag = pipeline.diagnose(cfg, safe=True)
142 assert spy.call_count == 1
143 assert "x" in diag["available_sources"]
144 assert diag["x_pending_browser_auth"] is True
145
145 lines PYTHON