Skip to content
Merged
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
133 changes: 133 additions & 0 deletions tests/test_buffer_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
"""Tests for buffer_utils -- targeting 80%+ coverage.

Ported from packages/adapter-shared/src/buffer-utils.test.ts.
"""

from __future__ import annotations

import pytest

from chat_sdk.shared.buffer_utils import buffer_to_data_uri, to_buffer, to_buffer_sync

# ---------------------------------------------------------------------------
# to_buffer (async)
# ---------------------------------------------------------------------------


class TestToBuffer:
async def test_bytes_passthrough(self):
data = b"hello"
result = await to_buffer(data, "slack")
assert result == b"hello"
assert isinstance(result, bytes)

async def test_bytearray_conversion(self):
data = bytearray(b"hello")
result = await to_buffer(data, "slack")
assert result == b"hello"
assert isinstance(result, bytes)

async def test_memoryview_conversion(self):
data = memoryview(b"hello")
result = await to_buffer(data, "slack")
assert result == b"hello"
assert isinstance(result, bytes)

async def test_raises_for_unsupported_type_string(self):
from chat_sdk.shared.errors import ValidationError

with pytest.raises(ValidationError):
await to_buffer("string", "slack")

async def test_raises_for_unsupported_type_int(self):
from chat_sdk.shared.errors import ValidationError

with pytest.raises(ValidationError):
await to_buffer(123, "slack")

async def test_raises_for_unsupported_type_dict(self):
from chat_sdk.shared.errors import ValidationError

with pytest.raises(ValidationError):
await to_buffer({}, "slack")

async def test_raises_for_unsupported_type_none(self):
from chat_sdk.shared.errors import ValidationError

with pytest.raises(ValidationError):
await to_buffer(None, "slack")

async def test_returns_none_when_throw_disabled(self):
result = await to_buffer("string", "teams", throw_on_unsupported=False)
assert result is None

async def test_includes_platform_in_error(self):
from chat_sdk.shared.errors import ValidationError

try:
await to_buffer("invalid", "slack")
except ValidationError as e:
assert e.adapter == "slack"
Comment on lines +67 to +70
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This test uses a try...except block to verify that an exception is raised, but it lacks a failure condition if the exception is not raised. If to_buffer were to return normally (e.g., due to a regression where throw_on_unsupported defaults to False), the test would silently pass. Using pytest.raises is more idiomatic and ensures the test fails if the expected exception is missing.

        with pytest.raises(ValidationError) as excinfo:
            await to_buffer("invalid", "slack")
        assert excinfo.value.adapter == "slack"

Comment on lines +67 to +70
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Assert the expected exception is raised

test_includes_platform_in_error uses a try/except without any failure path when no exception is raised, so the test will pass even if to_buffer stops raising ValidationError. That makes this regression check ineffective and can hide breakages in error handling (the same pattern is repeated in the sync variant); use pytest.raises(...) and assert on the captured exception instead.

Useful? React with 👍 / 👎.



# ---------------------------------------------------------------------------
# to_buffer_sync
# ---------------------------------------------------------------------------


class TestToBufferSync:
def test_bytes_passthrough(self):
data = b"hello"
result = to_buffer_sync(data, "slack")
assert result == b"hello"

def test_bytearray_conversion(self):
data = bytearray(b"hello")
result = to_buffer_sync(data, "slack")
assert result == b"hello"

def test_memoryview_conversion(self):
data = memoryview(b"hello")
result = to_buffer_sync(data, "slack")
assert result == b"hello"

def test_raises_for_unsupported_type(self):
from chat_sdk.shared.errors import ValidationError

with pytest.raises(ValidationError):
to_buffer_sync("string", "slack")

def test_returns_none_when_throw_disabled(self):
result = to_buffer_sync("string", "teams", throw_on_unsupported=False)
assert result is None

def test_includes_platform_in_error(self):
from chat_sdk.shared.errors import ValidationError

try:
to_buffer_sync("invalid", "teams")
except ValidationError as e:
assert e.adapter == "teams"
Comment on lines +107 to +110
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the async version, this try...except pattern for testing exceptions is prone to silent passes if the function fails to raise the expected ValidationError. Refactoring to use pytest.raises makes the test more robust and readable.

        with pytest.raises(ValidationError) as excinfo:
            to_buffer_sync("invalid", "teams")
        assert excinfo.value.adapter == "teams"



# ---------------------------------------------------------------------------
# buffer_to_data_uri
# ---------------------------------------------------------------------------


class TestBufferToDataUri:
def test_default_mime_type(self):
result = buffer_to_data_uri(b"hello")
assert result == "data:application/octet-stream;base64,aGVsbG8="

def test_custom_mime_type(self):
result = buffer_to_data_uri(b"hello", "text/plain")
assert result == "data:text/plain;base64,aGVsbG8="

def test_image_mime_type(self):
result = buffer_to_data_uri(bytes([0x89, 0x50, 0x4E, 0x47]), "image/png")
assert result.startswith("data:image/png;base64,")

def test_empty_buffer(self):
result = buffer_to_data_uri(b"")
assert result == "data:application/octet-stream;base64,"
Loading
Loading