Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions python/beeai_framework/emitter/emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,12 @@ def _create_event(self, name: str) -> EventMeta:

async def clone(self) -> "Emitter":
cloned = Emitter(
str(self._group_id),
self.namespace.copy(),
self.creator if self.creator else None,
self.context.copy(),
self.trace.model_copy() if self.trace else None,
self._events.copy(),
group_id=self._group_id,
namespace=self.namespace.copy(),
creator=self.creator,
context=self.context.copy(),
trace=self.trace.model_copy() if self.trace else None,
events=self._events.copy(),
)
for listener in self._listeners:
cloned.on(listener.raw, listener.callback, listener.options.model_copy() if listener.options else None)
Expand Down
46 changes: 46 additions & 0 deletions python/tests/test_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,52 @@ async def test_clone() -> None:
assert clone.events is not emitter.events


@pytest.mark.unit
@pytest.mark.asyncio
async def test_clone_preserves_group_id() -> None:
emitter = Emitter(group_id="test_group", namespace=["namespace"])
clone = await emitter.clone()

assert clone._group_id == "test_group"


@pytest.mark.unit
@pytest.mark.asyncio
async def test_clone_keeps_absent_group_id_absent() -> None:
# Agents build their emitter without a group id, so the clone must not turn
# the absent value into something truthy.
emitter = Emitter(namespace=["namespace"])
assert emitter._group_id is None

clone = await emitter.clone()

assert clone._group_id is None


@pytest.mark.unit
@pytest.mark.asyncio
async def test_clone_emits_events_without_a_group_id() -> None:
emitter = Emitter(namespace=["app"])
clone = await emitter.clone()

group_ids: list[str | None] = []
clone.on("*", lambda _, event: group_ids.append(event.group_id))
await clone.emit("a", 1)

assert group_ids == [None]


@pytest.mark.unit
@pytest.mark.asyncio
async def test_child_of_clone_inherits_absent_group_id() -> None:
emitter = Emitter(namespace=["app"])
clone = await emitter.clone()

child = clone.child(namespace=["child"])

assert child._group_id is None


class TestEventsPropagation:
@pytest.mark.unit
@pytest.mark.asyncio
Expand Down
Loading