Skip to content

Commit ce7fb7d

Browse files
tests, as per review, plus bonus
1 parent 34e907d commit ce7fb7d

7 files changed

Lines changed: 120 additions & 25 deletions

File tree

packages/destination-actions/src/destinations/airship/__tests__/__snapshots__/snapshot.test.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Array [
1313
},
1414
"occurred": false,
1515
"user": Object {
16-
"Z8PnKI8*YgWA": "Z8PnKI8*YgWA",
16+
"channel": "Z8PnKI8*YgWA",
1717
},
1818
},
1919
]
@@ -40,7 +40,7 @@ Array [
4040
exports[`Testing snapshot for actions-airship destination: manageTags action - all fields 1`] = `
4141
Object {
4242
"audience": Object {
43-
"XvmIpuaw": "XvmIpuaw",
43+
"channel": "XvmIpuaw",
4444
},
4545
}
4646
`;
@@ -207,7 +207,7 @@ Object {
207207
},
208208
],
209209
"audience": Object {
210-
"*N75*": "*N75*",
210+
"channel": "*N75*",
211211
},
212212
}
213213
`;

packages/destination-actions/src/destinations/airship/__tests__/airship.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@ import { DecoratedResponse } from '@segment/actions-core'
66
const testDestination = createTestIntegration(Airship)
77

88
describe('Airship', () => {
9+
describe('extendRequest headers', () => {
10+
it('should set the Segment attribution User-Agent header with the app key', async () => {
11+
const now = new Date().toISOString()
12+
const event = createTestEvent({
13+
userId: 'test-user-rzoj4u7gqw',
14+
timestamp: now,
15+
traits: { trait1: 1 }
16+
})
17+
18+
nock('https://go.urbanairship.com').post('/api/channels/attributes').reply(200, {})
19+
20+
const responses = await testDestination.testAction('setAttributes', {
21+
event,
22+
useDefaultMappings: true,
23+
settings: {
24+
access_token: 'foo',
25+
app_key: 'bar',
26+
endpoint: 'US'
27+
}
28+
})
29+
expect(responses[0].options.headers?.get('user-agent')).toBe('PartnerIntegrations/Segment (bar)')
30+
})
31+
})
32+
933
describe('setAttribute', () => {
1034
it('should work for US', async () => {
1135
const now = new Date().toISOString()
@@ -98,6 +122,46 @@ describe('Airship', () => {
98122
expect(responses[0].status).toBe(200)
99123
expect(responses[0].data).toMatchObject({})
100124
})
125+
126+
it('should batch events, building each user object independently', async () => {
127+
const now = new Date().toISOString()
128+
129+
nock('https://go.urbanairship.com').post('/api/custom-events').reply(200, {})
130+
131+
const responses = await testDestination.testBatchAction('customEvents', {
132+
settings: {
133+
access_token: 'foo',
134+
app_key: 'bar',
135+
endpoint: 'US'
136+
},
137+
events: [
138+
createTestEvent({ type: 'track', timestamp: now, event: 'Event A', userId: 'named-user-1' }),
139+
createTestEvent({
140+
type: 'track',
141+
timestamp: now,
142+
event: 'Event B',
143+
userId: undefined,
144+
properties: { channel_id: 'chan-xyz' },
145+
context: { device: { type: 'ios' } }
146+
})
147+
],
148+
mapping: {
149+
named_user_id: { '@path': '$.userId' },
150+
channel_id: { '@path': '$.properties.channel_id' },
151+
channel_type: { '@path': '$.context.device.type' },
152+
name: { '@path': '$.event' },
153+
occurred: { '@path': '$.timestamp' },
154+
enable_batching: true
155+
}
156+
})
157+
158+
expect(responses[0].status).toBe(200)
159+
const sent = JSON.parse(responses[0].options.body as string)
160+
expect(sent).toHaveLength(2)
161+
// Each event resolves its own audience: first a named user, second an iOS channel
162+
expect(sent[0].user).toEqual({ named_user_id: 'named-user-1' })
163+
expect(sent[1].user).toEqual({ ios_channel: 'chan-xyz' })
164+
})
101165
})
102166

103167
describe('manageTags', () => {

packages/destination-actions/src/destinations/airship/__tests__/utilities.test.ts

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -270,30 +270,40 @@ describe('Testing _parse_and_format_date', () => {
270270
})
271271
})
272272

273+
describe('Testing _channel_type_to_key', () => {
274+
it('should map known platform types to platform-specific keys', () => {
275+
expect(_private._channel_type_to_key('ios')).toBe('ios_channel')
276+
expect(_private._channel_type_to_key('android')).toBe('android_channel')
277+
expect(_private._channel_type_to_key('amazon')).toBe('amazon_channel')
278+
expect(_private._channel_type_to_key('web')).toBe('web_channel')
279+
})
280+
281+
it('should fall back to the generic channel key for unknown types', () => {
282+
expect(_private._channel_type_to_key('email')).toBe('channel')
283+
expect(_private._channel_type_to_key('sms')).toBe('channel')
284+
})
285+
})
286+
273287
describe('Testing _build_audience', () => {
274288
it('should return named_user_id audience when only named_user_id is provided', () => {
275289
expect(_private._build_audience({ named_user_id: 'user-123' })).toEqual({ named_user_id: 'user-123' })
276290
})
277291

278-
it('should return channel audience when channel_id and channel_type are provided', () => {
279-
expect(_private._build_audience({ channel_id: 'chan-abc', channel_type: 'email' })).toEqual({
280-
email: 'chan-abc'
281-
})
292+
it('should use the platform-specific key when channel_type maps to one', () => {
293+
expect(_private._build_audience({ channel_id: 'chan-abc', channel_type: 'ios' })).toEqual({ ios_channel: 'chan-abc' })
282294
})
283295

284-
it('should use channel selector when all three fields are provided (channel_id takes precedence)', () => {
285-
expect(_private._build_audience({ named_user_id: 'user-123', channel_id: 'chan-abc', channel_type: 'email' })).toEqual({
286-
email: 'chan-abc'
287-
})
296+
it('should use the generic channel key when channel_type is not a known platform', () => {
297+
expect(_private._build_audience({ channel_id: 'chan-abc', channel_type: 'email' })).toEqual({ channel: 'chan-abc' })
288298
})
289299

290-
it('should use generic channel audience when channel_id provided without channel_type', () => {
300+
it('should use the generic channel key when channel_type is omitted', () => {
291301
expect(_private._build_audience({ channel_id: 'chan-abc' })).toEqual({ channel: 'chan-abc' })
292302
})
293303

294-
it('should use generic channel audience when channel_id provided without channel_type even if named_user_id is present', () => {
295-
expect(_private._build_audience({ named_user_id: 'user-123', channel_id: 'chan-abc' })).toEqual({
296-
channel: 'chan-abc'
304+
it('should prefer channel_id over named_user_id when both provided', () => {
305+
expect(_private._build_audience({ named_user_id: 'user-123', channel_id: 'chan-abc', channel_type: 'ios' })).toEqual({
306+
ios_channel: 'chan-abc'
297307
})
298308
})
299309

@@ -303,28 +313,49 @@ describe('Testing _build_audience', () => {
303313
})
304314

305315
describe('Testing _build_custom_event_object with channel_id', () => {
306-
it('should use channel audience when channel_id is provided', () => {
316+
it('should use the platform-specific key for a known channel type', () => {
317+
const payload: CustomEventsPayload = {
318+
channel_id: 'bddb6f5d-dcc3-4b0b-ba50-61b169077302',
319+
channel_type: 'ios',
320+
name: 'Test Event',
321+
occurred: occurred.toISOString(),
322+
enable_batching: false
323+
}
324+
const result = _private._build_custom_event_object(payload) as any
325+
expect(result.user).toEqual({ ios_channel: 'bddb6f5d-dcc3-4b0b-ba50-61b169077302' })
326+
})
327+
328+
it('should use the generic channel key when channel_type is omitted', () => {
307329
const payload: CustomEventsPayload = {
308330
channel_id: 'chan-abc',
309-
channel_type: 'email',
310331
name: 'Test Event',
311332
occurred: occurred.toISOString(),
312333
enable_batching: false
313334
}
314335
const result = _private._build_custom_event_object(payload) as any
315-
expect(result.user).toEqual({ email: 'chan-abc' })
336+
expect(result.user).toEqual({ channel: 'chan-abc' })
316337
})
317338
})
318339

319340
describe('Testing _build_tags_object with channel_id', () => {
320-
it('should use channel audience when channel_id is provided', () => {
341+
it('should use the platform-specific key for a known channel type', () => {
342+
const payload: ManageTagsPayload = {
343+
channel_id: 'bddb6f5d-dcc3-4b0b-ba50-61b169077302',
344+
channel_type: 'ios',
345+
tag_group: 'segment-integration',
346+
tags: { trait3: true }
347+
}
348+
const result = _private._build_tags_object(payload) as any
349+
expect(result.audience).toEqual({ ios_channel: 'bddb6f5d-dcc3-4b0b-ba50-61b169077302' })
350+
})
351+
352+
it('should use the generic channel key when channel_type is omitted', () => {
321353
const payload: ManageTagsPayload = {
322354
channel_id: 'chan-abc',
323-
channel_type: 'sms',
324355
tag_group: 'segment-integration',
325356
tags: { trait3: true }
326357
}
327358
const result = _private._build_tags_object(payload) as any
328-
expect(result.audience).toEqual({ sms: 'chan-abc' })
359+
expect(result.audience).toEqual({ channel: 'chan-abc' })
329360
})
330361
})

packages/destination-actions/src/destinations/airship/customEvents/__tests__/__snapshots__/snapshot.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Array [
1313
},
1414
"occurred": false,
1515
"user": Object {
16-
"5C5XZ3fWrH)z48": "5C5XZ3fWrH)z48",
16+
"channel": "5C5XZ3fWrH)z48",
1717
},
1818
},
1919
]

packages/destination-actions/src/destinations/airship/manageTags/__tests__/__snapshots__/index.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`Testing snapshot for Airship's manageTags destination action: all fields 1`] = `
44
Object {
55
"audience": Object {
6-
"s0s43LdN(JFJswcY3#g3": "s0s43LdN(JFJswcY3#g3",
6+
"channel": "s0s43LdN(JFJswcY3#g3",
77
},
88
}
99
`;

packages/destination-actions/src/destinations/airship/manageTags/__tests__/__snapshots__/snapshot.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`Testing snapshot for Airship's manageTags destination action: all fields 1`] = `
44
Object {
55
"audience": Object {
6-
"s0s43LdN(JFJswcY3#g3": "s0s43LdN(JFJswcY3#g3",
6+
"channel": "s0s43LdN(JFJswcY3#g3",
77
},
88
}
99
`;

packages/destination-actions/src/destinations/airship/setAttributes/__tests__/__snapshots__/snapshot.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ Object {
143143
},
144144
],
145145
"audience": Object {
146-
"Tw4VW": "Tw4VW",
146+
"channel": "Tw4VW",
147147
},
148148
}
149149
`;

0 commit comments

Comments
 (0)