# retoor <retoor@molodetz.nl>

from devplacepy.services.devii.store import ConversationStore
from devplacepy.utils import generate_uid


def _msgs(text):
    return [{"role": "user", "content": text}]


def test_channels_isolate_one_owner(local_db):
    store = ConversationStore()
    owner = f"chan-{generate_uid()[-12:]}"
    store.save("user", owner, _msgs("main thread"), "main")
    store.save("user", owner, _msgs("docs thread"), "docs")
    assert store.load("user", owner, "main") == _msgs("main thread")
    assert store.load("user", owner, "docs") == _msgs("docs thread")


def test_clear_one_channel_keeps_other(local_db):
    store = ConversationStore()
    owner = f"chan-{generate_uid()[-12:]}"
    store.save("user", owner, _msgs("main"), "main")
    store.save("user", owner, _msgs("docs"), "docs")
    store.clear("user", owner, "docs")
    assert store.load("user", owner, "docs") is None
    assert store.load("user", owner, "main") == _msgs("main")


def test_save_updates_in_place_per_channel(local_db):
    store = ConversationStore()
    owner = f"chan-{generate_uid()[-12:]}"
    store.save("user", owner, _msgs("first"), "docs")
    store.save("user", owner, _msgs("second"), "docs")
    assert store.load("user", owner, "docs") == _msgs("second")


def test_default_channel_is_main(local_db):
    store = ConversationStore()
    owner = f"chan-{generate_uid()[-12:]}"
    store.save("user", owner, _msgs("default"))
    assert store.load("user", owner) == _msgs("default")
    assert store.load("user", owner, "main") == _msgs("default")
    assert store.load("user", owner, "docs") is None
