@@ -2411,6 +2411,208 @@ it("keeps a workspace PDF open across URL renewals and reloads it when the file
24112411 URL . revokeObjectURL ( edited ) ;
24122412 }
24132413} ) ;
2414+ function planningPdf (
2415+ titles = [ "Quarterly outlook" , "Delivery milestones" , "Budget and risks" ] ,
2416+ firstHeight = 140 ,
2417+ pageHeight = 792 ,
2418+ ) {
2419+ const objects = [
2420+ "<< /Type /Catalog /Pages 2 0 R >>" ,
2421+ `<< /Type /Pages /Count ${ titles . length } /Kids [${ titles . map ( ( _ , index ) => `${ 3 + index * 2 } 0 R` ) . join ( " " ) } ] >>` ,
2422+ ] ;
2423+ const font = 3 + titles . length * 2 ;
2424+ for ( const [ index , title ] of titles . entries ( ) ) {
2425+ const text = `/GS1 gs BT /F1 24 Tf 30 ${ index === 0 ? 70 : 700 } Td ${ index === 0 ? "(Quarter) Tj /F2 24 Tf (ly outlook) Tj" : `(${ title } ) Tj` } ET` ;
2426+ objects . push (
2427+ `<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 ${ index === 0 ? firstHeight : pageHeight } ] /Rotate ${ index === 2 ? 90 : 0 } /Resources << /Font << /F1 ${ font } 0 R /F2 ${ font + 1 } 0 R >> /ExtGState << /GS1 << /TR << /FunctionType 2 /Domain [0 1] /C0 [0] /C1 [1] /N 2 >> >> >> >> /Contents ${ 4 + index * 2 } 0 R >>` ,
2428+ `<< /Length ${ text . length } >>\nstream\n${ text } \nendstream` ,
2429+ ) ;
2430+ }
2431+ objects . push ( "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>" ) ;
2432+ objects . push ( "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica-Bold >>" ) ;
2433+ let source = "%PDF-1.4\n" ;
2434+ const offsets = [ 0 ] ;
2435+ for ( const [ index , object ] of objects . entries ( ) ) {
2436+ offsets . push ( source . length ) ;
2437+ source += `${ index + 1 } 0 obj\n${ object } \nendobj\n` ;
2438+ }
2439+ const xref = source . length ;
2440+ source += `xref\n0 ${ objects . length + 1 } \n0000000000 65535 f \n${ offsets
2441+ . slice ( 1 )
2442+ . map ( ( offset ) => `${ String ( offset ) . padStart ( 10 , "0" ) } 00000 n \n` )
2443+ . join ( "" ) } trailer\n<< /Size ${ objects . length + 1 } /Root 1 0 R >>\nstartxref\n${ xref } \n%%EOF`;
2444+ return new Blob ( [ source ] , { type : "application/pdf" } ) ;
2445+ }
2446+
2447+ async function workspacePdfPreview ( ) {
2448+ assetUrlState . mockReset ( ) ;
2449+ 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 preview = ( src : string , mutation : string , path = "/repo/planning.pdf" ) => {
2472+ assetUrlState . mockReturnValue ( {
2473+ _tag : "Success" ,
2474+ url : `${ src } #preview` ,
2475+ expiresAt : Date . now ( ) + 3_600_000 ,
2476+ } ) ;
2477+ return (
2478+ < div className = "flex h-[600px] w-[320px]" >
2479+ < WorkspaceBrowserPreview
2480+ environmentId = { EnvironmentId . make ( "test" ) }
2481+ threadRef = { { environmentId : EnvironmentId . make ( "test" ) , threadId : ThreadId . make ( "test" ) } }
2482+ draft = { false }
2483+ absolutePath = { path }
2484+ workspaceRoot = "/repo"
2485+ title = "Planning PDF"
2486+ workspaceMutationId = { mutation }
2487+ />
2488+ </ div >
2489+ ) ;
2490+ } ;
2491+ return {
2492+ preview,
2493+ urls,
2494+ [ Symbol . dispose ] ( ) {
2495+ native . mockRestore ( ) ;
2496+ urls [ Symbol . dispose ] ( ) ;
2497+ } ,
2498+ } ;
2499+ }
2500+
2501+ function pdfScroll ( screen : Screen ) {
2502+ const region = ( ) => screen . container . querySelector < HTMLElement > ( '[role="region"]' ) ! ;
2503+ const canvas = ( page : number ) =>
2504+ region ( ) . querySelector < HTMLCanvasElement > ( `canvas[aria-label="Page ${ page } "]` ) ! ;
2505+ const offset = ( page : number ) =>
2506+ region ( ) . getBoundingClientRect ( ) . top - canvas ( page ) . getBoundingClientRect ( ) . top ;
2507+ const scrollTo = ( options : ScrollToOptions ) =>
2508+ act ( async ( ) => {
2509+ const node = region ( ) ;
2510+ const scrolled = new Promise < void > ( ( resolve ) =>
2511+ node . addEventListener ( "scroll" , ( ) => resolve ( ) , { once : true } ) ,
2512+ ) ;
2513+ node . scrollTo ( options ) ;
2514+ await scrolled ;
2515+ } ) ;
2516+ return { region, canvas, offset, scrollTo } ;
2517+ }
2518+
2519+ it ( "keeps workspace PDF scroll and zoom after reloads" , async ( ) => {
2520+ using fixture = await workspacePdfPreview ( ) ;
2521+ const { preview, urls } = fixture ;
2522+ await using screen = await render ( preview ( urls . original , "one" ) ) ;
2523+ await expect . element ( screen . getByText ( "1 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2524+ await screen . getByRole ( "button" , { name : "Next page" , exact : true } ) . click ( ) ;
2525+ await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "2 / 3 pages" ) ) ;
2526+ await screen . getByRole ( "button" , { name : "Next page" , exact : true } ) . click ( ) ;
2527+ await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "3 / 3 pages" ) ) ;
2528+ for ( let index = 0 ; index < 4 ; index ++ ) {
2529+ await screen . getByRole ( "button" , { name : "Zoom in" , exact : true } ) . click ( ) ;
2530+ }
2531+ const { region, offset, scrollTo } = pdfScroll ( screen ) ;
2532+ const zoom = screen . container . textContent ! . match ( / \d + % / ) ! [ 0 ] ;
2533+ const top = region ( ) . scrollTop ;
2534+ await scrollTo ( { top : top + 100 , left : 80 } ) ;
2535+ await vi . waitFor ( ( ) => {
2536+ expect ( region ( ) . scrollTop ) . toBeCloseTo ( top + 100 , 0 ) ;
2537+ expect ( region ( ) . scrollLeft ) . toBe ( 80 ) ;
2538+ } ) ;
2539+ const savedOffset = offset ( 3 ) ;
2540+ await screen . rerender ( preview ( urls . updated , "two" ) ) ;
2541+ await expect . element ( screen . getByText ( "3 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2542+ await vi . waitFor ( ( ) => {
2543+ expect ( screen . container . querySelector ( '[role="region"]' ) ?. textContent ) . toContain (
2544+ "Updated budget" ,
2545+ ) ;
2546+ expect ( screen . container . textContent ) . toContain ( zoom ) ;
2547+ expect ( region ( ) . scrollLeft ) . toBe ( 80 ) ;
2548+ expect ( offset ( 3 ) ) . toBeCloseTo ( savedOffset , 0 ) ;
2549+ } ) ;
2550+ await screen . getByRole ( "button" , { name : "Previous page" , exact : true } ) . click ( ) ;
2551+ await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "2 / 3 pages" ) ) ;
2552+ await screen . getByRole ( "button" , { name : "Fit width" , exact : true } ) . click ( ) ;
2553+ await scrollTo ( { top : region ( ) . scrollTop + 30 } ) ;
2554+ const fitOffset = offset ( 2 ) ;
2555+ await screen . rerender ( preview ( urls . original , "fit-width" ) ) ;
2556+ await expect . element ( screen . getByText ( "2 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2557+ await vi . waitFor ( ( ) => {
2558+ expect ( screen . container . textContent ) . toContain ( "Fit width" ) ;
2559+ expect ( region ( ) . scrollLeft ) . toBe ( 0 ) ;
2560+ expect ( offset ( 2 ) ) . toBeCloseTo ( fitOffset , 0 ) ;
2561+ } ) ;
2562+ await screen . getByRole ( "button" , { name : "Previous page" , exact : true } ) . click ( ) ;
2563+ await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "1 / 3 pages" ) ) ;
2564+ await screen . getByRole ( "button" , { name : "Zoom in" , exact : true } ) . click ( ) ;
2565+ const firstPageZoom = screen . container . textContent ! . match ( / \d + % / ) ! [ 0 ] ;
2566+ await screen . rerender ( preview ( urls . original , "first-page" ) ) ;
2567+ await expect . element ( screen . getByText ( "1 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2568+ await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( firstPageZoom ) ) ;
2569+ await screen . getByRole ( "button" , { name : "Next page" , exact : true } ) . click ( ) ;
2570+ await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "2 / 3 pages" ) ) ;
2571+ await screen . rerender ( preview ( urls . original , "one" , "/repo/another.pdf" ) ) ;
2572+ await expect . element ( screen . getByText ( "1 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2573+ await vi . waitFor ( ( ) => {
2574+ expect ( screen . container . textContent ) . toContain ( "Fit width" ) ;
2575+ expect ( region ( ) . scrollTop ) . toBe ( 0 ) ;
2576+ expect ( region ( ) . scrollLeft ) . toBe ( 0 ) ;
2577+ } ) ;
2578+ } ) ;
2579+
2580+ it ( "clamps workspace PDF scroll when pages shrink or disappear" , async ( ) => {
2581+ using fixture = await workspacePdfPreview ( ) ;
2582+ const { preview, urls } = fixture ;
2583+ await using screen = await render ( preview ( urls . original , "one" ) ) ;
2584+ await expect . element ( screen . getByText ( "1 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2585+ await screen . getByRole ( "button" , { name : "Next page" , exact : true } ) . click ( ) ;
2586+ await expect . element ( screen . getByText ( "2 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2587+ for ( let index = 0 ; index < 4 ; index ++ ) {
2588+ await screen . getByRole ( "button" , { name : "Zoom in" , exact : true } ) . click ( ) ;
2589+ }
2590+ const { region, canvas, offset, scrollTo } = pdfScroll ( screen ) ;
2591+ const zoom = screen . container . textContent ! . match ( / \d + % / ) ! [ 0 ] ;
2592+ const secondPageTop = region ( ) . scrollTop ;
2593+ await scrollTo ( { top : secondPageTop + 600 , left : 80 } ) ;
2594+ await vi . waitFor ( ( ) => expect ( region ( ) . scrollTop ) . toBeCloseTo ( secondPageTop + 600 , 0 ) ) ;
2595+ await screen . rerender ( preview ( urls . shrunk , "smaller-page" ) ) ;
2596+ await expect . element ( screen . getByText ( "2 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2597+ await vi . waitFor ( ( ) => {
2598+ expect ( screen . container . textContent ) . toContain ( zoom ) ;
2599+ expect ( region ( ) . scrollLeft ) . toBe ( 80 ) ;
2600+ expect ( offset ( 2 ) ) . toBeGreaterThan ( 0 ) ;
2601+ expect ( offset ( 2 ) ) . toBeLessThan ( canvas ( 2 ) . getBoundingClientRect ( ) . height ) ;
2602+ } ) ;
2603+ await screen . rerender ( preview ( urls . shorter , "three" ) ) ;
2604+ await expect . element ( screen . getByText ( "2 / 2 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2605+ await vi . waitFor ( ( ) => {
2606+ expect ( screen . container . querySelector ( '[role="region"]' ) ?. textContent ) . toContain (
2607+ "Updated milestones" ,
2608+ ) ;
2609+ } ) ;
2610+ await screen . rerender ( preview ( urls . single , "four" ) ) ;
2611+ await expect . element ( screen . getByText ( "1 / 1 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2612+ await screen . rerender ( preview ( urls . original , "five" ) ) ;
2613+ await expect . element ( screen . getByText ( "1 / 3 pages" , { exact : true } ) ) . toBeVisible ( ) ;
2614+ } ) ;
2615+
24142616it ( "unwraps one text attachment without turning off the word wrap setting" , async ( ) => {
24152617 const file = new Blob ( [ "a long line" ] , { type : "text/plain" } ) ;
24162618 await using screen = await render (
0 commit comments