You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.5 KiB
85 lines
2.5 KiB
import json |
|
import tempfile |
|
import time |
|
from pathlib import Path |
|
|
|
import bech32 |
|
|
|
from imwald.core.database import Database |
|
from imwald.core.md_render import markdown_html_fragment |
|
from imwald.core.nip19 import decode_nip19_entity, encode_npub |
|
|
|
|
|
def _note_bech32(event_id_hex: str) -> str: |
|
raw = bytes.fromhex(event_id_hex) |
|
conv = bech32.convertbits(list(raw), 8, 5, True) |
|
assert conv is not None |
|
return bech32.bech32_encode("note", conv) |
|
|
|
|
|
def test_decode_nip19_npub_roundtrip() -> None: |
|
pk = "11" * 32 |
|
n = encode_npub(pk) |
|
dec = decode_nip19_entity(n) |
|
assert dec == {"hrp": "npub", "pubkey": pk} |
|
|
|
|
|
def test_decode_nip19_note_roundtrip() -> None: |
|
eid = "22" * 32 |
|
n = _note_bech32(eid) |
|
dec = decode_nip19_entity(n) |
|
assert dec == {"hrp": "note", "event_id": eid} |
|
|
|
|
|
def test_markdown_renders_nostr_npub_badge_with_db() -> None: |
|
pk = "33" * 32 |
|
npub = encode_npub(pk) |
|
with tempfile.TemporaryDirectory() as td: |
|
db = Database(Path(td) / "t.sqlite") |
|
db.connect() |
|
db.upsert_event( |
|
{ |
|
"id": "44" * 32, |
|
"pubkey": pk, |
|
"created_at": int(time.time()), |
|
"kind": 0, |
|
"content": json.dumps( |
|
{ |
|
"name": "River", |
|
"display_name": "River Tam", |
|
"picture": "https://example.com/avatar.png", |
|
} |
|
), |
|
"sig": "aa" * 64, |
|
"tags": [], |
|
} |
|
) |
|
html = markdown_html_fragment(f"Hi nostr:{npub} bye", db=db) |
|
assert "nostr-user-badge" in html |
|
assert "@River Tam" in html or "River Tam" in html |
|
assert "Hi" in html and "bye" in html |
|
assert "example.com" in html and "avatar.png" in html |
|
|
|
|
|
def test_markdown_renders_nostr_note_embed() -> None: |
|
pk = "66" * 32 |
|
eid = "77" * 32 |
|
note = _note_bech32(eid) |
|
with tempfile.TemporaryDirectory() as td: |
|
db = Database(Path(td) / "t.sqlite") |
|
db.connect() |
|
db.upsert_event( |
|
{ |
|
"id": eid, |
|
"pubkey": pk, |
|
"created_at": 1700000000, |
|
"kind": 1, |
|
"content": "embedded **note** body", |
|
"sig": "bb" * 64, |
|
"tags": [], |
|
} |
|
) |
|
html = markdown_html_fragment(f"Ref nostr:{note}", db=db) |
|
assert "nostr-embed" in html |
|
assert "embedded" in html |
|
assert "kind 1" in html
|
|
|