refactor: make code more readable
This commit is contained in:
parent
3d1511a545
commit
c4b498c28c
2 changed files with 70 additions and 72 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
from json import dumps
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi import WebSocket
|
from fastapi import WebSocket
|
||||||
|
|
@ -8,9 +8,13 @@ from lnbits.extensions.nostrrelay.client_manager import (
|
||||||
NostrClientConnection,
|
NostrClientConnection,
|
||||||
NostrClientManager,
|
NostrClientManager,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .helpers import get_fixtures
|
from .helpers import get_fixtures
|
||||||
|
|
||||||
fixtures = get_fixtures("clients")
|
fixtures = get_fixtures("clients")
|
||||||
|
alice = fixtures["alice"]
|
||||||
|
bob = fixtures["bob"]
|
||||||
|
|
||||||
|
|
||||||
class MockWebSocket(WebSocket):
|
class MockWebSocket(WebSocket):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
@ -28,13 +32,26 @@ class MockWebSocket(WebSocket):
|
||||||
async def send_text(self, data: str):
|
async def send_text(self, data: str):
|
||||||
self.sent_messages.append(data)
|
self.sent_messages.append(data)
|
||||||
|
|
||||||
async def wire_mock_message(self, data: str):
|
async def wire_mock_data(self, data: dict):
|
||||||
await self.fake_wire.put(data)
|
await self.fake_wire.put(dumps(data))
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_alice_and_bob():
|
async def test_alice_and_bob():
|
||||||
|
ws_alice, ws_bob = init_clients()
|
||||||
|
|
||||||
|
await alice_wire_meta_and_post01(ws_alice)
|
||||||
|
|
||||||
|
await bob_wire_meta_and_folow_alice(ws_bob)
|
||||||
|
|
||||||
|
await alice_wire_post02_and_bob_is_notified(ws_alice, ws_bob)
|
||||||
|
|
||||||
|
await bob_likes_posts_alice_subscribes_and_receives_notifications(ws_alice, ws_bob)
|
||||||
|
|
||||||
|
await bob_likes_and_comments___alice_receives_notifications(ws_alice, ws_bob)
|
||||||
|
|
||||||
|
|
||||||
|
def init_clients():
|
||||||
client_manager = NostrClientManager()
|
client_manager = NostrClientManager()
|
||||||
|
|
||||||
ws_alice = MockWebSocket()
|
ws_alice = MockWebSocket()
|
||||||
|
|
@ -46,157 +63,138 @@ async def test_alice_and_bob():
|
||||||
client_bob = NostrClientConnection(websocket=ws_bob)
|
client_bob = NostrClientConnection(websocket=ws_bob)
|
||||||
client_manager.add_client(client_bob)
|
client_manager.add_client(client_bob)
|
||||||
asyncio.create_task(client_bob.start())
|
asyncio.create_task(client_bob.start())
|
||||||
|
return ws_alice, ws_bob
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
|
|
||||||
await alice_wire_meta_and_post01(ws_alice, fixtures)
|
|
||||||
|
|
||||||
await bob_wire_meta_and_folow_alice(ws_bob, fixtures)
|
|
||||||
|
|
||||||
await alice_wire_post02_and_bob_is_notified(ws_alice, ws_bob, fixtures)
|
|
||||||
|
|
||||||
await bob_likes_posts_alice_subscribes_and_receives_notifications(
|
|
||||||
ws_alice, ws_bob, fixtures
|
|
||||||
)
|
|
||||||
|
|
||||||
await bob_likes_and_comments___alice_receives_notifications(
|
|
||||||
ws_alice, ws_bob, fixtures
|
|
||||||
)
|
|
||||||
|
|
||||||
print("### ws_alice.sent_messages", ws_alice.sent_messages)
|
|
||||||
print("### ws_bob.sent_messages", ws_bob.sent_messages)
|
|
||||||
|
|
||||||
|
|
||||||
async def alice_wire_meta_and_post01(ws_alice: MockWebSocket, fixtures):
|
async def alice_wire_meta_and_post01(ws_alice: MockWebSocket):
|
||||||
ws_alice.sent_messages.clear()
|
ws_alice.sent_messages.clear()
|
||||||
|
|
||||||
await ws_alice.wire_mock_message(json.dumps(fixtures["alice"]["meta"]))
|
await ws_alice.wire_mock_data(alice["meta"])
|
||||||
await ws_alice.wire_mock_message(json.dumps(fixtures["alice"]["post01"]))
|
await ws_alice.wire_mock_data(alice["post01"])
|
||||||
await ws_alice.wire_mock_message(json.dumps(fixtures["alice"]["post01"]))
|
await ws_alice.wire_mock_data(alice["post01"])
|
||||||
await asyncio.sleep(0.5)
|
await asyncio.sleep(0.5)
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
len(ws_alice.sent_messages) == 3
|
len(ws_alice.sent_messages) == 3
|
||||||
), "Alice: Expected 3 confirmations to be sent"
|
), "Alice: Expected 3 confirmations to be sent"
|
||||||
assert ws_alice.sent_messages[0] == json.dumps(
|
assert ws_alice.sent_messages[0] == dumps(
|
||||||
fixtures["alice"]["meta_response"]
|
alice["meta_response"]
|
||||||
), "Alice: Wrong confirmation for meta"
|
), "Alice: Wrong confirmation for meta"
|
||||||
assert ws_alice.sent_messages[1] == json.dumps(
|
assert ws_alice.sent_messages[1] == dumps(
|
||||||
fixtures["alice"]["post01_response_ok"]
|
alice["post01_response_ok"]
|
||||||
), "Alice: Wrong confirmation for post01"
|
), "Alice: Wrong confirmation for post01"
|
||||||
assert ws_alice.sent_messages[2] == json.dumps(
|
assert ws_alice.sent_messages[2] == dumps(
|
||||||
fixtures["alice"]["post01_response_duplicate"]
|
alice["post01_response_duplicate"]
|
||||||
), "Alice: Expected failure for double posting"
|
), "Alice: Expected failure for double posting"
|
||||||
|
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
|
|
||||||
async def bob_wire_meta_and_folow_alice(ws_bob: MockWebSocket, fixtures):
|
|
||||||
|
async def bob_wire_meta_and_folow_alice(ws_bob: MockWebSocket):
|
||||||
ws_bob.sent_messages.clear()
|
ws_bob.sent_messages.clear()
|
||||||
|
|
||||||
await ws_bob.wire_mock_message(json.dumps(fixtures["bob"]["meta"]))
|
await ws_bob.wire_mock_data(bob["meta"])
|
||||||
await ws_bob.wire_mock_message(json.dumps(fixtures["bob"]["request_meta_alice"]))
|
await ws_bob.wire_mock_data(bob["request_meta_alice"])
|
||||||
await ws_bob.wire_mock_message(json.dumps(fixtures["bob"]["request_posts_alice"]))
|
await ws_bob.wire_mock_data(bob["request_posts_alice"])
|
||||||
|
|
||||||
await asyncio.sleep(0.5)
|
await asyncio.sleep(0.5)
|
||||||
|
|
||||||
assert len(ws_bob.sent_messages) == 5, "Bob: Expected 5 confirmations to be sent"
|
assert len(ws_bob.sent_messages) == 5, "Bob: Expected 5 confirmations to be sent"
|
||||||
assert ws_bob.sent_messages[0] == json.dumps(
|
assert ws_bob.sent_messages[0] == dumps(
|
||||||
fixtures["bob"]["meta_response"]
|
bob["meta_response"]
|
||||||
), "Bob: Wrong confirmation for meta"
|
), "Bob: Wrong confirmation for meta"
|
||||||
assert ws_bob.sent_messages[1] == json.dumps(
|
assert ws_bob.sent_messages[1] == dumps(
|
||||||
["EVENT", "profile", fixtures["alice"]["meta"][1]]
|
["EVENT", "profile", alice["meta"][1]]
|
||||||
), "Bob: Wrong response for Alice's meta"
|
), "Bob: Wrong response for Alice's meta"
|
||||||
assert ws_bob.sent_messages[2] == json.dumps(
|
assert ws_bob.sent_messages[2] == dumps(
|
||||||
["EOSE", "profile"]
|
["EOSE", "profile"]
|
||||||
), "Bob: Wrong End Of Streaming Event for profile"
|
), "Bob: Wrong End Of Streaming Event for profile"
|
||||||
assert ws_bob.sent_messages[3] == json.dumps(
|
assert ws_bob.sent_messages[3] == dumps(
|
||||||
["EVENT", "sub0", fixtures["alice"]["post01"][1]]
|
["EVENT", "sub0", alice["post01"][1]]
|
||||||
), "Bob: Wrong posts for Alice"
|
), "Bob: Wrong posts for Alice"
|
||||||
assert ws_bob.sent_messages[4] == json.dumps(
|
assert ws_bob.sent_messages[4] == dumps(
|
||||||
["EOSE", "sub0"]
|
["EOSE", "sub0"]
|
||||||
), "Bob: Wrong End Of Streaming Event for sub0"
|
), "Bob: Wrong End Of Streaming Event for sub0"
|
||||||
|
|
||||||
|
|
||||||
async def alice_wire_post02_and_bob_is_notified(
|
async def alice_wire_post02_and_bob_is_notified(
|
||||||
ws_alice: MockWebSocket, ws_bob: MockWebSocket, fixtures
|
ws_alice: MockWebSocket, ws_bob: MockWebSocket
|
||||||
):
|
):
|
||||||
ws_bob.sent_messages.clear()
|
ws_bob.sent_messages.clear()
|
||||||
ws_alice.sent_messages.clear()
|
ws_alice.sent_messages.clear()
|
||||||
|
|
||||||
await ws_alice.wire_mock_message(json.dumps(fixtures["alice"]["post02"]))
|
await ws_alice.wire_mock_data(alice["post02"])
|
||||||
await asyncio.sleep(0.5)
|
await asyncio.sleep(0.5)
|
||||||
|
|
||||||
assert ws_alice.sent_messages[0] == json.dumps(
|
assert ws_alice.sent_messages[0] == dumps(
|
||||||
fixtures["alice"]["post02_response_ok"]
|
alice["post02_response_ok"]
|
||||||
), "Alice: Wrong confirmation for post02"
|
), "Alice: Wrong confirmation for post02"
|
||||||
assert ws_bob.sent_messages[0] == json.dumps(
|
assert ws_bob.sent_messages[0] == dumps(
|
||||||
["EVENT", "sub0", fixtures["alice"]["post02"][1]]
|
["EVENT", "sub0", alice["post02"][1]]
|
||||||
), "Bob: Wrong notification for post02"
|
), "Bob: Wrong notification for post02"
|
||||||
|
|
||||||
|
|
||||||
async def bob_likes_posts_alice_subscribes_and_receives_notifications(
|
async def bob_likes_posts_alice_subscribes_and_receives_notifications(
|
||||||
ws_alice: MockWebSocket, ws_bob: MockWebSocket, fixtures
|
ws_alice: MockWebSocket, ws_bob: MockWebSocket
|
||||||
):
|
):
|
||||||
ws_alice.sent_messages.clear()
|
ws_alice.sent_messages.clear()
|
||||||
ws_bob.sent_messages.clear()
|
ws_bob.sent_messages.clear()
|
||||||
|
|
||||||
await ws_bob.wire_mock_message(json.dumps(fixtures["bob"]["like_post01"]))
|
await ws_bob.wire_mock_data(bob["like_post01"])
|
||||||
await asyncio.sleep(0.1)
|
await asyncio.sleep(0.1)
|
||||||
await ws_alice.wire_mock_message(
|
await ws_alice.wire_mock_data(alice["subscribe_reactions_to_me"])
|
||||||
json.dumps(fixtures["alice"]["subscribe_reactions_to_me"])
|
|
||||||
)
|
|
||||||
await asyncio.sleep(0.1)
|
await asyncio.sleep(0.1)
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
len(ws_alice.sent_messages) == 2
|
len(ws_alice.sent_messages) == 2
|
||||||
), "Alice: Expected 2 confirmations to be sent"
|
), "Alice: Expected 2 confirmations to be sent"
|
||||||
|
|
||||||
assert ws_alice.sent_messages[0] == json.dumps(
|
assert ws_alice.sent_messages[0] == dumps(
|
||||||
[
|
[
|
||||||
"EVENT",
|
"EVENT",
|
||||||
"notifications:0b29ecc73ba400e5b4bd1e4cb0d8f524e9958345",
|
"notifications:0b29ecc73ba400e5b4bd1e4cb0d8f524e9958345",
|
||||||
fixtures["bob"]["like_post01"][1],
|
bob["like_post01"][1],
|
||||||
]
|
]
|
||||||
), "Alice: must receive 'like' notification"
|
), "Alice: must receive 'like' notification"
|
||||||
|
|
||||||
assert ws_alice.sent_messages[1] == json.dumps(
|
assert ws_alice.sent_messages[1] == dumps(
|
||||||
["EOSE", "notifications:0b29ecc73ba400e5b4bd1e4cb0d8f524e9958345"]
|
["EOSE", "notifications:0b29ecc73ba400e5b4bd1e4cb0d8f524e9958345"]
|
||||||
), "Alice: receive stored notifications done"
|
), "Alice: receive stored notifications done"
|
||||||
|
|
||||||
|
|
||||||
async def bob_likes_and_comments___alice_receives_notifications(
|
async def bob_likes_and_comments___alice_receives_notifications(
|
||||||
ws_alice: MockWebSocket, ws_bob: MockWebSocket, fixtures
|
ws_alice: MockWebSocket, ws_bob: MockWebSocket
|
||||||
):
|
):
|
||||||
ws_alice.sent_messages.clear()
|
ws_alice.sent_messages.clear()
|
||||||
ws_bob.sent_messages.clear()
|
ws_bob.sent_messages.clear()
|
||||||
|
|
||||||
await ws_bob.wire_mock_message(json.dumps(fixtures["bob"]["like_post02"]))
|
await ws_bob.wire_mock_data(bob["like_post02"])
|
||||||
await ws_bob.wire_mock_message(
|
await ws_bob.wire_mock_data(bob["comment_on_alice_post01"])
|
||||||
json.dumps(fixtures["bob"]["comment_on_alice_post01"])
|
|
||||||
)
|
|
||||||
await asyncio.sleep(0.5)
|
await asyncio.sleep(0.5)
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
len(ws_bob.sent_messages) == 2
|
len(ws_bob.sent_messages) == 2
|
||||||
), "Bob: Expected 2 confirmations to be sent (for like & comment)"
|
), "Bob: Expected 2 confirmations to be sent (for like & comment)"
|
||||||
assert ws_bob.sent_messages[0] == json.dumps(
|
assert ws_bob.sent_messages[0] == dumps(
|
||||||
fixtures["bob"]["like_post02_response"]
|
bob["like_post02_response"]
|
||||||
), "Bob: Wrong confirmation for like on post02"
|
), "Bob: Wrong confirmation for like on post02"
|
||||||
assert ws_bob.sent_messages[1] == json.dumps(
|
assert ws_bob.sent_messages[1] == dumps(
|
||||||
fixtures["bob"]["comment_on_alice_post01_response"]
|
bob["comment_on_alice_post01_response"]
|
||||||
), "Bob: Wrong confirmation for comment on post01"
|
), "Bob: Wrong confirmation for comment on post01"
|
||||||
assert (
|
assert (
|
||||||
len(ws_alice.sent_messages) == 2
|
len(ws_alice.sent_messages) == 2
|
||||||
), "Alice: Expected 2 notifications to be sent (for like & comment)"
|
), "Alice: Expected 2 notifications to be sent (for like & comment)"
|
||||||
assert ws_alice.sent_messages[0] == json.dumps(
|
assert ws_alice.sent_messages[0] == dumps(
|
||||||
[
|
[
|
||||||
"EVENT",
|
"EVENT",
|
||||||
"notifications:0b29ecc73ba400e5b4bd1e4cb0d8f524e9958345",
|
"notifications:0b29ecc73ba400e5b4bd1e4cb0d8f524e9958345",
|
||||||
fixtures["bob"]["like_post02"][1],
|
bob["like_post02"][1],
|
||||||
]
|
]
|
||||||
), "Alice: Wrong notification for like on post02"
|
), "Alice: Wrong notification for like on post02"
|
||||||
assert ws_alice.sent_messages[1] == json.dumps(
|
assert ws_alice.sent_messages[1] == dumps(
|
||||||
[
|
[
|
||||||
"EVENT",
|
"EVENT",
|
||||||
"notifications:0b29ecc73ba400e5b4bd1e4cb0d8f524e9958345",
|
"notifications:0b29ecc73ba400e5b4bd1e4cb0d8f524e9958345",
|
||||||
fixtures["bob"]["comment_on_alice_post01"][1],
|
bob["comment_on_alice_post01"][1],
|
||||||
]
|
]
|
||||||
), "Alice: Wrong notification for comment on post01"
|
), "Alice: Wrong notification for comment on post01"
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@ from loguru import logger
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from lnbits.extensions.nostrrelay.crud import create_event, get_event, get_events
|
from lnbits.extensions.nostrrelay.crud import create_event, get_event, get_events
|
||||||
|
|
||||||
from lnbits.extensions.nostrrelay.models import NostrEvent, NostrFilter
|
from lnbits.extensions.nostrrelay.models import NostrEvent, NostrFilter
|
||||||
|
|
||||||
from .helpers import get_fixtures
|
from .helpers import get_fixtures
|
||||||
|
|
||||||
RELAY_ID = "r1"
|
RELAY_ID = "r1"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue