GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — devstack/scripts/test_localpibox_config.py at docs · lpb-stack/devstack Expand file tree/ Copy pathtest_localpibox_config.pyMore file actions 658 lines (550 loc) · 25.4 KB / Copy pathtest_localpibox_config.pyFile metadata and controls658 lines (550 loc) · 25.4 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 #!/usr/bin/env python3 """scripts/lpb-config tests: legacy layout migration, update (clone/fast-forward/refuse-dirty), reset (force/abort), status states, merge.""" from __future__ import annotations from testharness import run_lpbx_suite, _quiet_console, log_mod, _load_script, SCRIPTS_DIR import io import json import os import subprocess from unittest import mock from localpibox.setup import ProbeResult lc = _load_script('lpb_config', SCRIPTS_DIR / 'lpb-config') # NOTE: These tests are stubs for lpb-config features (repo listing, # mirror management) that are not yet implemented. They are skipped # until the underlying functions exist. # ═══════════════════════════════════════════════════════════════════════════ def _setup_git_remote(tmpdir): """Create a bare remote + work clone with one commit on 'main'.""" remote = tmpdir / "remote.git" subprocess.run(["git", "init", "-q", "--bare", str(remote)], check=True) work = tmpdir / "work" subprocess.run(["git", "clone", "-q", str(remote), str(work)], check=True) subprocess.run(["git", "-C", str(work), "config", "user.email", "t@t"], check=True) subprocess.run(["git", "-C", str(work), "config", "user.name", "t"], check=True) subprocess.run(["git", "-C", str(work), "branch", "-M", "main"], check=True) (work / "f").write_text("one") subprocess.run(["git", "-C", str(work), "add", "."], check=True) subprocess.run(["git", "-C", str(work), "commit", "-qm", "one"], check=True) subprocess.run(["git", "-C", str(work), "push", "-q", "origin", "main"], check=True) return remote def _push_commit(work, msg, content): (work / "f").write_text(content) subprocess.run(["git", "-C", str(work), "commit", "-qam", msg], check=True) subprocess.run(["git", "-C", str(work), "push", "-q", "origin", "main"], check=True) def test_lpb_config_migrate_legacy_layout(tmpdir): pi_root = tmpdir / ".pi" pi_root.mkdir() (pi_root / ".git").mkdir() (pi_root / "auth.json").write_text("{}") (pi_root / ".initialized").touch() (pi_root / "agent").mkdir() cons = _quiet_console() lc.migrate_legacy_layout(pi_root, tmpdir / "agent_dir", cons) assert (tmpdir / "agent_dir" / "auth.json").exists() assert (pi_root / ".initialized").exists() # marker preserved assert (pi_root / "agent").exists() # subdir preserved assert not (pi_root / "auth.json").exists() def test_lpb_config_migrate_noop_when_already_migrated(tmpdir): pi_root = tmpdir / ".pi" pi_root.mkdir() agent = tmpdir / "agent" agent.mkdir() (agent / ".git").mkdir() cons = _quiet_console() lc.migrate_legacy_layout(pi_root, agent, cons) # must not raise assert True def test_lpb_config_update_clones(tmpdir): remote = _setup_git_remote(tmpdir) agent = tmpdir / "agent" cons = _quiet_console() code = lc.cmd_update(agent, str(remote), "main", cons) assert code == 0 assert (agent / "f").exists() def test_lpb_config_update_fast_forward(tmpdir): remote = _setup_git_remote(tmpdir) agent = tmpdir / "agent" lc.cmd_update(agent, str(remote), "main", _quiet_console()) _push_commit(tmpdir / "work", "two", "two") cons = _quiet_console() assert lc.cmd_update(agent, str(remote), "main", cons) == 0 assert (agent / "f").read_text() == "two" def test_lpb_config_update_refuses_when_dirty(tmpdir): remote = _setup_git_remote(tmpdir) agent = tmpdir / "agent" lc.cmd_update(agent, str(remote), "main", _quiet_console()) _push_commit(tmpdir / "work", "two", "two") (agent / "local").write_text("keep") # untracked = dirty cons = _quiet_console() assert lc.cmd_update(agent, str(remote), "main", cons) == 1 assert (agent / "local").exists() def test_lpb_config_reset_force(tmpdir): remote = _setup_git_remote(tmpdir) agent = tmpdir / "agent" lc.cmd_update(agent, str(remote), "main", _quiet_console()) _push_commit(tmpdir / "work", "two", "two") (agent / "junk").write_text("x") cons = _quiet_console() assert lc.cmd_reset(agent, str(remote), "main", cons, force=True) == 0 assert not (agent / "junk").exists() assert (agent / "f").read_text() == "two" def test_lpb_config_reset_abort_without_confirmation(tmpdir): remote = _setup_git_remote(tmpdir) agent = tmpdir / "agent" lc.cmd_update(agent, str(remote), "main", _quiet_console()) (agent / "junk").write_text("x") cons = _quiet_console() assert lc.cmd_reset(agent, str(remote), "main", cons, force=False, inp=io.StringIO("n\n")) == 0 assert (agent / "junk").exists() def test_lpb_config_status_states(tmpdir): remote = _setup_git_remote(tmpdir) agent = tmpdir / "agent" lc.cmd_update(agent, str(remote), "main", _quiet_console()) out, err = io.StringIO(), io.StringIO() cons = log_mod.Console(color=False, out=out, err=err) assert lc.cmd_status(agent, str(remote), "main", cons) == 0 text = out.getvalue() + err.getvalue() assert "Current:" in text and "Remote:" in text and "clean" in text # no repo yet out2, err2 = io.StringIO(), io.StringIO() cons2 = log_mod.Console(color=False, out=out2, err=err2) assert lc.cmd_status(tmpdir / "nope", str(remote), "main", cons2) == 0 assert "No config repo" in (out2.getvalue() + err2.getvalue()) def _status_text(tmpdir, agent, remote): out, err = io.StringIO(), io.StringIO() cons = log_mod.Console(color=False, out=out, err=err) assert lc.cmd_status(agent, str(remote), "main", cons) == 0 return out.getvalue() + err.getvalue() def test_lpb_config_status_ahead_not_behind(tmpdir): """Local commits not pushed must report ahead, not 'behind remote'.""" remote = _setup_git_remote(tmpdir) agent = tmpdir / "agent" lc.cmd_update(agent, str(remote), "main", _quiet_console()) (agent / "f").write_text("local only") subprocess.run(["git", "-C", str(agent), "config", "user.email", "t@t"], check=True) subprocess.run(["git", "-C", str(agent), "config", "user.name", "t"], check=True) subprocess.run(["git", "-C", str(agent), "commit", "-qam", "local"], check=True) text = _status_text(tmpdir, agent, remote) assert "ahead of origin/main by 1" in text assert "behind" not in text def test_lpb_config_status_behind(tmpdir): remote = _setup_git_remote(tmpdir) agent = tmpdir / "agent" lc.cmd_update(agent, str(remote), "main", _quiet_console()) _push_commit(tmpdir / "work", "two", "two") subprocess.run(["git", "-C", str(agent), "fetch", "-q", "origin", "main"], check=True) text = _status_text(tmpdir, agent, remote) assert "behind origin/main by 1" in text assert "run 'lpb-config update'" in text assert "ahead" not in text def test_lpb_config_status_up_to_date(tmpdir): remote = _setup_git_remote(tmpdir) agent = tmpdir / "agent" lc.cmd_update(agent, str(remote), "main", _quiet_console()) text = _status_text(tmpdir, agent, remote) assert "up to date with origin/main" in text def test_lpb_config_status_missing_remote_ref(tmpdir): """origin/<ref> not fetched locally: no fake remote head, no crash, no 'behind'.""" remote = _setup_git_remote(tmpdir) agent = tmpdir / "agent" # .git present but no origin ref at all (ref name the remote doesn't have) agent.mkdir() subprocess.run(["git", "-C", str(agent), "init", "-q", "-b", "main"], check=True) subprocess.run(["git", "-C", str(agent), "remote", "add", "origin", str(remote)], check=True) subprocess.run(["git", "-C", str(agent), "config", "user.email", "t@t"], check=True) subprocess.run(["git", "-C", str(agent), "config", "user.name", "t"], check=True) (agent / "f").write_text("x") subprocess.run(["git", "-C", str(agent), "add", "."], check=True) subprocess.run(["git", "-C", str(agent), "commit", "-qm", "x"], check=True) text = _status_text(tmpdir, agent, remote) assert "behind" not in text assert "not found locally" in text def test_lpb_config_status_diverged(tmpdir): remote = _setup_git_remote(tmpdir) agent = tmpdir / "agent" lc.cmd_update(agent, str(remote), "main", _quiet_console()) (agent / "f").write_text("local only") subprocess.run(["git", "-C", str(agent), "config", "user.email", "t@t"], check=True) subprocess.run(["git", "-C", str(agent), "config", "user.name", "t"], check=True) subprocess.run(["git", "-C", str(agent), "commit", "-qam", "local"], check=True) _push_commit(tmpdir / "work", "remote", "remote") subprocess.run(["git", "-C", str(agent), "fetch", "-q", "origin", "main"], check=True) text = _status_text(tmpdir, agent, remote) assert "diverged" in text and "ahead 1" in text and "behind 1" in text def test_lpb_config_merge_uptodate(tmpdir): remote = _setup_git_remote(tmpdir) agent = tmpdir / "agent" lc.cmd_update(agent, str(remote), "main", _quiet_console()) cons = _quiet_console() assert lc.cmd_merge(agent, str(remote), "main", cons) == 0 # merge when no repo -> error assert lc.cmd_merge(tmpdir / "nope", str(remote), "main", _quiet_console()) == 1 # ─── Template rendering (render / auto-render after reset/update/merge) ── def _make_templates(dir_): (dir_ / "settings.json.template").write_text(json.dumps({ "packages": ["git:github.com/lpb-stack/demo@__LPB_VERSION__", "npm:somepkg"], "theme": "dark", })) (dir_ / "lpb-memory-config.json.template").write_text(json.dumps({ "reviewTransport": "subprocess", "memoryCharLimit": 3000, })) def _push_templates(work): _make_templates(work) # Mirror the real config repo: rendered runtime files are gitignored, # otherwise they'd trip lpb-config's local-change guard. (work / ".gitignore").write_text("settings.json\nlpb-memory-config.json\n") subprocess.run(["git", "-C", str(work), "add", "."], check=True) subprocess.run(["git", "-C", str(work), "commit", "-qm", "templates"], check=True) subprocess.run(["git", "-C", str(work), "push", "-q", "origin", "main"], check=True) def _with_lpb_version(value: str): """Set LPB_VERSION for the duration of a test; returns restore closure.""" old = os.environ.get("LPB_VERSION") os.environ["LPB_VERSION"] = value def _restore(): if old is None: os.environ.pop("LPB_VERSION", None) else: os.environ["LPB_VERSION"] = old return _restore def _capture_console(): out, err = io.StringIO(), io.StringIO() cons = log_mod.Console(color=False, out=out, err=err) return cons, lambda: out.getvalue() + err.getvalue() def test_render_creates_from_template(tmpdir): agent = tmpdir / "agent" agent.mkdir() _make_templates(agent) restore = _with_lpb_version("0.9.9-lpb") try: cons, text = _capture_console() assert lc.cmd_render(agent, cons) == 0 finally: restore() text = text() raw = (agent / "settings.json").read_text() settings = json.loads(raw) assert "__LPB_VERSION__" not in raw mem = json.loads((agent / "lpb-memory-config.json").read_text()) assert mem["reviewTransport"] == "subprocess" assert "Generated" in text def test_render_no_env_uses_version_file_fallback(tmpdir): """With LPB_VERSION unset, falls back to the devstack VERSION file.""" agent = tmpdir / "agent" agent.mkdir() _make_templates(agent) old = os.environ.pop("LPB_VERSION", None) try: cons, text = _capture_console() assert lc.cmd_render(agent, cons) == 0 finally: if old is not None: os.environ["LPB_VERSION"] = old raw = (agent / "settings.json").read_text() # Must be a real version (no placeholder left, no empty pin) assert "__LPB_VERSION__" not in raw assert "demo@" in raw def test_render_nonforce_never_overwrites(tmpdir): agent = tmpdir / "agent" agent.mkdir() _make_templates(agent) customized = { "theme": "light", "defaultProvider": "lemonade", } (agent / "settings.json").write_text(json.dumps(customized)) restore = _with_lpb_version("0.2.0-lpb") try: cons, text = _capture_console() assert lc.cmd_render(agent, cons) == 0 finally: restore() # untouched file, but stale pins reported text = text() assert json.loads((agent / "settings.json").read_text()) == customized assert "stale" in text and "0.1.0-lpb -> 0.2.0-lpb" in text def test_render_force_repins_and_keeps_user_keys(tmpdir): agent = tmpdir / "agent" agent.mkdir() _make_templates(agent) customized = { "theme": "light", "defaultProvider": "lemonade", } (agent / "settings.json").write_text(json.dumps(customized)) restore = _with_lpb_version("0.2.0-lpb") try: cons, text = _capture_console() assert lc.cmd_render(agent, cons, force=True) == 0 finally: restore() s = json.loads((agent / "settings.json").read_text()) assert "npm:user-pkg" in s["packages"] # user package preserved assert s["defaultProvider"] == "lemonade" # user key preserved assert s["theme"] == "dark" # template key wins def test_render_force_memory_local_wins(tmpdir): agent = tmpdir / "agent" agent.mkdir() _make_templates(agent) (agent / "lpb-memory-config.json").write_text(json.dumps({ "reviewTransport": "direct", "llmModelOverride": "qwen-x", })) restore = _with_lpb_version("0.2.0-lpb") try: cons, text = _capture_console() assert lc.cmd_render(agent, cons, force=True) == 0 finally: restore() m = json.loads((agent / "lpb-memory-config.json").read_text()) assert m["reviewTransport"] == "direct" # local (wizard) wins assert m["llmModelOverride"] == "qwen-x" # wizard key kept assert m["memoryCharLimit"] == 3000 # template fills gaps def test_reset_renders_runtime_config(tmpdir): remote = _setup_git_remote(tmpdir) _push_templates(tmpdir / "work") agent = tmpdir / "agent" restore = _with_lpb_version("0.5.5-lpb") try: lc.cmd_update(agent, str(remote), "main", _quiet_console()) (agent / "junk").write_text("x") cons, text = _capture_console() assert lc.cmd_reset(agent, str(remote), "main", cons, force=True) == 0 finally: restore() assert not (agent / "junk").exists() s = json.loads((agent / "settings.json").read_text()) assert (agent / "lpb-memory-config.json").is_file() assert "regenerated from templates" in text() def test_update_restores_missing_rendered(tmpdir): remote = _setup_git_remote(tmpdir) _push_templates(tmpdir / "work") agent = tmpdir / "agent" restore = _with_lpb_version("0.6.6-lpb") try: lc.cmd_update(agent, str(remote), "main", _quiet_console()) assert (agent / "settings.json").is_file() (agent / "settings.json").unlink() _push_commit(tmpdir / "work", "bump", "three") cons, text = _capture_console() assert lc.cmd_update(agent, str(remote), "main", cons) == 0 finally: restore() raw = (agent / "settings.json").read_text() assert "0.6.6-lpb" in raw assert "Generated settings.json" in text() def test_update_warns_stale_pins_without_clobbering(tmpdir): """Stack version moves (new image), rendered file keeps old pins: update must warn, not silently overwrite.""" remote = _setup_git_remote(tmpdir) _push_templates(tmpdir / "work") agent = tmpdir / "agent" restore = _with_lpb_version("0.1.0-lpb") try: lc.cmd_update(agent, str(remote), "main", _quiet_console()) finally: restore() restore2 = _with_lpb_version("0.2.0-lpb") try: _push_commit(tmpdir / "work", "x", "four") cons, text = _capture_console() assert lc.cmd_update(agent, str(remote), "main", cons) == 0 finally: restore2() assert "stale" in text() s = json.loads((agent / "settings.json").read_text()) assert s["packages"][0].endswith("@0.1.0-lpb") # untouched def test_clone_or_init_survives_stale_remote(tmpdir): """Partial wipe: leftover .git already has an origin — repoint, don't abort.""" remote = _setup_git_remote(tmpdir) agent = tmpdir / "agent" agent.mkdir() subprocess.run(["git", "-C", str(agent), "init", "-q"], check=True) subprocess.run(["git", "-C", str(agent), "remote", "add", "origin", "/old/nonexistent.git"], check=True) (agent / "preexisting").write_text("keep me") cons, text = _capture_console() assert lc.clone_or_init(agent, str(remote), "main", cons) is True assert (agent / "f").read_text() == "one" r = subprocess.run( ["git", "-C", str(agent), "remote", "get-url", "origin"], capture_output=True, text=True, check=False) assert r.returncode == 0 and r.stdout.strip() == str(remote) assert (agent / "preexisting").exists() def test_reset_warns_on_incomplete_wipe(tmpdir): """rmtree that leaves survivors (files in use) must warn, not claim a clean reset.""" remote = _setup_git_remote(tmpdir) agent = tmpdir / "agent" lc.cmd_update(agent, str(remote), "main", _quiet_console()) (agent / "busy").write_text("in use") real_rmtree = lc.shutil.rmtree lc.shutil.rmtree = lambda *a, **k: None # simulate failure try: cons, text = _capture_console() assert lc.cmd_reset(agent, str(remote), "main", cons, force=True) == 0 finally: lc.shutil.rmtree = real_rmtree assert "Wipe incomplete" in text() and "busy" in text() assert (agent / "busy").exists() # untracked survivor preserved assert (agent / "f").read_text() == "one" # tracked tree still reset def test_render_missing_dir_errors(tmpdir): cons, text = _capture_console() assert lc.cmd_render(tmpdir / "nope", cons) == 1 assert "No config area" in text() # ─── Runtime settings: show / models / set ────────────────────────────────── def _make_agent_runtime(tmpdir): """Agent dir with rendered runtime files (settings/auth/mcp/memory).""" agent = tmpdir / "agent" agent.mkdir() (agent / "settings.json").write_text(json.dumps({ "packages": [ "git:github.com/lpb-stack/[email protected]", "npm:pi-mcp-adapter", ], "defaultProvider": "lemonade", "defaultModel": "ModelA", "defaultThinkingLevel": "medium", })) (agent / "auth.json").write_text(json.dumps({ "lemonade": { "type": "oauth", "refresh": json.dumps({"baseUrl": "http://10.0.0.5:13305", "apiKey": "sekret", "serverName": "10.0.0.5"}), "access": "sekret", }, })) (agent / "mcp.json").write_text(json.dumps({ "mcpServers": {"exa": {"enabled": True}, "chrome-devtools": {"enabled": False}}, })) (agent / "lpb-memory-config.json").write_text(json.dumps({ "memoryMode": "legacy-inject", "memoryCharLimit": 5000, "userCharLimit": 5000, "failureInjectionMaxEntries": 5, "failureInjectionMaxAgeDays": 3, "llmModelOverride": "small-model", })) return agent def _ok_probe(models=("ModelA", "ModelB"), loaded=()): return ProbeResult( ok=True, reachable=True, auth_failed=False, models=[{"id": m, "name": None, "ctx": None} for m in models], loaded=set(loaded), ) def test_show_human(tmpdir): agent = _make_agent_runtime(tmpdir) with mock.patch.object(lc, "probe_server", return_value=_ok_probe()): cons, text = _capture_console() assert lc.cmd_show(agent, cons) == 0 text = text() assert "ModelA" in text assert "http://10.0.0.5:13305" in text assert "reachable, 2 model(s)" in text assert "exa" in text and "chrome-devtools" in text and "disabled" in text assert "legacy-inject" in text and "small-model" in text assert "sekret" not in text # API key never printed def test_show_json(tmpdir): agent = _make_agent_runtime(tmpdir) buf = io.StringIO() with mock.patch.object(lc, "probe_server", return_value=_ok_probe()), \ mock.patch("sys.stdout", buf): assert lc.cmd_show(agent, _quiet_console(), as_json=True) == 0 data = json.loads(buf.getvalue()) assert data["model"] == "ModelA" assert data["provider"] == "lemonade" assert data["thinkingLevel"] == "medium" assert data["lemonade"]["baseUrl"] == "http://10.0.0.5:13305" assert data["lemonade"]["keySet"] is True assert data["lemonade"]["reachable"] is True assert data["lemonade"]["models"] == 2 assert data["mcpServers"] == {"exa": True, "chrome-devtools": False} assert data["extensionPins"]["lemonade-pi-plugin"] == "0.1.0-lpb-dev" assert data["memory"]["mode"] == "legacy-inject" def test_show_unconfigured(tmpdir): agent = tmpdir / "agent" agent.mkdir() (agent / "settings.json").write_text(json.dumps({"defaultModel": "X"})) with mock.patch.object(lc, "probe_server") as probe: cons, text = _capture_console() assert lc.cmd_show(agent, cons) == 0 probe.assert_not_called() assert "not configured" in text() def test_models_lists_and_marks_default(tmpdir): agent = _make_agent_runtime(tmpdir) with mock.patch.object(lc, "probe_server", return_value=_ok_probe(("ModelA", "ModelB"), ("ModelA",))): cons, text = _capture_console() assert lc.cmd_models(agent, cons) == 0 text = text() assert "ModelA" in text and "ModelB" in text assert "← default" in text and "[loaded]" in text def test_models_unreachable(tmpdir): agent = _make_agent_runtime(tmpdir) bad = ProbeResult(ok=False, reachable=False, auth_failed=False, error="connection refused") with mock.patch.object(lc, "probe_server", return_value=bad): cons, text = _capture_console() assert lc.cmd_models(agent, cons) == 1 assert "connection refused" in text() def test_set_model_writes_and_warns_on_unknown(tmpdir): agent = _make_agent_runtime(tmpdir) # ModelC is not served by the mocked server → warn, but still write with mock.patch.object(lc, "probe_server", return_value=_ok_probe()) as probe: cons, text = _capture_console() assert lc.cmd_set_model(agent, "ModelC", cons) == 0 s = json.loads((agent / "settings.json").read_text()) assert s["defaultModel"] == "ModelC" assert s["defaultProvider"] == "lemonade" # write_default_model sets provider assert probe.called and "not in the server's model list" in text() def test_set_model_valid_no_warning(tmpdir): agent = _make_agent_runtime(tmpdir) with mock.patch.object(lc, "probe_server", return_value=_ok_probe()): cons, text = _capture_console() assert lc.cmd_set_model(agent, "ModelB", cons) == 0 assert json.loads((agent / "settings.json").read_text())["defaultModel"] == "ModelB" assert "not in the server's model list" not in text() def test_set_thinking_preserves_other_keys(tmpdir): agent = _make_agent_runtime(tmpdir) cons, text = _capture_console() assert lc.cmd_set_thinking(agent, "high", cons) == 0 s = json.loads((agent / "settings.json").read_text()) assert s["defaultThinkingLevel"] == "high" assert s["defaultModel"] == "ModelA" # untouched assert s["packages"] # untouched def test_set_thinking_requires_settings(tmpdir): agent = tmpdir / "agent" agent.mkdir() cons, text = _capture_console() assert lc.cmd_set_thinking(agent, "high", cons) == 1 assert "settings.json not found" in text() def test_set_base_url_preserves_key(tmpdir): agent = _make_agent_runtime(tmpdir) with mock.patch.object(lc, "probe_server", return_value=_ok_probe()): cons, text = _capture_console() assert lc.cmd_set_base_url(agent, "http://10.0.0.9:13305/v1", cons) == 0 auth = json.loads((agent / "auth.json").read_text()) refresh = json.loads(auth["lemonade"]["refresh"]) assert refresh["baseUrl"] == "http://10.0.0.9:13305" # /v1 normalized away assert refresh["apiKey"] == "sekret" # key preserved def test_set_api_key_preserves_url(tmpdir): agent = _make_agent_runtime(tmpdir) with mock.patch.object(lc, "probe_server", return_value=_ok_probe()): cons, text = _capture_console() assert lc.cmd_set_api_key(agent, "newkey", cons) == 0 auth = json.loads((agent / "auth.json").read_text()) refresh = json.loads(auth["lemonade"]["refresh"]) assert refresh["baseUrl"] == "http://10.0.0.5:13305" # URL preserved assert refresh["apiKey"] == "newkey" def test_set_api_key_requires_url(tmpdir): agent = tmpdir / "agent" agent.mkdir() (agent / "settings.json").write_text(json.dumps({})) cons, text = _capture_console() assert lc.cmd_set_api_key(agent, "newkey", cons) == 1 assert "set base-url" in text() def main() -> int: return run_lpbx_suite("lpb-config tests", globals()) if __name__ == "__main__": raise SystemExit(main()) |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |