Repository: emcie-co/parlant
File: server/tests/api/test_sessions.py (Python)

Code before:
        action="answer like a cow",
        tool_function=get_cow_uttering,
    )

    event = await post_message(
        container=container,
        session_id=session_id,
        message="Hello there!",
        response_timeout=Timeout(30),
    )

    events_in_session = (
        client.get(
            f"/sessions/{session_id}/events",
            params={"min_offset": event.offset + 1},
        )
        .raise_for_status()
        .json()["events"]
    )

    message_event = next(e for e in events_in_session if e["kind"] == "message")
    tool_call_event = next(e for e in events_in_session if e["kind"] == "tool")
    assert message_event["correlation_id"] == tool_call_event["correlation_id"]


async def test_that_deleting_an_event_filters_out_subsequent_events(
    client: TestClient,
    container: Container,
    session_id: SessionId,
) -> None:
    session_events = [
        make_event_params("client"),
        make_event_params("server"),
        make_event_params("client"),
        make_event_params("server"),
        make_event_params("client"),
    ]
    await populate_session_id(container, session_id, session_events)

    initial_events = (
        client.get(f"/sessions/{session_id}/events").raise_for_status().json()["events"]
    )
    assert len(initial_events) == len(session_events)

    event_to_delete = initial_events[1]
    deleted_event_ids = (
        client.delete(f"/sessions/{session_id}/events/{event_to_delete['offset']}")
        .raise_for_status()
        .json()["event_ids"]
    )


Review comment (suggestion):
The notion that "deleting an event filters out subsequent events" is a weird description for functional behavior. In practice we're doing something simpler: we're just deleting all events starting from a specific offset. I suggest the name `test_that_deleted_events_no_longer_show_up_in_the_listing`

Code after:
        action="answer like a cow",
        tool_function=get_cow_uttering,
    )

    event = await post_message(
        container=container,
        session_id=session_id,
        message="Hello there!",
        response_timeout=Timeout(30),
    )

    events_in_session = (
        client.get(
            f"/sessions/{session_id}/events",
            params={"min_offset": event.offset + 1},
        )
        .raise_for_status()
        .json()["events"]
    )

    message_event = next(e for e in events_in_session if e["kind"] == "message")
    tool_call_event = next(e for e in events_in_session if e["kind"] == "tool")
    assert message_event["correlation_id"] == tool_call_event["correlation_id"]


async def test_that_deleted_events_no_longer_show_up_in_the_listing(
    client: TestClient,
    container: Container,
    session_id: SessionId,
) -> None:
    session_events = [
        make_event_params("client"),
        make_event_params("server"),
        make_event_params("client"),
        make_event_params("server"),
        make_event_params("client"),
    ]
    await populate_session_id(container, session_id, session_events)

    initial_events = (
        client.get(f"/sessions/{session_id}/events").raise_for_status().json()["events"]
    )
    assert len(initial_events) == len(session_events)

    event_to_delete = initial_events[1]
    deleted_event_ids = (
        client.delete(f"/sessions/{session_id}/events?min_offset={event_to_delete['offset']}")
        .raise_for_status()
        .json()["event_ids"]
    )