File size: 16,627 Bytes
8543971
 
 
 
 
e49e827
8543971
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e49e827
 
 
 
 
 
 
 
 
 
 
8543971
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e49e827
8543971
 
 
 
 
 
 
 
1f4cfcb
 
 
 
27e213d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f4cfcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8543971
 
 
e49e827
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8543971
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e49e827
8543971
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
from pathlib import Path

import pytest

from autoteam import cpa_sync
from autoteam import mail as mail_module


def test_list_cpa_files_raises_on_non_200(monkeypatch):
    class _Resp:
        status_code = 503
        text = "service unavailable"

        def json(self):
            raise AssertionError("json() should not be called for non-200 responses")

    monkeypatch.setattr(cpa_sync.requests, "get", lambda *_args, **_kwargs: _Resp())

    with pytest.raises(RuntimeError, match="auth-files list failed"):
        cpa_sync.list_cpa_files()


def test_list_cpa_files_raises_on_non_json(monkeypatch):
    class _Resp:
        status_code = 200
        text = "<html>not json</html>"

        def json(self):
            raise ValueError("not json")

    monkeypatch.setattr(cpa_sync.requests, "get", lambda *_args, **_kwargs: _Resp())

    with pytest.raises(RuntimeError, match="returned non-JSON"):
        cpa_sync.list_cpa_files()


def test_infer_mail_provider_from_email_uses_matching_domain(monkeypatch):
    monkeypatch.setenv("CLOUDMAIL_DOMAIN", "mail.example.com")
    monkeypatch.setenv("MAILLAB_DOMAIN", "lab.example.com")
    monkeypatch.setenv("ADDY_IO_DOMAIN", "alias.example.com")

    assert mail_module.infer_mail_provider_from_email("user@mail.example.com") == "cf_temp_email"
    assert mail_module.infer_mail_provider_from_email("user@lab.example.com") == "maillab"
    assert mail_module.infer_mail_provider_from_email("user@alias.example.com") == "addy_io"
    assert mail_module.infer_mail_provider_from_email("user@unknown.example.com") == ""


def test_sync_to_cpa_skips_disabled_accounts_and_keeps_protected_remote(monkeypatch, tmp_path):
    enabled_auth = tmp_path / "codex-enabled@example.com-team-a.json"
    disabled_auth = tmp_path / "codex-disabled@example.com-team-b.json"
    enabled_auth.write_text('{"access_token":"token-enabled"}', encoding="utf-8")
    disabled_auth.write_text('{"access_token":"token-disabled"}', encoding="utf-8")

    monkeypatch.setattr(
        "autoteam.accounts.load_accounts",
        lambda: [
            {
                "email": "enabled@example.com",
                "status": "active",
                "auth_file": str(enabled_auth),
                "disabled": False,
            },
            {
                "email": "disabled@example.com",
                "status": "active",
                "auth_file": str(disabled_auth),
                "disabled": True,
            },
        ],
    )
    monkeypatch.setattr("autoteam.accounts.save_accounts", lambda _accounts: None)
    monkeypatch.setattr(cpa_sync, "_cleanup_local_duplicates", lambda _accounts: (0, False))
    monkeypatch.setattr(
        cpa_sync,
        "list_cpa_files",
        lambda: [
            {"name": enabled_auth.name, "email": "enabled@example.com"},
            {"name": disabled_auth.name, "email": "disabled@example.com"},
        ],
    )

    uploaded = []
    deleted = []
    monkeypatch.setattr("autoteam.codex_auth.check_codex_quota", lambda *_args, **_kwargs: ("ok", {}))
    monkeypatch.setattr(cpa_sync, "upload_to_cpa", lambda path: uploaded.append(Path(path).name) or True)
    monkeypatch.setattr(cpa_sync, "delete_from_cpa", lambda name: deleted.append(name) or True)

    result = cpa_sync.sync_to_cpa()

    assert uploaded == [enabled_auth.name]
    assert deleted == []
    assert result["disabled_skipped"] == 1
    assert result["delete_guard"]["allow_remote_delete"] is False
    assert result["delete_guard"]["skipped_remote_delete"] == 1


