|
1 | | -import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest"; |
2 | | - |
3 | | -import { createDefaultUserAgentProvider, PreviouslyResolved } from "."; |
4 | | - |
5 | | -const ua = |
6 | | - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"; |
7 | | - |
8 | | -const mockConfig: PreviouslyResolved = { |
9 | | - userAgentAppId: vi.fn().mockResolvedValue(undefined), |
10 | | -}; |
11 | | - |
12 | | -describe("createDefaultUserAgentProvider", () => { |
13 | | - beforeEach(() => { |
14 | | - vi.spyOn(window.navigator, "userAgent", "get").mockReturnValue(ua); |
15 | | - }); |
16 | | - |
17 | | - afterEach(() => { |
18 | | - vi.clearAllMocks(); |
19 | | - }); |
20 | | - |
21 | | - it("should populate metrics", async () => { |
22 | | - const userAgent = await createDefaultUserAgentProvider({ serviceId: "s3", clientVersion: "0.1.0" })(mockConfig); |
23 | | - expect(userAgent[0]).toEqual(["aws-sdk-js", "0.1.0"]); |
24 | | - expect(userAgent[1]).toEqual(["ua", "2.1"]); |
25 | | - expect(userAgent[2]).toEqual(["os/macOS", "10.15.7"]); |
26 | | - expect(userAgent[3]).toEqual(["lang/js"]); |
27 | | - expect(userAgent[4]).toEqual(["md/browser", "Chrome_86.0.4240.111"]); |
28 | | - expect(userAgent[5]).toEqual(["api/s3", "0.1.0"]); |
29 | | - expect(userAgent.length).toBe(6); |
30 | | - }); |
31 | | - |
32 | | - it("should populate metrics when service id not available", async () => { |
33 | | - const userAgent = await createDefaultUserAgentProvider({ serviceId: undefined, clientVersion: "0.1.0" })( |
34 | | - mockConfig |
35 | | - ); |
36 | | - expect(userAgent).not.toContainEqual(["api/s3", "0.1.0"]); |
37 | | - expect(userAgent.length).toBe(5); |
38 | | - }); |
39 | | - |
40 | | - it("should include appId when provided", async () => { |
41 | | - const configWithAppId: PreviouslyResolved = { |
42 | | - userAgentAppId: vi.fn().mockResolvedValue("test-app-id"), |
43 | | - }; |
44 | | - const userAgent = await createDefaultUserAgentProvider({ serviceId: "s3", clientVersion: "0.1.0" })( |
45 | | - configWithAppId |
46 | | - ); |
47 | | - expect(userAgent[6]).toEqual(["app/test-app-id"]); |
48 | | - expect(userAgent.length).toBe(7); |
49 | | - }); |
50 | | - |
51 | | - it("should not include appId when not provided", async () => { |
52 | | - const userAgent = await createDefaultUserAgentProvider({ serviceId: "s3", clientVersion: "0.1.0" })(mockConfig); |
53 | | - expect(userAgent).not.toContainEqual(expect.arrayContaining(["app/"])); |
54 | | - expect(userAgent.length).toBe(6); |
| 1 | +import { describe, expect, test as it } from "vitest"; |
| 2 | + |
| 3 | +import { fallback } from "./index"; |
| 4 | + |
| 5 | +describe("ua fallback parsing", () => { |
| 6 | + const samples = [ |
| 7 | + `Mozilla/5.0 (Macintosh; Intel Mac OS X 15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6 Safari/605.1.15`, |
| 8 | + `Mozilla/5.0 (iPhone; CPU iPhone OS 18_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6.2 Mobile/15E148 Safari/604.1`, |
| 9 | + `Mozilla/5.0 (iPad; CPU OS 18_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6.2 Mobile/15E148 Safari/604.1`, |
| 10 | + `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36`, |
| 11 | + `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36`, |
| 12 | + `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36`, |
| 13 | + `Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.7390.44 Mobile Safari/537.36`, |
| 14 | + `Mozilla/5.0 (Linux; Android 16; LM-Q710(FGN)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.7390.44 Mobile Safari/537.36`, |
| 15 | + `Mozilla/5.0 (Android 16; Mobile; LG-M255; rv:143.0) Gecko/143.0 Firefox/143.0`, |
| 16 | + `Mozilla/5.0 (Android 16; Mobile; rv:68.0) Gecko/68.0 Firefox/143.0`, |
| 17 | + `Mozilla/5.0 (iPhone; CPU iPhone OS 18_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 EdgiOS/140.3485.94 Mobile/15E148 Safari/605.1.15`, |
| 18 | + `Mozilla/5.0 (Linux; Android 10; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.7390.44 Mobile Safari/537.36 EdgA/140.0.3485.98`, |
| 19 | + `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.3537.57`, |
| 20 | + `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.3537.57`, |
| 21 | + `Mozilla/5.0 (X11; Linux i686; rv:143.0) Gecko/20100101 Firefox/143.0`, |
| 22 | + ]; |
| 23 | + |
| 24 | + const expectedBrands = [ |
| 25 | + ["macOS", "Safari"], |
| 26 | + ["iOS", "Safari"], |
| 27 | + ["iOS", "Safari"], |
| 28 | + ["macOS", "Chrome"], |
| 29 | + ["Windows", "Chrome"], |
| 30 | + ["Linux", "Chrome"], |
| 31 | + ["Android", "Chrome"], |
| 32 | + ["Android", "Chrome"], |
| 33 | + ["Android", "Firefox"], |
| 34 | + ["Android", "Firefox"], |
| 35 | + ["iOS", "Microsoft Edge"], |
| 36 | + ["Android", "Microsoft Edge"], |
| 37 | + ["macOS", "Microsoft Edge"], |
| 38 | + ["Windows", "Microsoft Edge"], |
| 39 | + ["Linux", "Firefox"], |
| 40 | + ]; |
| 41 | + |
| 42 | + it("should detect os and browser", () => { |
| 43 | + for (let i = 0; i < expectedBrands.length; ++i) { |
| 44 | + expect([fallback.os(samples[i]), fallback.browser(samples[i])]).toEqual(expectedBrands[i]); |
| 45 | + } |
55 | 46 | }); |
56 | 47 | }); |
0 commit comments