|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { |
| 3 | + isIOSInAppBrowser, |
| 4 | + isAndroidInAppBrowser, |
| 5 | + pickMobileFallbackUrl, |
| 6 | +} from './redirect.js'; |
| 7 | + |
| 8 | +// Real-world UA strings (truncated where helpful) for use across test cases. |
| 9 | +const UA = { |
| 10 | + // Regular browsers — should NOT be detected as in-app |
| 11 | + iosSafari: |
| 12 | + 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1', |
| 13 | + iosChrome: |
| 14 | + 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/123.0.6312.52 Mobile/15E148 Safari/604.1', |
| 15 | + androidChrome: |
| 16 | + 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36', |
| 17 | + androidFirefox: |
| 18 | + 'Mozilla/5.0 (Android 14; Mobile; rv:125.0) Gecko/125.0 Firefox/125.0', |
| 19 | + |
| 20 | + // iOS in-app browsers |
| 21 | + iosGmail: |
| 22 | + 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 GSA/322.0.616052181 Safari/604.1', |
| 23 | + iosFacebook: |
| 24 | + 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 [FBAN/FBIOS;FBAV/450.0.0.0]', |
| 25 | + iosInstagram: |
| 26 | + 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Instagram 320.0.0.0', |
| 27 | + iosOutlook: |
| 28 | + 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Outlook-iOS/2.0', |
| 29 | + iosTwitter: |
| 30 | + 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Twitter for iPhone/10.0', |
| 31 | + |
| 32 | + // Android in-app browsers |
| 33 | + androidWebview: |
| 34 | + 'Mozilla/5.0 (Linux; Android 14; Pixel 8; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/124.0.0.0 Mobile Safari/537.36', |
| 35 | + androidFacebook: |
| 36 | + 'Mozilla/5.0 (Linux; Android 14; Pixel 8; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/124.0.0.0 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/450.0.0.0]', |
| 37 | + androidInstagram: |
| 38 | + 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36 Instagram 320.0.0.0', |
| 39 | + androidLine: |
| 40 | + 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36 Line/13.0.0', |
| 41 | + androidWhatsapp: |
| 42 | + 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36 WhatsApp/2.24.0', |
| 43 | +}; |
| 44 | + |
| 45 | +const URLS = { |
| 46 | + iosStore: 'https://apps.apple.com/app/example/id123', |
| 47 | + androidStore: 'https://play.google.com/store/apps/details?id=com.example', |
| 48 | + webFallback: 'https://example.com/landing', |
| 49 | +}; |
| 50 | + |
| 51 | +describe('isIOSInAppBrowser', () => { |
| 52 | + it('detects Gmail (GSA token)', () => { |
| 53 | + expect(isIOSInAppBrowser(UA.iosGmail)).toBe(true); |
| 54 | + }); |
| 55 | + it('detects Facebook (FBAN/FBAV)', () => { |
| 56 | + expect(isIOSInAppBrowser(UA.iosFacebook)).toBe(true); |
| 57 | + }); |
| 58 | + it('detects Instagram', () => { |
| 59 | + expect(isIOSInAppBrowser(UA.iosInstagram)).toBe(true); |
| 60 | + }); |
| 61 | + it('detects Outlook', () => { |
| 62 | + expect(isIOSInAppBrowser(UA.iosOutlook)).toBe(true); |
| 63 | + }); |
| 64 | + it('detects Twitter', () => { |
| 65 | + expect(isIOSInAppBrowser(UA.iosTwitter)).toBe(true); |
| 66 | + }); |
| 67 | + it('does not flag Safari as in-app', () => { |
| 68 | + expect(isIOSInAppBrowser(UA.iosSafari)).toBe(false); |
| 69 | + }); |
| 70 | + it('does not flag Chrome on iOS as in-app', () => { |
| 71 | + expect(isIOSInAppBrowser(UA.iosChrome)).toBe(false); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe('isAndroidInAppBrowser', () => { |
| 76 | + it('detects generic Android WebView via wv) marker', () => { |
| 77 | + expect(isAndroidInAppBrowser(UA.androidWebview)).toBe(true); |
| 78 | + }); |
| 79 | + it('detects Facebook (FB_IAB/FBAN/FBAV)', () => { |
| 80 | + expect(isAndroidInAppBrowser(UA.androidFacebook)).toBe(true); |
| 81 | + }); |
| 82 | + it('detects Instagram', () => { |
| 83 | + expect(isAndroidInAppBrowser(UA.androidInstagram)).toBe(true); |
| 84 | + }); |
| 85 | + it('detects Line', () => { |
| 86 | + expect(isAndroidInAppBrowser(UA.androidLine)).toBe(true); |
| 87 | + }); |
| 88 | + it('detects WhatsApp', () => { |
| 89 | + expect(isAndroidInAppBrowser(UA.androidWhatsapp)).toBe(true); |
| 90 | + }); |
| 91 | + it('does not flag Chrome as in-app', () => { |
| 92 | + expect(isAndroidInAppBrowser(UA.androidChrome)).toBe(false); |
| 93 | + }); |
| 94 | + it('does not flag Firefox as in-app', () => { |
| 95 | + expect(isAndroidInAppBrowser(UA.androidFirefox)).toBe(false); |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +describe('pickMobileFallbackUrl — iOS regular browser (Safari/Chrome)', () => { |
| 100 | + it('prefers iOS store URL over web fallback', () => { |
| 101 | + const r = pickMobileFallbackUrl('ios', UA.iosSafari, URLS.iosStore, URLS.androidStore, URLS.webFallback); |
| 102 | + expect(r).toEqual({ url: URLS.iosStore, reason: 'ios_app_store_url' }); |
| 103 | + }); |
| 104 | + it('falls back to web fallback when iOS store URL is absent', () => { |
| 105 | + const r = pickMobileFallbackUrl('ios', UA.iosSafari, null, URLS.androidStore, URLS.webFallback); |
| 106 | + expect(r).toEqual({ url: URLS.webFallback, reason: 'web_fallback_url' }); |
| 107 | + }); |
| 108 | + it('uses iOS store URL when web fallback is absent', () => { |
| 109 | + const r = pickMobileFallbackUrl('ios', UA.iosSafari, URLS.iosStore, URLS.androidStore, null); |
| 110 | + expect(r).toEqual({ url: URLS.iosStore, reason: 'ios_app_store_url' }); |
| 111 | + }); |
| 112 | + it('returns null when both iOS store URL and web fallback are absent', () => { |
| 113 | + expect(pickMobileFallbackUrl('ios', UA.iosSafari, null, URLS.androidStore, null)).toBeNull(); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +describe('pickMobileFallbackUrl — iOS in-app browser (Gmail/FB/Instagram/etc.)', () => { |
| 118 | + it('prefers web fallback over iOS store URL', () => { |
| 119 | + const r = pickMobileFallbackUrl('ios', UA.iosGmail, URLS.iosStore, URLS.androidStore, URLS.webFallback); |
| 120 | + expect(r).toEqual({ url: URLS.webFallback, reason: 'web_fallback_url' }); |
| 121 | + }); |
| 122 | + it('falls back to iOS store URL when web fallback is absent', () => { |
| 123 | + const r = pickMobileFallbackUrl('ios', UA.iosGmail, URLS.iosStore, URLS.androidStore, null); |
| 124 | + expect(r).toEqual({ url: URLS.iosStore, reason: 'ios_app_store_url' }); |
| 125 | + }); |
| 126 | + it('returns null when both iOS store URL and web fallback are absent', () => { |
| 127 | + expect(pickMobileFallbackUrl('ios', UA.iosGmail, null, URLS.androidStore, null)).toBeNull(); |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +describe('pickMobileFallbackUrl — Android regular browser (Chrome/Firefox)', () => { |
| 132 | + it('prefers Android store URL over web fallback', () => { |
| 133 | + const r = pickMobileFallbackUrl('android', UA.androidChrome, URLS.iosStore, URLS.androidStore, URLS.webFallback); |
| 134 | + expect(r).toEqual({ url: URLS.androidStore, reason: 'android_app_store_url' }); |
| 135 | + }); |
| 136 | + it('falls back to web fallback when Android store URL is absent', () => { |
| 137 | + const r = pickMobileFallbackUrl('android', UA.androidChrome, URLS.iosStore, null, URLS.webFallback); |
| 138 | + expect(r).toEqual({ url: URLS.webFallback, reason: 'web_fallback_url' }); |
| 139 | + }); |
| 140 | + it('uses Android store URL when web fallback is absent', () => { |
| 141 | + const r = pickMobileFallbackUrl('android', UA.androidChrome, URLS.iosStore, URLS.androidStore, null); |
| 142 | + expect(r).toEqual({ url: URLS.androidStore, reason: 'android_app_store_url' }); |
| 143 | + }); |
| 144 | + it('returns null when both Android store URL and web fallback are absent', () => { |
| 145 | + expect(pickMobileFallbackUrl('android', UA.androidChrome, URLS.iosStore, null, null)).toBeNull(); |
| 146 | + }); |
| 147 | +}); |
| 148 | + |
| 149 | +describe('pickMobileFallbackUrl — Android in-app browser (FB/Instagram/Line/WebView)', () => { |
| 150 | + it('prefers web fallback over Android store URL', () => { |
| 151 | + const r = pickMobileFallbackUrl('android', UA.androidFacebook, URLS.iosStore, URLS.androidStore, URLS.webFallback); |
| 152 | + expect(r).toEqual({ url: URLS.webFallback, reason: 'web_fallback_url' }); |
| 153 | + }); |
| 154 | + it('detects WebView via wv) marker and prefers web fallback', () => { |
| 155 | + const r = pickMobileFallbackUrl('android', UA.androidWebview, URLS.iosStore, URLS.androidStore, URLS.webFallback); |
| 156 | + expect(r).toEqual({ url: URLS.webFallback, reason: 'web_fallback_url' }); |
| 157 | + }); |
| 158 | + it('falls back to Android store URL when web fallback is absent', () => { |
| 159 | + const r = pickMobileFallbackUrl('android', UA.androidFacebook, URLS.iosStore, URLS.androidStore, null); |
| 160 | + expect(r).toEqual({ url: URLS.androidStore, reason: 'android_app_store_url' }); |
| 161 | + }); |
| 162 | +}); |
| 163 | + |
| 164 | +describe('pickMobileFallbackUrl — reporter scenario regression test', () => { |
| 165 | + // Reproduces SIT-163: user creates a link with iOS, Android, and web fallback URLs; |
| 166 | + // mobile visitor expects the App/Play Store; previously got the web fallback. |
| 167 | + it('iOS Safari → iOS App Store (was: web fallback)', () => { |
| 168 | + const r = pickMobileFallbackUrl('ios', UA.iosSafari, URLS.iosStore, URLS.androidStore, URLS.webFallback); |
| 169 | + expect(r?.url).toBe(URLS.iosStore); |
| 170 | + expect(r?.reason).toBe('ios_app_store_url'); |
| 171 | + }); |
| 172 | + it('Android Chrome → Play Store (was: web fallback)', () => { |
| 173 | + const r = pickMobileFallbackUrl('android', UA.androidChrome, URLS.iosStore, URLS.androidStore, URLS.webFallback); |
| 174 | + expect(r?.url).toBe(URLS.androidStore); |
| 175 | + expect(r?.reason).toBe('android_app_store_url'); |
| 176 | + }); |
| 177 | + // And the email-marketing flow that the prior fix protected stays correct: |
| 178 | + it('iOS Gmail in-app → web fallback (preserves UL second-chance)', () => { |
| 179 | + const r = pickMobileFallbackUrl('ios', UA.iosGmail, URLS.iosStore, URLS.androidStore, URLS.webFallback); |
| 180 | + expect(r?.url).toBe(URLS.webFallback); |
| 181 | + }); |
| 182 | + it('Android Facebook in-app → web fallback (preserves UL second-chance)', () => { |
| 183 | + const r = pickMobileFallbackUrl('android', UA.androidFacebook, URLS.iosStore, URLS.androidStore, URLS.webFallback); |
| 184 | + expect(r?.url).toBe(URLS.webFallback); |
| 185 | + }); |
| 186 | +}); |
0 commit comments