@@ -148,6 +148,63 @@ test("tryRunAttachmentCommand dispatches upload subcommands from urls", async ()
148148 } ) ;
149149} ) ;
150150
151+ test ( "tryRunAttachmentCommand infers media type for local file uploads" , async ( ) => {
152+ silenceStdout ( ) ;
153+
154+ const uploadAttachmentForConsole = vi . fn ( ) . mockResolvedValue ( {
155+ data : {
156+ metadata : {
157+ name : "attachment-1" ,
158+ } ,
159+ spec : {
160+ displayName : "image.png" ,
161+ } ,
162+ status : {
163+ permalink : "https://example.com/image.png" ,
164+ } ,
165+ } ,
166+ } ) ;
167+ const runtimeMock = {
168+ getClientsForOptions : vi . fn ( ) . mockResolvedValue ( {
169+ profile : {
170+ baseUrl : "https://example.com" ,
171+ } ,
172+ clients : {
173+ axios : { } ,
174+ } ,
175+ } ) ,
176+ } ;
177+
178+ const { AttachmentV1alpha1ConsoleApi } = await import ( "@halo-dev/api-client" ) ;
179+ vi . spyOn ( AttachmentV1alpha1ConsoleApi . prototype , "uploadAttachmentForConsole" ) . mockImplementation (
180+ uploadAttachmentForConsole ,
181+ ) ;
182+
183+ const fsPromises = await import ( "node:fs/promises" ) ;
184+ const tempFilePath = "/tmp/halo-cli-attachment-upload-image.png" ;
185+ await fsPromises . writeFile ( tempFilePath , Buffer . from ( "png" ) ) ;
186+
187+ try {
188+ await expect (
189+ tryRunAttachmentCommand (
190+ [ "attachment" , "upload" , "--file" , tempFilePath , "--json" ] ,
191+ runtimeMock as never ,
192+ ) ,
193+ ) . resolves . toBe ( true ) ;
194+ } finally {
195+ await fsPromises . unlink ( tempFilePath ) . catch ( ( ) => undefined ) ;
196+ }
197+
198+ expect ( runtimeMock . getClientsForOptions ) . toHaveBeenCalledOnce ( ) ;
199+ expect ( uploadAttachmentForConsole ) . toHaveBeenCalledOnce ( ) ;
200+ expect ( uploadAttachmentForConsole . mock . calls [ 0 ] ?. [ 0 ] ) . toMatchObject ( {
201+ file : expect . objectContaining ( {
202+ name : "halo-cli-attachment-upload-image.png" ,
203+ type : "image/png" ,
204+ } ) ,
205+ } ) ;
206+ } ) ;
207+
151208test ( "tryRunAttachmentCommand dispatches delete subcommands" , async ( ) => {
152209 silenceStdout ( ) ;
153210
0 commit comments