diff options
Diffstat (limited to 'tests/tasks/test_scanner.py')
| -rw-r--r-- | tests/tasks/test_scanner.py | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/tests/tasks/test_scanner.py b/tests/tasks/test_scanner.py index 8dee47d..4e6c818 100644 --- a/tests/tasks/test_scanner.py +++ b/tests/tasks/test_scanner.py @@ -735,6 +735,155 @@ def test_scan_creates_source_and_webmentions( assert set(queued) == identifiers +def test_duplicate_canonical_url_skips_alias_and_commits_other_sources( + app: Flask, + scanner_module: ModuleType, + tmp_path: Path, + monkeypatch: MonkeyPatch, + caplog: pytest.LogCaptureFixture, +) -> None: + configure_scanner(app, tmp_path) + + for slug in ("first", "second"): + write_post( + tmp_path, + slug, + f'<a href="https://example.com/{slug}">Link</a>', + canonical=SOURCE_URL, + entry_url=SOURCE_URL, + ) + write_post( + tmp_path, "unrelated", '<a href="https://example.com/unrelated">Link</a>' + ) + + queued: list[UUID] = [] + monkeypatch.setattr(scanner_module, "send_webmention", queued.append) + + run_scan(app, scanner_module) + run_scan(app, scanner_module) + + with app.app_context(): + sources = list(db.session.scalars(sa.select(Source))) + webmentions = list(db.session.scalars(sa.select(SentWebmention))) + + assert len(sources) == 2 + assert {source.url for source in sources} == { + SOURCE_URL, + f"{BASE_URL}unrelated/", + } + duplicate_paths = {source.path for source in sources} & { + "first/index.html", + "second/index.html", + } + assert len(duplicate_paths) == 1 + assert all(source.revision == 1 for source in sources) + assert len(webmentions) == 2 + assert {webmention.target for webmention in webmentions} in ( + {"https://example.com/first", "https://example.com/unrelated"}, + {"https://example.com/second", "https://example.com/unrelated"}, + ) + assert set(queued) == {webmention.uuid for webmention in webmentions} + + assert "duplicate canonical URL" in caplog.text + assert SOURCE_URL in caplog.text + + +def test_existing_source_owns_canonical_url_when_alias_is_added( + app: Flask, + scanner_module: ModuleType, + tmp_path: Path, + monkeypatch: MonkeyPatch, + caplog: pytest.LogCaptureFixture, +) -> None: + configure_scanner(app, tmp_path) + write_post( + tmp_path, + "owner", + "<p>Original content.</p>", + canonical=SOURCE_URL, + entry_url=SOURCE_URL, + ) + monkeypatch.setattr(scanner_module, "send_webmention", lambda identifier: None) + + run_scan(app, scanner_module) + + # The later-added alias must not take the persisted owner's URL, regardless + # of filesystem traversal order. + write_post( + tmp_path, + "alias", + '<a href="https://example.com/alias">Link</a>', + canonical=SOURCE_URL, + entry_url=SOURCE_URL, + ) + write_post( + tmp_path, + "owner", + '<a href="https://example.com/owner">Link</a>', + canonical=SOURCE_URL, + entry_url=SOURCE_URL, + ) + + run_scan(app, scanner_module) + + with app.app_context(): + sources = list(db.session.scalars(sa.select(Source))) + targets = list(db.session.scalars(sa.select(SentWebmention.target))) + + assert len(sources) == 1 + assert sources[0].path == "owner/index.html" + assert sources[0].url == SOURCE_URL + assert sources[0].revision == 2 + assert targets == ["https://example.com/owner"] + + assert "duplicate canonical URL" in caplog.text + assert "alias/index.html" in caplog.text + assert "owner/index.html" in caplog.text + + +def test_deleted_source_still_reserves_its_canonical_url( + app: Flask, + scanner_module: ModuleType, + tmp_path: Path, + monkeypatch: MonkeyPatch, + caplog: pytest.LogCaptureFixture, +) -> None: + configure_scanner(app, tmp_path) + path = write_post( + tmp_path, + "owner", + "<p>Original content.</p>", + canonical=SOURCE_URL, + entry_url=SOURCE_URL, + ) + monkeypatch.setattr(scanner_module, "send_webmention", lambda identifier: None) + + run_scan(app, scanner_module) + path.unlink() + run_scan(app, scanner_module) + + write_post( + tmp_path, + "alias", + "<p>Alias content.</p>", + canonical=SOURCE_URL, + entry_url=SOURCE_URL, + ) + write_post(tmp_path, "unrelated", "<p>Another source.</p>") + run_scan(app, scanner_module) + + with app.app_context(): + sources = { + source.path: source for source in db.session.scalars(sa.select(Source)) + } + assert set(sources) == {"owner/index.html", "unrelated/index.html"} + assert sources["owner/index.html"].deleted_at is not None + assert sources["unrelated/index.html"].deleted_at is None + + assert "duplicate canonical URL" in caplog.text + assert SOURCE_URL in caplog.text + + def test_unchanged_source_does_not_increment_revision( app: Flask, scanner_module: ModuleType, tmp_path: Path, monkeypatch: MonkeyPatch ) -> None: |
