forked from Alishahryar1/free-claude-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_entrypoints.py
More file actions
1023 lines (846 loc) · 35.5 KB
/
Copy pathtest_entrypoints.py
File metadata and controls
1023 lines (846 loc) · 35.5 KB
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Tests for cli/entrypoints.py — fcc-init scaffolding logic."""
import json
import tomllib
from collections.abc import Callable
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
from urllib.error import URLError
from urllib.request import Request
import pytest
from free_claude_code.config.settings import Settings
def _launcher_settings(
*,
port: int = 8082,
token: str = "freecc",
open_admin_browser: bool = True,
) -> Settings:
return Settings.model_construct(
host="0.0.0.0",
port=port,
anthropic_auth_token=token,
model="nvidia_nim/test-model",
open_admin_browser=open_admin_browser,
)
def _run_init(tmp_home: Path) -> tuple[str, Path]:
"""Run init() with home directory redirected to tmp_home. Returns (printed output, env_file path)."""
from free_claude_code.cli.entrypoints import init
env_file = tmp_home / ".fcc" / ".env"
printed: list[str] = []
with (
patch("pathlib.Path.home", return_value=tmp_home),
patch(
"builtins.print",
side_effect=lambda *a: printed.append(" ".join(str(x) for x in a)),
),
):
init()
return "\n".join(printed), env_file
class _JsonResponse:
def __init__(self, payload: dict[str, object]) -> None:
self._payload = payload
def __enter__(self) -> _JsonResponse:
return self
def __exit__(self, *_args: object) -> None:
return None
def read(self) -> bytes:
return json.dumps(self._payload).encode("utf-8")
def test_init_creates_env_file(tmp_path: Path) -> None:
"""init() creates .env from the bundled template when it doesn't exist yet."""
output, env_file = _run_init(tmp_path)
assert env_file.exists()
assert env_file.stat().st_size > 0
assert str(env_file) in output
def test_init_copies_template_content(tmp_path: Path) -> None:
"""init() writes the canonical root env.example content, not an empty file."""
template = (Path(__file__).resolve().parents[2] / ".env.example").read_text(
encoding="utf-8"
)
_, env_file = _run_init(tmp_path)
assert env_file.read_text("utf-8") == template
def test_init_migrates_home_checkout_env_before_template(tmp_path: Path) -> None:
"""init() preserves users who kept config in ~/free-claude-code/.env."""
legacy_env = tmp_path / "free-claude-code" / ".env"
legacy_env.parent.mkdir(parents=True)
legacy_env.write_text("MODEL=deepseek/deepseek-chat\n", encoding="utf-8")
output, env_file = _run_init(tmp_path)
assert env_file.read_text("utf-8") == "MODEL=deepseek/deepseek-chat\n"
assert f"Config migrated from {legacy_env}" in output
def test_init_migrates_legacy_xdg_env_before_template(tmp_path: Path) -> None:
"""init() preserves users who kept config in ~/.config/free-claude-code/.env."""
legacy_env = tmp_path / ".config" / "free-claude-code" / ".env"
legacy_env.parent.mkdir(parents=True)
legacy_env.write_text("MODEL=open_router/free-model\n", encoding="utf-8")
output, env_file = _run_init(tmp_path)
assert env_file.read_text("utf-8") == "MODEL=open_router/free-model\n"
assert f"Config migrated from {legacy_env}" in output
def test_legacy_env_migration_does_not_overwrite_managed_env(
tmp_path: Path,
) -> None:
"""Legacy migration never overwrites an existing ~/.fcc/.env."""
from free_claude_code.cli.entrypoints import _migrate_legacy_env_if_missing
managed_env = tmp_path / ".fcc" / ".env"
managed_env.parent.mkdir(parents=True)
managed_env.write_text("MODEL=nvidia_nim/current\n", encoding="utf-8")
legacy_env = tmp_path / "free-claude-code" / ".env"
legacy_env.parent.mkdir(parents=True)
legacy_env.write_text("MODEL=deepseek/legacy\n", encoding="utf-8")
with patch("pathlib.Path.home", return_value=tmp_path):
migrated_from = _migrate_legacy_env_if_missing()
assert migrated_from is None
assert managed_env.read_text("utf-8") == "MODEL=nvidia_nim/current\n"
def test_env_template_loader_uses_root_template_in_source_checkout() -> None:
"""Source checkout fallback uses the root .env.example as the single source."""
from free_claude_code.config.env_template import load_env_template
template = (Path(__file__).resolve().parents[2] / ".env.example").read_text(
encoding="utf-8"
)
assert load_env_template() == template
def test_init_creates_parent_directories(tmp_path: Path) -> None:
"""init() creates ~/.fcc/ even if it doesn't exist."""
config_dir = tmp_path / ".fcc"
assert not config_dir.exists()
_run_init(tmp_path)
assert config_dir.is_dir()
def test_init_skips_if_env_already_exists(tmp_path: Path) -> None:
"""init() does not overwrite an existing .env and prints a warning."""
# Create it first
_run_init(tmp_path)
env_file = tmp_path / ".fcc" / ".env"
env_file.write_text("existing content", encoding="utf-8")
output, _ = _run_init(tmp_path)
assert env_file.read_text("utf-8") == "existing content"
assert "already exists" in output
def test_init_prints_next_step_hint(tmp_path: Path) -> None:
"""init() tells the user to run fcc-server after editing .env."""
output, _ = _run_init(tmp_path)
assert "fcc-server" in output
def test_cli_scripts_are_registered() -> None:
pyproject = tomllib.loads(
(Path(__file__).resolve().parents[2] / "pyproject.toml").read_text(
encoding="utf-8"
)
)
scripts = pyproject["project"]["scripts"]
assert scripts["fcc-server"] == "free_claude_code.cli.entrypoints:serve"
assert scripts["free-claude-code"] == "free_claude_code.cli.entrypoints:serve"
assert scripts["fcc-claude"] == "free_claude_code.cli.launchers.claude:launch"
assert scripts["fcc-codex"] == "free_claude_code.cli.launchers.codex:launch"
assert scripts["fcc-pi"] == "free_claude_code.cli.launchers.pi:launch"
@pytest.mark.parametrize("entrypoint_name", ["serve", "init"])
@pytest.mark.parametrize(
"argv",
[("--version",), ("--version", "--help"), ("--help", "--version")],
)
def test_fcc_owned_entrypoints_report_version_without_side_effects(
entrypoint_name: str,
argv: tuple[str, ...],
capsys: pytest.CaptureFixture[str],
) -> None:
from free_claude_code.cli import entrypoints
with (
patch.object(entrypoints, "package_version", return_value="9.8.7"),
patch.object(entrypoints, "_migrate_legacy_env_if_missing") as migrate_legacy,
patch.object(entrypoints, "_migrate_config_env_keys") as migrate_keys,
patch.object(entrypoints, "get_settings") as get_settings,
patch.object(
entrypoints, "_run_supervised_server", return_value=False
) as run_server,
patch.object(entrypoints, "kill_all_best_effort") as kill_all,
patch.object(entrypoints, "config_dir_path") as config_dir,
patch.object(entrypoints, "managed_env_path") as managed_env,
patch.object(entrypoints, "load_env_template") as load_template,
):
getattr(entrypoints, entrypoint_name)(argv)
assert capsys.readouterr() == ("free-claude-code 9.8.7\n", "")
for side_effect in {
migrate_legacy,
migrate_keys,
get_settings,
run_server,
kill_all,
config_dir,
managed_env,
load_template,
}:
side_effect.assert_not_called()
def test_schedule_open_admin_browser_opens_when_health_ready() -> None:
"""Opening /admin runs after /health preflight succeeds."""
from free_claude_code.cli import entrypoints
from free_claude_code.config.server_urls import local_admin_url
settings = _launcher_settings(port=31337)
opened_urls: list[str] = []
class ImmediateThread:
def __init__(self, target=None, **_kwargs: object) -> None:
self._target = target
def start(self) -> None:
assert self._target is not None
self._target()
with (
patch.object(entrypoints.threading, "Thread", ImmediateThread),
patch.object(entrypoints, "preflight_proxy", return_value=None),
patch.object(
entrypoints.webbrowser,
"open",
side_effect=lambda url: opened_urls.append(url),
),
patch.object(entrypoints.time, "sleep"),
):
entrypoints._schedule_open_admin_browser(settings)
assert opened_urls == [local_admin_url(settings)]
def test_serve_skips_admin_browser_when_setting_is_disabled() -> None:
from free_claude_code.cli import entrypoints
settings = _launcher_settings(open_admin_browser=False)
get_settings = MagicMock(return_value=settings)
get_settings.cache_clear = MagicMock()
with (
patch.object(entrypoints, "get_settings", get_settings),
patch.object(
entrypoints, "_run_supervised_server", return_value=False
) as run_server,
patch.object(entrypoints, "kill_all_best_effort"),
):
entrypoints.serve()
run_server.assert_called_once_with(settings, open_admin_browser=False)
def test_serve_supervisor_restarts_when_app_requests_restart() -> None:
from free_claude_code.cli import entrypoints
settings = _launcher_settings()
get_settings = MagicMock(side_effect=[settings, settings])
get_settings.cache_clear = MagicMock()
servers: list[object] = []
restart_callbacks: list[Callable[[], None]] = []
apps: list[SimpleNamespace] = []
def build_asgi_app(_settings: Settings, restart_callback: Callable[[], None]):
restart_callbacks.append(restart_callback)
app = SimpleNamespace(runtime=SimpleNamespace(is_closed=False))
apps.append(app)
return app
class FakeServer:
def __init__(self, config):
self.config = config
self.should_exit = False
servers.append(self)
def run(self):
if len(servers) == 1:
restart_callbacks[-1]()
assert self.should_exit is True
self.config.app.runtime.is_closed = True
def fake_config(app, **kwargs):
return SimpleNamespace(app=app, kwargs=kwargs)
with (
patch.object(entrypoints, "get_settings", get_settings),
patch.object(entrypoints.uvicorn, "Config", side_effect=fake_config),
patch.object(entrypoints.uvicorn, "Server", side_effect=FakeServer),
patch.object(entrypoints, "build_asgi_app", side_effect=build_asgi_app),
patch.object(
entrypoints, "_schedule_open_admin_browser"
) as schedule_open_admin,
patch.object(entrypoints, "kill_all_best_effort") as kill_all,
):
entrypoints.serve()
assert len(servers) == 2
schedule_open_admin.assert_called_once_with(settings)
get_settings.cache_clear.assert_called_once()
kill_all.assert_called_once()
def test_serve_supervisor_refuses_restart_after_incomplete_shutdown() -> None:
from free_claude_code.cli import entrypoints
settings = _launcher_settings()
get_settings = MagicMock(return_value=settings)
get_settings.cache_clear = MagicMock()
servers: list[object] = []
restart_callbacks: list[Callable[[], None]] = []
def build_asgi_app(_settings: Settings, restart_callback: Callable[[], None]):
restart_callbacks.append(restart_callback)
return SimpleNamespace(runtime=SimpleNamespace(is_closed=False))
class FakeServer:
def __init__(self, config):
self.config = config
self.should_exit = False
servers.append(self)
def run(self):
restart_callbacks[-1]()
assert self.should_exit is True
def fake_config(app, **kwargs):
return SimpleNamespace(app=app, kwargs=kwargs)
with (
patch.object(entrypoints, "get_settings", get_settings),
patch.object(entrypoints.uvicorn, "Config", side_effect=fake_config),
patch.object(entrypoints.uvicorn, "Server", side_effect=FakeServer),
patch.object(entrypoints, "build_asgi_app", side_effect=build_asgi_app),
patch.object(entrypoints, "_schedule_open_admin_browser"),
patch.object(entrypoints, "kill_all_best_effort") as kill_all,
):
entrypoints.serve()
assert len(servers) == 1
get_settings.cache_clear.assert_not_called()
kill_all.assert_called_once()
def test_serve_migrates_legacy_env_before_loading_settings(tmp_path: Path) -> None:
from free_claude_code.cli import entrypoints
legacy_env = tmp_path / "free-claude-code" / ".env"
legacy_env.parent.mkdir(parents=True)
legacy_env.write_text("MODEL=deepseek/deepseek-chat\n", encoding="utf-8")
settings = _launcher_settings()
get_settings = MagicMock(return_value=settings)
get_settings.cache_clear = MagicMock()
with (
patch("pathlib.Path.home", return_value=tmp_path),
patch.object(entrypoints, "get_settings", get_settings),
patch.object(entrypoints, "_run_supervised_server", return_value=False),
patch.object(entrypoints, "kill_all_best_effort"),
):
entrypoints.serve()
assert (tmp_path / ".fcc" / ".env").read_text("utf-8") == (
"MODEL=deepseek/deepseek-chat\n"
)
get_settings.assert_called_once_with()
def test_serve_migrates_hf_token_before_loading_settings(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
) -> None:
from free_claude_code.cli import entrypoints
repo = tmp_path / "repo"
repo.mkdir()
(repo / ".env").write_text("HF_TOKEN=legacy-hf\n", encoding="utf-8")
settings = _launcher_settings()
get_settings = MagicMock(return_value=settings)
get_settings.cache_clear = MagicMock()
monkeypatch.chdir(repo)
with (
patch("pathlib.Path.home", return_value=tmp_path),
patch.object(entrypoints, "get_settings", get_settings),
patch.object(entrypoints, "_run_supervised_server", return_value=False),
patch.object(entrypoints, "kill_all_best_effort"),
patch.object(entrypoints, "explicit_env_file_huggingface_warning"),
):
entrypoints.serve()
assert (repo / ".env").read_text(encoding="utf-8") == (
"HUGGINGFACE_API_KEY=legacy-hf\n"
)
get_settings.assert_called_once_with()
def test_config_env_key_migration_warns_for_explicit_env_file(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
from free_claude_code.cli import entrypoints
explicit = tmp_path / "custom.env"
explicit.write_text("HF_TOKEN=legacy-hf\n", encoding="utf-8")
with patch.dict(entrypoints.os.environ, {"FCC_ENV_FILE": str(explicit)}):
migrated = entrypoints._migrate_config_env_keys()
assert migrated == ()
assert "HF_TOKEN" in capsys.readouterr().err
assert explicit.read_text(encoding="utf-8") == "HF_TOKEN=legacy-hf\n"
def test_serve_handles_keyboard_interrupt_without_traceback() -> None:
from free_claude_code.cli import entrypoints
settings = _launcher_settings()
get_settings = MagicMock(return_value=settings)
get_settings.cache_clear = MagicMock()
with (
patch.object(entrypoints, "get_settings", get_settings),
patch.object(
entrypoints,
"_run_supervised_server",
side_effect=KeyboardInterrupt,
),
patch.object(entrypoints, "kill_all_best_effort") as kill_all,
):
entrypoints.serve()
get_settings.cache_clear.assert_not_called()
kill_all.assert_called_once()
def test_claude_child_env_targets_current_proxy_config() -> None:
from free_claude_code.cli.claude_env import build_claude_proxy_env
env = build_claude_proxy_env(
proxy_root_url="http://127.0.0.1:9090",
auth_token=" proxy-token ",
base_env={
"PATH": "keep",
"ANTHROPIC_API_URL": "https://api.anthropic.com/v1",
"ANTHROPIC_BASE_URL": "https://api.anthropic.com",
"ANTHROPIC_AUTH_TOKEN": "old-token",
"ANTHROPIC_API_KEY": "official-key",
"CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY": "0",
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",
"DISABLE_AUTOUPDATER": "0",
"DISABLE_FEEDBACK_COMMAND": "0",
"DISABLE_ERROR_REPORTING": "0",
"DISABLE_TELEMETRY": "0",
},
)
assert env["PATH"] == "keep"
assert env["ANTHROPIC_BASE_URL"] == "http://127.0.0.1:9090"
assert env["ANTHROPIC_AUTH_TOKEN"] == "proxy-token"
assert env["CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY"] == "1"
assert env["CLAUDE_CODE_AUTO_COMPACT_WINDOW"] == "190000"
assert env["DISABLE_AUTOUPDATER"] == "1"
assert env["DISABLE_FEEDBACK_COMMAND"] == "1"
assert env["DISABLE_ERROR_REPORTING"] == "1"
assert env["DISABLE_TELEMETRY"] == "1"
assert "ANTHROPIC_API_URL" not in env
assert "ANTHROPIC_API_KEY" not in env
assert "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC" not in env
def test_claude_child_env_uses_sentinel_for_blank_configured_auth_token() -> None:
from free_claude_code.cli.claude_env import build_claude_proxy_env
env = build_claude_proxy_env(
proxy_root_url="http://127.0.0.1:8082",
auth_token="",
base_env={
"ANTHROPIC_AUTH_TOKEN": "inherited-token",
"ANTHROPIC_API_KEY": "official-key",
},
)
assert env["ANTHROPIC_AUTH_TOKEN"] == "fcc-no-auth"
assert "ANTHROPIC_API_KEY" not in env
def test_launch_claude_passes_args_and_child_env(
monkeypatch: pytest.MonkeyPatch,
) -> None:
from free_claude_code.cli.launchers.claude import launch
monkeypatch.setenv("ANTHROPIC_BASE_URL", "https://api.anthropic.com")
monkeypatch.setenv("ANTHROPIC_AUTH_TOKEN", "old-token")
monkeypatch.setenv("KEEP_ME", "yes")
settings = _launcher_settings(port=9191, token="proxy-token")
with (
patch(
"free_claude_code.cli.launchers.claude.get_settings", return_value=settings
),
patch(
"free_claude_code.cli.launchers.claude.preflight_proxy", return_value=None
),
patch(
"free_claude_code.cli.launchers.common.shutil.which",
return_value="resolved-claude.cmd",
),
patch("free_claude_code.cli.launchers.common.subprocess.Popen") as popen,
patch("free_claude_code.cli.launchers.common.register_pid") as register_pid,
patch("free_claude_code.cli.launchers.common.unregister_pid") as unregister_pid,
pytest.raises(SystemExit) as exc_info,
):
process = popen.return_value
process.pid = 12345
process.wait.return_value = 7
launch(["--model", "sonnet"])
assert exc_info.value.code == 7
popen.assert_called_once()
assert popen.call_args.args[0] == ["resolved-claude.cmd", "--model", "sonnet"]
child_env = popen.call_args.kwargs["env"]
assert child_env["ANTHROPIC_BASE_URL"] == "http://127.0.0.1:9191"
assert child_env["ANTHROPIC_AUTH_TOKEN"] == "proxy-token"
assert child_env["CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY"] == "1"
assert child_env["CLAUDE_CODE_AUTO_COMPACT_WINDOW"] == "190000"
assert child_env["DISABLE_AUTOUPDATER"] == "1"
assert child_env["DISABLE_FEEDBACK_COMMAND"] == "1"
assert child_env["DISABLE_ERROR_REPORTING"] == "1"
assert child_env["DISABLE_TELEMETRY"] == "1"
assert "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC" not in child_env
assert child_env["KEEP_ME"] == "yes"
register_pid.assert_called_once_with(12345)
unregister_pid.assert_called_once_with(12345)
def test_launch_codex_passes_responses_config_and_child_env(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
) -> None:
from free_claude_code.cli.launchers.codex import launch
monkeypatch.setenv("OPENAI_API_KEY", "official-key")
monkeypatch.setenv("OPENAI_BASE_URL", "https://api.openai.com/v1")
monkeypatch.setenv("CODEX_HOME", "keep-home")
monkeypatch.setenv("CODEX_INTERNAL_ORIGINATOR_OVERRIDE", "Codex Desktop")
monkeypatch.setenv("CODEX_PERMISSION_PROFILE", "danger-full-access")
monkeypatch.setenv("CODEX_SHELL", "1")
monkeypatch.setenv("CODEX_THREAD_ID", "parent-thread")
settings = _launcher_settings(port=9191, token="proxy-token")
catalog_path = tmp_path / "codex-model-catalog.json"
requests: list[Request] = []
def fake_urlopen(request: Request, *, timeout: float) -> _JsonResponse:
requests.append(request)
assert timeout == 1.5
return _JsonResponse(
{
"data": [
{
"id": "anthropic/nvidia_nim/provider-model",
"display_name": "NVIDIA model",
},
{
"id": ("claude-3-freecc-no-thinking/nvidia_nim/provider-model"),
"display_name": "NVIDIA model (no thinking)",
},
{
"id": "claude-opus-4-20250514",
"display_name": "Claude Opus 4",
},
]
}
)
with (
patch(
"free_claude_code.cli.launchers.codex.get_settings", return_value=settings
),
patch(
"free_claude_code.cli.launchers.codex.preflight_proxy", return_value=None
),
patch(
"free_claude_code.cli.launchers.common.shutil.which",
return_value="resolved-codex.cmd",
),
patch(
"free_claude_code.cli.launchers.codex.codex_model_catalog_path",
return_value=catalog_path,
),
patch("free_claude_code.cli.launchers.codex.urlopen", side_effect=fake_urlopen),
patch("free_claude_code.cli.launchers.common.subprocess.Popen") as popen,
patch("free_claude_code.cli.launchers.common.register_pid") as register_pid,
patch("free_claude_code.cli.launchers.common.unregister_pid") as unregister_pid,
pytest.raises(SystemExit) as exc_info,
):
process = popen.return_value
process.pid = 12345
process.wait.return_value = 0
launch(["exec", "hello"])
assert exc_info.value.code == 0
command = popen.call_args.args[0]
assert command[0] == "resolved-codex.cmd"
assert 'model_provider="fcc"' in command
assert 'model_providers.fcc.base_url="http://127.0.0.1:9191/v1"' in command
assert 'model_providers.fcc.wire_api="responses"' in command
assert f"model_catalog_json={json.dumps(str(catalog_path))}" in command
assert command[-2:] == ["exec", "hello"]
assert len(requests) == 1
request = requests[0]
assert request.full_url == "http://127.0.0.1:9191/v1/models"
headers = {key.lower(): value for key, value in request.header_items()}
assert headers["authorization"] == "Bearer proxy-token"
assert "x-api-key" not in headers
catalog = json.loads(catalog_path.read_text(encoding="utf-8"))
assert [model["slug"] for model in catalog["models"]] == [
"nvidia_nim/provider-model"
]
child_env = popen.call_args.kwargs["env"]
assert child_env["FCC_CODEX_API_KEY"] == "proxy-token"
assert child_env["CODEX_HOME"] == "keep-home"
assert "CODEX_INTERNAL_ORIGINATOR_OVERRIDE" not in child_env
assert "CODEX_PERMISSION_PROFILE" not in child_env
assert "CODEX_SHELL" not in child_env
assert "CODEX_THREAD_ID" not in child_env
assert "OPENAI_API_KEY" not in child_env
assert "OPENAI_BASE_URL" not in child_env
register_pid.assert_called_once_with(12345)
unregister_pid.assert_called_once_with(12345)
def test_launch_codex_catalog_failure_warns_and_continues(
capsys: pytest.CaptureFixture[str],
tmp_path: Path,
) -> None:
from free_claude_code.cli.launchers.codex import launch
settings = _launcher_settings(port=9191, token="proxy-token")
with (
patch(
"free_claude_code.cli.launchers.codex.get_settings", return_value=settings
),
patch(
"free_claude_code.cli.launchers.codex.preflight_proxy", return_value=None
),
patch(
"free_claude_code.cli.launchers.common.shutil.which",
return_value="resolved-codex.cmd",
),
patch(
"free_claude_code.cli.launchers.codex.codex_model_catalog_path",
return_value=tmp_path / "codex-model-catalog.json",
),
patch(
"free_claude_code.cli.launchers.codex.urlopen", side_effect=URLError("boom")
),
patch("free_claude_code.cli.launchers.common.subprocess.Popen") as popen,
patch("free_claude_code.cli.launchers.common.register_pid"),
patch("free_claude_code.cli.launchers.common.unregister_pid"),
pytest.raises(SystemExit) as exc_info,
):
process = popen.return_value
process.pid = 12345
process.wait.return_value = 0
launch(["exec", "hello"])
assert exc_info.value.code == 0
command = popen.call_args.args[0]
assert not any("model_catalog_json=" in arg for arg in command)
captured = capsys.readouterr()
assert "could not prepare Codex model catalog" in captured.err
assert "launching without model picker catalog" in captured.err
def test_pi_launcher_builds_scoped_session_command_and_proxy_env(
tmp_path: Path,
) -> None:
from free_claude_code.cli.launchers.pi import (
build_pi_launcher_command,
build_pi_launcher_env,
)
extension = tmp_path / "pi_extension.ts"
env = build_pi_launcher_env(
proxy_root_url="http://127.0.0.1:9191/",
auth_token=" proxy-token ",
base_env={
"PATH": "keep",
"ANTHROPIC_API_KEY": "native-pi-credential",
"FCC_PI_API_KEY": "stale-key",
"FCC_PI_BASE_URL": "https://stale.invalid",
},
)
assert build_pi_launcher_command(
binary_path="resolved-pi.cmd",
extension_path=extension,
argv=["--print", "hello"],
) == [
"resolved-pi.cmd",
"-e",
str(extension),
"--models",
"free-claude-code/**",
"--print",
"hello",
]
assert env == {
"PATH": "keep",
"ANTHROPIC_API_KEY": "native-pi-credential",
"FCC_PI_BASE_URL": "http://127.0.0.1:9191",
"FCC_PI_API_KEY": "proxy-token",
}
def test_pi_launcher_uses_no_auth_sentinel_for_blank_token() -> None:
from free_claude_code.cli.launchers.pi import build_pi_launcher_env
env = build_pi_launcher_env(
proxy_root_url="http://127.0.0.1:8082",
auth_token="",
base_env={},
)
assert env["FCC_PI_API_KEY"] == "fcc-no-auth"
def test_launch_pi_registers_bundled_extension_for_sessions(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
) -> None:
from free_claude_code.cli.launchers.pi import launch
monkeypatch.setenv("KEEP_ME", "yes")
monkeypatch.setenv("FCC_PI_API_KEY", "stale-key")
extension = tmp_path / "pi_extension.ts"
extension.write_text("export default () => {};", encoding="utf-8")
settings = _launcher_settings(port=9191, token="proxy-token")
with (
patch("free_claude_code.cli.launchers.pi.get_settings", return_value=settings),
patch("free_claude_code.cli.launchers.pi.preflight_proxy", return_value=None),
patch(
"free_claude_code.cli.launchers.pi.pi_extension_path",
return_value=extension,
),
patch(
"free_claude_code.cli.launchers.common.shutil.which",
return_value="resolved-pi.cmd",
),
patch(
"free_claude_code.cli.launchers.pi.pi_binary_is_compatible",
return_value=True,
),
patch("free_claude_code.cli.launchers.common.subprocess.Popen") as popen,
patch("free_claude_code.cli.launchers.common.register_pid"),
patch("free_claude_code.cli.launchers.common.unregister_pid"),
pytest.raises(SystemExit) as exc_info,
):
process = popen.return_value
process.pid = 12345
process.wait.return_value = 0
launch(["--print", "hello"])
assert exc_info.value.code == 0
assert popen.call_args.args[0] == [
"resolved-pi.cmd",
"-e",
str(extension),
"--models",
"free-claude-code/**",
"--print",
"hello",
]
child_env = popen.call_args.kwargs["env"]
assert child_env["FCC_PI_BASE_URL"] == "http://127.0.0.1:9191"
assert child_env["FCC_PI_API_KEY"] == "proxy-token"
assert child_env["KEEP_ME"] == "yes"
@pytest.mark.parametrize(
"argv",
[
["--help"],
["--version"],
["config", "set", "theme", "dark"],
["install", "npm:example"],
["list"],
["remove", "npm:example"],
["uninstall", "npm:example"],
["update"],
],
)
def test_launch_pi_passes_management_commands_through_without_proxy(
argv: list[str],
) -> None:
from free_claude_code.cli.launchers.pi import launch
with (
patch("free_claude_code.cli.launchers.pi.get_settings") as get_settings,
patch("free_claude_code.cli.launchers.pi.preflight_proxy") as preflight,
patch(
"free_claude_code.cli.launchers.common.shutil.which",
return_value="resolved-pi",
),
patch(
"free_claude_code.cli.launchers.pi.pi_binary_is_compatible",
return_value=True,
),
patch("free_claude_code.cli.launchers.common.subprocess.Popen") as popen,
patch("free_claude_code.cli.launchers.common.register_pid"),
patch("free_claude_code.cli.launchers.common.unregister_pid"),
pytest.raises(SystemExit) as exc_info,
):
process = popen.return_value
process.pid = 12345
process.wait.return_value = 0
launch(argv)
assert exc_info.value.code == 0
assert popen.call_args.args[0] == ["resolved-pi", *argv]
get_settings.assert_not_called()
preflight.assert_not_called()
def test_launch_pi_fails_closed_when_bundled_extension_is_missing(
capsys: pytest.CaptureFixture[str],
tmp_path: Path,
) -> None:
from free_claude_code.cli.launchers.pi import launch
settings = _launcher_settings(port=9191)
with (
patch("free_claude_code.cli.launchers.pi.get_settings", return_value=settings),
patch("free_claude_code.cli.launchers.pi.preflight_proxy", return_value=None),
patch(
"free_claude_code.cli.launchers.pi.pi_extension_path",
return_value=tmp_path / "missing.ts",
),
patch(
"free_claude_code.cli.launchers.common.shutil.which",
return_value="resolved-pi",
),
patch(
"free_claude_code.cli.launchers.pi.pi_binary_is_compatible",
return_value=True,
),
patch("free_claude_code.cli.launchers.common.subprocess.Popen") as popen,
pytest.raises(SystemExit) as exc_info,
):
launch([])
assert exc_info.value.code == 1
popen.assert_not_called()
assert "bundled Pi extension is missing" in capsys.readouterr().err
def test_pi_install_hints_use_official_platform_installers() -> None:
from free_claude_code.cli.launchers.pi import pi_install_hint
assert "https://pi.dev/install.ps1" in pi_install_hint("win32")
assert "https://pi.dev/install.sh" in pi_install_hint("darwin")
@pytest.mark.parametrize(
("help_output", "return_code", "expected"),
[
("--extension <path>\n--models <patterns>\n", 0, True),
("--models <patterns>\n", 0, False),
("--extension <path>\n", 0, False),
("--extension <path>\n--models <patterns>\n", 1, False),
],
)
def test_pi_binary_compatibility_requires_both_launcher_capabilities(
help_output: str,
return_code: int,
expected: bool,
) -> None:
from free_claude_code.cli.launchers.pi import pi_binary_is_compatible
with patch(
"free_claude_code.cli.launchers.pi.subprocess.run",
return_value=SimpleNamespace(returncode=return_code, stdout=help_output),
):
assert pi_binary_is_compatible("resolved-pi") is expected
def test_launch_pi_rejects_unrelated_pi_binary(
capsys: pytest.CaptureFixture[str],
) -> None:
from free_claude_code.cli.launchers.pi import launch
with (
patch(
"free_claude_code.cli.launchers.common.shutil.which",
return_value="unrelated-pi",
),
patch(
"free_claude_code.cli.launchers.pi.pi_binary_is_compatible",
return_value=False,
),
patch("free_claude_code.cli.launchers.pi.get_settings") as get_settings,
patch("free_claude_code.cli.launchers.common.subprocess.Popen") as popen,
pytest.raises(SystemExit) as exc_info,
):
launch([])
assert exc_info.value.code == 126
get_settings.assert_not_called()
popen.assert_not_called()
captured = capsys.readouterr()
assert "not a compatible Pi Coding Agent" in captured.err
assert "https://pi.dev/install." in captured.err
def test_launch_claude_keyboard_interrupt_kills_child_tree() -> None:
from free_claude_code.cli.launchers.claude import launch
settings = _launcher_settings(port=9191, token="proxy-token")
with (
patch(
"free_claude_code.cli.launchers.claude.get_settings", return_value=settings
),
patch(
"free_claude_code.cli.launchers.claude.preflight_proxy", return_value=None
),
patch(
"free_claude_code.cli.launchers.common.shutil.which",
return_value="resolved-claude.cmd",
),
patch("free_claude_code.cli.launchers.common.subprocess.Popen") as popen,
patch("free_claude_code.cli.launchers.common.register_pid"),
patch(
"free_claude_code.cli.launchers.common.kill_pid_tree_best_effort"
) as kill_tree,
patch("free_claude_code.cli.launchers.common.unregister_pid") as unregister_pid,
pytest.raises(KeyboardInterrupt),
):
process = popen.return_value
process.pid = 12345
process.wait.side_effect = [KeyboardInterrupt, 0]
launch([])
kill_tree.assert_called_once_with(12345)
unregister_pid.assert_called_once_with(12345)
def test_launch_claude_exits_when_command_cannot_be_resolved(
capsys: pytest.CaptureFixture[str],
) -> None:
from free_claude_code.cli.launchers.claude import launch
settings = _launcher_settings()
with (
patch(
"free_claude_code.cli.launchers.claude.get_settings", return_value=settings
),
patch(
"free_claude_code.cli.launchers.claude.preflight_proxy", return_value=None
),
patch("free_claude_code.cli.launchers.common.shutil.which", return_value=None),
patch("free_claude_code.cli.launchers.common.subprocess.Popen") as popen,
pytest.raises(SystemExit) as exc_info,
):
launch([])
assert exc_info.value.code == 127
popen.assert_not_called()
captured = capsys.readouterr()
assert "Could not find Claude Code command: claude" in captured.err
assert "npm install -g @anthropic-ai/claude-code" in captured.err
def test_launch_claude_unreachable_proxy_exits_with_hint(