Files
FabledCurator/tests/test_series_match_service.py
T
bvandeusen 59746d213d
CI / lint (push) Successful in 3s
CI / frontend-build (push) Successful in 21s
CI / backend-lint-and-test (push) Successful in 35s
CI / integration (push) Successful in 3m13s
feat(series): flat series sequence + cosmetic chapter dividers (#789 Phase 1)
Reframe a series from "ordered chapters that own pages" to ONE flat,
series-global ordered run of pages with optional cosmetic chapter DIVIDERS
over it. A chapter no longer wraps content — it's a labeled divider anchored
to the page that begins it; a page's chapter is derived as the nearest
preceding divider. This is what lets installments assembled from multiple
sources sit in one continuous, correctly-numbered sequence (operator's
Goblin Juice case).

- migration 0047: flatten each series to a series-global page_number
  (preserving today's reading order); convert each existing chapter to a
  divider anchored at its first page (keeping title/stated_part); drop
  series_page.chapter_id; reshape series_chapter (anchor_page_id UNIQUE FK,
  drop chapter_number/is_placeholder/stated_page_start/end). Loss-safe for
  content; drops empty placeholder chapters + a redundant page-1 divider.
- series_page: page_number is now the series-global order; no chapter_id.
- series_chapter: anchored divider (anchor_page_id, title, stated_part).
- series_service: flat list_pages (one run + derived dividers + per-page
  source_post + part_gaps), series-wide reorder/renumber, divider CRUD
  (create/update/move/delete); retired per-chapter reorder/merge/placement.
- api/tags: drop chapter_id from add; /chapters endpoints are divider
  create/update/delete (removed chapter reorder/merge/page-reorder).
- series_match_service: series "end" reads max(series_page.stated_page);
  accept appends via add_post. tag_service series-merge appends src's pages
  after tgt's max so the merged series stays one clean run.
- frontend: seriesManage store + SeriesManageView → one continuous
  drag-reorder grid with inline divider bars + series-global page numbers;
  reader walks the flat run, headings from dividers; PostSeriesMenu copy.
- tests reworked across the series suite for the divider model.

Phase 2 (pending staging for add-from-post) is separate.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-11 21:30:01 -04:00

129 lines
4.3 KiB
Python

"""FC-6.3 — matcher writes pending suggestions; accept/dismiss lifecycle."""
import pytest
from sqlalchemy import func, select
from backend.app.models import Artist, ImageRecord, Post, TagKind
from backend.app.models.series_suggestion import SeriesSuggestion
from backend.app.services.series_match_service import SeriesMatchService
from backend.app.services.series_service import SeriesService
pytestmark = pytest.mark.integration
_SEQ = 0
async def _artist(db, name):
a = Artist(name=name, slug=name.lower().replace(" ", "-"))
db.add(a)
await db.flush()
return a
async def _post_with_images(db, artist, title, ext, n):
global _SEQ
p = Post(artist_id=artist.id, external_post_id=ext, post_title=title)
db.add(p)
await db.flush()
for _ in range(n):
_SEQ += 1
db.add(ImageRecord(
path=f"/tmp/fc_sm_{_SEQ}.png", sha256=f"{_SEQ:064d}",
size_bytes=1, mime="image/png", origin="downloaded",
primary_post_id=p.id, artist_id=artist.id,
))
await db.flush()
return p
async def _series_from(db, post):
out = await SeriesService(db).promote_post_to_series(post.id)
return out["series_tag_id"]
@pytest.mark.asyncio
async def test_match_post_suggests_continuation(db):
artist = await _artist(db, "Story Artist")
a = await _post_with_images(db, artist, "Story pages 1-4", "a", 4)
sid = await _series_from(db, a) # series stated 1-4
b = await _post_with_images(db, artist, "Story pages 5-8", "b", 4)
svc = SeriesMatchService(db)
made = await svc.match_post(b.id, threshold=0.5)
assert made == 1
pending = await svc.list_pending()
assert len(pending) == 1
sug = pending[0]
assert sug["series_tag_id"] == sid
assert sug["post_id"] == b.id
# title 1.0 + artist 1.0 + pages 1.0 (5 == 4+1) → 0.4+0.2+0.25 = 0.85
assert sug["score"] >= 0.85
assert sug["signals"]["pages"] == 1.0
@pytest.mark.asyncio
async def test_match_is_idempotent_under_unique(db):
artist = await _artist(db, "Idem Artist")
a = await _post_with_images(db, artist, "Tale pages 1-4", "a", 2)
await _series_from(db, a)
b = await _post_with_images(db, artist, "Tale pages 5-8", "b", 2)
svc = SeriesMatchService(db)
await svc.match_post(b.id, threshold=0.5)
await svc.match_post(b.id, threshold=0.5) # again
cnt = await db.scalar(
select(func.count()).select_from(SeriesSuggestion)
.where(SeriesSuggestion.post_id == b.id)
)
assert cnt == 1
@pytest.mark.asyncio
async def test_accept_appends_pages_and_marks_added(db):
artist = await _artist(db, "Accept Artist")
a = await _post_with_images(db, artist, "Saga pages 1-4", "a", 3)
sid = await _series_from(db, a)
b = await _post_with_images(db, artist, "Saga pages 5-8", "b", 3)
svc = SeriesMatchService(db)
await svc.match_post(b.id, threshold=0.5)
sug = (await svc.list_pending())[0]
await svc.accept(sug["id"])
data = await SeriesService(db).list_pages(sid)
# b's pages were appended to the series' flat run (3 from a + 3 from b).
assert len(data["pages"]) == 6
status = await db.scalar(
select(SeriesSuggestion.status).where(SeriesSuggestion.id == sug["id"])
)
assert status == "added"
@pytest.mark.asyncio
async def test_dismiss_then_rematch_skips(db):
artist = await _artist(db, "Dismiss Artist")
a = await _post_with_images(db, artist, "Epic pages 1-4", "a", 2)
await _series_from(db, a)
b = await _post_with_images(db, artist, "Epic pages 5-8", "b", 2)
svc = SeriesMatchService(db)
await svc.match_post(b.id, threshold=0.5)
sug = (await svc.list_pending())[0]
await svc.dismiss(sug["id"])
assert await svc.list_pending() == []
# A re-match must respect the dismissal (decided → skipped).
made = await svc.match_post(b.id, threshold=0.5)
assert made == 0
@pytest.mark.asyncio
async def test_rescan_scans_posts(db):
artist = await _artist(db, "Rescan Artist")
a = await _post_with_images(db, artist, "Quest pages 1-4", "a", 2)
await _series_from(db, a)
await _post_with_images(db, artist, "Quest pages 5-8", "b", 2)
svc = SeriesMatchService(db)
summary = await svc.rescan(threshold=0.5)
assert summary["scanned"] >= 2
assert summary["suggested"] >= 1
assert summary["partial"] is False