def test_sync_to_cpa_does_not_upload_degraded_grace_accounts(monkeypatch, tmp_path):
    first_auth = tmp_path / "codex-first@example.com-team-a.json"
    second_auth = tmp_path / "codex-second@example.com-team-b.json"
    grace_auth = tmp_path / "codex-grace@example.com-team-c.json"
    first_auth.write_text('{"access_token":"token-first"}', encoding="utf-8")
    second_auth.write_text('{"access_token":"token-second"}', encoding="utf-8")
    grace_auth.write_text('{"access_token":"token-grace"}', encoding="utf-8")

    monkeypatch.setattr(
        "autoteam.accounts.load_accounts",
        lambda: [
            {
                "email": "first@example.com",
                "status": "active",
                "auth_file": str(first_auth),
                "disabled": False,
            },
            {
                "email": "second@example.com",
                "status": "active",
                "auth_file": str(second_auth),
                "disabled": False,
            },
            {
                "email": "grace@example.com",
                "status": "degraded_grace",
                "auth_file": str(grace_auth),
                "disabled": False,
                "mail_provider": "cf_temp_email",
                "mail_account_id": "mail-grace",
            },
        ],
    )
    monkeypatch.setattr("autoteam.accounts.save_accounts", lambda _accounts: None)
    monkeypatch.setattr(cpa_sync, "_cleanup_local_duplicates", lambda _accounts: (0, False))
    monkeypatch.setattr(
        cpa_sync,
        "list_cpa_files",
        lambda: [
            {"name": first_auth.name, "email": "first@example.com"},
            {"name": second_auth.name, "email": "second@example.com"},
            {"name": grace_auth.name, "email": "grace@example.com"},
        ],
    )

    uploaded = []
    deleted = []
    monkeypatch.setattr("autoteam.codex_auth.check_codex_quota", lambda *_args, **_kwargs: ("ok", {}))
    monkeypatch.setattr(cpa_sync, "upload_to_cpa", lambda path: uploaded.append(Path(path).name) or True)
    monkeypatch.setattr(cpa_sync, "delete_from_cpa", lambda name: deleted.append(name) or True)

    result = cpa_sync.sync_to_cpa()

    assert uploaded == [first_auth.name, second_auth.name]
    assert grace_auth.name not in uploaded
    assert deleted == [grace_auth.name]
    assert result["synced_active"] == 2
    assert result["delete_guard"]["allow_remote_delete"] is True


def test_sync_to_cpa_allows_remote_delete_when_active_pool_is_stable(monkeypatch, tmp_path):
    first_auth = tmp_path / "codex-first@example.com-team-a.json"
    second_auth = tmp_path / "codex-second@example.com-team-b.json"
    stale_auth = tmp_path / "codex-stale@example.com-team-c.json"
    first_auth.write_text('{"access_token":"token-first"}', encoding="utf-8")
    second_auth.write_text('{"access_token":"token-second"}', encoding="utf-8")
    stale_auth.write_text('{"access_token":"token-stale"}', encoding="utf-8")

    monkeypatch.setattr(
        "autoteam.accounts.load_accounts",
        lambda: [
            {
                "email": "first@example.com",
                "status": "active",
                "auth_file": str(first_auth),
                "disabled": False,
            },
            {
                "email": "second@example.com",
                "status": "active",
                "auth_file": str(second_auth),
                "disabled": False,
            },
            {
                "email": "stale@example.com",
                "status": "standby",
                "auth_file": "",
                "disabled": False,
            },
        ],
    )
    monkeypatch.setattr("autoteam.accounts.save_accounts", lambda _accounts: None)
    monkeypatch.setattr(cpa_sync, "_cleanup_local_duplicates", lambda _accounts: (0, False))
    monkeypatch.setattr(
        cpa_sync,
        "list_cpa_files",
        lambda: [
            {"name": first_auth.name, "email": "first@example.com"},
            {"name": second_auth.name, "email": "second@example.com"},
            {"name": stale_auth.name, "email": "stale@example.com"},
        ],
    )

    uploaded = []
    deleted = []
    monkeypatch.setattr("autoteam.codex_auth.check_codex_quota", lambda *_args, **_kwargs: ("ok", {}))
    monkeypatch.setattr(cpa_sync, "upload_to_cpa", lambda path: uploaded.append(Path(path).name) or True)
    monkeypatch.setattr(cpa_sync, "delete_from_cpa", lambda name: deleted.append(name) or True)

    result = cpa_sync.sync_to_cpa()

    assert uploaded == [first_auth.name, second_auth.name]
    assert deleted == [stale_auth.name]
    assert result["delete_guard"]["allow_remote_delete"] is True
    assert result["delete_guard"]["skipped_remote_delete"] == 0


