diff --git a/tests/reading/test_router.py b/tests/reading/test_router.py index 7e8966602..d86c4ddcf 100644 --- a/tests/reading/test_router.py +++ b/tests/reading/test_router.py @@ -504,6 +504,36 @@ def test_unit_text_is_addressed_by_locator(client: TestClient) -> None: assert "scaled dot-product" in body["text"] +@pytest.mark.parametrize("headings", [True, False]) +def test_large_markdown_upload_keeps_every_unit_accessible_through_the_api(client, headings): + """#1641: heading/fence fallback must never make the end of a document unreachable.""" + chunks = ["# Opening\n\n"] + for index in range(24): + if headings: + chunks.append(f"## Section {index}\n\n") + chunks.append(f"Document paragraph {index}. " * 100 + "\n\n") + if index == 5: + chunks.append("````markdown\n```python\n# Code, not an outline heading\n```\n````\n\n") + chunks.append("END-OF-DOCUMENT-SENTINEL") + source = "".join(chunks) + response = client.post( + "/api/reading/materials", files={"file": ("long.md", source.encode(), "text/markdown")} + ) + assert response.status_code == 200 + material = response.json() + assert material["unit_count"] > 1 + detail = client.get(f"/api/reading/materials/{material['material_id']}").json() + assert detail["unit_count"] == material["unit_count"] + units = [] + for locator in range(1, material["unit_count"] + 1): + unit = client.get(f"/api/reading/materials/{material['material_id']}/units/{locator}") + assert unit.status_code == 200 + units.append(unit.json()["text"]) + assert "END-OF-DOCUMENT-SENTINEL" in units[-1] + # Section boundaries may normalize whitespace, but cannot discard source characters. + assert "".join("".join(units).split()) == "".join(source.split()) + + def test_unit_text_out_of_range_is_a_400_with_the_real_range(client: TestClient) -> None: material = _upload(client) diff --git a/web/components/reading/TextUnitView.tsx b/web/components/reading/TextUnitView.tsx index ad4ec0ea3..81fcb88bb 100644 --- a/web/components/reading/TextUnitView.tsx +++ b/web/components/reading/TextUnitView.tsx @@ -492,11 +492,15 @@ export function TextUnitView({ the new value on press, which the silent spans never did), and the position is the header's to state, since that is the one line that is there in every render mode. What is left here is what you can *do*. */} -
- +
+ {/* #1641: scroll display controls independently so a narrow reader + never hides the only way to reach the next section. */} +
+ +
({ + useTranslation: () => ({ + t: (value: string, options?: { unit?: string }) => + value.replace("{{unit}}", options?.unit ?? ""), + }), +})); +vi.mock("@/lib/reading-api", () => ({ + getUnitText: vi.fn(), + getMaterialMedia: vi.fn(), + materialMediaUrl: vi.fn(), +})); +vi.mock("@/components/common/RichMarkdownRenderer", () => ({ default: () => null })); +vi.mock("@recogito/text-annotator", () => ({ + createTextAnnotator: () => ({ destroy: vi.fn(), setAnnotations: vi.fn(), on: vi.fn() }), +})); + +describe("Markdown section navigation (#1641)", () => { + beforeEach(() => { + vi.mocked(getMaterialMedia).mockResolvedValue([]); + }); + + it.each([true, false])("reaches the final unit and returns with headings=%s", async (headings) => { + const units = [ + (headings ? "# Opening\n\n" : "") + "Opening paragraph.", + "````markdown\n```python\n# This is code\n```\n````\nMiddle paragraph.", + (headings ? "## Last section\n\n" : "") + "END OF DOCUMENT", + ]; + vi.mocked(getUnitText).mockImplementation(async (_id, locator) => ({ + locator, unit: "section", text: units[locator - 1], + })); + const user = userEvent.setup(); + render(); + + await screen.findByText("Opening paragraph."); + expect(screen.getByRole("button", { name: "Previous Section" })).toBeDisabled(); + for (let locator = 2; locator <= units.length; locator++) { + await user.click(screen.getByRole("button", { name: "Next Section" })); + await waitFor(() => expect(getUnitText).toHaveBeenLastCalledWith("long-markdown", locator)); + await waitFor(() => expect(screen.queryByText("Loading…")).not.toBeInTheDocument()); + } + await screen.findByText("END OF DOCUMENT"); + expect(screen.getByRole("button", { name: "Next Section" })).toBeDisabled(); + await user.click(screen.getByRole("button", { name: "Previous Section" })); + await waitFor(() => expect(screen.getByRole("article")).toHaveTextContent("Middle paragraph.")); + expect(screen.getByRole("button", { name: "Next Section" })).toBeEnabled(); + }); +});