@@ -5,16 +5,29 @@ import { describe, expect, it, vi } from "vite-plus/test";
55import { page , userEvent } from "vite-plus/test/browser" ;
66import "~/index.css" ;
77
8- const { refresh, assetUrlState } = vi . hoisted ( ( ) => ( {
8+ const { refresh, assetUrlState, pdfOpens } = vi . hoisted ( ( ) => ( {
99 refresh : vi . fn < ( ) => Promise < string | null > > ( ) ,
1010 assetUrlState : vi . fn < ( ) => import ( "~/assets/assetUrls" ) . AssetUrlState > ( ) ,
11+ pdfOpens : [ ] as Array < ( task : import ( "pdfjs-dist" ) . PDFDocumentLoadingTask ) => void > ,
1112} ) ) ;
1213
1314vi . mock ( import ( "~/assets/assetUrls" ) , async ( original ) => ( {
1415 ...( await original ( ) ) ,
1516 useAssetUrlRefresh : ( ) => refresh ,
1617 useAssetUrlState : assetUrlState ,
1718} ) ) ;
19+ // Hands each document pdf.js opens to the PDF test, which awaits its load instead of a poll window.
20+ vi . mock ( import ( "./pdfDocument" ) , async ( original ) => {
21+ const actual = await original ( ) ;
22+ return {
23+ ...actual ,
24+ openPdf : ( bytes ) => {
25+ const task = actual . openPdf ( bytes ) ;
26+ pdfOpens . shift ( ) ?.( task ) ;
27+ return task ;
28+ } ,
29+ } ;
30+ } ) ;
1831// The real source view highlights through a worker pool; the text it receives is what matters here.
1932// Never load the original: its highlighter graph can outlast waitFor on a cold CI browser.
2033vi . mock ( import ( "./ReadOnlySourcePreview" ) , ( ) => ( {
@@ -55,6 +68,7 @@ import BinaryViewer from "./BinaryViewer";
5568import { decodeImagePreview } from "./decodeImagePreview" ;
5669import type { BinaryWorkerResult } from "./binaryPreviewProtocol" ;
5770import type { TextPreviewResult } from "./largeTextProtocol" ;
71+ import type { ReactElement } from "react" ;
5872
5973// Real worker imports and WASM initialization can exceed waitFor's default second
6074// when the full browser project runs concurrently on CI.
@@ -2444,31 +2458,28 @@ function planningPdf(
24442458 return new Blob ( [ source ] , { type : "application/pdf" } ) ;
24452459}
24462460
2447- async function workspacePdfPreview ( ) {
2461+ // Resolves once pdf.js has parsed the next document and every page size the viewer reads.
2462+ function nextPdfLoad ( ) {
2463+ return new Promise < import ( "pdfjs-dist" ) . PDFDocumentLoadingTask > ( ( resolve ) =>
2464+ pdfOpens . push ( resolve ) ,
2465+ ) . then ( async ( task ) => {
2466+ const document = await task . promise ;
2467+ await Promise . all (
2468+ Array . from ( { length : document . numPages } , ( _ , index ) => document . getPage ( index + 1 ) ) ,
2469+ ) ;
2470+ } ) ;
2471+ }
2472+
2473+ it ( "keeps a workspace PDF's page, offset and zoom when a new revision reloads it" , async ( ) => {
24482474 assetUrlState . mockReset ( ) ;
2475+ pdfOpens . length = 0 ;
24492476 const { WorkspaceBrowserPreview } = await import ( "./FilePreviewPanel" ) ;
2450- const native = vi . spyOn ( navigator , "pdfViewerEnabled" , "get" ) . mockReturnValue ( false ) ;
2451- const urls = ( ( ) => {
2452- const original = URL . createObjectURL ( planningPdf ( ) ) ;
2453- const updated = URL . createObjectURL (
2454- planningPdf ( [ "Quarterly outlook" , "Delivery milestones" , "Updated budget" ] , 500 ) ,
2455- ) ;
2456- const shorter = URL . createObjectURL ( planningPdf ( [ "Quarterly outlook" , "Updated milestones" ] ) ) ;
2457- const shrunk = URL . createObjectURL (
2458- planningPdf ( [ "Quarterly outlook" , "Small page" , "Budget and risks" ] , 500 , 200 ) ,
2459- ) ;
2460- const single = URL . createObjectURL ( planningPdf ( [ "Quarterly outlook" ] ) ) ;
2461- return {
2462- original,
2463- updated,
2464- shorter,
2465- shrunk,
2466- single,
2467- [ Symbol . dispose ] : ( ) =>
2468- [ original , updated , shorter , shrunk , single ] . forEach ( URL . revokeObjectURL ) ,
2469- } ;
2470- } ) ( ) ;
2471- const threadId = ThreadId . make ( urls . original ) ;
2477+ using _native = vi . spyOn ( navigator , "pdfViewerEnabled" , "get" ) . mockReturnValue ( false ) ;
2478+ const original = URL . createObjectURL ( planningPdf ( ) ) ;
2479+ // A taller first page moves page 2, so only a page-relative restore lands on the same spot.
2480+ const updated = URL . createObjectURL (
2481+ planningPdf ( [ "Quarterly outlook" , "Delivery milestones" , "Updated budget" ] , 500 ) ,
2482+ ) ;
24722483 const preview = ( src : string , mutation : string , path = "/repo/planning.pdf" ) => {
24732484 assetUrlState . mockReturnValue ( {
24742485 _tag : "Success" ,
@@ -2479,7 +2490,10 @@ async function workspacePdfPreview() {
24792490 < div className = "flex h-[600px] w-[320px]" >
24802491 < WorkspaceBrowserPreview
24812492 environmentId = { EnvironmentId . make ( "test" ) }
2482- threadRef = { { environmentId : EnvironmentId . make ( "test" ) , threadId } }
2493+ threadRef = { {
2494+ environmentId : EnvironmentId . make ( "test" ) ,
2495+ threadId : ThreadId . make ( original ) ,
2496+ } }
24832497 draft = { false }
24842498 absolutePath = { path }
24852499 workspaceRoot = "/repo"
@@ -2489,131 +2503,50 @@ async function workspacePdfPreview() {
24892503 </ div >
24902504 ) ;
24912505 } ;
2492- return {
2493- preview,
2494- urls,
2495- [ Symbol . dispose ] ( ) {
2496- native . mockRestore ( ) ;
2497- urls [ Symbol . dispose ] ( ) ;
2498- } ,
2506+ const reload = async ( screen : Screen , element : ReactElement ) => {
2507+ const loaded = nextPdfLoad ( ) ;
2508+ await screen . rerender ( element ) ;
2509+ await loaded ;
24992510 } ;
2500- }
2501-
2502- function pdfScroll ( screen : Screen ) {
2503- const region = ( ) => screen . container . querySelector < HTMLElement > ( '[role="region"]' ) ! ;
2504- const canvas = ( page : number ) =>
2505- region ( ) . querySelector < HTMLCanvasElement > ( `canvas[aria-label="Page ${ page } "]` ) ! ;
2506- const offset = ( page : number ) =>
2507- region ( ) . getBoundingClientRect ( ) . top - canvas ( page ) . getBoundingClientRect ( ) . top ;
2508- const scrollTo = ( options : ScrollToOptions ) =>
2509- act ( async ( ) => {
2510- const node = region ( ) ;
2511- const scrolled = new Promise < void > ( ( resolve ) =>
2512- node . addEventListener ( "scroll" , ( ) => resolve ( ) , { once : true } ) ,
2511+ try {
2512+ const loaded = nextPdfLoad ( ) ;
2513+ await using screen = await render ( preview ( original , "one" ) ) ;
2514+ await loaded ;
2515+ await expect . element ( screen . getByText ( "1 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2516+ using workers = vi . spyOn ( window , "Worker" ) ;
2517+ const region = ( ) => screen . getByRole ( "region" , { name : "Planning PDF" } ) . element ( ) ;
2518+ const offset = ( ) =>
2519+ region ( ) . getBoundingClientRect ( ) . top -
2520+ region ( ) . querySelector ( 'canvas[aria-label="Page 2"]' ) ! . getBoundingClientRect ( ) . top ;
2521+ await screen . getByRole ( "button" , { name : "Zoom in" , exact : true } ) . click ( ) ;
2522+ await screen . getByRole ( "button" , { name : "Next page" , exact : true } ) . click ( ) ;
2523+ await expect . element ( screen . getByText ( "2 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2524+ const zoom = screen . container . textContent ! . match ( / \d + % / ) ! [ 0 ] ;
2525+ const top = region ( ) . scrollTop + 100 ;
2526+ await act ( async ( ) => {
2527+ const scrolled = new Promise ( ( resolve ) =>
2528+ region ( ) . addEventListener ( "scroll" , resolve , { once : true } ) ,
25132529 ) ;
2514- node . scrollTo ( options ) ;
2530+ region ( ) . scrollTo ( { top , left : 80 } ) ;
25152531 await scrolled ;
25162532 } ) ;
2517- return { region, canvas, offset, scrollTo } ;
2518- }
2533+ const savedOffset = offset ( ) ;
25192534
2520- it ( "keeps workspace PDF scroll and zoom after reloads" , async ( ) => {
2521- using fixture = await workspacePdfPreview ( ) ;
2522- const { preview, urls } = fixture ;
2523- await using screen = await render ( preview ( urls . original , "one" ) ) ;
2524- await expect . element ( screen . getByText ( "1 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2525- using workers = vi . spyOn ( window , "Worker" ) ;
2526- await screen . getByRole ( "button" , { name : "Next page" , exact : true } ) . click ( ) ;
2527- await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "2 / 3 pages" ) ) ;
2528- await screen . getByRole ( "button" , { name : "Next page" , exact : true } ) . click ( ) ;
2529- await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "3 / 3 pages" ) ) ;
2530- for ( let index = 0 ; index < 4 ; index ++ ) {
2531- await screen . getByRole ( "button" , { name : "Zoom in" , exact : true } ) . click ( ) ;
2532- }
2533- const { region, offset, scrollTo } = pdfScroll ( screen ) ;
2534- const zoom = screen . container . textContent ! . match ( / \d + % / ) ! [ 0 ] ;
2535- const top = region ( ) . scrollTop ;
2536- await scrollTo ( { top : top + 100 , left : 80 } ) ;
2537- await vi . waitFor ( ( ) => {
2538- expect ( region ( ) . scrollTop ) . toBeCloseTo ( top + 100 , 0 ) ;
2539- expect ( region ( ) . scrollLeft ) . toBe ( 80 ) ;
2540- } ) ;
2541- const savedOffset = offset ( 3 ) ;
2542- await screen . rerender ( preview ( urls . updated , "two" ) ) ;
2543- await expect . element ( screen . getByText ( "3 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2544- await vi . waitFor ( ( ) => {
2545- expect ( screen . container . querySelector ( '[role="region"]' ) ?. textContent ) . toContain (
2546- "Updated budget" ,
2547- ) ;
2535+ await reload ( screen , preview ( updated , "two" ) ) ;
2536+ await expect . element ( screen . getByText ( "2 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
25482537 expect ( screen . container . textContent ) . toContain ( zoom ) ;
25492538 expect ( region ( ) . scrollLeft ) . toBe ( 80 ) ;
2550- expect ( offset ( 3 ) ) . toBeCloseTo ( savedOffset , 0 ) ;
2551- } ) ;
2552- await screen . getByRole ( "button" , { name : "Previous page" , exact : true } ) . click ( ) ;
2553- await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "2 / 3 pages" ) ) ;
2554- await screen . getByRole ( "button" , { name : "Fit width" , exact : true } ) . click ( ) ;
2555- await scrollTo ( { top : region ( ) . scrollTop + 30 } ) ;
2556- const fitOffset = offset ( 2 ) ;
2557- await screen . rerender ( preview ( urls . original , "fit-width" ) ) ;
2558- await expect . element ( screen . getByText ( "2 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2559- await vi . waitFor ( ( ) => {
2560- expect ( screen . container . textContent ) . toContain ( "Fit width" ) ;
2561- expect ( region ( ) . scrollLeft ) . toBe ( 0 ) ;
2562- expect ( offset ( 2 ) ) . toBeCloseTo ( fitOffset , 0 ) ;
2563- } ) ;
2564- await screen . getByRole ( "button" , { name : "Previous page" , exact : true } ) . click ( ) ;
2565- await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "1 / 3 pages" ) ) ;
2566- await screen . getByRole ( "button" , { name : "Zoom in" , exact : true } ) . click ( ) ;
2567- const firstPageZoom = screen . container . textContent ! . match ( / \d + % / ) ! [ 0 ] ;
2568- await screen . rerender ( preview ( urls . original , "first-page" ) ) ;
2569- await expect . element ( screen . getByText ( "1 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2570- await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( firstPageZoom ) ) ;
2571- await screen . getByRole ( "button" , { name : "Next page" , exact : true } ) . click ( ) ;
2572- await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "2 / 3 pages" ) ) ;
2573- await screen . rerender ( preview ( urls . original , "one" , "/repo/another.pdf" ) ) ;
2574- await expect . element ( screen . getByText ( "1 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2575- await vi . waitFor ( ( ) => {
2539+ expect ( offset ( ) ) . toBeCloseTo ( savedOffset , 0 ) ;
2540+ expect ( workers ) . not . toHaveBeenCalled ( ) ;
2541+
2542+ await reload ( screen , preview ( updated , "two" , "/repo/another.pdf" ) ) ;
2543+ await expect . element ( screen . getByText ( "1 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
25762544 expect ( screen . container . textContent ) . toContain ( "Fit width" ) ;
25772545 expect ( region ( ) . scrollTop ) . toBe ( 0 ) ;
2578- expect ( region ( ) . scrollLeft ) . toBe ( 0 ) ;
2579- } ) ;
2580- expect ( workers ) . not . toHaveBeenCalled ( ) ;
2581- } ) ;
2582-
2583- it ( "clamps workspace PDF scroll when pages shrink or disappear" , async ( ) => {
2584- using fixture = await workspacePdfPreview ( ) ;
2585- const { preview, urls } = fixture ;
2586- await using screen = await render ( preview ( urls . original , "one" ) ) ;
2587- await expect . element ( screen . getByText ( "1 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2588- await screen . getByRole ( "button" , { name : "Next page" , exact : true } ) . click ( ) ;
2589- await expect . element ( screen . getByText ( "2 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2590- for ( let index = 0 ; index < 4 ; index ++ ) {
2591- await screen . getByRole ( "button" , { name : "Zoom in" , exact : true } ) . click ( ) ;
2546+ } finally {
2547+ URL . revokeObjectURL ( original ) ;
2548+ URL . revokeObjectURL ( updated ) ;
25922549 }
2593- const { region, canvas, offset, scrollTo } = pdfScroll ( screen ) ;
2594- const zoom = screen . container . textContent ! . match ( / \d + % / ) ! [ 0 ] ;
2595- const secondPageTop = region ( ) . scrollTop ;
2596- await scrollTo ( { top : secondPageTop + 600 , left : 80 } ) ;
2597- await vi . waitFor ( ( ) => expect ( region ( ) . scrollTop ) . toBeCloseTo ( secondPageTop + 600 , 0 ) ) ;
2598- await screen . rerender ( preview ( urls . shrunk , "smaller-page" ) ) ;
2599- await expect . element ( screen . getByText ( "2 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2600- await vi . waitFor ( ( ) => {
2601- expect ( screen . container . textContent ) . toContain ( zoom ) ;
2602- expect ( region ( ) . scrollLeft ) . toBe ( 80 ) ;
2603- expect ( offset ( 2 ) ) . toBeGreaterThan ( 0 ) ;
2604- expect ( offset ( 2 ) ) . toBeLessThan ( canvas ( 2 ) . getBoundingClientRect ( ) . height ) ;
2605- } ) ;
2606- await screen . rerender ( preview ( urls . shorter , "three" ) ) ;
2607- await expect . element ( screen . getByText ( "2 / 2 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2608- await vi . waitFor ( ( ) => {
2609- expect ( screen . container . querySelector ( '[role="region"]' ) ?. textContent ) . toContain (
2610- "Updated milestones" ,
2611- ) ;
2612- } ) ;
2613- await screen . rerender ( preview ( urls . single , "four" ) ) ;
2614- await expect . element ( screen . getByText ( "1 / 1 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2615- await screen . rerender ( preview ( urls . original , "five" ) ) ;
2616- await expect . element ( screen . getByText ( "1 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
26172550} ) ;
26182551
26192552it ( "unwraps one text attachment without turning off the word wrap setting" , async ( ) => {
0 commit comments