"""Tests for MetadataSerializer implementations."""

import pytest

from storage_core.serializer import (
    JsonMetadataSerializer,
    PassthroughMetadataSerializer,
)


class TestJsonMetadataSerializer:
    def test_serialize_strings_pass_through(self):
        ser = JsonMetadataSerializer()
        result = ser.serialize({"author": "dream", "source": "crawler"})
        assert result == {"author": "dream", "source": "crawler"}

    def test_serialize_non_strings_json_encoded(self):
        ser = JsonMetadataSerializer()
        result = ser.serialize({"count": 42, "tags": ["a", "b"]})
        assert result["count"] == 'json:42'
        assert result["tags"] == 'json:["a", "b"]'

    def test_deserialize_strings_returned_as_is(self):
        ser = JsonMetadataSerializer()
        result = ser.deserialize({"author": "dream"})
        assert result == {"author": "dream"}

    def test_deserialize_json_values_parsed(self):
        ser = JsonMetadataSerializer()
        result = ser.deserialize({"count": "json:42", "tags": 'json:["a", "b"]'})
        assert result["count"] == 42
        assert result["tags"] == ["a", "b"]

    def test_roundtrip_mixed(self):
        ser = JsonMetadataSerializer()
        original = {"author": "dream", "version": 2, "nested": {"x": 1}}
        serialized = ser.serialize(original)
        restored = ser.deserialize(serialized)
        assert restored == original


class TestPassthroughMetadataSerializer:
    def test_serialize_coerces_to_strings(self):
        ser = PassthroughMetadataSerializer()
        result = ser.serialize({"count": 42, "name": "test"})
        assert result == {"count": "42", "name": "test"}

    def test_deserialize_identity(self):
        ser = PassthroughMetadataSerializer()
        result = ser.deserialize({"key": "value"})
        assert result == {"key": "value"}

    def test_roundtrip_strings(self):
        ser = PassthroughMetadataSerializer()
        original = {"a": "b", "c": "d"}
        result = ser.deserialize(ser.serialize(original))
        assert result == original
