150 lines
4.5 KiB
Python
150 lines
4.5 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
from yuxi.channels.adapters.nostr.thread_simulator import NostrThreadSimulator
|
||
|
|
|
||
|
|
|
||
|
|
class TestNostrThreadSimulator:
|
||
|
|
def test_get_parent_event_id_reply_tag(self):
|
||
|
|
event = {
|
||
|
|
"tags": [
|
||
|
|
["e", "parent_event_id", "", "reply"],
|
||
|
|
]
|
||
|
|
}
|
||
|
|
assert NostrThreadSimulator._get_parent_event_id(event) == "parent_event_id"
|
||
|
|
|
||
|
|
def test_get_parent_event_id_no_reply(self):
|
||
|
|
event = {
|
||
|
|
"tags": [
|
||
|
|
["p", "pubkey_hex"],
|
||
|
|
]
|
||
|
|
}
|
||
|
|
assert NostrThreadSimulator._get_parent_event_id(event) is None
|
||
|
|
|
||
|
|
def test_get_parent_event_id_simple_e_tag(self):
|
||
|
|
event = {
|
||
|
|
"tags": [
|
||
|
|
["e", "root_event_id"],
|
||
|
|
]
|
||
|
|
}
|
||
|
|
assert NostrThreadSimulator._get_parent_event_id(event) == "root_event_id"
|
||
|
|
|
||
|
|
def test_resolve_e_tags(self):
|
||
|
|
event = {
|
||
|
|
"tags": [
|
||
|
|
["e", "e1"],
|
||
|
|
["p", "pub1"],
|
||
|
|
["e", "e2"],
|
||
|
|
]
|
||
|
|
}
|
||
|
|
assert NostrThreadSimulator.resolve_e_tags(event) == ["e1", "e2"]
|
||
|
|
|
||
|
|
def test_resolve_p_tags(self):
|
||
|
|
event = {
|
||
|
|
"tags": [
|
||
|
|
["p", "pub1"],
|
||
|
|
["p", "pub2"],
|
||
|
|
["e", "e1"],
|
||
|
|
]
|
||
|
|
}
|
||
|
|
assert NostrThreadSimulator.resolve_p_tags(event) == ["pub1", "pub2"]
|
||
|
|
|
||
|
|
def test_is_root_event(self):
|
||
|
|
event = {
|
||
|
|
"tags": [
|
||
|
|
["p", "pub1"],
|
||
|
|
]
|
||
|
|
}
|
||
|
|
assert NostrThreadSimulator._is_root_event(None, event) is True
|
||
|
|
|
||
|
|
def test_is_not_root_event(self):
|
||
|
|
event = {
|
||
|
|
"tags": [
|
||
|
|
["e", "parent_id", "", "root"],
|
||
|
|
]
|
||
|
|
}
|
||
|
|
assert NostrThreadSimulator._is_root_event(None, event) is False
|
||
|
|
|
||
|
|
def test_event_cache(self):
|
||
|
|
sim = NostrThreadSimulator()
|
||
|
|
event = {"id": "e1", "content": "test"}
|
||
|
|
sim._event_cache["e1"] = event
|
||
|
|
assert sim._event_cache["e1"] == event
|
||
|
|
|
||
|
|
def test_max_depth_configured(self):
|
||
|
|
sim = NostrThreadSimulator(max_depth=5)
|
||
|
|
assert sim._max_depth == 5
|
||
|
|
|
||
|
|
def test_default_max_depth(self):
|
||
|
|
sim = NostrThreadSimulator()
|
||
|
|
assert sim._max_depth == 10
|
||
|
|
|
||
|
|
|
||
|
|
class TestNostrSession:
|
||
|
|
def test_resolve_thread_key_dm(self):
|
||
|
|
from yuxi.channels.adapters.nostr.session import resolve_thread_key
|
||
|
|
from yuxi.channels.models import (
|
||
|
|
ChannelIdentity,
|
||
|
|
ChannelMessage,
|
||
|
|
ChannelType,
|
||
|
|
)
|
||
|
|
|
||
|
|
msg = ChannelMessage(
|
||
|
|
identity=ChannelIdentity(
|
||
|
|
channel_id="nostr",
|
||
|
|
channel_type=ChannelType.NOSTR,
|
||
|
|
channel_user_id="pubkey_a",
|
||
|
|
channel_chat_id="dm:pubkey_b",
|
||
|
|
),
|
||
|
|
content="hello",
|
||
|
|
)
|
||
|
|
key = resolve_thread_key(msg)
|
||
|
|
assert key == "nostr:dm:pubkey_b"
|
||
|
|
|
||
|
|
def test_resolve_thread_key_channel(self):
|
||
|
|
from yuxi.channels.adapters.nostr.session import resolve_thread_key
|
||
|
|
from yuxi.channels.models import (
|
||
|
|
ChannelIdentity,
|
||
|
|
ChannelMessage,
|
||
|
|
ChannelType,
|
||
|
|
)
|
||
|
|
|
||
|
|
msg = ChannelMessage(
|
||
|
|
identity=ChannelIdentity(
|
||
|
|
channel_id="nostr",
|
||
|
|
channel_type=ChannelType.NOSTR,
|
||
|
|
channel_user_id="pubkey_a",
|
||
|
|
channel_chat_id="channel:event_id_123",
|
||
|
|
),
|
||
|
|
content="hello",
|
||
|
|
)
|
||
|
|
key = resolve_thread_key(msg)
|
||
|
|
assert key == "nostr:channel:event_id_123"
|
||
|
|
|
||
|
|
def test_resolve_agent_route(self):
|
||
|
|
from yuxi.channels.adapters.nostr.session import resolve_agent_route
|
||
|
|
from yuxi.channels.models import (
|
||
|
|
ChannelIdentity,
|
||
|
|
ChannelMessage,
|
||
|
|
ChannelType,
|
||
|
|
)
|
||
|
|
|
||
|
|
msg = ChannelMessage(
|
||
|
|
identity=ChannelIdentity(
|
||
|
|
channel_id="nostr",
|
||
|
|
channel_type=ChannelType.NOSTR,
|
||
|
|
channel_user_id="pubkey_a",
|
||
|
|
channel_chat_id="dm:pubkey_b",
|
||
|
|
),
|
||
|
|
content="hello",
|
||
|
|
)
|
||
|
|
route = resolve_agent_route(msg, default_agent_id="agent_1")
|
||
|
|
assert route.startswith("agent:agent_1:")
|
||
|
|
assert "nostr:dm:pubkey_b" in route
|
||
|
|
|
||
|
|
def test_normalize_target(self):
|
||
|
|
from yuxi.channels.adapters.nostr.session import normalize_target
|
||
|
|
|
||
|
|
assert normalize_target("dm:pubkey_b") == "nostr:dm:pubkey_b"
|
||
|
|
assert normalize_target("nostr:dm:pubkey_b") == "nostr:dm:pubkey_b"
|