@@ -174,6 +174,8 @@ vi.mock(import("./projectFilesQueryState"), async (original) => ({ |
174 | 174 | })); |
175 | 175 |
|
176 | 176 | import FilePreviewPanel from "./FilePreviewPanel"; |
| 177 | +import { ChunkedMarkdown } from "./ChunkedMarkdown"; |
| 178 | +import { splitMarkdown } from "@t3tools/shared/markdownChunks"; |
177 | 179 |
|
178 | 180 | const environmentId = EnvironmentId.make("markdown-preview-test"); |
179 | 181 | const threadRef = { environmentId, threadId: ThreadId.make("thread") }; |
@@ -806,3 +808,118 @@ describe("rendered Markdown relative images", () => { |
806 | 808 | ); |
807 | 809 | }); |
808 | 810 | }); |
| 811 | + |
| 812 | +describe("rendered Markdown scroll position", () => { |
| 813 | + const sections = Array.from( |
| 814 | + { length: 200 }, |
| 815 | + (_, index) => `## Section ${index}\n\nParagraph ${index}.\n`, |
| 816 | + ).join("\n"); |
| 817 | + const scroller = () => |
| 818 | + document.querySelector<HTMLElement>(".scrollbar-gutter-both") ?? expect.unreachable(); |
| 819 | + /** The first heading in view and its offset from the top of the viewport. */ |
| 820 | + const readingPosition = () => { |
| 821 | + const top = scroller().getBoundingClientRect().top; |
| 822 | + for (const heading of scroller().querySelectorAll("h2")) { |
| 823 | + const offset = Math.round(heading.getBoundingClientRect().top - top); |
| 824 | + if (offset > -100) return { heading: heading.textContent, offset }; |
| 825 | + } |
| 826 | + return null; |
| 827 | + }; |
| 828 | + const scrollDown = async () => { |
| 829 | + scroller().scrollTop = 3000; |
| 830 | + scroller().dispatchEvent(new Event("scroll")); |
| 831 | + await vi.waitFor(() => expect(readingPosition()?.heading).not.toBe("Section 0")); |
| 832 | + return readingPosition(); |
| 833 | + }; |
| 834 | + const expectReadingPosition = (saved: ReturnType<typeof readingPosition>) => |
| 835 | + vi.waitFor(() => { |
| 836 | + const position = readingPosition(); |
| 837 | + expect(position?.heading).toBe(saved?.heading); |
| 838 | + expect(Math.abs((position?.offset ?? 0) - (saved?.offset ?? 0))).toBeLessThanOrEqual(2); |
| 839 | + }); |
| 840 | + const withAppCss = () => { |
| 841 | + const style = document.createElement("style"); |
| 842 | + style.textContent = appCss; |
| 843 | + document.head.append(style); |
| 844 | + return { [Symbol.dispose]: () => style.remove() }; |
| 845 | + }; |
| 846 | + const sized = (children: ReactNode) => ( |
| 847 | + <div style={{ width: 500, height: 400, display: "flex", flexDirection: "column" }}> |
| 848 | + {children} |
| 849 | + </div> |
| 850 | + ); |
| 851 | + |
| 852 | + it("restores each file's reading position after the preview remounts", async () => { |
| 853 | + using _settings = setup(); |
| 854 | + using _style = withAppCss(); |
| 855 | + const preview = (relativePath: string) => sized(markdownPreview(false, sections, relativePath)); |
| 856 | + |
| 857 | + let saved: ReturnType<typeof readingPosition>; |
| 858 | + { |
| 859 | + await using screen = await render(preview("long.md")); |
| 860 | + await expect.element(screen.getByText("Section 0", { exact: true })).toBeInTheDocument(); |
| 861 | + saved = await scrollDown(); |
| 862 | + } |
| 863 | + { |
| 864 | + await using screen = await render(preview("other.md")); |
| 865 | + await expect.element(screen.getByText("Section 0", { exact: true })).toBeInTheDocument(); |
| 866 | + expect(scroller().scrollTop, "another file opens at the top").toBe(0); |
| 867 | + } |
| 868 | + await using _screen = await render(preview("long.md")); |
| 869 | + await expectReadingPosition(saved); |
| 870 | + }); |
| 871 | + |
| 872 | + it("waits for the saved chunk when a reopened file is still streaming in", async () => { |
| 873 | + using _settings = setup(); |
| 874 | + using _style = withAppCss(); |
| 875 | + const { chunks, definitions } = splitMarkdown(sections, 200); |
| 876 | + const preview = (loaded: number, loading: boolean) => |
| 877 | + sized( |
| 878 | + <ChunkedMarkdown |
| 879 | + chunks={chunks.slice(0, loaded)} |
| 880 | + definitions={definitions} |
| 881 | + loading={loading} |
| 882 | + cwd="/workspace" |
| 883 | + relativePath="streaming.md" |
| 884 | + threadRef={threadRef} |
| 885 | + workspaceMutationId={null} |
| 886 | + />, |
| 887 | + ); |
| 888 | + |
| 889 | + let saved: ReturnType<typeof readingPosition>; |
| 890 | + { |
| 891 | + await using screen = await render(preview(chunks.length, false)); |
| 892 | + await expect.element(screen.getByText("Section 0", { exact: true })).toBeInTheDocument(); |
| 893 | + saved = await scrollDown(); |
| 894 | + } |
| 895 | + await using screen = await render(preview(3, true)); |
| 896 | + expect(document.querySelector(".scrollbar-gutter-both"), "no list before the chunk").toBeNull(); |
| 897 | + await screen.rerender(preview(chunks.length, false)); |
| 898 | + await expectReadingPosition(saved); |
| 899 | + }); |
| 900 | + |
| 901 | + it("keeps each thread's position when the same file stays open across threads", async () => { |
| 902 | + using _settings = setup(); |
| 903 | + using _style = withAppCss(); |
| 904 | + const { chunks, definitions } = splitMarkdown(sections, 200); |
| 905 | + const preview = (threadId: string) => |
| 906 | + sized( |
| 907 | + <ChunkedMarkdown |
| 908 | + chunks={chunks} |
| 909 | + definitions={definitions} |
| 910 | + cwd="/workspace" |
| 911 | + relativePath="shared.md" |
| 912 | + threadRef={{ environmentId, threadId: ThreadId.make(threadId) }} |
| 913 | + workspaceMutationId={null} |
| 914 | + />, |
| 915 | + ); |
| 916 | + |
| 917 | + await using screen = await render(preview("first")); |
| 918 | + await expect.element(screen.getByText("Section 0", { exact: true })).toBeInTheDocument(); |
| 919 | + const saved = await scrollDown(); |
| 920 | + await screen.rerender(preview("second")); |
| 921 | + await vi.waitFor(() => expect(readingPosition()?.heading).toBe("Section 0")); |
| 922 | + await screen.rerender(preview("first")); |
| 923 | + await expectReadingPosition(saved); |
| 924 | + }); |
| 925 | +}); |