|
| 1 | +"""Packaging boundary for the Teams primitive subpaths (Wave-B T7). |
| 2 | +
|
| 3 | +The six SDK-free Teams primitive subpaths -- |
| 4 | +
|
| 5 | + chat_sdk.adapters.teams.format |
| 6 | + chat_sdk.adapters.teams.webhook |
| 7 | + chat_sdk.adapters.teams.api |
| 8 | + chat_sdk.adapters.teams.cards_input |
| 9 | + chat_sdk.adapters.teams.graph |
| 10 | + chat_sdk.adapters.teams.modals |
| 11 | +
|
| 12 | +-- each landed with a *static source-scan* boundary test that asserted the |
| 13 | +module's own source never imports the Teams SDK, an HTTP client, or the |
| 14 | +high-level adapter. Those per-PR tests explicitly *deferred* the runtime |
| 15 | +``sys.modules`` guarantee to this packaging PR, because importing any subpath |
| 16 | +ran the package's ``teams/__init__.py``, which (while still eager) pulled the |
| 17 | +adapter in transitively. |
| 18 | +
|
| 19 | +Now that ``teams/__init__.py`` is PEP-562 lazy (mirroring the 0.4.30 Slack |
| 20 | +subpath pattern), importing a primitive subpath in a *fresh interpreter* must |
| 21 | +leave the Teams SDK, the HTTP clients, and the adapter module entirely out of |
| 22 | +``sys.modules``. This is the runtime-free guarantee the source-scans deferred, |
| 23 | +proven once here for all six subpaths. |
| 24 | +
|
| 25 | +It also pins the public-API contract: making ``teams/__init__.py`` lazy must |
| 26 | +not change that ``TeamsAdapter`` / ``create_teams_adapter`` / |
| 27 | +``TeamsAdapterConfig`` / ``TeamsAuthCertificate`` still import from the package |
| 28 | +root (the 0.4.30 Teams SDK migration's public surface is coupled downstream). |
| 29 | +""" |
| 30 | + |
| 31 | +from __future__ import annotations |
| 32 | + |
| 33 | +import subprocess |
| 34 | +import sys |
| 35 | + |
| 36 | +import pytest |
| 37 | + |
| 38 | +# The six SDK-free primitive subpaths packaged by T7. |
| 39 | +_PRIMITIVE_SUBPATHS = [ |
| 40 | + "chat_sdk.adapters.teams.format", |
| 41 | + "chat_sdk.adapters.teams.webhook", |
| 42 | + "chat_sdk.adapters.teams.api", |
| 43 | + "chat_sdk.adapters.teams.cards_input", |
| 44 | + "chat_sdk.adapters.teams.graph", |
| 45 | + "chat_sdk.adapters.teams.modals", |
| 46 | +] |
| 47 | + |
| 48 | +# Modules that must NEVER load when a primitive subpath is imported: the |
| 49 | +# Microsoft Teams SDK (root + every submodule the adapter reaches for), both |
| 50 | +# HTTP clients, and the high-level adapter module itself. |
| 51 | +_FORBIDDEN_MODULES = [ |
| 52 | + "microsoft_teams", |
| 53 | + "microsoft_teams.apps", |
| 54 | + "microsoft_teams.api", |
| 55 | + "microsoft_teams.common", |
| 56 | + "httpx", |
| 57 | + "aiohttp", |
| 58 | + "chat_sdk.adapters.teams.adapter", |
| 59 | +] |
| 60 | + |
| 61 | + |
| 62 | +def _import_in_fresh_interpreter(subpath: str) -> subprocess.CompletedProcess[str]: |
| 63 | + """Import ``subpath`` in a brand-new interpreter and assert the forbidden |
| 64 | + runtime modules are absent from ``sys.modules`` afterwards.""" |
| 65 | + forbidden = repr(_FORBIDDEN_MODULES) |
| 66 | + code = ( |
| 67 | + "import sys\n" |
| 68 | + f"import {subpath}\n" |
| 69 | + f"forbidden = {forbidden}\n" |
| 70 | + "loaded = [name for name in forbidden if name in sys.modules]\n" |
| 71 | + f"assert not loaded, " |
| 72 | + f"'{subpath} eagerly imported runtime modules: ' + repr(loaded)\n" |
| 73 | + ) |
| 74 | + return subprocess.run( |
| 75 | + [sys.executable, "-c", code], |
| 76 | + capture_output=True, |
| 77 | + text=True, |
| 78 | + check=False, |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +class TestTeamsPrimitiveSubpathBoundary: |
| 83 | + """The runtime-free guarantee the per-PR source-scans deferred to T7.""" |
| 84 | + |
| 85 | + @pytest.mark.parametrize("subpath", _PRIMITIVE_SUBPATHS) |
| 86 | + def test_subpath_import_is_runtime_free(self, subpath: str) -> None: |
| 87 | + result = _import_in_fresh_interpreter(subpath) |
| 88 | + assert result.returncode == 0, ( |
| 89 | + f"{subpath} pulled in a forbidden runtime module\nstdout: {result.stdout}\nstderr: {result.stderr}" |
| 90 | + ) |
| 91 | + |
| 92 | + def test_all_six_subpaths_are_covered(self) -> None: |
| 93 | + # Guard against the list silently shrinking: T7 packages exactly the |
| 94 | + # six SDK-free primitive subpaths. |
| 95 | + assert len(_PRIMITIVE_SUBPATHS) == 6 |
| 96 | + assert len(set(_PRIMITIVE_SUBPATHS)) == 6 |
| 97 | + |
| 98 | + |
| 99 | +class TestTeamsPublicApiSurvivesLazyInit: |
| 100 | + """The lazy ``teams/__init__.py`` must preserve the public contract.""" |
| 101 | + |
| 102 | + def test_public_adapter_api_still_imports_from_package_root(self) -> None: |
| 103 | + # Fresh interpreter: the four public names must resolve from the |
| 104 | + # package root exactly as they did when __init__ imported eagerly. |
| 105 | + code = ( |
| 106 | + "from chat_sdk.adapters.teams import (\n" |
| 107 | + " TeamsAdapter,\n" |
| 108 | + " create_teams_adapter,\n" |
| 109 | + " TeamsAdapterConfig,\n" |
| 110 | + " TeamsAuthCertificate,\n" |
| 111 | + ")\n" |
| 112 | + "assert TeamsAdapter.__name__ == 'TeamsAdapter'\n" |
| 113 | + "assert create_teams_adapter.__name__ == 'create_teams_adapter'\n" |
| 114 | + "assert TeamsAdapterConfig.__name__ == 'TeamsAdapterConfig'\n" |
| 115 | + "assert TeamsAuthCertificate.__name__ == 'TeamsAuthCertificate'\n" |
| 116 | + ) |
| 117 | + result = subprocess.run( |
| 118 | + [sys.executable, "-c", code], |
| 119 | + capture_output=True, |
| 120 | + text=True, |
| 121 | + check=False, |
| 122 | + ) |
| 123 | + assert result.returncode == 0, result.stderr |
| 124 | + |
| 125 | + def test_lazily_resolved_objects_are_identical_to_direct_imports(self) -> None: |
| 126 | + from chat_sdk.adapters.teams import ( |
| 127 | + TeamsAdapter, |
| 128 | + TeamsAdapterConfig, |
| 129 | + TeamsAuthCertificate, |
| 130 | + create_teams_adapter, |
| 131 | + ) |
| 132 | + from chat_sdk.adapters.teams.adapter import ( |
| 133 | + TeamsAdapter as DirectAdapter, |
| 134 | + ) |
| 135 | + from chat_sdk.adapters.teams.adapter import ( |
| 136 | + create_teams_adapter as direct_factory, |
| 137 | + ) |
| 138 | + from chat_sdk.adapters.teams.types import ( |
| 139 | + TeamsAdapterConfig as DirectConfig, |
| 140 | + ) |
| 141 | + from chat_sdk.adapters.teams.types import ( |
| 142 | + TeamsAuthCertificate as DirectCert, |
| 143 | + ) |
| 144 | + |
| 145 | + assert TeamsAdapter is DirectAdapter |
| 146 | + assert create_teams_adapter is direct_factory |
| 147 | + assert TeamsAdapterConfig is DirectConfig |
| 148 | + assert TeamsAuthCertificate is DirectCert |
| 149 | + |
| 150 | + def test_public_all_is_unchanged(self) -> None: |
| 151 | + import chat_sdk.adapters.teams as teams_pkg |
| 152 | + |
| 153 | + assert teams_pkg.__all__ == [ |
| 154 | + "TeamsAdapter", |
| 155 | + "TeamsAdapterConfig", |
| 156 | + "TeamsAuthCertificate", |
| 157 | + "create_teams_adapter", |
| 158 | + ] |
| 159 | + |
| 160 | + def test_unknown_attribute_raises_attribute_error(self) -> None: |
| 161 | + import chat_sdk.adapters.teams as teams_pkg |
| 162 | + |
| 163 | + with pytest.raises(AttributeError): |
| 164 | + _ = teams_pkg.NoSuchExport # type: ignore[attr-defined] |
| 165 | + |
| 166 | + def test_top_level_adapters_package_still_resolves_teams(self) -> None: |
| 167 | + from chat_sdk.adapters import TeamsAdapter, create_teams_adapter |
| 168 | + from chat_sdk.adapters.teams.adapter import ( |
| 169 | + TeamsAdapter as DirectAdapter, |
| 170 | + ) |
| 171 | + from chat_sdk.adapters.teams.adapter import ( |
| 172 | + create_teams_adapter as direct_factory, |
| 173 | + ) |
| 174 | + |
| 175 | + assert TeamsAdapter is DirectAdapter |
| 176 | + assert create_teams_adapter is direct_factory |
0 commit comments