Files
VoiceStudio/tests/test_longform_import.py
T
Palash DebnathandClaude Opus 4.8 18e4c2347a fix(longform): correctness + robustness fixes from adversarial review (#418)
* fix(longform): correctness + robustness fixes from adversarial review

Fixes the confirmed findings from a multi-agent review of the convergence:

HIGH (correctness/output):
- MP3 + cover produced a corrupt file (-map 2:v -c:v copy is invalid for mp3).
  Cover art is now embedded for M4B only; mp3 skips it (m4b is the cover format).
- Chapter cache key omitted ref_text — editing only a profile's ref_text served
  stale audio. ref_text is now part of the voice signature.
- Preview wrote audiobook_cache/ but the render reads longform_cache/ (rename
  missed in PR 5) → cache-warming silently broke. Unified to longform_cache/.

Robustness (DoS/OOM guards):
- /audiobook/import caps upload at 64 MB; epub_to_chapter_script bounds per-entry
  (25 MB) and cumulative (300 MB) uncompressed reads (zip-bomb guard).
- /longform/render rejects > 10,000 chapters (422).

Frontend leaks:
- StoriesEditor.removeTrack revokes the line's preview blob URL.
- AudiobookTab revokes the cover blob URL on replace/unmount.

Deferred fast-follows (also from review): render-cache disk eviction; restoring
the standalone chapter cue-sheet export (needs chapter times in the done event).

Tests: mp3-drops-cover, epub entry/total caps, import + chapter-count limits;
updated the cache-hit test for the 4-field voice sig. 70 backend + 334 frontend green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* test(longform): pass EPUB caps as params, not monkeypatch (CI import-path fix)

The cap tests monkeypatched module constants, but in the full-suite CI context
the module loads under a different import path so the patch missed the function
(it used the real 300 MB cap → tests failed). epub_to_chapter_script now takes
max_entry_bytes/max_total_bytes kwargs (default to the constants); tests pass
small values directly — deterministic regardless of import path.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-13 16:31:13 +05:30

140 lines
5.9 KiB
Python

"""Text / EPUB import → chapter-delimited script.
Pure helpers, tested without a server. The EPUB case builds a minimal valid
EPUB zip in memory (no fixture file, no new dep).
"""
from __future__ import annotations
import io
import zipfile
import pytest
from services.longform_import import chapterize_plaintext, epub_to_chapter_script
from services.audiobook import parse_audiobook_script
# ── plain text ──────────────────────────────────────────────────────────────
def test_plaintext_leaves_existing_h1_untouched():
src = "# One\nhello\n\n# Two\nworld"
assert chapterize_plaintext(src) == src
def test_plaintext_promotes_chapter_lines():
src = "Chapter 1\nOnce upon a time.\n\nChapter 2\nThe end."
out = chapterize_plaintext(src)
assert "# Chapter 1" in out
assert "# Chapter 2" in out
# And it parses into two chapters.
assert len(parse_audiobook_script(out).chapters) == 2
def test_plaintext_ignores_sentences_starting_with_keyword():
# A long line beginning with "Chapter" is prose, not a heading.
src = "Chapter books were her favorite thing in the whole wide world to read."
out = chapterize_plaintext(src)
assert not out.startswith("# ")
def test_plaintext_promotes_prologue_and_part():
out = chapterize_plaintext("Prologue\nhi\n\nPart One\nthere")
assert "# Prologue" in out and "# Part One" in out
def test_plaintext_no_breaks_is_single_chapter():
out = chapterize_plaintext("just a flat blob of narration with no headings")
assert len(parse_audiobook_script(out).chapters) == 1
# ── EPUB ────────────────────────────────────────────────────────────────────
def _make_epub(chapters: list[tuple[str, str]]) -> bytes:
"""Build a minimal EPUB: container.xml → content.opf (manifest+spine) →
one XHTML per chapter."""
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as z:
z.writestr("mimetype", "application/epub+zip")
z.writestr(
"META-INF/container.xml",
'<?xml version="1.0"?>'
'<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">'
'<rootfiles><rootfile full-path="OEBPS/content.opf" '
'media-type="application/oebps-package+xml"/></rootfiles></container>',
)
items, refs = [], []
for i, (title, _body) in enumerate(chapters):
items.append(f'<item id="c{i}" href="ch{i}.xhtml" media-type="application/xhtml+xml"/>')
refs.append(f'<itemref idref="c{i}"/>')
opf = (
'<?xml version="1.0"?>'
'<package xmlns="http://www.idpf.org/2007/opf" version="3.0">'
f'<manifest>{"".join(items)}</manifest>'
f'<spine>{"".join(refs)}</spine></package>'
)
z.writestr("OEBPS/content.opf", opf)
for i, (title, body) in enumerate(chapters):
z.writestr(
f"OEBPS/ch{i}.xhtml",
f"<html><head><title>{title}</title></head><body>"
f"<h1>{title}</h1><p>{body}</p></body></html>",
)
return buf.getvalue()
def test_epub_extracts_chapters_in_spine_order():
data = _make_epub([("Intro", "Welcome aboard."), ("Finale", "Goodbye now.")])
script = epub_to_chapter_script(data)
assert "# Intro" in script and "# Finale" in script
assert "Welcome aboard." in script and "Goodbye now." in script
assert script.index("Intro") < script.index("Finale")
plan = parse_audiobook_script(script)
assert len(plan.chapters) == 2
def test_epub_skips_empty_documents():
data = _make_epub([("Real", "Has text."), ("Blank", "")])
plan = parse_audiobook_script(epub_to_chapter_script(data))
assert len(plan.chapters) == 1
def test_epub_strips_html_tags():
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w") as z:
z.writestr("META-INF/container.xml",
'<?xml version="1.0"?><container version="1.0" '
'xmlns="urn:oasis:names:tc:opendocument:xmlns:container"><rootfiles>'
'<rootfile full-path="content.opf" media-type="application/oebps-package+xml"/>'
'</rootfiles></container>')
z.writestr("content.opf",
'<?xml version="1.0"?><package xmlns="http://www.idpf.org/2007/opf" version="3.0">'
'<manifest><item id="a" href="a.xhtml" media-type="application/xhtml+xml"/></manifest>'
'<spine><itemref idref="a"/></spine></package>')
z.writestr("a.xhtml",
"<html><body><h1>T</h1><p>Hello <b>bold</b> "
"<script>ignore()</script>world.</p></body></html>")
script = epub_to_chapter_script(buf.getvalue())
assert "ignore()" not in script
assert "<b>" not in script
assert "Hello" in script and "world." in script
def test_epub_bad_zip_raises_valueerror():
with pytest.raises(ValueError):
epub_to_chapter_script(b"not a zip at all")
def test_epub_total_size_cap_truncates():
# Cap sits between one and two chapter docs (~1.1 KB uncompressed each), so
# the first is read and the rest skipped. Caps passed directly (no
# monkeypatch) so the bound holds regardless of module import path.
data = _make_epub([("One", "x" * 1000), ("Two", "y" * 1000), ("Three", "z" * 1000)])
plan = parse_audiobook_script(epub_to_chapter_script(data, max_total_bytes=1500))
assert 1 <= len(plan.chapters) < 3 # capped before reading all three
def test_epub_oversize_entry_skipped():
data = _make_epub([("Big", "x" * 500)])
with pytest.raises(ValueError): # the one entry exceeds the cap → all skipped
epub_to_chapter_script(data, max_entry_bytes=50)