Compare commits

...

3 commits

4 changed files with 222 additions and 1 deletions

View file

@ -201,7 +201,7 @@ def build_feed_xml(session: Session, settings: Settings) -> str:
items: list[str] = []
for article in articles:
link = article_url(settings, article)
content = f"<h1>{escape(article.title)}</h1>\n{render_article_html(article, rss_safe=True)}"
content = render_article_html(article, rss_safe=True)
frames = []
if article.video_id:
frames = session.scalars(

View file

@ -0,0 +1,130 @@
# RSS Title Deduplication Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Keep each Dzen article title in the RSS `<title>` metadata while removing the feed-generated duplicate `<h1>` from `content:encoded`.
**Architecture:** Keep the existing `render_article_html(..., rss_safe=True)` pipeline as the sole producer of the RSS article body. Add one focused integration-style unit test using an in-memory SQLite database to exercise the real feed query and inspect the generated XML.
**Tech Stack:** Python 3, SQLAlchemy, `unittest`, `xml.etree.ElementTree`
---
### Task 1: Remove the generated RSS body heading
**Files:**
- Create: `tests/test_rss.py`
- Modify: `app/rss.py:204`
- [ ] **Step 1: Write the failing regression test**
Create `tests/test_rss.py`:
```python
import unittest
from datetime import datetime, timedelta, timezone
from xml.etree import ElementTree
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from app.config import Settings
from app.database import Base
from app.models import Article
from app.rss import build_feed_xml
class RssFeedTest(unittest.TestCase):
def test_article_title_is_metadata_only_not_generated_body_heading(self):
engine = create_engine("sqlite:///:memory:")
Base.metadata.create_all(engine)
article = Article(
slug="test-article",
title="Тестовый заголовок",
lead="<p>Тестовый лид.</p>",
body_html="<p>Основной текст.</p>",
cta_html="",
feature_image_url="",
status="published",
rss_guid="test-guid",
published_at=datetime.now(timezone.utc) - timedelta(minutes=1),
)
with Session(engine) as session:
session.add(article)
session.commit()
xml = build_feed_xml(session, Settings(public_base_url="https://example.test"))
root = ElementTree.fromstring(xml)
item = root.find("./channel/item")
self.assertIsNotNone(item)
self.assertEqual(item.findtext("title"), "Тестовый заголовок")
content = item.findtext("{http://purl.org/rss/1.0/modules/content/}encoded") or ""
self.assertNotIn("<h1>Тестовый заголовок</h1>", content)
self.assertIn("<p>Тестовый лид.</p>", content)
self.assertIn("<p>Основной текст.</p>", content)
if __name__ == "__main__":
unittest.main()
```
- [ ] **Step 2: Run the focused test and verify the current behavior fails**
Run:
```bash
docker run --rm \
-e PYTHONPYCACHEPREFIX=/tmp/pycache \
-v "$PWD:/workspace:ro" \
-w /workspace \
dzen-assistant-test:local \
python -m unittest tests.test_rss -v
```
Expected: one failure because `content:encoded` contains `<h1>Тестовый заголовок</h1>`.
- [ ] **Step 3: Use the existing renderer as the complete RSS body**
In `app/rss.py`, replace the body construction inside `build_feed_xml` with:
```python
content = render_article_html(article, rss_safe=True)
```
Do not change the item `<title>`, cover caption, description, or article page renderer.
- [ ] **Step 4: Run the focused test and verify it passes**
Run the command from Step 2.
Expected: `Ran 1 test` followed by `OK`.
- [ ] **Step 5: Run the backend regression suite and compilation**
Run:
```bash
docker run --rm \
-e PYTHONPYCACHEPREFIX=/tmp/pycache \
-v "$PWD:/workspace:ro" \
-w /workspace \
dzen-assistant-test:local \
python -m unittest discover -s tests -v
docker run --rm \
-e PYTHONPYCACHEPREFIX=/tmp/pycache \
-v "$PWD:/workspace:ro" \
-w /workspace \
dzen-assistant-test:local \
python -m compileall -q app tests
```
Expected: all tests pass and compilation exits with status 0.
- [ ] **Step 6: Commit the implementation**
```bash
git add app/rss.py tests/test_rss.py
git commit -m "fix: avoid duplicate article titles in RSS bodies"
```

View file

@ -0,0 +1,45 @@
# RSS Title Deduplication Design
## Goal
Prevent Dzen articles imported from the RSS feed from displaying the article
title twice at the beginning of the publication.
## Current Behavior
Each RSS item already exposes the article title in the standard `<title>`
element. The feed generator additionally prepends the same value as an `<h1>`
inside `content:encoded`, causing Dzen to render a duplicate title.
The existing article renderer already removes a leading duplicate title from
saved article HTML. The duplicate is reintroduced afterward by the RSS feed
builder.
## Design
Build `content:encoded` directly from the RSS-safe article renderer without
prepending an `<h1>`. Keep the standard RSS `<title>` element unchanged.
The cover image caption remains unchanged. It may contain the article title,
but it is an image caption rather than the leading article heading and is
explicitly outside this change.
## Scope
- Change only the RSS item body construction.
- Preserve article pages rendered at `/posts/{slug}`.
- Preserve RSS metadata, including `<title>`, description, categories, media,
enclosure, and publication dates.
- Preserve cover image `alt` text and `<figcaption>`.
- Do not rewrite or strip other headings authored inside the article body.
## Verification
Add a regression test that builds an RSS item and verifies:
- the article title is present in the item `<title>`;
- `content:encoded` does not begin with the generated article-title `<h1>`;
- the lead and article body remain present in `content:encoded`.
Run the focused RSS test, the existing backend test suite, and Python bytecode
compilation.

View file

@ -0,0 +1,46 @@
import unittest
from datetime import datetime, timedelta, timezone
from xml.etree import ElementTree
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from app.config import Settings
from app.database import Base
from app.models import Article
from app.rss import build_feed_xml
class RssFeedTest(unittest.TestCase):
def test_article_title_is_metadata_only_not_generated_body_heading(self):
engine = create_engine("sqlite:///:memory:")
Base.metadata.create_all(engine)
article = Article(
slug="test-article",
title="Тестовый заголовок",
lead="<p>Тестовый лид.</p>",
body_html="<p>Основной текст.</p>",
cta_html="",
feature_image_url="",
status="published",
rss_guid="test-guid",
published_at=datetime.now(timezone.utc) - timedelta(minutes=1),
)
with Session(engine) as session:
session.add(article)
session.commit()
xml = build_feed_xml(session, Settings(public_base_url="https://example.test"))
root = ElementTree.fromstring(xml)
item = root.find("./channel/item")
self.assertIsNotNone(item)
self.assertEqual(item.findtext("title"), "Тестовый заголовок")
content = item.findtext("{http://purl.org/rss/1.0/modules/content/}encoded") or ""
self.assertNotIn("<h1>Тестовый заголовок</h1>", content)
self.assertIn("<p>Тестовый лид.</p>", content)
self.assertIn("<p>Основной текст.</p>", content)
if __name__ == "__main__":
unittest.main()