/** * The device labeller behind the security screen. * * The cases that matter are the impostors: every Chromium browser claims to be * Chrome, every iOS browser claims to be Safari, and Edge claims both. A parser * that gets the obvious strings right and these wrong is a parser that labels * most of the world "Chrome". */ import { describe, expect, it } from 'vitest'; import { describeDevice } from '../src/lib/user-agent'; const UA = { chromeWindows: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36', edgeWindows: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0', safariIphone: 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1', chromeAndroid: 'Mozilla/5.0 (Linux; Android 15; Pixel 9) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Mobile Safari/537.36', firefoxMac: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:130.0) Gecko/20100101 Firefox/130.0', }; describe('describeDevice', () => { it('reads the plain cases', () => { expect(describeDevice(UA.chromeWindows)).toBe('Chrome on Windows'); expect(describeDevice(UA.firefoxMac)).toBe('Firefox on Mac'); }); it('does not call Edge "Chrome", though Edge says it is', () => { expect(describeDevice(UA.edgeWindows)).toBe('Edge on Windows'); }); it('does not call an iPhone a Mac, though it says "like Mac OS X"', () => { expect(describeDevice(UA.safariIphone)).toBe('Safari on iPhone'); }); it('prefers Android over the Linux it is built on', () => { expect(describeDevice(UA.chromeAndroid)).toBe('Chrome on Android'); }); it('returns what it knows when it only knows half', () => { expect(describeDevice('Mozilla/5.0 (Windows NT 10.0)')).toBe('Windows'); expect(describeDevice('Firefox/130.0')).toBe('Firefox'); }); it('says nothing rather than guessing', () => { expect(describeDevice(null)).toBeNull(); expect(describeDevice(undefined)).toBeNull(); expect(describeDevice('')).toBeNull(); expect(describeDevice('curl/8.7.1')).toBeNull(); }); });