@@ -11,7 +11,8 @@ import {
1111 GlobalSetting ,
1212 isDirective ,
1313 DestinationDefinition as CloudModeDestinationDefinition ,
14- InputField
14+ InputField ,
15+ AudienceDestinationDefinition
1516} from '@segment/actions-core'
1617import Chance from 'chance'
1718import { getRawKeys } from '@segment/actions-core/mapping-kit/value-keys'
@@ -132,7 +133,7 @@ export default class GenerateTestPayload extends Command {
132133
133134 if ( actionToGenerate === 'all' ) {
134135 for ( const [ slug , action ] of actions ) {
135- await this . generatePayloadForAction ( destination , slug , action , flags . port )
136+ await this . generatePayloadForAction ( destination , slug , action )
136137 }
137138 } else {
138139 const action = actions . find ( ( [ slug ] ) => slug === actionToGenerate )
@@ -142,13 +143,13 @@ export default class GenerateTestPayload extends Command {
142143 return
143144 }
144145
145- await this . generatePayloadForAction ( destination , action [ 0 ] , action [ 1 ] , flags . port )
146+ await this . generatePayloadForAction ( destination , action [ 0 ] , action [ 1 ] )
146147 }
147148
148149 this . log ( chalk . green ( `\nDone generating test payloads! 🎉` ) )
149150 }
150151
151- async generatePayloadForAction ( destination : DestinationDefinition , actionSlug : string , action : any , port : number ) {
152+ async generatePayloadForAction ( destination : DestinationDefinition , actionSlug : string , action : any ) {
152153 this . spinner . start ( `Generating test payload for action: ${ actionSlug } ` )
153154
154155 try {
@@ -170,19 +171,32 @@ export default class GenerateTestPayload extends Command {
170171 }
171172 }
172173
174+ const audienceSettings = {
175+ ...( destination as AudienceDestinationDefinition ) ?. audienceFields
176+ }
177+
173178 // Generate sample mapping based on action fields
174179 const mapping = { } as Record < string , any >
175180 const fields = ( action . fields || { } ) as Record < string , InputField >
176181
177182 for ( const [ fieldKey , field ] of Object . entries ( fields ) ) {
178183 if ( field . default ) {
179184 mapping [ fieldKey ] = field . default
185+ } else if ( field . choices ) {
186+ // if choices is array of string, pick the first one
187+ // if choices is array of {label: string, value: string}, then pick the value of the first one
188+ mapping [ fieldKey ] = typeof field . choices [ 0 ] === 'string' ? field . choices [ 0 ] : field . choices [ 0 ] . value
180189 }
181190 }
182191
183192 // Generate sample payload based on the fields
184193 const payload = this . generateSamplePayloadFromMapping ( mapping )
185194
195+ // if audience settings exist, add them to the payload
196+ if ( audienceSettings ) {
197+ set ( payload , 'context.personas.audience_settings' , this . generateSampleFromSchema ( audienceSettings || { } ) )
198+ }
199+
186200 // Generate final sample request
187201 const sampleRequest = {
188202 settings,
@@ -194,7 +208,7 @@ export default class GenerateTestPayload extends Command {
194208
195209 // Print the curl command to the terminal
196210 this . log ( chalk . cyan ( `\n# Test payload for ${ chalk . bold ( destination . name ) } - ${ chalk . bold ( actionSlug ) } ` ) )
197- this . log ( chalk . yellow ( `curl -X POST http://localhost:${ port } /${ actionSlug } \\` ) )
211+ this . log ( chalk . yellow ( `curl -X POST http://localhost:3000 /${ actionSlug } \\` ) )
198212 this . log ( chalk . yellow ( ` -H "Content-Type: application/json" \\` ) )
199213 this . log ( chalk . yellow ( ` -d '${ JSON . stringify ( sampleRequest ) . replace ( / ' / g, "\\'" ) } '` ) )
200214 } catch ( error ) {
@@ -207,7 +221,7 @@ export default class GenerateTestPayload extends Command {
207221 for ( const [ propName , setting ] of Object . entries ( schema ) ) {
208222 if ( setting . default !== undefined ) {
209223 result [ propName ] = setting . default
210- } else if ( setting . required ) {
224+ } else {
211225 result [ propName ] = this . generatePlaceholderForSchema ( setting )
212226 }
213227 }
@@ -223,13 +237,13 @@ export default class GenerateTestPayload extends Command {
223237 if ( schema . choices ) {
224238 return schema . choices [ 0 ]
225239 }
226- return `YOUR_ ${ schema . label || 'VALUE' } `
240+ return `< ${ schema . label || 'VALUE' } > `
227241 case 'number' :
228242 return 0
229243 case 'boolean' :
230244 return false
231245 case 'password' :
232- return `YOUR_ ${ schema . label || 'PASSWORD' } `
246+ return `< ${ schema . label || 'PASSWORD' } > `
233247 default :
234248 return null
235249 }
0 commit comments