-
Notifications
You must be signed in to change notification settings - Fork 557
Expand file tree
/
Copy pathtest_onboard_cmd_headless_semantics.py
More file actions
957 lines (753 loc) · 30.2 KB
/
Copy pathtest_onboard_cmd_headless_semantics.py
File metadata and controls
957 lines (753 loc) · 30.2 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
"""Headless `onboard`/`configure` CLI semantics.
Pins the gate-flag error contract (explicit-but-incomplete flag sets exit 2),
keep-current re-saves, cancellation and config-error productization, restart
guidance, the voice-audio catalog section, and strict `--field` coercion.
All tests are offline and use synthetic dummy data only.
"""
from __future__ import annotations
import re
import pytest
from typer.testing import CliRunner
from opensquilla.cli.main import app
from opensquilla.onboarding.config_store import load_config
runner = CliRunner()
_ANSI_RE = re.compile(r"\x1b\[[0-?]*[ -/]*[@-~]")
def _plain(value: str) -> str:
return " ".join(_ANSI_RE.sub("", value).split())
# ---------------------------------------------------------------------------
# B2-1: explicit-but-incomplete headless flag combinations must exit 2 and
# name the missing gate flag instead of silently no-opping with exit 0.
# ---------------------------------------------------------------------------
@pytest.mark.parametrize(
("args", "missing_flag"),
[
(["configure", "router", "--default-tier", "c2"], "--router"),
(["configure", "provider", "--model", "some/model"], "--provider"),
(["configure", "search", "--max-results", "9"], "--search-provider"),
(["configure", "channels", "--channel-type", "slack"], "--name"),
(["configure", "channels", "--name", "work"], "--channel-type"),
(["configure", "image", "--primary", "openrouter/x"], "--image-provider"),
(["configure", "memory", "--onnx-dir", "models/bge"], "--memory-provider"),
],
)
def test_configure_incomplete_flags_exit_2_naming_missing_gate(
tmp_path, monkeypatch, args, missing_flag
):
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(app, ["onboard", *args])
assert result.exit_code == 2, result.output
assert missing_flag in _plain(result.stderr)
assert "no changes were made" in _plain(result.stderr)
assert not target.exists()
def test_configure_router_incomplete_flags_do_not_touch_existing_config(
tmp_path, monkeypatch
):
target = tmp_path / "c.toml"
target.write_text(
'[llm]\nprovider = "openrouter"\nmodel = "m"\napi_key = "sk"\n',
encoding="utf-8",
)
before = target.read_text(encoding="utf-8")
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(app, ["onboard", "configure", "router", "--default-tier", "c2"])
assert result.exit_code == 2
assert target.read_text(encoding="utf-8") == before
def test_configure_flags_without_section_exit_2(tmp_path, monkeypatch):
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(app, ["onboard", "configure", "--router", "recommended"])
assert result.exit_code == 2
assert "target section" in _plain(result.stderr)
assert not target.exists()
def test_configure_unknown_section_exits_2(tmp_path, monkeypatch):
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(app, ["onboard", "configure", "not-a-section"])
assert result.exit_code == 2
assert "unknown configure section" in _plain(result.stderr)
assert "not-a-section" in _plain(result.stderr)
assert not target.exists()
def test_configure_audio_section_exits_2_with_catalog_pointer(tmp_path, monkeypatch):
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(app, ["onboard", "configure", "audio"])
assert result.exit_code == 2
assert "opensquilla onboard catalog audio" in _plain(result.stderr)
assert not target.exists()
def test_configure_bare_non_tty_exits_2_with_hint(tmp_path, monkeypatch):
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(app, ["onboard", "configure"])
assert result.exit_code == 2
assert "requires a TTY" in result.stdout
assert not target.exists()
# ---------------------------------------------------------------------------
# B2-2: `onboard catalog audio` must render usable output, and the overview
# must not leak the raw audioProviders key.
# ---------------------------------------------------------------------------
def test_onboard_catalog_audio_prints_provider_rows(tmp_path, monkeypatch):
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(app, ["onboard", "catalog", "audio"])
assert result.exit_code == 0, result.stdout
assert result.stdout.strip()
assert "voice audio provider options" in result.stdout
assert "elevenlabs" in result.stdout
assert "ELEVENLABS_API_KEY" in result.stdout
assert "Try:" in result.stdout
assert not target.exists()
def test_onboard_catalog_overview_names_voice_audio_without_raw_key(
tmp_path, monkeypatch
):
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(app, ["onboard", "catalog"])
assert result.exit_code == 0, result.stdout
assert "Voice audio providers" in result.stdout
assert "audioProviders" not in result.stdout
compact = "".join(result.stdout.split())
assert "opensquillaonboardcatalogaudio" in compact
# ---------------------------------------------------------------------------
# B2-3: an explicitly stored enabled=false must survive an image key rotation
# that omits --image-enabled/--no-image-enabled.
# ---------------------------------------------------------------------------
def test_configure_image_rotation_keeps_deliberate_disabled_state(
tmp_path, monkeypatch
):
target = tmp_path / "c.toml"
target.write_text(
'[llm]\nprovider = "openrouter"\nmodel = "m"\napi_key = "sk"\n'
"\n"
"[image_generation]\n"
"enabled = false\n"
'primary = "openrouter/google/gemini-3.1-flash-image-preview"\n'
"\n"
"[image_generation.providers.openrouter]\n"
'api_key_env = "OPENSQUILLA_TEST_IMAGE_KEY"\n',
encoding="utf-8",
)
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
monkeypatch.setenv("OPENSQUILLA_TEST_IMAGE_KEY", "sk-image-env")
result = runner.invoke(
app,
[
"onboard",
"configure",
"image",
"--image-provider",
"openrouter",
"--primary",
"openrouter/google/gemini-3.1-flash-image-preview",
"--api-key-env",
"OPENSQUILLA_TEST_IMAGE_KEY",
],
)
assert result.exit_code == 0, result.output
assert load_config(target).image_generation.enabled is False
def test_configure_image_rotation_keeps_enabled_true(tmp_path, monkeypatch):
target = tmp_path / "c.toml"
target.write_text(
"[image_generation]\n"
"enabled = true\n"
'primary = "openrouter/google/gemini-3.1-flash-image-preview"\n'
"\n"
"[image_generation.providers.openrouter]\n"
'api_key_env = "OPENSQUILLA_TEST_IMAGE_KEY"\n',
encoding="utf-8",
)
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
monkeypatch.setenv("OPENSQUILLA_TEST_IMAGE_KEY", "sk-image-env")
result = runner.invoke(
app,
[
"onboard",
"configure",
"image",
"--image-provider",
"openrouter",
"--primary",
"openrouter/google/gemini-3.1-flash-image-preview",
"--api-key-env",
"OPENSQUILLA_TEST_IMAGE_KEY",
],
)
assert result.exit_code == 0, result.output
assert load_config(target).image_generation.enabled is True
# ---------------------------------------------------------------------------
# B2-4: key rotation must not clobber stored provider/search settings when
# the corresponding flags are omitted.
# ---------------------------------------------------------------------------
def test_configure_provider_key_rotation_keeps_model_base_url_and_proxy(
tmp_path, monkeypatch
):
target = tmp_path / "c.toml"
target.write_text(
"[llm]\n"
'provider = "openrouter"\n'
'model = "custom/model-x"\n'
'api_key = "sk-old"\n'
'base_url = "https://gateway.example.test/v1"\n'
'proxy = "http://127.0.0.1:7890"\n',
encoding="utf-8",
)
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(
app,
[
"onboard",
"configure",
"provider",
"--provider",
"openrouter",
"--api-key",
"sk-new",
],
)
assert result.exit_code == 0, result.output
cfg = load_config(target)
assert cfg.llm.model == "custom/model-x"
assert cfg.llm.base_url == "https://gateway.example.test/v1"
assert cfg.llm.proxy == "http://127.0.0.1:7890"
assert cfg.llm.api_key == "sk-new"
def test_configure_provider_explicit_router_disabled_is_authoritative(
tmp_path, monkeypatch
):
# Issue #1341: `configure provider --router disabled` on a fresh config
# used to drop the flag and synthesize the default tier ladder instead.
# The explicit flag must be authoritative, matching the command help.
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(
app,
[
"onboard",
"configure",
"provider",
"--provider",
"vllm",
"--model",
"qa-model",
"--base-url",
"http://127.0.0.1:18001/v1",
"--router",
"disabled",
],
)
assert result.exit_code == 0, result.output
cfg = load_config(target)
assert cfg.squilla_router.enabled is False
def test_configure_provider_omitted_router_keeps_disabled_state(
tmp_path, monkeypatch
):
# Keep-current contract: a provider re-save without --router must never
# re-enable a deliberately disabled router.
target = tmp_path / "c.toml"
target.write_text(
"[llm]\n"
'provider = "vllm"\n'
'model = "qa-model"\n'
"\n"
"[squilla_router]\n"
"enabled = false\n",
encoding="utf-8",
)
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(
app,
[
"onboard",
"configure",
"provider",
"--provider",
"vllm",
"--api-key",
"sk-new",
],
)
assert result.exit_code == 0, result.output
cfg = load_config(target)
assert cfg.squilla_router.enabled is False
assert cfg.llm.api_key == "sk-new"
def test_configure_provider_invalid_router_mode_exits_2(tmp_path, monkeypatch):
# Plumb-through makes an invalid --router a real mutation error: it must
# exit 2 with the mode validation message instead of being ignored.
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(
app,
[
"onboard",
"configure",
"provider",
"--provider",
"vllm",
"--model",
"qa-model",
"--base-url",
"http://127.0.0.1:18001/v1",
"--router",
"nonsense",
],
)
assert result.exit_code == 2, result.output
assert "router mode" in _plain(result.stderr)
def test_onboard_provider_key_rotation_keeps_base_url_and_proxy(
tmp_path, monkeypatch
):
# The top-level `onboard --provider` path must honor the same
# keep-current contract as `configure provider`: an omitted --router
# never re-applies a router profile on a configured install, so the
# stored model and endpoint settings all survive a key rotation.
target = tmp_path / "c.toml"
target.write_text(
"[llm]\n"
'provider = "openrouter"\n'
'model = "custom/model-x"\n'
'api_key = "sk-old"\n'
'base_url = "https://gateway.example.test/v1"\n'
'proxy = "http://127.0.0.1:7890"\n',
encoding="utf-8",
)
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(
app,
["onboard", "--provider", "openrouter", "--api-key", "sk-new", "--minimal"],
)
assert result.exit_code == 0, result.output
cfg = load_config(target)
assert cfg.llm.model == "custom/model-x"
assert cfg.llm.base_url == "https://gateway.example.test/v1"
assert cfg.llm.proxy == "http://127.0.0.1:7890"
assert cfg.llm.api_key == "sk-new"
def test_onboard_provider_key_rotation_with_router_disabled_keeps_model(
tmp_path, monkeypatch
):
target = tmp_path / "c.toml"
target.write_text(
"[llm]\n"
'provider = "openrouter"\n'
'model = "custom/model-x"\n'
'api_key = "sk-old"\n',
encoding="utf-8",
)
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(
app,
[
"onboard",
"--provider",
"openrouter",
"--api-key",
"sk-new",
"--router",
"disabled",
"--minimal",
],
)
assert result.exit_code == 0, result.output
cfg = load_config(target)
assert cfg.llm.model == "custom/model-x"
assert cfg.llm.api_key == "sk-new"
def test_configure_search_key_rotation_keeps_stored_global_settings(
tmp_path, monkeypatch
):
target = tmp_path / "c.toml"
target.write_text(
'search_provider = "brave"\n'
'search_api_key = "sk-old"\n'
"search_max_results = 9\n"
'search_proxy = "http://127.0.0.1:7890"\n'
"search_use_env_proxy = true\n"
'search_fallback_policy = "network"\n'
"search_diagnostics = true\n",
encoding="utf-8",
)
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(
app,
[
"onboard",
"configure",
"search",
"--search-provider",
"brave",
"--api-key",
"sk-new",
],
)
assert result.exit_code == 0, result.output
cfg = load_config(target)
assert cfg.search_max_results == 9
assert cfg.search_proxy == "http://127.0.0.1:7890"
assert cfg.search_use_env_proxy is True
assert cfg.search_fallback_policy == "network"
assert cfg.search_diagnostics is True
assert cfg.search_api_key == "sk-new"
def test_configure_search_explicit_flags_still_override_stored(tmp_path, monkeypatch):
target = tmp_path / "c.toml"
target.write_text(
'search_provider = "duckduckgo"\n'
"search_max_results = 9\n"
'search_proxy = "http://127.0.0.1:7890"\n',
encoding="utf-8",
)
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(
app,
[
"onboard",
"configure",
"search",
"--search-provider",
"duckduckgo",
"--max-results",
"3",
"--proxy",
"",
],
)
assert result.exit_code == 0, result.output
cfg = load_config(target)
assert cfg.search_max_results == 3
assert cfg.search_proxy == ""
# ---------------------------------------------------------------------------
# B2-5: a wizard cancellation (Esc/Ctrl+C) must exit with one short line and
# code 130 instead of a raw traceback.
# ---------------------------------------------------------------------------
def test_onboard_wizard_cancellation_is_productized(tmp_path, monkeypatch):
from opensquilla.cli import onboard_cmd
from opensquilla.onboarding.errors import UserCancelledError
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
def cancelled(_options):
raise UserCancelledError(section="provider")
monkeypatch.setattr(onboard_cmd, "run_interactive_onboard", cancelled)
result = runner.invoke(app, ["onboard"])
assert result.exit_code == 130
assert "Setup cancelled" in _plain(result.stderr)
assert "Traceback" not in result.output + result.stderr
assert not target.exists()
def test_configure_wizard_cancellation_is_productized(tmp_path, monkeypatch):
from opensquilla.cli import onboard_cmd
from opensquilla.onboarding.errors import UserCancelledError
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
def cancelled(_section, *, config_path=None):
raise UserCancelledError(section="configure")
monkeypatch.setattr(onboard_cmd, "run_interactive_configure", cancelled)
result = runner.invoke(app, ["onboard", "configure", "router"])
assert result.exit_code == 130
assert "Setup cancelled" in _plain(result.stderr)
assert "Traceback" not in result.output + result.stderr
assert not target.exists()
# ---------------------------------------------------------------------------
# B2-6: bare `onboard` with a corrupt config must route through the
# productized config-error handoff (exit 2), never a raw traceback.
# ---------------------------------------------------------------------------
def test_bare_onboard_toml_decode_error_is_productized(tmp_path, monkeypatch):
target = tmp_path / "c.toml"
target.write_text("not toml :::", encoding="utf-8")
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(app, ["onboard"])
assert result.exit_code == 2
assert "OpenSquilla config error" in result.stderr
assert "Traceback" not in result.output + result.stderr
def test_bare_onboard_validation_error_is_productized(tmp_path, monkeypatch):
target = tmp_path / "c.toml"
target.write_text("[search]\n", encoding="utf-8")
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(app, ["onboard"])
assert result.exit_code == 2
assert "OpenSquilla config error" in result.stderr
assert "pydantic_core" not in result.stderr
assert "Traceback" not in result.output + result.stderr
def test_bare_onboard_os_error_is_productized(tmp_path, monkeypatch):
target = tmp_path / "config-dir.toml"
target.mkdir() # opening a directory raises IsADirectoryError (OSError)
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(app, ["onboard"])
assert result.exit_code == 2
assert "OpenSquilla config error" in result.stderr
assert "Traceback" not in result.output + result.stderr
def test_onboard_provider_with_corrupt_config_is_productized(tmp_path, monkeypatch):
target = tmp_path / "c.toml"
target.write_text("not toml :::", encoding="utf-8")
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(
app,
["onboard", "--provider", "openrouter", "--api-key", "sk", "--minimal"],
)
assert result.exit_code == 2
assert "OpenSquilla config error" in result.stderr
assert "Traceback" not in result.output + result.stderr
# ---------------------------------------------------------------------------
# B2-7: a ValidationError on the `onboard --provider` path must not echo
# pydantic's input_value (which can carry a mispasted secret).
# ---------------------------------------------------------------------------
def test_onboard_provider_validation_error_never_echoes_input_value(
tmp_path, monkeypatch
):
from pydantic import BaseModel, ValidationError
from opensquilla.cli import onboard_cmd
class _Probe(BaseModel):
max_tokens: int
try:
_Probe(max_tokens="sk-super-secret-value") # type: ignore[arg-type]
except ValidationError as exc:
captured = exc
else: # pragma: no cover - the construction above always fails
raise AssertionError("expected a ValidationError")
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
class _RaisingEngine:
def __init__(self, *_a, **_kw):
pass
def apply(self, *_a, **_kw):
raise captured
def persist(self, *_a, **_kw): # pragma: no cover - apply raises first
raise AssertionError("persist must not run after a failed apply")
monkeypatch.setattr(onboard_cmd, "SetupEngine", _RaisingEngine)
result = runner.invoke(
app,
["onboard", "--provider", "openrouter", "--api-key", "sk", "--minimal"],
)
assert result.exit_code == 2
combined = result.output + result.stderr
assert "sk-super-secret-value" not in combined
assert "input_value" not in combined
assert "max_tokens" in _plain(result.stderr)
assert "Traceback" not in combined
# ---------------------------------------------------------------------------
# B2-8: headless channels/memory saves must surface PersistResult's
# restart_required instead of printing only the saved path.
# ---------------------------------------------------------------------------
def test_configure_channels_prints_restart_guidance(tmp_path, monkeypatch):
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(
app,
[
"onboard",
"configure",
"channels",
"--channel-type",
"slack",
"--name",
"work",
"--token",
"xoxb-secret",
"--field",
"signing_secret=ss",
],
)
assert result.exit_code == 0, result.output
plain = _plain(result.stdout)
assert "restart required" in plain
assert "opensquilla gateway restart" in plain
def test_configure_memory_prints_restart_guidance_with_config_path(
tmp_path, monkeypatch
):
default_target = tmp_path / "default.toml"
target = tmp_path / "custom.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(default_target))
result = runner.invoke(
app,
[
"onboard",
"configure",
"memory",
"--memory-provider",
"local",
"--onnx-dir",
"models/bge",
"--config",
str(target),
],
)
assert result.exit_code == 0, result.output
plain = _plain(result.stdout)
assert "restart required" in plain
assert "opensquilla gateway restart" in plain
assert str(target) in plain
assert not default_target.exists()
def test_configure_search_does_not_print_restart_guidance(tmp_path, monkeypatch):
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(
app,
["onboard", "configure", "search", "--search-provider", "duckduckgo"],
)
assert result.exit_code == 0, result.output
assert "restart required" not in _plain(result.stdout)
# ---------------------------------------------------------------------------
# B2-10: strict --field coercion (bool typos and numeric garbage must name
# the offending field and the accepted spellings).
# ---------------------------------------------------------------------------
def test_configure_channels_field_bool_typo_exits_2_naming_field(
tmp_path, monkeypatch
):
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(
app,
[
"onboard",
"configure",
"channels",
"--channel-type",
"slack",
"--name",
"work",
"--token",
"xoxb-secret",
"--field",
"signing_secret=ss",
"--field",
"enabled=ture",
],
)
assert result.exit_code == 2
plain = _plain(result.output + result.stderr)
assert "enabled" in plain
assert "true/false" in plain
assert "'ture'" in plain
assert not target.exists()
def test_configure_channels_field_int_garbage_exits_2_naming_field(
tmp_path, monkeypatch
):
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(
app,
[
"onboard",
"configure",
"channels",
"--channel-type",
"discord",
"--name",
"guild",
"--token",
"bot-secret",
"--field",
"intents=lots",
],
)
assert result.exit_code == 2
plain = _plain(result.output + result.stderr)
assert "intents" in plain
assert "integer" in plain
assert not target.exists()
def test_parse_channel_field_pairs_strict_coercion_unit():
import typer
from opensquilla.cli.channel_fields import parse_channel_field_pairs
assert parse_channel_field_pairs(["enabled=TRUE"], "slack")["enabled"] is True
assert parse_channel_field_pairs(["enabled=off"], "slack")["enabled"] is False
with pytest.raises(typer.BadParameter, match=r"--field enabled .*'ture'"):
parse_channel_field_pairs(["enabled=ture"], "slack")
with pytest.raises(typer.BadParameter, match=r"--field intents .*integer"):
parse_channel_field_pairs(["intents=lots"], "discord")
with pytest.raises(typer.BadParameter, match=r"--field poll_idle_sleep_s .*number"):
parse_channel_field_pairs(["poll_idle_sleep_s=fast"], "telegram")
# ---------------------------------------------------------------------------
# B2-11: previously untested branches.
# ---------------------------------------------------------------------------
def test_onboard_probe_flag_exits_nonzero_when_probe_raises(tmp_path, monkeypatch):
from opensquilla.onboarding import probe as probe_module
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
async def exploding_probe(**_kwargs):
raise RuntimeError("socket exploded mid-flight")
monkeypatch.setattr(probe_module, "probe_llm_provider", exploding_probe)
result = runner.invoke(
app,
[
"onboard",
"--provider",
"openrouter",
"--model",
"deepseek/deepseek-v4-flash",
"--api-key",
"sk",
"--minimal",
"--probe",
],
)
assert result.exit_code == 1
assert "Probe failed" in result.stderr
assert "socket exploded mid-flight" in result.stderr
assert "Traceback" not in result.output + result.stderr
# The probe is a gate, not a rollback: the save sticks.
assert "openrouter" in target.read_text()
def test_onboard_catalog_unknown_section_exits_2(tmp_path, monkeypatch):
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(app, ["onboard", "catalog", "not-a-section"])
assert result.exit_code == 2
assert "unknown setup section" in _plain(result.stderr)
assert "Traceback" not in result.output + result.stderr
def test_onboard_if_needed_names_unfinished_sections(tmp_path, monkeypatch):
# has_config=True but the referenced env key is missing: the gate must
# explain which sections are unfinished before falling into the wizard.
target = tmp_path / "c.toml"
target.write_text(
"[llm]\n"
'provider = "openrouter"\n'
'model = "deepseek/deepseek-v4-flash"\n'
'api_key_env = "CUSTOM_LLM_KEY"\n',
encoding="utf-8",
)
monkeypatch.delenv("CUSTOM_LLM_KEY", raising=False)
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(app, ["onboard", "--if-needed"])
assert result.exit_code == 2 # non-TTY still exits 2 afterwards
assert "onboarding has unfinished sections" in result.stdout
assert "Provider" in result.stdout
def test_bare_configure_hub_exit_without_changes_is_success(tmp_path, monkeypatch):
import sys
import types
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
from opensquilla.onboarding import flow
monkeypatch.setattr(flow, "_is_tty", lambda: True)
class _Answer:
def __init__(self, value):
self.value = value
def ask(self):
return self.value
class _Questionary(types.SimpleNamespace):
def select(self, message: str, **kwargs):
assert message == "Section"
assert kwargs["choices"][-1] == "Exit (nothing changed)"
return _Answer("Exit (nothing changed)")
def text(self, message: str, **_kwargs):
raise AssertionError(f"unexpected text prompt: {message}")
def confirm(self, message: str, **_kwargs):
raise AssertionError(f"unexpected confirm prompt: {message}")
def password(self, message: str, **_kwargs):
raise AssertionError(f"unexpected password prompt: {message}")
def checkbox(self, message: str, **_kwargs):
raise AssertionError(f"unexpected checkbox prompt: {message}")
monkeypatch.setitem(sys.modules, "questionary", _Questionary())
result = runner.invoke(app, ["onboard", "configure"])
assert result.exit_code == 0, result.output
assert "requires a TTY" not in result.stdout
assert not target.exists()
# ---------------------------------------------------------------------------
# B2-9: router tier ids in the CLI stay sourced from the router_tiers
# helpers instead of raw tuples/literals.
# ---------------------------------------------------------------------------
def test_router_catalog_tracks_router_tier_helpers(tmp_path, monkeypatch):
from opensquilla.cli import onboard_cmd
from opensquilla.router_tiers import DEFAULT_TEXT_TIER, TEXT_TIERS
assert (
f"--default-tier {DEFAULT_TEXT_TIER}"
in onboard_cmd._CATALOG_COMMANDS["routerProfiles"]
)
target = tmp_path / "c.toml"
monkeypatch.setenv("OPENSQUILLA_GATEWAY_CONFIG_PATH", str(target))
result = runner.invoke(app, ["onboard", "catalog", "router"])
assert result.exit_code == 0, result.stdout
for tier in TEXT_TIERS:
assert f"{tier}:" in result.stdout