def test_sync_to_cpa_preserves_credential_seat_when_remote_delete_is_allowed(monkeypatch, tmp_path):
    active_auth = tmp_path / "codex-active@example.com-team-a.json"
    second_active_auth = tmp_path / "codex-second-active@example.com-team-c.json"
    protected_auth = tmp_path / "codex-protected@example.com-team-b.json"
    active_auth.write_text('{"access_token":"token-active"}', encoding="utf-8")
    second_active_auth.write_text('{"access_token":"token-second-active"}', encoding="utf-8")
    protected_auth.write_text('{"access_token":"token-protected"}', encoding="utf-8")

    monkeypatch.setattr(
        "autoteam.accounts.load_accounts",
        lambda: [
            {
                "email": "active@example.com",
                "status": "active",
                "auth_file": str(active_auth),
                "disabled": False,
                "mail_provider": "cloudflare_temp_email",
                "mail_account_id": 1,
                "cloudmail_account_id": None,
            },
            {
                "email": "second-active@example.com",
                "status": "active",
                "auth_file": str(second_active_auth),
                "disabled": False,
                "mail_provider": "cloudflare_temp_email",
                "mail_account_id": 2,
                "cloudmail_account_id": None,
            },
            {
                "email": "protected@example.com",
                "status": "auth_invalid",
                "auth_file": str(protected_auth),
                "disabled": False,
                "mail_provider": "",
                "mail_account_id": None,
                "cloudmail_account_id": None,
            },
        ],
    )
    monkeypatch.setattr("autoteam.accounts.save_accounts", lambda _accounts: None)
    monkeypatch.setattr(cpa_sync, "_cleanup_local_duplicates", lambda _accounts: (0, False))
    monkeypatch.setattr(
        cpa_sync,
        "list_cpa_files",
        lambda: [
            {"name": active_auth.name, "email": "active@example.com"},
            {"name": second_active_auth.name, "email": "second-active@example.com"},
            {"name": protected_auth.name, "email": "protected@example.com"},
        ],
    )

    uploaded = []
    deleted = []
    monkeypatch.setattr("autoteam.codex_auth.check_codex_quota", lambda *_args, **_kwargs: ("ok", {}))
    monkeypatch.setattr(cpa_sync, "upload_to_cpa", lambda path: uploaded.append(Path(path).name) or True)
    monkeypatch.setattr(cpa_sync, "delete_from_cpa", lambda name: deleted.append(name) or True)

    result = cpa_sync.sync_to_cpa()

    assert uploaded == [active_auth.name, second_active_auth.name]
    assert deleted == []
    assert result["delete_guard"]["allow_remote_delete"] is True
    assert result["delete_guard"]["skipped_protected"] == 1


def test_sync_to_cpa_skips_exhausted_active_credential_before_upload(monkeypatch, tmp_path):
    first_auth = tmp_path / "codex-first@example.com-team-a.json"
    second_auth = tmp_path / "codex-second@example.com-team-b.json"
    exhausted_auth = tmp_path / "codex-exhausted@example.com-team-c.json"
    first_auth.write_text('{"email":"first@example.com","access_token":"token-first"}', encoding="utf-8")
    second_auth.write_text('{"email":"second@example.com","access_token":"token-second"}', encoding="utf-8")
    exhausted_auth.write_text('{"email":"exhausted@example.com","access_token":"token-exhausted"}', encoding="utf-8")

    monkeypatch.setattr(
        "autoteam.accounts.load_accounts",
        lambda: [
            {
                "email": "first@example.com",
                "status": "active",
                "auth_file": str(first_auth),
                "disabled": False,
                "mail_provider": "cf_temp_email",
                "mail_account_id": "mail-1",
            },
            {
                "email": "second@example.com",
                "status": "active",
                "auth_file": str(second_auth),
                "disabled": False,
                "mail_provider": "cf_temp_email",
                "mail_account_id": "mail-2",
            },
            {
                "email": "exhausted@example.com",
                "status": "active",
                "auth_file": str(exhausted_auth),
                "disabled": False,
                "mail_provider": "cf_temp_email",
                "mail_account_id": "mail-3",
            },
        ],
    )
    monkeypatch.setattr("autoteam.accounts.save_accounts", lambda _accounts: None)
    monkeypatch.setattr(cpa_sync, "_cleanup_local_duplicates", lambda _accounts: (0, False))
    monkeypatch.setattr(
        cpa_sync,
        "list_cpa_files",
        lambda: [
            {"name": first_auth.name, "email": "first@example.com"},
            {"name": second_auth.name, "email": "second@example.com"},
            {"name": exhausted_auth.name, "email": "exhausted@example.com"},
        ],
    )

    def fake_quota(token, **_kwargs):
        if token == "token-exhausted":
            return "exhausted", {"primary_pct": 0}
        return "ok", {"primary_pct": 50}

    uploaded = []
    deleted = []
    monkeypatch.setattr("autoteam.codex_auth.check_codex_quota", fake_quota)
    monkeypatch.setattr(cpa_sync, "upload_to_cpa", lambda path: uploaded.append(Path(path).name) or True)
    monkeypatch.setattr(cpa_sync, "delete_from_cpa", lambda name: deleted.append(name) or True)

    result = cpa_sync.sync_to_cpa()

    assert uploaded == [first_auth.name, second_auth.name]
    assert deleted == [exhausted_auth.name]
    assert result["synced_active"] == 2
    assert result["active_publish"]["delete_remote"] == 1
    assert result["delete_guard"]["allow_remote_delete"] is True


