@@ -522,7 +522,7 @@ describe("nodes camera helpers", () => {
522522 expect ( tracked . wasCanceled ( ) ) . toBe ( true ) ;
523523 } ) ;
524524
525- it ( "removes partially written file when url stream fails" , async ( ) => {
525+ it ( "preserves an existing file when url stream fails" , async ( ) => {
526526 const stream = new ReadableStream < Uint8Array > ( {
527527 start ( controller ) {
528528 controller . enqueue ( new TextEncoder ( ) . encode ( "partial" ) ) ;
@@ -533,14 +533,51 @@ describe("nodes camera helpers", () => {
533533
534534 await withCameraTempDir ( async ( dir ) => {
535535 const out = path . join ( dir , "broken.bin" ) ;
536+ const sentinel = Buffer . from ( "existing-camera" ) ;
537+ await fs . writeFile ( out , sentinel ) ;
538+ await fs . chmod ( out , 0o640 ) ;
539+
536540 await expect (
537541 writeCameraPayloadToFile ( {
538542 filePath : out ,
539543 payload : { url : "https://198.51.100.42/broken.bin" } ,
540544 expectedHost : "198.51.100.42" ,
541545 } ) ,
542546 ) . rejects . toThrow ( / s t r e a m e x p l o d e d / i) ;
543- await expectPathMissing ( out ) ;
547+ await expect ( fs . readFile ( out ) ) . resolves . toEqual ( sentinel ) ;
548+ if ( process . platform !== "win32" ) {
549+ expect ( ( await fs . stat ( out ) ) . mode & 0o777 ) . toBe ( 0o640 ) ;
550+ }
551+ expect ( await fs . readdir ( dir ) ) . toEqual ( [ "broken.bin" ] ) ;
552+ } ) ;
553+ } ) ;
554+
555+ it ( "rejects a url stream that closes without data" , async ( ) => {
556+ const stream = new ReadableStream < Uint8Array > ( {
557+ start ( controller ) {
558+ controller . close ( ) ;
559+ } ,
560+ } ) ;
561+ stubFetchResponse ( new Response ( stream , { status : 200 } ) ) ;
562+
563+ await withCameraTempDir ( async ( dir ) => {
564+ const out = path . join ( dir , "empty.bin" ) ;
565+ const sentinel = Buffer . from ( "existing-camera" ) ;
566+ await fs . writeFile ( out , sentinel ) ;
567+ await fs . chmod ( out , 0o640 ) ;
568+
569+ await expect (
570+ writeCameraPayloadToFile ( {
571+ filePath : out ,
572+ payload : { url : "https://198.51.100.42/empty.bin" } ,
573+ expectedHost : "198.51.100.42" ,
574+ } ) ,
575+ ) . rejects . toThrow ( / e m p t y d o w n l o a d / i) ;
576+ await expect ( fs . readFile ( out ) ) . resolves . toEqual ( sentinel ) ;
577+ if ( process . platform !== "win32" ) {
578+ expect ( ( await fs . stat ( out ) ) . mode & 0o777 ) . toBe ( 0o640 ) ;
579+ }
580+ expect ( await fs . readdir ( dir ) ) . toEqual ( [ "empty.bin" ] ) ;
544581 } ) ;
545582 } ) ;
546583} ) ;
0 commit comments