@@ -918,6 +918,152 @@ test('uploadArtifact uploads APK, AAB, and IPA files without wrapping them', asy
918918 }
919919} ) ;
920920
921+ /**
922+ * A daemon fake that answers every upload route and records the lease header each request carried,
923+ * keyed by `METHOD path`. The direct ticket points at `ticketOrigin` (the daemon's own by default).
924+ */
925+ async function startLeaseRecordingUploadServer ( options : {
926+ preflight : 'direct' | 'unsupported' ;
927+ ticketOrigin ?: ( baseUrl : string ) => string ;
928+ } ) : Promise < {
929+ baseUrl : string ;
930+ close : ( ) => Promise < void > ;
931+ leaseHeaders : Map < string , unknown > ;
932+ } > {
933+ const leaseHeaders = new Map < string , unknown > ( ) ;
934+ const server = await startServer ( async ( req , res ) => {
935+ leaseHeaders . set ( `${ req . method } ${ req . url } ` , req . headers [ 'x-agent-device-lease-id' ] ) ;
936+ await readRequestBody ( req ) ;
937+ if ( req . method === 'POST' && req . url === '/upload/preflight' ) {
938+ if ( options . preflight === 'unsupported' ) {
939+ res . statusCode = 404 ;
940+ res . end ( 'not found' ) ;
941+ return ;
942+ }
943+ const origin = ( options . ticketOrigin ?? ( ( baseUrl ) => baseUrl ) ) ( server . baseUrl ) ;
944+ sendJson ( res , {
945+ ok : true ,
946+ cacheHit : false ,
947+ uploadId : 'ticket-1' ,
948+ upload : { url : `${ origin } /upload/direct/ticket-1` , headers : { } } ,
949+ } ) ;
950+ return ;
951+ }
952+ if ( req . method === 'PUT' && req . url === '/upload/direct/ticket-1' ) {
953+ res . statusCode = 200 ;
954+ res . end ( 'ok' ) ;
955+ return ;
956+ }
957+ if ( req . method === 'POST' && ( req . url === '/upload/finalize' || req . url === '/upload' ) ) {
958+ sendJson ( res , { ok : true , uploadId : 'upload-done' } ) ;
959+ return ;
960+ }
961+ res . statusCode = 404 ;
962+ res . end ( 'not found' ) ;
963+ } ) ;
964+ return { ...server , leaseHeaders } ;
965+ }
966+
967+ test ( 'uploadArtifact names the held lease on preflight, direct upload and finalize' , async ( ) => {
968+ const artifactPath = createTempFile ( 'app.apk' , 'leased-direct-payload' ) ;
969+ const server = await startLeaseRecordingUploadServer ( { preflight : 'direct' } ) ;
970+ try {
971+ const uploadId = await uploadArtifact ( {
972+ localPath : artifactPath ,
973+ baseUrl : server . baseUrl ,
974+ token : TEST_TOKEN ,
975+ leaseId : 'lease-1' ,
976+ } ) ;
977+ assert . equal ( uploadId , 'upload-done' ) ;
978+ assert . deepEqual ( Object . fromEntries ( server . leaseHeaders ) , {
979+ 'POST /upload/preflight' : 'lease-1' ,
980+ 'PUT /upload/direct/ticket-1' : 'lease-1' ,
981+ 'POST /upload/finalize' : 'lease-1' ,
982+ } ) ;
983+ } finally {
984+ await server . close ( ) ;
985+ }
986+ } ) ;
987+
988+ test ( 'uploadArtifact names the held lease on the legacy upload' , async ( ) => {
989+ const artifactPath = createTempFile ( 'app.apk' , 'leased-legacy-payload' ) ;
990+ const server = await startLeaseRecordingUploadServer ( {
991+ preflight : 'unsupported' ,
992+ } ) ;
993+ try {
994+ await uploadArtifact ( {
995+ localPath : artifactPath ,
996+ baseUrl : server . baseUrl ,
997+ token : TEST_TOKEN ,
998+ leaseId : 'lease-1' ,
999+ } ) ;
1000+ assert . deepEqual ( Object . fromEntries ( server . leaseHeaders ) , {
1001+ 'POST /upload/preflight' : 'lease-1' ,
1002+ 'POST /upload' : 'lease-1' ,
1003+ } ) ;
1004+ } finally {
1005+ await server . close ( ) ;
1006+ }
1007+ } ) ;
1008+
1009+ test ( 'uploadArtifact sends no lease header when no lease is held' , async ( ) => {
1010+ const directPath = createTempFile ( 'app.apk' , 'unleased-direct-payload' ) ;
1011+ const legacyPath = createTempFile ( 'app.apk' , 'unleased-legacy-payload' ) ;
1012+ const direct = await startLeaseRecordingUploadServer ( { preflight : 'direct' } ) ;
1013+ const legacy = await startLeaseRecordingUploadServer ( {
1014+ preflight : 'unsupported' ,
1015+ } ) ;
1016+ try {
1017+ await uploadArtifact ( {
1018+ localPath : directPath ,
1019+ baseUrl : direct . baseUrl ,
1020+ token : TEST_TOKEN ,
1021+ } ) ;
1022+ await uploadArtifact ( {
1023+ localPath : legacyPath ,
1024+ baseUrl : legacy . baseUrl ,
1025+ token : TEST_TOKEN ,
1026+ } ) ;
1027+ assert . deepEqual ( Object . fromEntries ( direct . leaseHeaders ) , {
1028+ 'POST /upload/preflight' : undefined ,
1029+ 'PUT /upload/direct/ticket-1' : undefined ,
1030+ 'POST /upload/finalize' : undefined ,
1031+ } ) ;
1032+ assert . deepEqual ( Object . fromEntries ( legacy . leaseHeaders ) , {
1033+ 'POST /upload/preflight' : undefined ,
1034+ 'POST /upload' : undefined ,
1035+ } ) ;
1036+ } finally {
1037+ await direct . close ( ) ;
1038+ await legacy . close ( ) ;
1039+ }
1040+ } ) ;
1041+
1042+ test ( 'uploadArtifact keeps the lease off a direct upload ticket on another origin' , async ( ) => {
1043+ const artifactPath = createTempFile ( 'app.apk' , 'foreign-ticket-payload' ) ;
1044+ // `localhost` and `127.0.0.1` reach the same fake but are different origins, as a signed
1045+ // storage URL would be.
1046+ const server = await startLeaseRecordingUploadServer ( {
1047+ preflight : 'direct' ,
1048+ ticketOrigin : ( baseUrl ) => baseUrl . replace ( '127.0.0.1' , 'localhost' ) ,
1049+ } ) ;
1050+ try {
1051+ await uploadArtifact ( {
1052+ localPath : artifactPath ,
1053+ baseUrl : server . baseUrl ,
1054+ token : TEST_TOKEN ,
1055+ leaseId : 'lease-1' ,
1056+ } ) ;
1057+ assert . deepEqual ( Object . fromEntries ( server . leaseHeaders ) , {
1058+ 'POST /upload/preflight' : 'lease-1' ,
1059+ 'PUT /upload/direct/ticket-1' : undefined ,
1060+ 'POST /upload/finalize' : 'lease-1' ,
1061+ } ) ;
1062+ } finally {
1063+ await server . close ( ) ;
1064+ }
1065+ } ) ;
1066+
9211067function createTempDir ( ) : string {
9221068 const dir = mkdtempForTestSync ( `agent-device-upload-client-${ randomUUID ( ) } -` ) ;
9231069 tempDirs . push ( dir ) ;
0 commit comments