def test_sync_to_cpa_keeps_remote_on_active_quota_network_error(monkeypatch, tmp_path):
    auth_file = tmp_path / "codex-active@example.com-team-a.json"
    auth_file.write_text('{"email":"active@example.com","access_token":"token-active"}', encoding="utf-8")

    monkeypatch.setattr(
        "autoteam.accounts.load_accounts",
        lambda: [{"email": "active@example.com", "status": "active", "auth_file": str(auth_file), "disabled": False}],
    )
    monkeypatch.setattr("autoteam.accounts.save_accounts", lambda _accounts: None)
    monkeypatch.setattr(cpa_sync, "_cleanup_local_duplicates", lambda _accounts: (0, False))
    monkeypatch.setattr(cpa_sync, "list_cpa_files", lambda: [{"name": auth_file.name, "email": "active@example.com"}])

    uploaded = []
    deleted = []
    monkeypatch.setattr("autoteam.codex_auth.check_codex_quota", lambda *_args, **_kwargs: ("network_error", {}))
    monkeypatch.setattr(cpa_sync, "upload_to_cpa", lambda path: uploaded.append(Path(path).name) or True)
    monkeypatch.setattr(cpa_sync, "delete_from_cpa", lambda name: deleted.append(name) or True)

    result = cpa_sync.sync_to_cpa()

    assert uploaded == []
    assert deleted == []
    assert result["synced_active"] == 0
    assert result["active_publish"]["kept_remote"] == 1
    assert result["delete_guard"]["allow_remote_delete"] is False


def test_sync_to_cpa_refreshes_proxy_url_before_upload(monkeypatch, tmp_path):
    auth_file = tmp_path / "codex-enabled@example.com-team-a.json"
    auth_file.write_text('{"email":"enabled@example.com","access_token":"token-enabled"}', encoding="utf-8")

    monkeypatch.setattr(
        "autoteam.accounts.load_accounts",
        lambda: [
            {
                "email": "enabled@example.com",
                "status": "active",
                "auth_file": str(auth_file),
                "disabled": False,
            }
        ],
    )
    monkeypatch.setattr("autoteam.accounts.save_accounts", lambda _accounts: None)
    monkeypatch.setattr(cpa_sync, "_cleanup_local_duplicates", lambda _accounts: (0, False))
    monkeypatch.setattr(cpa_sync, "list_cpa_files", lambda: [])
    monkeypatch.setattr("autoteam.codex_auth.check_codex_quota", lambda *_args, **_kwargs: ("ok", {}))
    monkeypatch.setattr(
        "autoteam.ipv6_pool.ipv6_pool.ensure",
        lambda _email: "socks5://proxy.example:30000",
    )

    uploaded = []
    monkeypatch.setattr(cpa_sync, "upload_to_cpa", lambda path: uploaded.append(Path(path).read_text()) or True)

    result = cpa_sync.sync_to_cpa()

    assert result["uploaded"] == 1
    assert '"proxy_url": "socks5://proxy.example:30000"' in uploaded[0]