# Copyright (c) 2025 Vertex AI Batch Embeddings API Contributors
#
# Licensed under the MIT License. See LICENSE file in the project root for details.


"""
Test compression functionality in EmbeddingService
"""

import gzip
import json

import pytest


class TestCompressionLogic:
    """Test compression features in EmbeddingService."""

    def test_gzip_compression_works(self):
        """Test that gzip compression actually works on JSONL data."""
        import io

        # Sample JSONL data
        texts = ["text1", "text2", "text3"]
        jsonl_data = "\n".join(json.dumps({"text": text}) for text in texts)

        # Compress the data
        compressed_buffer = io.BytesIO()
        with gzip.GzipFile(fileobj=compressed_buffer, mode="wb") as f:
            f.write(jsonl_data.encode("utf-8"))

        compressed_data = compressed_buffer.getvalue()

        # Verify it's compressed (should be smaller than original)
        assert len(compressed_data) < len(jsonl_data.encode("utf-8"))

        # Verify we can decompress it back
        decompressed_buffer = io.BytesIO(compressed_data)
        with gzip.GzipFile(fileobj=decompressed_buffer, mode="rb") as f:
            decompressed_data = f.read().decode("utf-8")

        assert decompressed_data == jsonl_data


if __name__ == "__main__":
    pytest.main([__file__])
