@@ -1054,6 +1054,122 @@ describe('CLI', () => {
10541054 }
10551055 } ) ;
10561056
1057+ it ( 'runs a direct authenticated full scan with scan <url> --full' , async ( ) => {
1058+ let requestBody ;
1059+ let requestAuth ;
1060+ const config = createExplicitConfigDir ( {
1061+ agent_session : {
1062+ id : 'sess_scan_full' ,
1063+ access_token : 'aas_scan_full' ,
1064+ refresh_token : 'aar_scan_full' ,
1065+ access_expires_at : 1893456000000 ,
1066+ refresh_expires_at : 1924992000000 ,
1067+ scopes : [ 'account:read' ] ,
1068+ } ,
1069+ } ) ;
1070+ const server = await startServer ( async ( req , res ) => {
1071+ if ( req . method === 'POST' && req . url === '/website-scans' ) {
1072+ requestAuth = req . headers . authorization ;
1073+ requestBody = await readRequestJson ( req ) ;
1074+ res . writeHead ( 200 , { 'Content-Type' : 'application/json' } ) ;
1075+ res . end ( JSON . stringify ( {
1076+ ok : true ,
1077+ analysis_id : 'scan_full_direct' ,
1078+ mode : 'full' ,
1079+ normalized_url : 'https://example.com/' ,
1080+ result : {
1081+ minimum_viable_instrumentation : [ { event : 'primary_cta_clicked' , priority : 1 } ] ,
1082+ } ,
1083+ } ) ) ;
1084+ return ;
1085+ }
1086+ res . writeHead ( 404 , { 'Content-Type' : 'application/json' } ) ;
1087+ res . end ( JSON . stringify ( { error : 'not found' } ) ) ;
1088+ } ) ;
1089+
1090+ try {
1091+ const { code, stdout } = await run ( [
1092+ '--config-dir' , config . configDir ,
1093+ 'scan' ,
1094+ 'https://example.com/' ,
1095+ '--full' ,
1096+ '--json' ,
1097+ ] , {
1098+ env : {
1099+ AGENT_ANALYTICS_URL : server . baseUrl ,
1100+ AGENT_ANALYTICS_API_KEY : '' ,
1101+ } ,
1102+ } ) ;
1103+ const data = JSON . parse ( stdout ) ;
1104+
1105+ assert . equal ( code , 0 ) ;
1106+ assert . equal ( requestAuth , 'Bearer aas_scan_full' ) ;
1107+ assert . deepEqual ( requestBody , {
1108+ url : 'https://example.com/' ,
1109+ mode : 'full' ,
1110+ } ) ;
1111+ assert . equal ( data . mode , 'full' ) ;
1112+ } finally {
1113+ await server . close ( ) ;
1114+ config . cleanup ( ) ;
1115+ }
1116+ } ) ;
1117+
1118+ it ( 'uses saved auth for scan <url> previews when logged in' , async ( ) => {
1119+ let requestAuth ;
1120+ const config = createExplicitConfigDir ( {
1121+ agent_session : {
1122+ id : 'sess_scan_preview' ,
1123+ access_token : 'aas_scan_preview' ,
1124+ refresh_token : 'aar_scan_preview' ,
1125+ access_expires_at : 1893456000000 ,
1126+ refresh_expires_at : 1924992000000 ,
1127+ scopes : [ 'account:read' ] ,
1128+ } ,
1129+ } ) ;
1130+ const server = await startServer ( async ( req , res ) => {
1131+ if ( req . method === 'POST' && req . url === '/website-scans' ) {
1132+ requestAuth = req . headers . authorization ;
1133+ await readRequestJson ( req ) ;
1134+ res . writeHead ( 200 , { 'Content-Type' : 'application/json' } ) ;
1135+ res . end ( JSON . stringify ( {
1136+ ok : true ,
1137+ analysis_id : 'scan_auth_preview' ,
1138+ mode : 'authenticated_preview' ,
1139+ normalized_url : 'https://example.com/' ,
1140+ preview : {
1141+ minimum_viable_instrumentation : [ { event : 'primary_cta_clicked' , priority : 1 } ] ,
1142+ } ,
1143+ } ) ) ;
1144+ return ;
1145+ }
1146+ res . writeHead ( 404 , { 'Content-Type' : 'application/json' } ) ;
1147+ res . end ( JSON . stringify ( { error : 'not found' } ) ) ;
1148+ } ) ;
1149+
1150+ try {
1151+ const { code, stdout } = await run ( [
1152+ '--config-dir' , config . configDir ,
1153+ 'scan' ,
1154+ 'https://example.com/' ,
1155+ '--json' ,
1156+ ] , {
1157+ env : {
1158+ AGENT_ANALYTICS_URL : server . baseUrl ,
1159+ AGENT_ANALYTICS_API_KEY : '' ,
1160+ } ,
1161+ } ) ;
1162+ const data = JSON . parse ( stdout ) ;
1163+
1164+ assert . equal ( code , 0 ) ;
1165+ assert . equal ( requestAuth , 'Bearer aas_scan_preview' ) ;
1166+ assert . equal ( data . mode , 'authenticated_preview' ) ;
1167+ } finally {
1168+ await server . close ( ) ;
1169+ config . cleanup ( ) ;
1170+ }
1171+ } ) ;
1172+
10571173 it ( 'prints login guidance for scan --full without auth' , async ( ) => {
10581174 const tempHome = createTempConfigHome ( ) ;
10591175 const { code, stdout } = await run ( [
0 commit comments