""" Microformats2 fixes (v2.8.0) Pages are rendered and parsed with mf2py; assertions are on the parsed data, which is what IndieWeb readers and Webmention receivers see. """ import re import sqlite3 from unittest.mock import patch import mf2py import pytest from starpunk import mf2_utils from starpunk.models import split_title from starpunk.webmention_processor import extract_author, extract_source_details from starpunk.webmention_worker import WebmentionWorker BASE = 'http://localhost:5000' def parse(client, path): response = client.get(path) assert response.status_code == 200 return mf2py.parse(doc=response.data.decode(), url=BASE + path) def entry_of(parsed): return next(i for i in parsed['items'] if 'h-entry' in i['type']) def make_note(app, content, **kw): from starpunk.notes import create_note with app.test_request_context(): return create_note(content, published=True, **kw) def add_mention(app, note, kind, n, photo=True, **cols): db = sqlite3.connect(app.config['DATABASE_PATH']) fields = { 'source_url': f'https://src.example/{kind}/{n}', 'target_url': f'{BASE}/note/{note.slug}', 'target_note_id': note.id, 'mention_type': kind, 'author_name': f'{kind.title()} Person {n}', 'author_url': f'https://{kind}{n}.example/', 'author_photo': f'https://{kind}{n}.example/me.jpg' if photo else None, 'content_snippet': f'
{kind} text link
', 'validation_status': 'valid', 'moderation_status': 'approved', **cols, } db.execute(f"INSERT INTO webmentions ({', '.join(fields)}) VALUES ({', '.join('?' * len(fields))})", tuple(fields.values())) db.commit() db.close() # A1/A2: received responses are properties of the entry def test_responses_are_entry_properties(app, client): note = make_note(app, "A note") add_mention(app, note, 'reply', 1, source_post_url='https://src.example/reply/1#canonical', source_published='2026-11-01T10:00:00Z') for n in range(10): add_mention(app, note, 'like', n, photo=n % 2 == 0) add_mention(app, note, 'repost', 1) add_mention(app, note, 'bookmark', 1) add_mention(app, note, 'mention', 1) props = entry_of(parse(client, f'/note/{note.slug}'))['properties'] assert 'children' not in entry_of(parse(client, f'/note/{note.slug}')) (comment,) = props['comment'] assert comment['type'] == ['h-cite'] author = comment['properties']['author'][0] assert author['type'] == ['h-card'] assert author['properties']['name'] == ['Reply Person 1'] assert author['properties']['url'] == ['https://reply1.example/'] assert author['properties']['photo'][0] in ('https://reply1.example/me.jpg', {'value': 'https://reply1.example/me.jpg', 'alt': ''}) assert 'x' '') assert mf2_utils.first(mf2_utils.representative_hcard(_cards(other + relme), 'https://me.example/'), 'name') == 'Me' # only one h-card, url = page single = '' assert mf2_utils.first(mf2_utils.representative_hcard(_cards(single), 'https://me.example/'), 'name') == 'Me' # B4: the responding entry and its author FEED_PAGE = ''' ''' def test_responding_entry_and_feed_author(): parsed = mf2py.parse(doc=FEED_PAGE, url='https://feed.example/') entry = mf2_utils.responding_entry(parsed, 'https://me.example/note/a#frag') assert mf2_utils.first(entry, 'url') == 'https://feed.example/2' assert mf2_utils.response_type(entry, 'https://me.example/note/a') == 'reply' author = extract_author(parsed, 'https://feed.example/', entry) assert author['author_name'] == 'Feed Owner' assert author['author_url'] == 'https://feed.example/' details = extract_source_details(entry) assert details['source_post_url'] == 'https://feed.example/2' assert details['source_published'] == '2026-11-01T16:00:00Z' def test_author_as_url_or_name(): url_author = '' parsed = mf2py.parse(doc=url_author, url='https://s.example/') assert extract_author(parsed, 'https://s.example/')['author_url'] == 'https://al.example/' name_author = '