Skip to content

test(reader): extract PdfReaderScreen decision logic + coverage (#606) - #610

Merged
github-actions[bot] merged 6 commits into
devfrom
test/pdf-reader-screen-coverage
Aug 25, 2026
Merged

github-actions[bot] merged 6 commits into
devfrom
test/pdf-reader-screen-coverage

Conversation

@libravault-pipeline-bot

@libravault-pipeline-bot libravault-pipeline-bot Bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Part of #606 (Phase 7 remaining coverage target, split from #521).

  • Extracted PdfReaderScreen's pure decisions (page-index clamping, tap-zone resolution, rendered-page aspect ratio, open-error messaging, page-indicator text) into PdfReaderScreenLogic.kt, covered with 14 JUnit5 tests — no Robolectric needed for these.
  • Found and fixed a real bug in the extraction: page.coerceIn(0, pageCount - 1) throws IllegalArgumentException whenever pageCount is 0 (a validly-opened but empty PDF) — coerceIn rejects inverted bounds. coercePageIndex now returns 0 instead. Confirmed the regression test fails against the pre-fix logic (reverted the fix, reran, saw red, restored) before trusting it, per AGENTS.md.
  • Added 2 Robolectric Compose tests for PdfReaderScreen's error branch (SecurityException → permission-denied message, other exceptions → generic message with cause), driven through the real composable and its real DisposableEffect/coroutine plumbing via a mocked PdfReaderViewModel. This path returns before ever touching PdfRenderer, 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 PdfRenderer before assuming the usual wrapper + pure-content + Roborazzi-baseline template applies (per §6/§1b of docs/TEST_COVERAGE_PRD.md). Empirically verified locally with feature:reader's existing Robolectric setup (4.12.2, sdk 34) — full writeup in docs/TEST_COVERAGE_PRD.md §1c (new):

  • PdfRenderer's constructor and openPage run without UnsatisfiedLinkError — Robolectric instruments the real class rather than stubbing it.
  • However, opening a real PDF — tried both a byte-exact hand-built minimal single-page PDF and a full ReportLab-generated one, to rule out the file itself being malformed — consistently reports 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 XContent in Dark/Light/Sepia" step of the Phase 7 recipe can't apply to this screen's actual rendered page content the way it did for SettingsScreenKt/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.
  • No existing tests changed behaviour; the only production-code change besides the extraction is the 0-page-crash fix (behaviour-preserving for every page count that was already valid).

Risk

No sensitive_paths touched (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

…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.
@github-actions
github-actions Bot marked this pull request as ready for review August 25, 2026 11:39
…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.
@github-actions

Copy link
Copy Markdown
Contributor

📱 iOS Build Status: PASSED

⏭️ iOS Simulator Build & Unit Tests - skipped
✅ Android Core Modules - success

iOS UI tests run in a separate, informational job and are not reflected above — check "iOS UI Tests (informational)" for those.

View run details

@github-actions github-actions Bot added the risk:low Principal review classified this PR as low risk label Aug 25, 2026
@libravault-pipeline-bot libravault-pipeline-bot Bot added the status:needs-qa PR open, awaiting qa-agent verification label Aug 25, 2026
@claude

claude Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

QA verification — PR #610 vs issue #606

Issue #606 is an investigation-scoped ticket ("before starting: confirm whether Robolectric can drive PdfRenderer..."), not a feature checklist, so I'm treating its three implicit deliverables as the acceptance criteria.

  • ✅ Robolectric/PdfRenderer feasibility investigated before assuming the usual template applies. docs/TEST_COVERAGE_PRD.md §1c documents concrete, reproducible findings (constructor/openPage run without UnsatisfiedLinkError, but pageCount stays 0 for both a hand-built and ReportLab-generated real PDF). The claim is falsifiable and specific enough to trust, not a vague "couldn't get it working."
  • ✅ ViewModel-sharing shape re-checked. Confirmed PdfReaderScreen takes a single viewModel: PdfReaderViewModel = hiltViewModel() with no sibling sharing — the ReaderScreenKt-style blocker from §1b genuinely doesn't apply here.
  • ✅ Coverage delivered honestly scoped to what the investigation found feasible, rather than forcing the full wrapper+content+Roborazzi-baseline template onto content that can't actually be produced under this Robolectric setup. Remaining gap (render path) is real and disclosed, not hidden.

Tests run

./gradlew :feature:reader:testDebugUnitTest --continue — BUILD SUCCESSFUL, ran to completion in the foreground.

  • Full module: 308 tests, 0 failures, 0 errors (confirmed by summing tests= across all TEST-*.xml in build/test-results/testDebugUnitTest/, and separately confirming zero files contain failures="1+" or errors="1+"). Matches the PR description exactly.
  • PdfReaderScreenLogicTest: 14/14 passed.
  • PdfReaderScreenStateTest (Robolectric): 2/2 passed.

Test-quality check (this repo's known "mocked past the broken part" failure mode)

  • PdfReaderScreenLogicTest is plain JUnit5 against pure functions — no mocking possible, results are directly observable.
  • PdfReaderScreenStateTest uses mockk<PdfReaderViewModel>() without relaxed = true, so any unstubbed call throws rather than silently returning an empty default — the failure mode this repo has been burned by before doesn't apply here. The test drives the real composable, real DisposableEffect, and a real coroutine launch; only openFileDescriptor is mocked, and only to make it throw. This exercises real production code, not a mock standing in for it.
  • Regression-guard claim (coercePageIndex bug): independently verified the underlying claim — Kotlin's Int.coerceIn(min, max) throws IllegalArgumentException unconditionally whenever min > max, so page.coerceIn(0, pageCount - 1) with pageCount == 0 (coerceIn(0, -1)) throws regardless of page's value. The bug and fix are real, not a misdiagnosis.
  • Diff-read the extraction itself (PdfReaderScreen.kt before/after): the moved logic is behavior-preserving in every branch except the disclosed 0-page fix. No silent behavior drift.

Regressions

No existing tests reference this screen (these are new files), no Roborazzi baselines touched, no other module affected — diff is confined to feature/reader's PDF package plus docs.

AGENTS.md conventions

One nit: commit e3adf77 ("refactor(reader): extract PdfReaderScreen's decision logic...") bundles the pure extraction and the 0-page behavior fix in a single commit. AGENTS.md's commit-hygiene section is explicit that "a refactor and a behaviour change must be two [commits]" — this should have been split. Not a functional problem (tests cover it, the diff is easy to audit either way), so I'm not failing on it, but flagging for the dev agent to internalize since it seems like a clean scoping win they otherwise nailed.

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.

Verdict

Investigation 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

@github-actions github-actions Bot removed the status:needs-qa PR open, awaiting qa-agent verification label Aug 25, 2026
@libravault-pipeline-bot libravault-pipeline-bot Bot added the status:needs-review QA passed, awaiting principal-review-agent label Aug 25, 2026

@github-actions github-actions Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 of PdfReaderScreen.kt,
    PdfReaderScreenLogic.kt, PdfReaderViewModel.kt, ContentSource.kt, ReaderSettings.kt.
  • Ran ./gradlew :feature:reader:testDebugUnitTest myself: 308 tests, 0 failures —
    matches the PR body exactly, including the 16 new tests (14 in
    PdfReaderScreenLogicTest, 2 in PdfReaderScreenStateTest), all green.
  • Ran koverXmlReportDebug and parsed the XML: PdfReaderScreenKt (+ its lambdas)
    is 138 missed / 39 covered, PdfReaderScreenLogicKt is 0 missed / 12 covered —
    matches the PR's §1c coverage numbers exactly.
  • Re-ran .github/scripts/classify_pr_risk.py origin/dev HEAD myself: risk:low,
    311 changed lines, no sensitive-path match — confirms the risk:low label is
    current (no commits landed after it was set).
  • Checked .github/agent-policy.yml's sensitive_paths: none of the 5 changed
    files match. No new networking dependency introduced. AGENTS.md test
    conventions (JUnit5 + MockK, internal pure helpers, <Subject>Test.kt naming,
    backtick test names) are followed correctly by the new files.
  • Confirmed mockk (used by the new PdfReaderScreenStateTest) is actually on the
    test classpath — not directly declared in feature/reader/build.gradle.kts, but
    added transitively by the libravault.android.feature convention plugin
    (build-logic/convention/.../AndroidConventionPlugins.kt). Not a gap.
  • Verified the coercePageIndex/resolvePaginatedTapZone/isCentreTapZone/
    renderedPageHeightPx/pdfOpenErrorMessage/pageIndicatorText extractions 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.

@github-actions github-actions Bot added status:needs-human-merge Principal review done, requires human merge decision and removed status:needs-review QA passed, awaiting principal-review-agent labels Aug 25, 2026
robster7674 and others added 3 commits August 25, 2026 15:45
#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
@github-actions

Copy link
Copy Markdown
Contributor

📱 iOS Build Status: PASSED

⏭️ iOS Simulator Build & Unit Tests - skipped
✅ Android Core Modules - success

iOS UI tests run in a separate, informational job and are not reflected above — check "iOS UI Tests (informational)" for those.

View run details

@github-actions
github-actions Bot merged commit 2faa05b into dev Aug 25, 2026
8 checks passed
@github-actions
github-actions Bot deleted the test/pdf-reader-screen-coverage branch August 25, 2026 16:42
github-actions Bot added a commit that referenced this pull request Aug 25, 2026
…616)

Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
@libravault-pipeline-bot libravault-pipeline-bot Bot added the status:needs-review QA passed, awaiting principal-review-agent label Aug 25, 2026
@github-actions github-actions Bot removed the status:needs-review QA passed, awaiting principal-review-agent label Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:low Principal review classified this PR as low risk status:needs-human-merge Principal review done, requires human merge decision

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test coverage: PdfReaderScreenKt (Phase 7 split from #521)

1 participant