import test from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { runInNewContext } from "node:vm"; const sdkSource = readFileSync(new URL("../public/static/common/web-sdk-v1.2.0.js", import.meta.url), "utf8"); interface SDKContext { navigator?: { userAgent: string; platform: string; maxTouchPoints: number }; WebSDK?: { getCommonFields(fields?: string[]): Promise> }; } function loadSDK(userAgent?: string, platform = "", maxTouchPoints = 0) { const context: SDKContext = {}; if (userAgent !== undefined) context.navigator = { userAgent, platform, maxTouchPoints }; runInNewContext(sdkSource, context, { filename: "web-sdk-v1.2.0.js" }); assert.ok(context.WebSDK); return context.WebSDK; } const userAgents = [ { name: "iPhone", ua: "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 Version/18.0 Mobile/15E148 Safari/604.1", platform: "iPhone", touch: 5, system: "iOS", version: "18.0", }, { name: "iPad mobile mode", ua: "Mozilla/5.0 (iPad; CPU OS 17_6_1 like Mac OS X) AppleWebKit/605.1.15 Version/17.6 Mobile/15E148 Safari/604.1", platform: "iPad", touch: 5, system: "iOS", version: "17.6.1", }, { name: "Android", ua: "Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 Chrome/129.0.0.0 Mobile Safari/537.36", platform: "Linux armv8l", touch: 5, system: "Android", version: "14", }, { name: "Windows NT", ua: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/129.0.0.0 Safari/537.36", platform: "Win32", touch: 0, system: "Windows", version: "10.0", }, { name: "macOS", ua: "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_6_1) AppleWebKit/605.1.15 Version/17.6 Safari/605.1.15", platform: "MacIntel", touch: 0, system: "macOS", version: "14.6.1", }, { name: "iPad desktop mode without an exposed iOS version", ua: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 Version/18.0 Safari/605.1.15", platform: "MacIntel", touch: 5, system: "iOS", version: "", }, { name: "Linux without an exposed kernel version", ua: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/129.0.0.0 Safari/537.36", platform: "Linux x86_64", touch: 0, system: "Linux", version: "", }, { name: "unknown operating system", ua: "ExampleBrowser/1.0", platform: "", touch: 0, system: "Other", version: "", }, ]; for (const device of userAgents) { test(`bundled SDK common fields report the operating system for ${device.name}`, async () => { const sdk = loadSDK(device.ua, device.platform, device.touch); const common = await sdk.getCommonFields(); assert.equal(common.system_name, device.system); assert.equal(common.system_version, device.version); assert.equal(common.user_agent, device.ua); const selected = await sdk.getCommonFields(["system_name", "system_version"]); assert.deepEqual(JSON.parse(JSON.stringify(selected)), { system_name: device.system, system_version: device.version, }); }); } test("bundled SDK OS fields tolerate a missing navigator without inventing a version", async () => { const common = await loadSDK().getCommonFields(); assert.equal(common.system_name, "Other"); assert.equal(common.system_version, ""); });