|
| 1 | +"""The ADR 0095 managed-projects projection — `projects:` feeds the repo picker. |
| 2 | +
|
| 3 | +Host-free: `graph.sdk` doesn't exist in this suite, so the default path exercises |
| 4 | +the real "no host" degrade. Where a host IS needed, a fake `graph.sdk` module is |
| 5 | +installed in `sys.modules` (the standalone-plugin pattern) — nothing imports the |
| 6 | +host at module scope, so this stays a pure unit test. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import sys |
| 12 | +import types |
| 13 | + |
| 14 | +import pytest |
| 15 | +from ghplugin.gh_issue import effective_default_repo |
| 16 | +from ghplugin.projects import effective_repos, registry_repos |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def fake_host(monkeypatch): |
| 21 | + """Install a fake `graph.sdk` whose config() returns the given projects list.""" |
| 22 | + |
| 23 | + def _install(projects, *, raises: bool = False): |
| 24 | + sdk = types.ModuleType("graph.sdk") |
| 25 | + |
| 26 | + def config(): |
| 27 | + if raises: |
| 28 | + raise RuntimeError("config not loaded") |
| 29 | + return types.SimpleNamespace(projects=projects) |
| 30 | + |
| 31 | + sdk.config = config |
| 32 | + graph = types.ModuleType("graph") |
| 33 | + graph.sdk = sdk |
| 34 | + monkeypatch.setitem(sys.modules, "graph", graph) |
| 35 | + monkeypatch.setitem(sys.modules, "graph.sdk", sdk) |
| 36 | + |
| 37 | + return _install |
| 38 | + |
| 39 | + |
| 40 | +# ── degrade paths — the plugin floor stays 0.27.0, the registry is 0.115.0+ ── |
| 41 | + |
| 42 | + |
| 43 | +def test_no_host_yields_no_repos(): |
| 44 | + """The host-free case, and equally any pre-0.115.0 host: never raises.""" |
| 45 | + assert registry_repos() == [] |
| 46 | + |
| 47 | + |
| 48 | +def test_host_without_the_registry_yields_no_repos(fake_host): |
| 49 | + """A pre-0.115.0 host has no `projects` attribute at all.""" |
| 50 | + sdk = types.ModuleType("graph.sdk") |
| 51 | + sdk.config = lambda: types.SimpleNamespace() # no .projects |
| 52 | + graph = types.ModuleType("graph") |
| 53 | + graph.sdk = sdk |
| 54 | + sys.modules["graph"], sys.modules["graph.sdk"] = graph, sdk |
| 55 | + try: |
| 56 | + assert registry_repos() == [] |
| 57 | + finally: |
| 58 | + del sys.modules["graph.sdk"], sys.modules["graph"] |
| 59 | + |
| 60 | + |
| 61 | +def test_a_raising_host_config_yields_no_repos(fake_host): |
| 62 | + """Config not loaded yet must not take the picker down with it.""" |
| 63 | + fake_host([], raises=True) |
| 64 | + assert registry_repos() == [] |
| 65 | + |
| 66 | + |
| 67 | +# ── the projection itself ── |
| 68 | + |
| 69 | + |
| 70 | +def test_registry_repos_are_deduped_in_config_order(fake_host): |
| 71 | + fake_host( |
| 72 | + [ |
| 73 | + {"name": "a", "path": "/a", "github": "o/a"}, |
| 74 | + {"name": "b", "path": "/b"}, # no github — contributes nothing |
| 75 | + {"name": "c", "path": "/c", "github": "o/c"}, |
| 76 | + {"name": "d", "path": "/d", "github": "o/a"}, # dupe |
| 77 | + {"name": "e", "path": "/e", "github": " "}, # blank |
| 78 | + "not-a-dict", |
| 79 | + ] |
| 80 | + ) |
| 81 | + assert registry_repos() == ["o/a", "o/c"] |
| 82 | + |
| 83 | + |
| 84 | +def test_explicit_repos_win_over_the_registry(fake_host): |
| 85 | + """Non-regression: a configured picker list means the registry is never consulted.""" |
| 86 | + fake_host([{"name": "a", "path": "/a", "github": "o/registry"}]) |
| 87 | + assert effective_repos(["o/explicit"]) == ["o/explicit"] |
| 88 | + |
| 89 | + |
| 90 | +def test_registry_fills_an_empty_picker(fake_host): |
| 91 | + fake_host([{"name": "a", "path": "/a", "github": "o/a"}]) |
| 92 | + assert effective_repos([]) == ["o/a"] |
| 93 | + assert effective_repos(None) == ["o/a"] |
| 94 | + |
| 95 | + |
| 96 | +def test_blank_only_explicit_list_falls_through(fake_host): |
| 97 | + """`repos: ["", " "]` is not a configured list — it's an empty one.""" |
| 98 | + fake_host([{"name": "a", "path": "/a", "github": "o/a"}]) |
| 99 | + assert effective_repos(["", " "]) == ["o/a"] |
| 100 | + |
| 101 | + |
| 102 | +# ── composition with the default-repo resolution ── |
| 103 | + |
| 104 | + |
| 105 | +def test_default_repo_falls_through_to_the_registry(fake_host): |
| 106 | + """effective_default_repo stays PURE — the registry arrives as its `repos` arg, |
| 107 | + so /issue, the tools and the picker keep agreeing on one answer.""" |
| 108 | + fake_host([{"name": "a", "path": "/a", "github": "o/a"}]) |
| 109 | + assert effective_default_repo("", effective_repos([])) == "o/a" |
| 110 | + # explicit default still beats everything |
| 111 | + assert effective_default_repo("o/explicit", effective_repos([])) == "o/explicit" |
0 commit comments