@@ -39,6 +39,19 @@ function parseOptionalIntegerOption(
3939 return parsed ;
4040}
4141
42+ function parseBrowserChoiceOption < const T extends string > (
43+ value : string ,
44+ label : string ,
45+ choices : readonly T [ ] ,
46+ ) : T | undefined {
47+ if ( ( choices as readonly string [ ] ) . includes ( value ) ) {
48+ return value as T ;
49+ }
50+ defaultRuntime . error ( danger ( `Invalid ${ label } : expected ${ choices . join ( " or " ) } ` ) ) ;
51+ defaultRuntime . exit ( 1 ) ;
52+ return undefined ;
53+ }
54+
4255/** Registers Browser screenshot and snapshot commands. */
4356export function registerBrowserInspectCommands (
4457 browser : Command ,
@@ -60,6 +73,10 @@ export function registerBrowserInspectCommands(
6073 . action ( async ( targetId : string | undefined , opts , cmd ) => {
6174 const parent = parentOpts ( cmd ) ;
6275 const profile = parent ?. browserProfile ;
76+ const type = parseBrowserChoiceOption ( opts . type , "--type" , [ "png" , "jpeg" ] ) ;
77+ if ( type === undefined ) {
78+ return ;
79+ }
6380 try {
6481 const result = await callBrowserRequest < { path : string } > (
6582 parent ,
@@ -73,7 +90,7 @@ export function registerBrowserInspectCommands(
7390 ref : normalizeOptionalString ( opts . ref ) ,
7491 element : normalizeOptionalString ( opts . element ) ,
7592 labels : Boolean ( opts . labels ) ,
76- type : opts . type === "jpeg" ? "jpeg" : "png" ,
93+ type,
7794 } ,
7895 } ,
7996 { timeoutMs : 20000 } ,
@@ -108,15 +125,26 @@ export function registerBrowserInspectCommands(
108125 . action ( async ( opts , cmd : Command ) => {
109126 const parent = parentOpts ( cmd ) ;
110127 const profile = parent ?. browserProfile ;
111- const format = opts . format === "aria" ? "aria" : "ai" ;
128+ const format = parseBrowserChoiceOption ( opts . format , "--format" , [ "aria" , "ai" ] ) ;
129+ if ( format === undefined ) {
130+ return ;
131+ }
132+ const explicitMode =
133+ opts . mode === undefined
134+ ? undefined
135+ : parseBrowserChoiceOption ( opts . mode , "--mode" , [ "efficient" ] ) ;
136+ if ( opts . mode !== undefined && explicitMode === undefined ) {
137+ return ;
138+ }
112139 const formatWasExplicit = cmd . getOptionValueSource ( "format" ) === "cli" ;
113140 const configMode =
114141 ! formatWasExplicit &&
115142 format === "ai" &&
116143 getRuntimeConfig ( ) . browser ?. snapshotDefaults ?. mode === "efficient"
117144 ? "efficient"
118145 : undefined ;
119- const mode = opts . efficient === true || opts . mode === "efficient" ? "efficient" : configMode ;
146+ const mode =
147+ opts . efficient === true || explicitMode === "efficient" ? "efficient" : configMode ;
120148 const limit = parseOptionalIntegerOption ( opts . limit , "--limit" , { min : 1 } ) ;
121149 const depth = parseOptionalIntegerOption ( opts . depth , "--depth" , { min : 0 } ) ;
122150 if (
0 commit comments