Repository navigation
test(reader): extract PdfReaderScreen decision logic + coverage (#606) - #610
Conversation
…h tests Pulls the pure decisions out of PdfReaderScreen.kt into PdfReaderScreenLogic.kt (page-index clamping, tap-zone resolution, rendered-page aspect ratio, open-error messaging, page-indicator text) and covers them with JUnit5 tests — no Android/Robolectric dependency needed. Fixes a real bug found in the extraction: `page.coerceIn(0, pageCount - 1)` threw IllegalArgumentException whenever pageCount was 0 (a validly-opened but empty PDF), since coerceIn rejects inverted bounds (0 <= -1 is false). coercePageIndex now returns 0 in that case. Confirmed the regression test fails against the pre-fix logic before restoring the fix. Part of #606.
…ric finding Adds Robolectric Compose tests for PdfReaderScreen's error branch (both SecurityException and generic-failure messages), driven through the real composable via a mocked PdfReaderViewModel — the error path returns before ever touching PdfRenderer, so no working native PDF backend is needed. Confirmed both tests go red against a deliberately broken error message before restoring, per AGENTS.md. Documents the Robolectric/PdfRenderer investigation issue #606 asked for before assuming the usual wrapper+content+Roborazzi-baseline template applies: docs/TEST_COVERAGE_PRD.md §1c. Empirically verified (not just plausible) that Robolectric 4.12.2 instruments the real PdfRenderer class without throwing, but its native PDF-parsing backend isn't functional here — pageCount stays 0 for both a hand-built and a ReportLab-generated real PDF — so screenshotting real rendered page content isn't achievable, and a bitmap-pipeline isolation refactor is flagged as optional future work rather than attempted in this PR. Part of #606.
…en work PdfReaderScreenKt: 146 missed/0% -> 138 missed, 39 covered (~22%). PdfReaderScreenLogicKt (new): 12/12, 100%. Remaining gap is the render path itself, which §1c already explains can't be exercised here.
📱 iOS Build Status: PASSED⏭️ iOS Simulator Build & Unit Tests - skipped iOS UI tests run in a separate, informational job and are not reflected above — check "iOS UI Tests (informational)" for those. |
QA verification — PR #610 vs issue #606Issue #606 is an investigation-scoped ticket ("before starting: confirm whether Robolectric can drive
Tests run
Test-quality check (this repo's known "mocked past the broken part" failure mode)
RegressionsNo existing tests reference this screen (these are new files), no Roborazzi baselines touched, no other module affected — diff is confined to AGENTS.md conventionsOne nit: commit Test coverage for a non-trivial change, red→green discipline (per commit messages, confirmed plausible via independent verification above), branch naming, and no hardcoded versions all check out. VerdictInvestigation is genuine (not asserted, backed by specific empirical detail), the fix is real, the tests exercise real code paths without relaxed-mock cover, and the full module suite is green. The one commit-hygiene nit doesn't rise to a functional or safety concern. QA verdict: PASS |
There was a problem hiding this comment.
Principal review — verdict: human-merge
Verification performed (not just reading the diff/description)
- Read the full diff (
gh pr diff 610) and the current state ofPdfReaderScreen.kt,
PdfReaderScreenLogic.kt,PdfReaderViewModel.kt,ContentSource.kt,ReaderSettings.kt. - Ran
./gradlew :feature:reader:testDebugUnitTestmyself: 308 tests, 0 failures —
matches the PR body exactly, including the 16 new tests (14 in
PdfReaderScreenLogicTest, 2 inPdfReaderScreenStateTest), all green. - Ran
koverXmlReportDebugand parsed the XML:PdfReaderScreenKt(+ its lambdas)
is 138 missed / 39 covered,PdfReaderScreenLogicKtis 0 missed / 12 covered —
matches the PR's §1c coverage numbers exactly. - Re-ran
.github/scripts/classify_pr_risk.py origin/dev HEADmyself:risk:low,
311 changed lines, no sensitive-path match — confirms therisk:lowlabel is
current (no commits landed after it was set). - Checked
.github/agent-policy.yml'ssensitive_paths: none of the 5 changed
files match. No new networking dependency introduced.AGENTS.mdtest
conventions (JUnit5 + MockK,internalpure helpers,<Subject>Test.ktnaming,
backtick test names) are followed correctly by the new files. - Confirmed
mockk(used by the newPdfReaderScreenStateTest) is actually on the
test classpath — not directly declared infeature/reader/build.gradle.kts, but
added transitively by thelibravault.android.featureconvention plugin
(build-logic/convention/.../AndroidConventionPlugins.kt). Not a gap. - Verified the
coercePageIndex/resolvePaginatedTapZone/isCentreTapZone/
renderedPageHeightPx/pdfOpenErrorMessage/pageIndicatorTextextractions are
behaviour-preserving 1:1 ports of the inline logic they replace.
CONFIRMED finding — filed as #613
The PR's headline bugfix claim doesn't fully hold. coercePageIndex correctly stops
the synchronous IllegalArgumentException that page.coerceIn(0, pageCount - 1)
threw for a 0-page PDF — verified, and the regression test for that pure function is
good. But PdfReaderScreen.kt's ScrollMode.PAGINATED path (the default
ReaderSettings.scrollMode) unconditionally composes PdfPageImage with the
now-clamped pageIndex = 0, with no pageCount > 0 guard anywhere in that path.
PdfPageImage's LaunchedEffect then calls renderer.openPage(0), which
android.graphics.pdf.PdfRenderer throws IllegalArgumentException for on any
0-page document — uncaught, from a background (Dispatchers.IO) coroutine, outside
the DisposableEffect's try/catch that populates openError.
Net effect: a 0-page PDF opened in paginated mode crashes the app both before and
after this PR — #610 only moved where the crash happens (composition-time →
async render-time), not whether it happens. ScrollMode.SCROLLING genuinely is
fixed, since LazyColumn.items(pageCount = 0) composes zero PdfPageImage calls.
This is exactly the "a level that cannot observe the property" trap AGENTS.md
itself warns about: the new JUnit5 test proves coercePageIndex(0, 0) == 0, which
is true and correct, but that's the wrong level to observe whether the screen
still crashes for a 0-page document — that needs a Robolectric/instrumented test
driving PdfPaginatedView/PdfReaderScreen end-to-end with a 0-page renderer,
which doesn't exist here (the one new Robolectric test only covers the
already-safe openError branch).
Full details and a suggested fix (guard the render call site on pageCount > 0,
mirroring how openError already gates the open-failure case) are in #613.
Other notes (non-blocking)
Everything else checked out clean: the aspect-ratio/tap-zone/error-message/
page-indicator extractions are correct, the §1c Robolectric/PdfRenderer
investigation write-up is credible and appropriately scoped (not attempting a
bitmap-pipeline isolation refactor for a placeholder screenshot is the right call
for this issue's scope), and the coverage/test-count claims all check out against
ground truth.
Outcome
risk:low per the classifier, but this PR carries a CONFIRMED finding (#613), which
per the review policy forces human-merge regardless of the risk label. Routing to
a human — worth deciding in the same pass whether to land the paginated-mode guard
in this PR or as a fast follow-up referencing #613.
#613 (found in principal review of #610): coercePageIndex stopped the synchronous crash for a 0-page PDF, but ScrollMode.PAGINATED still unconditionally composed PdfPageImage with the clamped pageIndex = 0, which calls renderer.openPage(0) — PdfRenderer throws for any index on a 0-page document, uncaught, from a background coroutine. ScrollMode.SCROLLING was already safe. Guards both modes on pageCount <= 0 before dispatch, same shape as the existing openError gate. Reproduced the crash first (temporarily removed the guard, confirmed the new paginated-mode test throws the exact IllegalArgumentException #613 described) before restoring the fix. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
# Conflicts: # docs/TEST_COVERAGE_PRD.md # feature/reader/src/main/kotlin/xyz/libravault/feature/reader/pdf/PdfReaderScreen.kt
📱 iOS Build Status: PASSED⏭️ iOS Simulator Build & Unit Tests - skipped iOS UI tests run in a separate, informational job and are not reflected above — check "iOS UI Tests (informational)" for those. |
Summary
Part of #606 (Phase 7 remaining coverage target, split from #521).
PdfReaderScreen's pure decisions (page-index clamping, tap-zone resolution, rendered-page aspect ratio, open-error messaging, page-indicator text) intoPdfReaderScreenLogic.kt, covered with 14 JUnit5 tests — no Robolectric needed for these.page.coerceIn(0, pageCount - 1)throwsIllegalArgumentExceptionwheneverpageCountis 0 (a validly-opened but empty PDF) —coerceInrejects inverted bounds.coercePageIndexnow returns 0 instead. Confirmed the regression test fails against the pre-fix logic (reverted the fix, reran, saw red, restored) before trusting it, perAGENTS.md.PdfReaderScreen's error branch (SecurityException→ permission-denied message, other exceptions → generic message with cause), driven through the real composable and its realDisposableEffect/coroutine plumbing via a mockedPdfReaderViewModel. This path returns before ever touchingPdfRenderer, so it doesn't need working native PDF content. Also confirmed red-then-green by deliberately breaking the error message and reverting.Investigation (issue's "before starting" requirement)
The issue asks to confirm whether Robolectric can drive
PdfRendererbefore assuming the usual wrapper + pure-content + Roborazzi-baseline template applies (per §6/§1b ofdocs/TEST_COVERAGE_PRD.md). Empirically verified locally withfeature:reader's existing Robolectric setup (4.12.2, sdk 34) — full writeup indocs/TEST_COVERAGE_PRD.md§1c (new):PdfRenderer's constructor andopenPagerun withoutUnsatisfiedLinkError— Robolectric instruments the real class rather than stubbing it.pageCount == 0. The native PDF-parsing backend isn't actually functional under this Robolectric setup: no exception, just silently zero pages, for any real input.Consequence: the "screenshot
XContentin Dark/Light/Sepia" step of the Phase 7 recipe can't apply to this screen's actual rendered page content the way it did forSettingsScreenKt/CreateVaultScreenKt— there's no way to get Robolectric to produce real page pixels. I scoped this PR to what the split does honestly enable (pure logic + the real composable's non-rendering branches) rather than attempting a bitmap-pipeline isolation refactor (injecting a fake page renderer so the chrome could be screenshotted against placeholder content) — that's a larger, riskier change to production rendering code for comparatively low value (a placeholder screenshot isn't validating the PDF pipeline), so I flagged it as optional future work in the PRD instead of attempting it here.Tests
./gradlew :feature:reader:testDebugUnitTest— full module suite green (308 test cases, 0 failures), including the 16 new tests.Risk
No
sensitive_pathstouched (no workflow files, no vault/crypto modules, no screenshot baselines). Diff is ~305 lines, under the 400-line auto-risk threshold. Not security/release-blocker — pure test-coverage plus a defensive bugfix in reader UI logic (a 0-page PDF crashing the paginated/scrolling views).Closes #606