@@ -6,6 +6,22 @@ import type { ToolContext } from '../../src/types.js';
66import fs from 'node:fs' ;
77import path from 'node:path' ;
88
9+ function getBuildBody ( ctx : ToolContext ) : FormData | Record < string , unknown > {
10+ const call = ( ctx . client . post as any ) . mock . calls . at ( - 1 ) ;
11+ expect ( call [ 0 ] ) . toBe ( 'build' ) ;
12+ return call [ 1 ] ;
13+ }
14+
15+ function getInstructions ( body : FormData | Record < string , unknown > ) : Record < string , any > {
16+ if ( body instanceof FormData ) {
17+ const instructionsRaw = body . get ( 'instructions' ) ;
18+ expect ( typeof instructionsRaw ) . toBe ( 'string' ) ;
19+ return JSON . parse ( instructionsRaw as string ) ;
20+ }
21+
22+ return body as Record < string , any > ;
23+ }
24+
925describe ( 'nutrient_convert_to_pdf' , ( ) => {
1026 let ctx : ToolContext ;
1127
@@ -26,7 +42,10 @@ describe('nutrient_convert_to_pdf', () => {
2642 ) ;
2743 expect ( result . success ) . toBe ( true ) ;
2844 expect ( result . output ) . toContain ( 'output.pdf' ) ;
29- expect ( ctx . client . post ) . toHaveBeenCalledWith ( 'build' , expect . anything ( ) ) ;
45+ const body = getBuildBody ( ctx ) ;
46+ const instructions = getInstructions ( body ) ;
47+ expect ( instructions . parts [ 0 ] . file ) . toBeTruthy ( ) ;
48+ expect ( instructions . parts [ 0 ] . html ) . toBeUndefined ( ) ;
3049 expect ( fs . existsSync ( path . join ( ctx . sandboxDir ! , 'output.pdf' ) ) ) . toBe ( true ) ;
3150 } ) ;
3251
@@ -41,8 +60,8 @@ describe('nutrient_convert_to_pdf', () => {
4160 expect ( call [ 0 ] ) . toBe ( 'build' ) ;
4261 } ) ;
4362
44- it ( 'forwards HTML layout options ' , async ( ) => {
45- writeSandboxFile ( ctx . sandboxDir ! , 'page.html' ) ;
63+ it ( 'uses part.html for local HTML and keeps htmlLayout on part.layout ' , async ( ) => {
64+ writeSandboxFile ( ctx . sandboxDir ! , 'page.html' , '<html><body>test</body></html>' ) ;
4665 await convertToPdfTool . execute (
4766 {
4867 filePath : 'page.html' ,
@@ -51,7 +70,35 @@ describe('nutrient_convert_to_pdf', () => {
5170 } ,
5271 ctx ,
5372 ) ;
54- expect ( ctx . client . post ) . toHaveBeenCalled ( ) ;
73+
74+ const body = getBuildBody ( ctx ) ;
75+ expect ( body ) . toBeInstanceOf ( FormData ) ;
76+
77+ const instructions = getInstructions ( body ) ;
78+ expect ( instructions . parts [ 0 ] . html ) . toBeTruthy ( ) ;
79+ expect ( instructions . parts [ 0 ] . file ) . toBeUndefined ( ) ;
80+ expect ( instructions . parts [ 0 ] . layout ) . toEqual ( { orientation : 'landscape' , size : 'A4' } ) ;
81+
82+ const htmlPart = ( body as FormData ) . get ( instructions . parts [ 0 ] . html ) ;
83+ expect ( htmlPart ) . toBeTruthy ( ) ;
84+ expect ( typeof htmlPart ) . not . toBe ( 'string' ) ;
85+ if ( htmlPart && typeof htmlPart !== 'string' ) {
86+ expect ( htmlPart . type ) . toBe ( 'text/html' ) ;
87+ }
88+ } ) ;
89+
90+ it ( 'uses part.html for HTML URLs' , async ( ) => {
91+ const htmlUrl = 'https://example.com/page.html?utm=test' ;
92+ await convertToPdfTool . execute (
93+ { filePath : htmlUrl , outputPath : 'out.pdf' } ,
94+ ctx ,
95+ ) ;
96+
97+ const body = getBuildBody ( ctx ) ;
98+ expect ( body ) . not . toBeInstanceOf ( FormData ) ;
99+ const instructions = getInstructions ( body ) ;
100+ expect ( instructions . parts [ 0 ] . html ) . toBe ( htmlUrl ) ;
101+ expect ( instructions . parts [ 0 ] . file ) . toBeUndefined ( ) ;
55102 } ) ;
56103
57104 it ( 'forwards page ranges' , async ( ) => {
0 commit comments