|
1 | 1 | import { describe, expect, mock, test } from 'bun:test' |
2 | 2 | import { |
3 | 3 | buildPaidSocialRequest, |
| 4 | + hashPaidSocialEmail, |
4 | 5 | paidSocialId, |
5 | 6 | sendPaidSocialConversion, |
6 | 7 | xEventId, |
@@ -43,7 +44,125 @@ const base: Omit<SendPaidSocialConversionParams, 'config'> = { |
43 | 44 | campaign: { utm_campaign: 'internal-only-campaign' }, |
44 | 45 | }, |
45 | 46 | } |
| 47 | +const emailHash = |
| 48 | + '5806dc6b2f04fb728708a8f7b81c14edcd4fba36f77914cf0c9368d9a3a25f76' |
46 | 49 | describe('paid social transport contracts', () => { |
| 50 | + test('normalizes a real email before unsalted SHA256 and rejects invalid input', () => { |
| 51 | + expect(hashPaidSocialEmail(' Test@X.com ')).toBe(emailHash) |
| 52 | + for (const value of [ |
| 53 | + undefined, |
| 54 | + null, |
| 55 | + 1, |
| 56 | + '', |
| 57 | + 'not-email', |
| 58 | + 'a@@b.com', |
| 59 | + 'a b@c.com', |
| 60 | + `${'x'.repeat(255)}@x.com`, |
| 61 | + ]) |
| 62 | + expect(hashPaidSocialEmail(value)).toBeUndefined() |
| 63 | + }) |
| 64 | + test.each([x, xPixelToken])( |
| 65 | + 'X accepts genuine signup and native email matching without a click', |
| 66 | + (config) => { |
| 67 | + for (const eventName of [ |
| 68 | + 'CompleteRegistration', |
| 69 | + 'CodingActivation', |
| 70 | + ] as const) { |
| 71 | + const request = buildPaidSocialRequest({ |
| 72 | + ...base, |
| 73 | + config, |
| 74 | + eventName, |
| 75 | + attribution: { userAgent: 'Browser', hashedEmail: emailHash }, |
| 76 | + }) |
| 77 | + expect(request.body).toEqual({ |
| 78 | + conversions: [ |
| 79 | + { |
| 80 | + conversion_time: '2026-09-18T12:00:00.000Z', |
| 81 | + event_id: `${config.pixelToken ? 'tw-abc-' : ''}${eventName === 'CompleteRegistration' ? 'signup' : 'activation'}`, |
| 82 | + identifiers: [{ hashed_email: emailHash }], |
| 83 | + conversion_id: 'stable-occurrence', |
| 84 | + }, |
| 85 | + ], |
| 86 | + }) |
| 87 | + expect(JSON.stringify(request.body)).not.toContain('internal-account') |
| 88 | + expect(JSON.stringify(request.body)).not.toContain('user_agent') |
| 89 | + expect(JSON.stringify(request.body)).not.toContain('url') |
| 90 | + } |
| 91 | + }, |
| 92 | + ) |
| 93 | + test('X includes both genuine matching identifiers when available', () => { |
| 94 | + const request = buildPaidSocialRequest({ |
| 95 | + ...base, |
| 96 | + config: xPixelToken, |
| 97 | + attribution: { ...base.attribution, hashedEmail: emailHash }, |
| 98 | + }) |
| 99 | + expect(request.body.conversions).toMatchObject([ |
| 100 | + { identifiers: [{ twclid: 'click-id', hashed_email: emailHash }] }, |
| 101 | + ]) |
| 102 | + }) |
| 103 | + test.each([ |
| 104 | + { clickId: undefined }, |
| 105 | + { clickId: undefined, hashedEmail: 'raw@example.com' }, |
| 106 | + { clickId: undefined, hashedEmail: emailHash.toUpperCase() }, |
| 107 | + { clickId: 'https://private/path', hashedEmail: emailHash }, |
| 108 | + { clickId: '', hashedEmail: emailHash }, |
| 109 | + { hashedEmail: 'not-a-hash' }, |
| 110 | + { userAgent: '' }, |
| 111 | + { userAgent: ' '.repeat(10) }, |
| 112 | + { userAgent: 'a'.repeat(513) }, |
| 113 | + ])( |
| 114 | + 'X rejects missing or malformed matching data before network delivery', |
| 115 | + async (fields) => { |
| 116 | + const fetchImpl = mock(async () => |
| 117 | + Response.json({ data: { conversions_processed: 1 } }), |
| 118 | + ) as unknown as typeof fetch |
| 119 | + await expect( |
| 120 | + sendPaidSocialConversion({ |
| 121 | + ...base, |
| 122 | + config: xPixelToken, |
| 123 | + attribution: { ...base.attribution, ...fields }, |
| 124 | + fetchImpl, |
| 125 | + }), |
| 126 | + ).rejects.toThrow('Invalid paid social matching data') |
| 127 | + expect(fetchImpl).not.toHaveBeenCalled() |
| 128 | + }, |
| 129 | + ) |
| 130 | + test('TikTok organic registration omits the click and never forwards X email matching', () => { |
| 131 | + const request = buildPaidSocialRequest({ |
| 132 | + ...base, |
| 133 | + config: tiktok, |
| 134 | + attribution: { |
| 135 | + ...base.attribution, |
| 136 | + clickId: undefined, |
| 137 | + hashedEmail: emailHash, |
| 138 | + }, |
| 139 | + }) |
| 140 | + expect(request.body.data).toMatchObject([ |
| 141 | + { |
| 142 | + user: { |
| 143 | + external_id: paidSocialId('tiktok', 'internal-account'), |
| 144 | + user_agent: 'Browser', |
| 145 | + }, |
| 146 | + }, |
| 147 | + ]) |
| 148 | + const body = JSON.stringify(request.body) |
| 149 | + expect(body).not.toContain('ttclid') |
| 150 | + expect(body).not.toContain('email') |
| 151 | + expect(body).not.toContain(emailHash) |
| 152 | + }) |
| 153 | + test.each([ |
| 154 | + { userAgent: '' }, |
| 155 | + { userAgent: 'x'.repeat(513) }, |
| 156 | + { clickId: 'malformed click' }, |
| 157 | + ])('TikTok still rejects invalid browser matching data', (fields) => { |
| 158 | + expect(() => |
| 159 | + buildPaidSocialRequest({ |
| 160 | + ...base, |
| 161 | + config: tiktok, |
| 162 | + attribution: { ...base.attribution, ...fields }, |
| 163 | + }), |
| 164 | + ).toThrow() |
| 165 | + }) |
47 | 166 | test('X sends the configured event with stable occurrence, ISO time and only click matching', () => { |
48 | 167 | const request = buildPaidSocialRequest({ ...base, config: x }) |
49 | 168 | expect(request.url).toBe( |
|
0 commit comments