Files
odysseus/tests/test_contacts_routes_shim.py
T
Tal.Yuan 5acd0ceae9 refactor(routes): move contacts domain into routes/contacts/ subpackage (#5227)
Slice 2e of the route-domain reorganization (#4082/#4071, per
specs/architecture-runtime-inventory.md §6.3). Moves contacts_routes.py into
routes/contacts/, leaving a backward-compat sys.modules shim at the old path.
Pure file reorganization, no behavior change.

The shim uses sys.modules replacement (same pattern as the merged gallery
#4903, research #4975, memory #5007, and history #5090 slices) so that
`import routes.contacts_routes`, `from routes.contacts_routes import X`,
`importlib.import_module(...)`, the string-targeted
`monkeypatch.setattr("routes.contacts_routes.SETTINGS_FILE", ...)` used by
test_carddav_password_encryption.py, and the `import ... as cr` +
`setattr(cr, ...)` pattern in test_contacts_add_null_name.py all operate on
the same module object the application uses. This also keeps the mutable
module state `_contact_cache` identical across import paths.

The canonical module does NOT depend on the shim — routes/contacts/
contacts_routes.py imports only from core/, src/, and stdlib (zero internal
routes/ coupling). The inbound edge from routes/email_helpers.py (imports
_fetch_contacts) keeps working through the shim.

Zero source-introspection landmines — no test reads this file by path.

Adds tests/test_contacts_routes_shim.py to pin the sys.modules shim contract
(same-object + string-targeted monkeypatch reach-through).

Verified: compileall clean; full suite 4485 passed, 3 skipped.
2026-07-05 03:58:34 +02:00

47 lines
2.0 KiB
Python

"""Regression test for the contacts route shim (slice 2e, #4082/#4071).
The backward-compat shim at ``routes/contacts_routes.py`` uses ``sys.modules``
replacement so the legacy import path and the canonical ``routes.contacts.*``
path resolve to the *same* module object. This is required because:
* ``test_carddav_password_encryption.py`` uses string-targeted
``monkeypatch.setattr("routes.contacts_routes.SETTINGS_FILE", ...)`` which
must reach the canonical module to take effect;
* ``test_contacts_add_null_name.py`` / ``test_contacts_carddav_security.py``
use ``import routes.contacts_routes as cr`` + ``setattr(cr, ...)``;
* the module owns mutable state (``_contact_cache``) that must be shared
across import paths.
"""
import importlib
import routes.contacts_routes as _shim_contacts # noqa: F401
def test_legacy_and_canonical_contacts_module_are_same_object():
"""``import routes.contacts_routes`` must alias the canonical module."""
legacy = importlib.import_module("routes.contacts_routes")
canonical = importlib.import_module("routes.contacts.contacts_routes")
assert legacy is canonical, (
"routes.contacts_routes shim must resolve to the canonical "
"routes.contacts.contacts_routes module object"
)
def test_string_targeted_monkeypatch_reaches_canonical(monkeypatch):
"""String-targeted ``monkeypatch.setattr`` via the legacy path must reach
the canonical module.
``test_carddav_password_encryption.py`` patches
``"routes.contacts_routes.SETTINGS_FILE"`` as a fixture setup; for that
to take effect at runtime, the legacy module name and the canonical
module must be identical.
"""
canonical = importlib.import_module("routes.contacts.contacts_routes")
sentinel = object()
monkeypatch.setattr("routes.contacts_routes.setup_contacts_routes", sentinel)
assert canonical.setup_contacts_routes is sentinel, (
"string-targeted monkeypatch via legacy path did not reach the canonical module"
)