Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

clientMetadata.test.ts

90 lines2.6 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 browserClientOs,
5 browserDeviceType,
6 browserFamily,
7 clientPresentationMetadata,
8} from "./clientMetadata";
9​
10const desktopChrome = {
11 userAgent:
12 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/140.0.0.0 Safari/537.36",
13 platform: "Win32",
14 maxTouchPoints: 0,
15};
16​
17describe("client telemetry metadata", () => {
18 it("distinguishes hosted web from server-served web", () => {
19 expect(
20 clientPresentationMetadata({
21 appVersion: "1.2.3",
22 hosted: true,
23 identity: desktopChrome,
24 desktopBridge: undefined,
25 }),
26 ).toMatchObject({
27 surface: "web",
28 webDeployment: "hosted",
29 deviceType: "desktop",
30 os: "Windows",
31 browser: "Chrome",
32 appVersion: "1.2.3",
33 });
34​
35 expect(
36 clientPresentationMetadata({
37 appVersion: "0.0.0",
38 hosted: false,
39 identity: desktopChrome,
40 desktopBridge: undefined,
41 }),
42 ).toMatchObject({ surface: "web", webDeployment: "server" });
43 });
44​
45 it("identifies phone and tablet browsers", () => {
46 const iphone = {
47 userAgent:
48 "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",
49 platform: "iPhone",
50 maxTouchPoints: 5,
51 };
52 const androidTablet = {
53 userAgent:
54 "Mozilla/5.0 (Linux; Android 15; Pixel Tablet) AppleWebKit/537.36 Chrome/140.0.0.0 Safari/537.36",
55 platform: "Linux armv8l",
56 maxTouchPoints: 5,
57 };
58 const ipadosDesktopUa = {
59 userAgent:
60 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 Version/18.0 Safari/605.1.15",
61 platform: "MacIntel",
62 maxTouchPoints: 5,
63 };
64​
65 expect(browserDeviceType(iphone)).toBe("mobile");
66 expect(browserClientOs(iphone)).toBe("iOS");
67 expect(browserFamily(iphone.userAgent)).toBe("Safari");
68 expect(browserDeviceType(androidTablet)).toBe("tablet");
69 expect(browserClientOs(androidTablet)).toBe("Android");
70 expect(browserDeviceType(ipadosDesktopUa)).toBe("tablet");
71 expect(browserClientOs(ipadosDesktopUa)).toBe("iOS");
72 });
73​
74 it("uses Electron's client platform for desktop", () => {
75 expect(
76 clientPresentationMetadata({
77 appVersion: "1.2.3",
78 hosted: false,
79 identity: desktopChrome,
80 desktopBridge: { getClientPlatform: () => "darwin" },
81 }),
82 ).toEqual({
83 label: "T3 Code Desktop",
84 deviceType: "desktop",
85 os: "macOS",
86 surface: "desktop",
87 appVersion: "1.2.3",
88 });
89 });
90});