# -*- coding: utf-8 -*-

from hypothesis import given
import hypothesis.strategies as st

import pytest

from vdirsyncer.metasync import MetaSyncConflict, metasync
from vdirsyncer.storage.memory import MemoryStorage

from . import blow_up


def test_irrelevant_status():
    a = MemoryStorage()
    b = MemoryStorage()
    status = {'foo': 'bar'}

    metasync(a, b, status, keys=())
    assert not status


def test_basic(monkeypatch):
    a = MemoryStorage()
    b = MemoryStorage()
    status = {}

    a.set_meta('foo', 'bar')
    metasync(a, b, status, keys=['foo'])
    assert a.get_meta('foo') == b.get_meta('foo') == 'bar'

    a.set_meta('foo', 'baz')
    metasync(a, b, status, keys=['foo'])
    assert a.get_meta('foo') == b.get_meta('foo') == 'baz'

    monkeypatch.setattr(a, 'set_meta', blow_up)
    monkeypatch.setattr(b, 'set_meta', blow_up)
    metasync(a, b, status, keys=['foo'])
    assert a.get_meta('foo') == b.get_meta('foo') == 'baz'
    monkeypatch.undo()
    monkeypatch.undo()

    b.set_meta('foo', None)
    metasync(a, b, status, keys=['foo'])
    assert not a.get_meta('foo') and not b.get_meta('foo')


def test_conflict():
    a = MemoryStorage()
    b = MemoryStorage()
    status = {}
    a.set_meta('foo', 'bar')
    b.set_meta('foo', 'baz')

    with pytest.raises(MetaSyncConflict):
        metasync(a, b, status, keys=['foo'])

    assert a.get_meta('foo') == 'bar'
    assert b.get_meta('foo') == 'baz'
    assert not status


def test_conflict_same_content():
    a = MemoryStorage()
    b = MemoryStorage()
    status = {}
    a.set_meta('foo', 'bar')
    b.set_meta('foo', 'bar')

    metasync(a, b, status, keys=['foo'])
    assert a.get_meta('foo') == b.get_meta('foo') == status['foo'] == 'bar'


@pytest.mark.parametrize('wins', 'ab')
def test_conflict_x_wins(wins):
    a = MemoryStorage()
    b = MemoryStorage()
    status = {}
    a.set_meta('foo', 'bar')
    b.set_meta('foo', 'baz')

    metasync(a, b, status, keys=['foo'],
             conflict_resolution='a wins' if wins == 'a' else 'b wins')

    assert a.get_meta('foo') == b.get_meta('foo') == status['foo'] == (
        'bar' if wins == 'a' else 'baz'
    )


keys = st.text(min_size=1).filter(lambda x: x.strip() == x)

metadata = st.dictionaries(
    keys,
    st.text()
)


@given(
    a=metadata, b=metadata,
    status=metadata, keys=st.sets(keys),
    conflict_resolution=st.just('a wins') | st.just('b wins')
)
def test_fuzzing(a, b, status, keys, conflict_resolution):
    def _get_storage(m, instance_name):
        s = MemoryStorage(instance_name=instance_name)
        s.metadata = m
        return s

    a = _get_storage(a, 'A')
    b = _get_storage(b, 'B')

    winning_storage = (a if conflict_resolution == 'a wins' else b)
    expected_values = dict((key, winning_storage.get_meta(key))
                           for key in keys)

    metasync(a, b, status,
             keys=keys, conflict_resolution=conflict_resolution)

    for key in keys:
        assert a.get_meta(key) == b.get_meta(key) == status.get(key, '')
        if expected_values[key]:
            assert status[key] == expected_values[key]

@@ -157,16 +157,73 @@
 etasync%0A
+from vdirsyncer.storage.base import normalize_meta_value%0A
 from vdi
@@ -2281,16 +2281,81 @@
 ) == x)%0A
+values = st.text().filter(lambda x: normalize_meta_value(x) == x)
 %0Ametadat
@@ -2378,33 +2378,20 @@
 ies(
-%0A    keys,%0A    st.text()%0A
+keys, values
 )%0A%0A%0A

Fix hypothesis metadata tests