"""Search consumes get_asset_presentation for plugin-owned routes (ADR-010).

Core no longer hardcodes a plugin's search route once the plugin declares one:
measuring tools (which declare a by-asset presentation route) link through it,
while a type that has not declared falls back to the legacy id-keyed route.
"""

from types import SimpleNamespace

from shopdb.core.api.search import _get_asset_result


def _fake_asset(assettype, assetid, **extra):
    asset = SimpleNamespace(
        assettype=SimpleNamespace(assettype=assettype),
        assetid=assetid,
        display_name=f'{assettype}-{assetid}',
        name=None,
        assetnumber=f'AN{assetid}',
        location=None,
        machine=None, computer=None, network_device=None, printer=None,
        measuringtool=None,
    )
    for key, value in extra.items():
        setattr(asset, key, value)
    return asset


def test_measuring_tool_uses_declared_presentation_route(app):
    """A declared by-asset route is used, keyed on the core assetid."""
    with app.app_context():
        tool = _fake_asset(
            'measuring_tool', 641,
            measuringtool=SimpleNamespace(measuringtoolid=99))
        result = _get_asset_result(tool, 'x')
    assert result['url'] == '/measuringtools/by-asset/641'
    assert result['id'] == 641


def test_machine_falls_back_to_legacy_route(app):
    """A type with no declared presentation keeps the legacy id-keyed route."""
    with app.app_context():
        machine = _fake_asset(
            'machine', 80, machine=SimpleNamespace(machineid=12))
        result = _get_asset_result(machine, 'x')
    assert result['url'] == '/machines/12'
    assert result['id'] == 12
