@@ -4,11 +4,12 @@ import { createHash, randomUUID } from 'node:crypto';
44import fs from 'node:fs' ;
55import http , { type IncomingMessage , type ServerResponse } from 'node:http' ;
66import path from 'node:path' ;
7- import { once } from 'node:events' ;
87import { uploadArtifact } from '../remote/upload-client.ts' ;
98import type { UploadProgressEvent } from '../remote/upload-progress.ts' ;
109import { runCmdSync , withCommandExecutorOverride } from '@agent-device/host-kit/command' ;
1110import { mkdtempForTestSync } from '@agent-device/test-kit/tmp-dir' ;
11+ import { closeLoopbackServer , listenOnLoopback } from '@agent-device/test-kit/loopback' ;
12+ import { DAEMON_HTTP_LEASE_HEADER } from '@agent-device/contracts/daemon-http' ;
1213
1314const TEST_TOKEN = 'agent-device-upload-test-token' ;
1415const tempDirs : string [ ] = [ ] ;
@@ -150,24 +151,15 @@ test('uploadArtifact falls back to upload when preflight is unsupported', async
150151 } ) ;
151152 assert . equal ( uploadId , 'upload-legacy' ) ;
152153 assert . deepEqual ( requests , [ 'POST /upload/preflight' , 'POST /upload' ] ) ;
153- assert . deepEqual (
154- progressEvents . map ( ( event ) => event . type ) ,
155- [ 'start' , 'progress' ] ,
156- ) ;
157- assert . deepEqual ( progressEvents [ 0 ] , {
158- type : 'start' ,
159- stage : 'legacy' ,
160- fileName : 'app.apk' ,
161- transferredBytes : 0 ,
162- totalBytes : Buffer . byteLength ( content ) ,
163- } ) ;
164- assert . deepEqual ( progressEvents [ 1 ] , {
165- type : 'progress' ,
154+ const progress = {
166155 stage : 'legacy' ,
167156 fileName : 'app.apk' ,
168- transferredBytes : Buffer . byteLength ( content ) ,
169157 totalBytes : Buffer . byteLength ( content ) ,
170- } ) ;
158+ } ;
159+ assert . deepEqual ( progressEvents , [
160+ { type : 'start' , ...progress , transferredBytes : 0 } ,
161+ { type : 'progress' , ...progress , transferredBytes : Buffer . byteLength ( content ) } ,
162+ ] ) ;
171163 } finally {
172164 await server . close ( ) ;
173165 }
@@ -211,6 +203,8 @@ test('uploadArtifact falls back to upload when preflight fails', async () => {
211203 }
212204} ) ;
213205
206+ const FIVE_UPLOAD_REDIRECTS = [ 1 , 2 , 3 , 4 , 5 ] . map ( ( index ) => `POST /upload-redirect-${ index } ` ) ;
207+
214208test ( 'uploadArtifact follows up to five upload redirects' , async ( ) => {
215209 const content = 'redirected-upload-payload' ;
216210 const artifactPath = createTempFile ( 'app.apk' , content ) ;
@@ -259,11 +253,7 @@ test('uploadArtifact follows up to five upload redirects', async () => {
259253 assert . deepEqual ( requests , [
260254 'POST /upload/preflight' ,
261255 'POST /upload' ,
262- 'POST /upload-redirect-1' ,
263- 'POST /upload-redirect-2' ,
264- 'POST /upload-redirect-3' ,
265- 'POST /upload-redirect-4' ,
266- 'POST /upload-redirect-5' ,
256+ ...FIVE_UPLOAD_REDIRECTS ,
267257 ] ) ;
268258 } finally {
269259 await server . close ( ) ;
@@ -316,11 +306,7 @@ test('uploadArtifact fails cleanly on the sixth upload redirect', async () => {
316306 assert . deepEqual ( requests , [
317307 'POST /upload/preflight' ,
318308 'POST /upload' ,
319- 'POST /upload-redirect-1' ,
320- 'POST /upload-redirect-2' ,
321- 'POST /upload-redirect-3' ,
322- 'POST /upload-redirect-4' ,
323- 'POST /upload-redirect-5' ,
309+ ...FIVE_UPLOAD_REDIRECTS ,
324310 ] ) ;
325311 } finally {
326312 await server . close ( ) ;
@@ -463,24 +449,15 @@ test('uploadArtifact uses direct upload ticket and finalize flow', async () => {
463449 } ) ;
464450 assert . equal ( uploadId , 'upload-finalized' ) ;
465451 assert . equal ( directUploadBody , content ) ;
466- assert . deepEqual (
467- progressEvents . map ( ( event ) => event . type ) ,
468- [ 'start' , 'progress' ] ,
469- ) ;
470- assert . deepEqual ( progressEvents [ 0 ] , {
471- type : 'start' ,
452+ const progress = {
472453 stage : 'direct' ,
473454 fileName : 'app.apk' ,
474- transferredBytes : 0 ,
475455 totalBytes : Buffer . byteLength ( content ) ,
476- } ) ;
477- assert . deepEqual ( progressEvents [ 1 ] , {
478- type : 'progress' ,
479- stage : 'direct' ,
480- fileName : 'app.apk' ,
481- transferredBytes : Buffer . byteLength ( content ) ,
482- totalBytes : Buffer . byteLength ( content ) ,
483- } ) ;
456+ } ;
457+ assert . deepEqual ( progressEvents , [
458+ { type : 'start' , ...progress , transferredBytes : 0 } ,
459+ { type : 'progress' , ...progress , transferredBytes : Buffer . byteLength ( content ) } ,
460+ ] ) ;
484461 assert . deepEqual ( requests , [
485462 'POST /upload/preflight' ,
486463 'PUT /signed-upload' ,
@@ -918,151 +895,38 @@ test('uploadArtifact uploads APK, AAB, and IPA files without wrapping them', asy
918895 }
919896} ) ;
920897
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- }
898+ const DIRECT_ROUTES = [ 'POST /upload/preflight' , 'PUT /upload/t' , 'POST /upload/finalize' ] ;
966899
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' ,
900+ // A `localhost` ticket reaches this fake but is another origin, as a signed storage URL is.
901+ test . each ( [
902+ { flow : 'direct' , host : '127.0.0.1' , routes : DIRECT_ROUTES , unleased : '' } ,
903+ { flow : 'legacy' , host : '' , routes : [ 'POST /upload/preflight' , 'POST /upload' ] , unleased : '' } ,
904+ { flow : 'direct' , host : 'localhost' , routes : DIRECT_ROUTES , unleased : 'PUT /upload/t' } ,
905+ ] ) (
906+ 'uploadArtifact names a held lease on a $flow upload to ticket host "$host"' ,
907+ async ( { flow, host, routes, unleased } ) => {
908+ let leases = new Map < string , unknown > ( ) ;
909+ const server = await startServer ( async ( req , res ) => {
910+ leases . set ( `${ req . method } ${ req . url } ` , req . headers [ DAEMON_HTTP_LEASE_HEADER ] ) ;
911+ await readRequestBody ( req ) ;
912+ if ( req . url !== '/upload/preflight' ) return sendJson ( res , { ok : true , uploadId : 'done' } ) ;
913+ if ( flow === 'legacy' ) return sendJson ( res , { ok : true , cacheHit : false } ) ;
914+ const url = `${ server . baseUrl . replace ( '127.0.0.1' , host ) } /upload/t` ;
915+ sendJson ( res , { ok : true , uploadId : 't' , upload : { url, headers : { } } } ) ;
1061916 } ) ;
1062- } finally {
1063- await server . close ( ) ;
1064- }
1065- } ) ;
917+ try {
918+ const localPath = createTempFile ( 'app.apk' , `${ flow } -payload` ) ;
919+ for ( const leaseId of [ 'lease-1' , undefined ] ) {
920+ leases = new Map ( ) ;
921+ await uploadArtifact ( { localPath, baseUrl : server . baseUrl , token : TEST_TOKEN , leaseId } ) ;
922+ const expected = routes . map ( ( route ) => [ route , route === unleased ? undefined : leaseId ] ) ;
923+ assert . deepEqual ( [ ...leases ] , expected ) ;
924+ }
925+ } finally {
926+ await server . close ( ) ;
927+ }
928+ } ,
929+ ) ;
1066930
1067931function createTempDir ( ) : string {
1068932 const dir = mkdtempForTestSync ( `agent-device-upload-client-${ randomUUID ( ) } -` ) ;
@@ -1101,22 +965,9 @@ async function startServer(
1101965 res . end ( error instanceof Error ? error . message : String ( error ) ) ;
1102966 } ) ;
1103967 } ) ;
1104- server . listen ( 0 , '127.0.0.1' ) ;
1105968 server . unref ( ) ;
1106- await once ( server , 'listening' ) ;
1107- const address = server . address ( ) ;
1108- assert . ok ( address && typeof address === 'object' ) ;
1109- return {
1110- baseUrl : `http://127.0.0.1:${ address . port } ` ,
1111- close : async ( ) => {
1112- await new Promise < void > ( ( resolve , reject ) => {
1113- server . close ( ( error ) => {
1114- if ( error ) reject ( error ) ;
1115- else resolve ( ) ;
1116- } ) ;
1117- } ) ;
1118- } ,
1119- } ;
969+ const port = await listenOnLoopback ( server ) ;
970+ return { baseUrl : `http://127.0.0.1:${ port } ` , close : ( ) => closeLoopbackServer ( server ) } ;
1120971}
1121972
1122973async function readRequestBody ( req : IncomingMessage ) : Promise < Buffer > {
0 commit comments