| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { browserClientOs, browserDeviceType, clientPresentationMetadata } from "./clientMetadata"; |
| 4 | |
| 5 | const desktopChrome = { |
| 6 | userAgent: |
| 7 | "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/140.0.0.0 Safari/537.36", |
| 8 | platform: "Win32", |
| 9 | maxTouchPoints: 0, |
| 10 | }; |
| 11 | |
| 12 | describe("client presentation metadata", () => { |
| 13 | it("identifies web clients and omits the development app version", () => { |
| 14 | expect( |
| 15 | clientPresentationMetadata({ |
| 16 | appVersion: "1.2.3", |
| 17 | identity: desktopChrome, |
| 18 | desktopBridge: undefined, |
| 19 | }), |
| 20 | ).toMatchObject({ |
| 21 | surface: "web", |
| 22 | deviceType: "desktop", |
| 23 | os: "Windows", |
| 24 | appVersion: "1.2.3", |
| 25 | }); |
| 26 | |
| 27 | expect( |
| 28 | clientPresentationMetadata({ |
| 29 | appVersion: "0.0.0", |
| 30 | identity: desktopChrome, |
| 31 | desktopBridge: undefined, |
| 32 | }), |
| 33 | ).not.toHaveProperty("appVersion"); |
| 34 | }); |
| 35 | |
| 36 | it("identifies phone and tablet browsers", () => { |
| 37 | const iphone = { |
| 38 | userAgent: |
| 39 | "Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 Version/18.6 Mobile/15E148 Safari/604.1", |
| 40 | platform: "iPhone", |
| 41 | maxTouchPoints: 5, |
| 42 | }; |
| 43 | const androidTablet = { |
| 44 | userAgent: |
| 45 | "Mozilla/5.0 (Linux; Android 15; Pixel Tablet) AppleWebKit/537.36 Chrome/140.0.0.0 Safari/537.36", |
| 46 | platform: "Linux armv8l", |
| 47 | maxTouchPoints: 5, |
| 48 | }; |
| 49 | const ipadosDesktopUa = { |
| 50 | userAgent: |
| 51 | "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 Version/18.0 Safari/605.1.15", |
| 52 | platform: "MacIntel", |
| 53 | maxTouchPoints: 5, |
| 54 | }; |
| 55 | |
| 56 | expect(browserDeviceType(iphone)).toBe("mobile"); |
| 57 | expect(browserClientOs(iphone)).toBe("iOS"); |
| 58 | expect(browserDeviceType(androidTablet)).toBe("tablet"); |
| 59 | expect(browserClientOs(androidTablet)).toBe("Android"); |
| 60 | expect(browserDeviceType(ipadosDesktopUa)).toBe("tablet"); |
| 61 | expect(browserClientOs(ipadosDesktopUa)).toBe("iOS"); |
| 62 | }); |
| 63 | |
| 64 | it("uses Electron's client platform for desktop", () => { |
| 65 | expect( |
| 66 | clientPresentationMetadata({ |
| 67 | appVersion: "1.2.3", |
| 68 | identity: desktopChrome, |
| 69 | desktopBridge: { getClientPlatform: () => "darwin" }, |
| 70 | }), |
| 71 | ).toEqual({ |
| 72 | label: "The Knife Desktop", |
| 73 | deviceType: "desktop", |
| 74 | os: "macOS", |
| 75 | surface: "desktop", |
| 76 | appVersion: "1.2.3", |
| 77 | }); |
| 78 | }); |
| 79 | }); |