|
| 1 | +"""Tests for buffer_utils -- targeting 80%+ coverage. |
| 2 | +
|
| 3 | +Ported from packages/adapter-shared/src/buffer-utils.test.ts. |
| 4 | +""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from chat_sdk.shared.buffer_utils import buffer_to_data_uri, to_buffer, to_buffer_sync |
| 11 | + |
| 12 | +# --------------------------------------------------------------------------- |
| 13 | +# to_buffer (async) |
| 14 | +# --------------------------------------------------------------------------- |
| 15 | + |
| 16 | + |
| 17 | +class TestToBuffer: |
| 18 | + async def test_bytes_passthrough(self): |
| 19 | + data = b"hello" |
| 20 | + result = await to_buffer(data, "slack") |
| 21 | + assert result == b"hello" |
| 22 | + assert isinstance(result, bytes) |
| 23 | + |
| 24 | + async def test_bytearray_conversion(self): |
| 25 | + data = bytearray(b"hello") |
| 26 | + result = await to_buffer(data, "slack") |
| 27 | + assert result == b"hello" |
| 28 | + assert isinstance(result, bytes) |
| 29 | + |
| 30 | + async def test_memoryview_conversion(self): |
| 31 | + data = memoryview(b"hello") |
| 32 | + result = await to_buffer(data, "slack") |
| 33 | + assert result == b"hello" |
| 34 | + assert isinstance(result, bytes) |
| 35 | + |
| 36 | + async def test_raises_for_unsupported_type_string(self): |
| 37 | + from chat_sdk.shared.errors import ValidationError |
| 38 | + |
| 39 | + with pytest.raises(ValidationError): |
| 40 | + await to_buffer("string", "slack") |
| 41 | + |
| 42 | + async def test_raises_for_unsupported_type_int(self): |
| 43 | + from chat_sdk.shared.errors import ValidationError |
| 44 | + |
| 45 | + with pytest.raises(ValidationError): |
| 46 | + await to_buffer(123, "slack") |
| 47 | + |
| 48 | + async def test_raises_for_unsupported_type_dict(self): |
| 49 | + from chat_sdk.shared.errors import ValidationError |
| 50 | + |
| 51 | + with pytest.raises(ValidationError): |
| 52 | + await to_buffer({}, "slack") |
| 53 | + |
| 54 | + async def test_raises_for_unsupported_type_none(self): |
| 55 | + from chat_sdk.shared.errors import ValidationError |
| 56 | + |
| 57 | + with pytest.raises(ValidationError): |
| 58 | + await to_buffer(None, "slack") |
| 59 | + |
| 60 | + async def test_returns_none_when_throw_disabled(self): |
| 61 | + result = await to_buffer("string", "teams", throw_on_unsupported=False) |
| 62 | + assert result is None |
| 63 | + |
| 64 | + async def test_includes_platform_in_error(self): |
| 65 | + from chat_sdk.shared.errors import ValidationError |
| 66 | + |
| 67 | + try: |
| 68 | + await to_buffer("invalid", "slack") |
| 69 | + except ValidationError as e: |
| 70 | + assert e.adapter == "slack" |
| 71 | + |
| 72 | + |
| 73 | +# --------------------------------------------------------------------------- |
| 74 | +# to_buffer_sync |
| 75 | +# --------------------------------------------------------------------------- |
| 76 | + |
| 77 | + |
| 78 | +class TestToBufferSync: |
| 79 | + def test_bytes_passthrough(self): |
| 80 | + data = b"hello" |
| 81 | + result = to_buffer_sync(data, "slack") |
| 82 | + assert result == b"hello" |
| 83 | + |
| 84 | + def test_bytearray_conversion(self): |
| 85 | + data = bytearray(b"hello") |
| 86 | + result = to_buffer_sync(data, "slack") |
| 87 | + assert result == b"hello" |
| 88 | + |
| 89 | + def test_memoryview_conversion(self): |
| 90 | + data = memoryview(b"hello") |
| 91 | + result = to_buffer_sync(data, "slack") |
| 92 | + assert result == b"hello" |
| 93 | + |
| 94 | + def test_raises_for_unsupported_type(self): |
| 95 | + from chat_sdk.shared.errors import ValidationError |
| 96 | + |
| 97 | + with pytest.raises(ValidationError): |
| 98 | + to_buffer_sync("string", "slack") |
| 99 | + |
| 100 | + def test_returns_none_when_throw_disabled(self): |
| 101 | + result = to_buffer_sync("string", "teams", throw_on_unsupported=False) |
| 102 | + assert result is None |
| 103 | + |
| 104 | + def test_includes_platform_in_error(self): |
| 105 | + from chat_sdk.shared.errors import ValidationError |
| 106 | + |
| 107 | + try: |
| 108 | + to_buffer_sync("invalid", "teams") |
| 109 | + except ValidationError as e: |
| 110 | + assert e.adapter == "teams" |
| 111 | + |
| 112 | + |
| 113 | +# --------------------------------------------------------------------------- |
| 114 | +# buffer_to_data_uri |
| 115 | +# --------------------------------------------------------------------------- |
| 116 | + |
| 117 | + |
| 118 | +class TestBufferToDataUri: |
| 119 | + def test_default_mime_type(self): |
| 120 | + result = buffer_to_data_uri(b"hello") |
| 121 | + assert result == "data:application/octet-stream;base64,aGVsbG8=" |
| 122 | + |
| 123 | + def test_custom_mime_type(self): |
| 124 | + result = buffer_to_data_uri(b"hello", "text/plain") |
| 125 | + assert result == "data:text/plain;base64,aGVsbG8=" |
| 126 | + |
| 127 | + def test_image_mime_type(self): |
| 128 | + result = buffer_to_data_uri(bytes([0x89, 0x50, 0x4E, 0x47]), "image/png") |
| 129 | + assert result.startswith("data:image/png;base64,") |
| 130 | + |
| 131 | + def test_empty_buffer(self): |
| 132 | + result = buffer_to_data_uri(b"") |
| 133 | + assert result == "data:application/octet-stream;base64," |
0 commit comments