Files
StarPunk/tests/test_migration_023.py
PhilandClaude Opus 5.5 978c13358b feat: article titles and summaries stored apart from the text (v2.16.0, ADR-076)
Articles as the IndieWeb defines them: a name separate from the content.

- Migration 023 adds notes.title and notes.summary. A one-time Python data
  step (migrations.DATA_MIGRATIONS) moves each note's '# Heading' first line
  into title, rewrites the file and hash, empties FTS for a rebuild, and puts
  files back if the migration fails. Trashed notes included.
- Micropub: name -> title, summary stored when sent with content, summary
  updatable, q=source returns both. MCP tools take a summary.
- Feeds: titles only for articles (RSS/JSON none, Atom empty); Atom and
  JSON Feed summaries; the title is no longer repeated in the content.
- Pages: p-name/p-summary on the article page. Homepage and tag pages show
  articles as title, summary and "Read more" without images (hidden u-photo
  keeps the post type for parsers).
- Crossposts: articles go out as title, summary, link and hashtags, no
  images; the Bluesky card is the article. Notes unchanged.
- Editor: Title and Summary fields; an article may have no body.
- Fix: a line starting with a hashtag ('#indieweb ...') was rendered as a
  heading and crossposted without its '#' (HashtagLineExtension).
- Fix: excerpts showed raw Markdown; they are plain text now.

Back up the database and the data folder before deploying: the migration
rewrites note files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 17:05:38 -07:00

128 lines
4.8 KiB
Python

"""
Migration 023 moves '# Heading' titles out of note files into notes.title (v2.16.0, ADR-076).
Starts from a fully migrated database, takes it back to before 023, then
runs the migrations again with the data directory.
"""
import sqlite3
import pytest
from starpunk import create_app
from starpunk.migrations import MigrationError, run_migrations
from starpunk.utils import calculate_content_hash
MIGRATION = "023_note_titles.sql"
TITLED = "# Old Title\n\nThe body."
@pytest.fixture
def setup(tmp_path):
data = tmp_path / "data"
data.mkdir()
db_path = data / "starpunk.db"
create_app(config={"TESTING": True, "DATABASE_PATH": db_path, "DATA_PATH": data,
"SESSION_SECRET": "x", "ADMIN_ME": "https://example.com",
"SITE_URL": "https://blog.example.com"})
files = {
"titled": ("notes/2026/11/titled.md", TITLED, None),
"untitled": ("notes/2026/11/untitled.md", "#indieweb is great", None),
"trashed": ("notes/2026/10/trashed.md", "# Gone\n\nBin.", "2026-11-01"),
}
conn = sqlite3.connect(db_path)
for slug, (rel, text, deleted_at) in files.items():
path = data / rel
if deleted_at: # delete_note_file put it in .trash/<year>/<month>/
path = data / ".trash" / "2026" / "10" / "trashed.md"
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text)
conn.execute(
"INSERT INTO notes (slug, file_path, published, created_at, updated_at, deleted_at,"
" content_hash) VALUES (?, ?, 1, '2026-11-01', '2026-11-01', ?, ?)",
(slug, rel, deleted_at, calculate_content_hash(text)),
)
conn.execute("INSERT INTO notes_fts (rowid, slug, title, content) VALUES (1, 'titled', 'x', 'y')")
# Back to before migration 023
conn.execute("ALTER TABLE notes DROP COLUMN title")
conn.execute("ALTER TABLE notes DROP COLUMN summary")
conn.execute("DELETE FROM schema_migrations WHERE migration_name = ?", (MIGRATION,))
conn.commit()
conn.close()
return db_path, data
def notes(db_path):
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
rows = {r["slug"]: dict(r) for r in conn.execute("SELECT * FROM notes")}
conn.close()
return rows
def test_titles_move_out_of_files(setup):
db_path, data = setup
run_migrations(db_path, data_dir=data)
rows = notes(db_path)
assert rows["titled"]["title"] == "Old Title"
assert (data / "notes/2026/11/titled.md").read_text() == "The body."
assert rows["titled"]["content_hash"] == calculate_content_hash("The body.")
assert rows["untitled"]["title"] is None # a hashtag isn't a heading
assert (data / "notes/2026/11/untitled.md").read_text() == "#indieweb is great"
assert rows["trashed"]["title"] == "Gone" # found in .trash, so undelete still works
assert (data / ".trash/2026/10/trashed.md").read_text() == "Bin."
conn = sqlite3.connect(db_path)
assert conn.execute("SELECT COUNT(*) FROM notes_fts").fetchone()[0] == 0 # rebuilt at startup
assert conn.execute("SELECT 1 FROM schema_migrations WHERE migration_name = ?",
(MIGRATION,)).fetchone()
conn.close()
def test_runs_once(setup):
"""Afterwards a heading in a note's text is just a heading"""
db_path, data = setup
run_migrations(db_path, data_dir=data)
(data / "notes/2026/11/untitled.md").write_text("# Now a heading\n\nNew text")
run_migrations(db_path, data_dir=data)
assert notes(db_path)["untitled"]["title"] is None
assert (data / "notes/2026/11/untitled.md").read_text() == "# Now a heading\n\nNew text"
def test_failure_puts_files_back(setup, monkeypatch):
db_path, data = setup
import starpunk.utils as utils
real_write = utils.write_note_file
written = []
def write_then_fail(path, content):
if written:
raise OSError("disk full")
written.append(path)
real_write(path, content)
monkeypatch.setattr(utils, "write_note_file", write_then_fail)
with pytest.raises(MigrationError):
run_migrations(db_path, data_dir=data)
assert written # one file was changed before the failure...
assert (data / "notes/2026/11/titled.md").read_text() == TITLED # ...and put back
assert (data / ".trash/2026/10/trashed.md").read_text() == "# Gone\n\nBin."
conn = sqlite3.connect(db_path)
assert not conn.execute("SELECT 1 FROM schema_migrations WHERE migration_name = ?",
(MIGRATION,)).fetchone()
assert "title" not in [r[1] for r in conn.execute("PRAGMA table_info(notes)")]
conn.close()
def test_without_data_dir_only_schema(setup):
db_path, data = setup
run_migrations(db_path)
assert notes(db_path)["titled"]["title"] is None
assert (data / "notes/2026/11/titled.md").read_text() == TITLED