from __future__ import annotations

import asyncio

from cmvr_edge_ai.plugins import PluginRegistry, register_builtin_plugins


def test_builtin_source_cannot_emit_an_undeclared_dynamic_port() -> None:
    registry = PluginRegistry()
    register_builtin_plugins(registry)
    source = registry.create(
        "core.sequence_source@1",
        "source",
        {"items": ["message"], "output_port": "undeclared"},
    )

    async def collect():  # type: ignore[no-untyped-def]
        return [emission async for emission in source.messages()]

    emissions = asyncio.run(collect())
    declared_ports = registry.resolve("core.sequence_source@1").outputs

    assert [emission.port for emission in emissions] == ["output"]
    assert all(emission.port in declared_ports for emission in emissions)
