diff options
| author | Dennis Fink | 2026-10-30 19:23:17 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-30 19:23:17 +0100 |
| commit | dcbbfdb38d4e18e377d4e326d82f35341840c6cc (patch) | |
| tree | 110d3dad8fcd0c875d86fbc630d8626f1a01de7b /tests | |
| parent | 1a9e7a49240533bb7b7f7b8fd0527f12d2a029bf (diff) | |
| download | webmentions-ssg-dcbbfdb38d4e18e377d4e326d82f35341840c6cc.tar.gz webmentions-ssg-dcbbfdb38d4e18e377d4e326d82f35341840c6cc.zip | |
fix(sender): stabilize source content fingerprints
Hash normalized reader-facing content and outgoing targets instead of
raw h-entry markup so generated attributes and relative timestamps do
not trigger spurious source revisions or repeat Webmentions.
Preserve meaningful changes such as image updates and reaction target
changes while ignoring target ordering and other markup noise.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/tasks/test_scanner.py | 167 |
1 files changed, 165 insertions, 2 deletions
diff --git a/tests/tasks/test_scanner.py b/tests/tasks/test_scanner.py index 21a13c6..7aefff4 100644 --- a/tests/tasks/test_scanner.py +++ b/tests/tasks/test_scanner.py @@ -508,6 +508,119 @@ def test_scan_source_uses_canonical_without_base_url( assert len(scanned.content_hash) == 32 +def test_scan_source_ignores_markup_noise_in_content( + scanner_module: ModuleType, tmp_path: Path +) -> None: + path = write_post( + tmp_path, + "example", + "<p>Hello <strong>world</strong>!</p>" + '<a href="https://example.com/target">Target</a>', + ) + first = scanner_module.scan_source_file(path, root=tmp_path, base_url=None) + + write_post( + tmp_path, + "example", + '<p class="generated" data-build="123"> Hello world! </p>' + '<a class="external" href="https://example.com/target"> Target </a>', + ) + second = scanner_module.scan_source_file(path, root=tmp_path, base_url=None) + + assert first.targets == second.targets + assert first.content_hash == second.content_hash + + +def test_scan_source_uses_stable_time_value_inside_content( + scanner_module: ModuleType, tmp_path: Path +) -> None: + path = write_post( + tmp_path, + "example", + '<time datetime="2026-10-30T12:00:00Z">5 minutes ago</time>', + ) + first = scanner_module.scan_source_file(path, root=tmp_path, base_url=None) + + path.write_text( + path.read_text(encoding="utf-8").replace("5 minutes ago", "6 minutes ago"), + encoding="utf-8", + ) + second = scanner_module.scan_source_file(path, root=tmp_path, base_url=None) + + assert first.content_hash == second.content_hash + + +def test_scan_source_hash_changes_when_image_changes( + scanner_module: ModuleType, tmp_path: Path +) -> None: + path = write_post(tmp_path, "example", '<img src="/images/a.jpg" alt="A photo">') + first = scanner_module.scan_source_file(path, root=tmp_path, base_url=None) + + path.write_text( + path.read_text(encoding="utf-8").replace("/images/a.jpg", "/images/b.jpg"), + encoding="utf-8", + ) + second = scanner_module.scan_source_file(path, root=tmp_path, base_url=None) + + assert first.content_hash != second.content_hash + + +def test_scan_source_hash_includes_reaction_targets_outside_content( + scanner_module: ModuleType, tmp_path: Path +) -> None: + path = write_post(tmp_path, "example", "<p>Same content.</p>") + html = path.read_text(encoding="utf-8") + path.write_text( + html.replace( + '<div class="e-content">', + '<a class="u-like-of" href="https://example.com/first">Like</a>' + '<div class="e-content">', + ), + encoding="utf-8", + ) + first = scanner_module.scan_source_file(path, root=tmp_path, base_url=None) + + path.write_text( + path.read_text(encoding="utf-8").replace( + "https://example.com/first", "https://example.com/second" + ), + encoding="utf-8", + ) + second = scanner_module.scan_source_file(path, root=tmp_path, base_url=None) + + assert first.targets == frozenset({"https://example.com/first"}) + assert second.targets == frozenset({"https://example.com/second"}) + assert first.content_hash != second.content_hash + + +def test_scan_source_hash_ignores_reaction_target_order( + scanner_module: ModuleType, tmp_path: Path +) -> None: + path = write_post(tmp_path, "example", "<p>Same content.</p>") + html = path.read_text(encoding="utf-8") + first_link = '<a class="u-like-of" href="https://example.com/a">A</a>' + second_link = '<a class="u-like-of" href="https://example.com/b">B</a>' + path.write_text( + html.replace( + '<div class="e-content">', + first_link + second_link + '<div class="e-content">', + ), + encoding="utf-8", + ) + first = scanner_module.scan_source_file(path, root=tmp_path, base_url=None) + + path.write_text( + path.read_text(encoding="utf-8").replace( + first_link + second_link, second_link + first_link + ), + encoding="utf-8", + ) + second = scanner_module.scan_source_file(path, root=tmp_path, base_url=None) + + assert first.targets == second.targets + assert first.content_hash == second.content_hash + + def test_scan_source_falls_back_to_base_url( scanner_module: ModuleType, tmp_path: Path ) -> None: @@ -976,6 +1089,56 @@ def test_unchanged_source_does_not_increment_revision( assert queued == [] +def test_changing_relative_timestamp_does_not_resend( + app: Flask, scanner_module: ModuleType, tmp_path: Path, monkeypatch: MonkeyPatch +) -> None: + configure_scanner(app, tmp_path) + path = write_post( + tmp_path, "example", '<a href="https://example.com/target">Target</a>' + ) + html = path.read_text(encoding="utf-8") + path.write_text( + html.replace( + '<div class="e-content">', + '<time class="dt-published" datetime="2026-10-30">5 minutes ago</time>' + '<div class="e-content">', + ), + encoding="utf-8", + ) + + queued: list[UUID] = [] + monkeypatch.setattr(scanner_module, "send_webmention", queued.append) + run_scan(app, scanner_module) + + with app.app_context(): + source = db.session.scalar(sa.select(Source)) + webmention = db.session.scalar(sa.select(SentWebmention)) + assert source is not None and webmention is not None + original_hash = source.content_hash + webmention.processed_revision = 1 + webmention.sent_revision = 1 + webmention.status = SentWebmentionStatus.SENT + db.session.commit() + + queued.clear() + path.write_text( + path.read_text(encoding="utf-8").replace("5 minutes ago", "6 minutes ago"), + encoding="utf-8", + ) + run_scan(app, scanner_module) + + with app.app_context(): + source = db.session.scalar(sa.select(Source)) + webmention = db.session.scalar(sa.select(SentWebmention)) + assert source is not None and webmention is not None + assert source.content_hash == original_hash + assert source.revision == 1 + assert webmention.desired_revision == 1 + assert webmention.processed_revision == 1 + + assert queued == [] + + def test_updated_source_increments_revision( app: Flask, scanner_module: ModuleType, tmp_path: Path, monkeypatch: MonkeyPatch ) -> None: @@ -1597,7 +1760,6 @@ def test_pending_webmention_is_requeued_on_next_scan( assert queued == [identifier] - def test_temporary_failure_waits_until_retry_deadline_and_revision_resets_budget( app: Flask, scanner_module: ModuleType, tmp_path: Path, monkeypatch: MonkeyPatch ) -> None: @@ -1631,7 +1793,7 @@ def test_temporary_failure_waits_until_retry_deadline_and_revision_resets_budget queued.clear() # Changing the h-entry creates a new desired revision with a fresh budget. - path.write_text(path.read_text().replace('>A</a>', '>A updated</a>')) + path.write_text(path.read_text().replace(">A</a>", ">A updated</a>")) with app.app_context(): webmention = db.session.get(SentWebmention, identifier) assert webmention is not None @@ -1647,6 +1809,7 @@ def test_temporary_failure_waits_until_retry_deadline_and_revision_resets_budget assert webmention.attempt_count == 0 assert webmention.next_retry_at is None + def test_invalid_known_source_is_not_deleted( app: Flask, scanner_module: ModuleType, |
