Repository navigation
test(parser): pin figure-aware reading order in PDF post-process - #18178
Conversation
Characterization/regression test for Borrow B (image-anchored reading order). Measurement shows the adapter layer already produces correct figure-aware order: sortSectionsByPosition (page->top->left, run unconditionally at pdf_postprocess.go:29) and reorderPDFMultiColumn (left->top, when enableMultiColumn) both place a column-spanning figure between the upper and lower text bands. So Borrow B as a separate feature is NOT needed — the detector's FinalReadingOrderMerge output is overwritten by these geometry-based sorts anyway. The only residual is a merged section whose vertical span straddles a figure (placed by its first top); that is inherent to sorting already-merged sections and out of scope. Co-Authored-by: CodeBuddy <[email protected]>
📝 WalkthroughWalkthroughAdds PDF post-processing measurement tests for figure ordering across two columns and for paragraphs that span a figure. ChangesPDF reading-order tests
Estimated code review effort: 2 (Simple) | ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/parser/parser/pdf_postprocess_measure_test.go`:
- Around line 60-73: Update the ordering assertions in
internal/parser/parser/pdf_postprocess_measure_test.go lines 60-73 to record
each expected upper and lower section index and fail if any required section is
missing before comparing positions; also update lines 122-131 to fail when
either para-col0 or figure has index -1 before asserting their order, using the
existing test symbols and preserving the intended ordering checks.
- Around line 3-14: Remove the obsolete planning commentary above the
measurement tests, including references to “Borrow B,” future figure-anchored
fixes, and implementation-planning details. Retain only concise documentation
describing the current behavior covered by the tests.
- Around line 94-101: Extend the reading-order assertions in the parser test to
verify the spanning figure is after both upper sections and before both lower
sections, including col1-upper and col1-lower alongside the existing col0
checks. If the implementation fails this contract, update reorderPDFMultiColumn
to produce that ordering while preserving the existing section-presence
validation.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 74498e41-374b-4fed-b6a5-f4b53b3ef1ce
📒 Files selected for processing (1)
internal/parser/parser/pdf_postprocess_measure_test.go
| // Measurement / characterization tests for Borrow B (image-anchored reading | ||
| // order). These pin the CURRENT behavior of the adapter layer | ||
| // (pdf_postprocess.go) on pages where a figure spans one or both columns, so | ||
| // we can decide WHERE a figure-anchored reading-order fix (if any) must live. | ||
| // | ||
| // Key fact established during planning: AssignColumn's FinalReadingOrderMerge | ||
| // (column-major) is OVERWRITTEN by sortSectionsByPosition (page -> top -> left, | ||
| // run unconditionally at pdf_postprocess.go:29) and, when enableMultiColumn, by | ||
| // reorderPDFMultiColumn (left -> top). Both derive order from section geometry, | ||
| // not from ColID, and are already figure-aware. These tests verify that and | ||
| // surface the one residual limitation (a merged section whose vertical span | ||
| // straddles a figure is placed by its FIRST top). |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Remove obsolete planning commentary.
This comment records the earlier “Borrow B” evaluation and a possible future fix. The PR objective states that the image-anchored DAG is unnecessary. Keep only documentation of the current test behavior.
As per coding guidelines, remove stale documentation.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/parser/parser/pdf_postprocess_measure_test.go` around lines 3 - 14,
Remove the obsolete planning commentary above the measurement tests, including
references to “Borrow B,” future figure-anchored fixes, and
implementation-planning details. Retain only concise documentation describing
the current behavior covered by the tests.
Source: Coding guidelines
| for _, name := range []string{"col0-upper", "col1-upper"} { | ||
| for i, s := range result.Sections { | ||
| if s.Text == name && i > figIdx { | ||
| t.Errorf("%s (idx %d) is after figure (idx %d); want before", name, i, figIdx) | ||
| } | ||
| } | ||
| } | ||
| for _, name := range []string{"col0-lower", "col1-lower"} { | ||
| for i, s := range result.Sections { | ||
| if s.Text == name && i < figIdx { | ||
| t.Errorf("%s (idx %d) is before figure (idx %d); want after", name, i, figIdx) | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Fail when a required fixture section is missing.
Both tests use ordering checks that can pass when required sections are absent.
internal/parser/parser/pdf_postprocess_measure_test.go#L60-L73: Record the index of every upper and lower section, then fail before comparing indices if any expected section is missing.internal/parser/parser/pdf_postprocess_measure_test.go#L122-L131: Fail ifpara-col0orfigurehas index-1before asserting their order.
📍 Affects 1 file
internal/parser/parser/pdf_postprocess_measure_test.go#L60-L73(this comment)internal/parser/parser/pdf_postprocess_measure_test.go#L122-L131
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/parser/parser/pdf_postprocess_measure_test.go` around lines 60 - 73,
Update the ordering assertions in
internal/parser/parser/pdf_postprocess_measure_test.go lines 60-73 to record
each expected upper and lower section index and fail if any required section is
missing before comparing positions; also update lines 122-131 to fail when
either para-col0 or figure has index -1 before asserting their order, using the
existing test symbols and preserving the intended ordering checks.
| f := idx("figure") | ||
| c0u, c0l := idx("col0-upper"), idx("col0-lower") | ||
| if f < 0 || c0u < 0 || c0l < 0 { | ||
| t.Fatal("expected sections missing") | ||
| } | ||
| if !(c0u < f && f < c0l) { | ||
| t.Errorf("figure not wrapped inside col0: want col0-upper(%d) < figure(%d) < col0-lower(%d)", c0u, f, c0l) | ||
| } |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Assert the stated multi-column reading order.
The test only checks column 0. For this fixture, reorderPDFMultiColumn places col1-upper after figure. The test therefore does not verify that the spanning figure is between the upper and lower text bands in both columns.
Define the required reading-order contract. If both upper sections must precede the figure, assert col0-upper and col1-upper before it, and both lower sections after it. Update reorderPDFMultiColumn if that assertion is the required behavior.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/parser/parser/pdf_postprocess_measure_test.go` around lines 94 -
101, Extend the reading-order assertions in the parser test to verify the
spanning figure is after both upper sections and before both lower sections,
including col1-upper and col1-lower alongside the existing col0 checks. If the
implementation fails this contract, update reorderPDFMultiColumn to produce that
ordering while preserving the existing section-presence validation.
Summary
Adds a characterization / regression test (
pdf_postprocess_measure_test.go) that pins the figure-aware reading order produced by the PDF adapter layer (internal/parser/parser/pdf_postprocess.go).While planning "Borrow B" (an image-anchored reading-order DAG inspired by
xugangqiang/pdf-inspector), measurement showed the adapter already produces correct order for pages where a figure spans one or both columns:sortSectionsByPosition(page → top → left, run unconditionally) places a column-spanning figure between the upper and lower text bands.reorderPDFMultiColumn(left → top, whenenableMultiColumnis set) wraps the figure correctly inside its own column.Both derive order from section geometry, not from
AssignColumn'sFinalReadingOrderMerge(column-major) output — which these sorts overwrite anyway. So the planned "figure-anchored DAG" would have no end-to-end effect, and Borrow B is not needed. The only residual limitation (a merged section whose vertical span straddles a figure is ordered by its first top) is inherent to sorting already-merged sections and is out of scope.This test locks the current correct behavior so a future refactor of the adapter cannot silently regress figure handling.
Test plan
bash build.sh --test ./internal/parser/parser/— newTestMeasure_*cases pass; they assert a spanning figure is placed between the upper and lower text in both adapter modes.Note
This PR is intentionally independent of PR #18150 (column detection). It only adds a test; no production code is changed.