feat: 移动端设备信息最大化采集(型号/UA-CH/视口/方向/触点/内存/网络/深浅色)
SDK:buildEnvContext 同步扩展 12 键(dpr/viewport/orientation/maxTouchPoints/ deviceMemory/languages/colorScheme/standalone/network downlink·rtt·saveData/ 微信版本),逐键 try/catch 静默缺则不挂;新增 collectHighEntropyHints(UA-CH model/platformVersion/architecture/bitness),init 后 Promise.race 2s 超时 异步并入 __ctk_ctx——批次 context flush 时读取,不阻塞首屏。 后端:Visitor 加 device_model/env_extra(迁移 0005);ingest 首批快照 EXTENDED_ENV_KEYS 白名单(值截 100),非首批不覆盖、唯 device_model 空且 本批有值时补填一次(晚到 hints);列表/详情 API 随行输出。 前端:详情访问环境卡设备行升级显示型号(手机 · Pixel 7),追加像素比/ 视口/屏幕方向/触点/内存/网络质量/深浅色/微信版本行(env_extra 逐键判空, 旧数据整块不显示);列表设备列主行拼型号(手机·Pixel 7)。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
+148
-2
@@ -1,5 +1,11 @@
|
||||
import { describe, it, expect, afterEach } from "vitest";
|
||||
import { buildEnvContext, parseUtm, sanitizeUrl, pageContext } from "../src/context";
|
||||
import { describe, it, expect, afterEach, vi } from "vitest";
|
||||
import {
|
||||
buildEnvContext,
|
||||
collectHighEntropyHints,
|
||||
parseUtm,
|
||||
sanitizeUrl,
|
||||
pageContext,
|
||||
} from "../src/context";
|
||||
|
||||
// userAgent 是 Navigator.prototype 上的 accessor;实例上 defineProperty 覆盖,delete 还原
|
||||
function stubUA(ua: string) {
|
||||
@@ -149,6 +155,146 @@ describe("buildEnvContext", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildEnvContext 扩展采集(移动端设备信息最大化)", () => {
|
||||
const restore: Array<() => void> = [];
|
||||
/** 实例属性覆盖 + 注册还原(navigator/screen/window 各 API 均 accessor,defineProperty 覆盖) */
|
||||
const stub = (obj: object, key: string, value: unknown) => {
|
||||
Object.defineProperty(obj, key, { value, configurable: true });
|
||||
restore.push(() => delete (obj as Record<string, unknown>)[key]);
|
||||
};
|
||||
/** 按查询串精确命中的 matchMedia 桩(orientation/color_scheme/standalone 共用) */
|
||||
const stubMatchMedia = (matched: string[] = []) => {
|
||||
vi.stubGlobal(
|
||||
"matchMedia",
|
||||
(q: string) => ({ matches: matched.includes(q), media: q }),
|
||||
);
|
||||
};
|
||||
afterEach(() => {
|
||||
restore.splice(0).forEach((fn) => fn());
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it("同步扩展键全量采集(dpr/视口/方向/触点/内存/语言/深浅色/standalone/网络/微信版本)", () => {
|
||||
stubUA(UA_ANDROID_WECHAT);
|
||||
stub(window, "devicePixelRatio", 3);
|
||||
stub(window, "innerWidth", 390);
|
||||
stub(window, "innerHeight", 844);
|
||||
stub(screen, "orientation", { type: "portrait-primary" });
|
||||
stub(navigator, "maxTouchPoints", 5);
|
||||
stub(navigator, "deviceMemory", 8);
|
||||
stub(navigator, "languages", ["zh-CN", "en", "ja"]);
|
||||
stub(navigator, "connection", {
|
||||
effectiveType: "4g",
|
||||
downlink: 10,
|
||||
rtt: 50,
|
||||
saveData: true,
|
||||
});
|
||||
stubMatchMedia(["(prefers-color-scheme: dark)"]); // orientation 走 screen.orientation,深色走桩
|
||||
|
||||
const env = buildEnvContext();
|
||||
expect(env.device_pixel_ratio).toBe(3);
|
||||
expect(env.viewport).toBe("390x844");
|
||||
expect(env.orientation).toBe("portrait-primary"); // screen.orientation.type 优先
|
||||
expect(env.max_touch_points).toBe(5);
|
||||
expect(env.device_memory).toBe(8);
|
||||
expect(env.languages).toBe("zh-CN,en,ja");
|
||||
expect(env.color_scheme).toBe("dark");
|
||||
expect(env.standalone).toBe("0"); // 桩未命中 standalone 查询
|
||||
expect(env.network_downlink).toBe(10);
|
||||
expect(env.network_rtt).toBe(50);
|
||||
expect(env.save_data).toBe("1");
|
||||
expect(env.wechat_version).toBe("8.0.49"); // UA 的 MicroMessenger/8.0.49
|
||||
});
|
||||
|
||||
it("screen.orientation 缺失 → matchMedia 兜底 portrait/landscape", () => {
|
||||
stubMatchMedia(["(orientation: portrait)"]);
|
||||
expect(buildEnvContext().orientation).toBe("portrait");
|
||||
stubMatchMedia([]); // portrait 不匹配 → landscape
|
||||
expect(buildEnvContext().orientation).toBe("landscape");
|
||||
});
|
||||
|
||||
it("languages 超 100 截断;saveData 布尔转 1/0", () => {
|
||||
stub(navigator, "languages", ["a".repeat(60), "b".repeat(60)]);
|
||||
stub(navigator, "connection", { effectiveType: "3g", saveData: false });
|
||||
const env = buildEnvContext();
|
||||
expect(env.languages).toHaveLength(100);
|
||||
expect(env.save_data).toBe("0");
|
||||
});
|
||||
|
||||
it("能力缺失环境:可选键不挂、不崩溃(无 matchMedia/触点/内存/连接)", () => {
|
||||
stubUA(UA_WIN_CHROME); // 无 MicroMessenger → 无 wechat_version
|
||||
stub(window, "matchMedia", undefined); // matchMedia 不可用
|
||||
stub(navigator, "maxTouchPoints", undefined);
|
||||
stub(navigator, "languages", undefined);
|
||||
const env = buildEnvContext();
|
||||
expect("orientation" in env).toBe(false); // screen.orientation 与 matchMedia 双缺
|
||||
expect("color_scheme" in env).toBe(false);
|
||||
expect("standalone" in env).toBe(false);
|
||||
expect("max_touch_points" in env).toBe(false);
|
||||
expect("languages" in env).toBe(false);
|
||||
expect("device_memory" in env).toBe(false);
|
||||
expect("network_downlink" in env).toBe(false);
|
||||
expect("save_data" in env).toBe(false);
|
||||
expect("wechat_version" in env).toBe(false);
|
||||
// 基础键照常(老环境只是缺扩展信号,主采集不受影响)
|
||||
expect(env.device_type).toBe("PC");
|
||||
expect(env.os).toBe("Windows");
|
||||
});
|
||||
});
|
||||
|
||||
describe("collectHighEntropyHints(UA-CH 高熵提示)", () => {
|
||||
const restore: Array<() => void> = [];
|
||||
const stubUAD = (impl: (hints: string[]) => Promise<Record<string, unknown>>) => {
|
||||
Object.defineProperty(navigator, "userAgentData", {
|
||||
value: { getHighEntropyValues: impl },
|
||||
configurable: true,
|
||||
});
|
||||
restore.push(() => delete (navigator as { userAgentData?: unknown }).userAgentData);
|
||||
};
|
||||
afterEach(() => restore.splice(0).forEach((fn) => fn()));
|
||||
|
||||
it("getHighEntropyValues → device_model/platform_version/architecture/bitness", async () => {
|
||||
stubUAD(async () => ({
|
||||
model: "Pixel 7",
|
||||
platformVersion: "13.0.0",
|
||||
architecture: "arm",
|
||||
bitness: "64",
|
||||
fullVersionList: [], // 未请求的键忽略
|
||||
}));
|
||||
expect(await collectHighEntropyHints()).toEqual({
|
||||
device_model: "Pixel 7",
|
||||
platform_version: "13.0.0",
|
||||
architecture: "arm",
|
||||
bitness: "64",
|
||||
});
|
||||
});
|
||||
|
||||
it("型号超长截断(64/32/16/8 各档)", async () => {
|
||||
stubUAD(async () => ({
|
||||
model: "M".repeat(80),
|
||||
platformVersion: "P".repeat(40),
|
||||
architecture: "A".repeat(20),
|
||||
bitness: "B".repeat(10),
|
||||
}));
|
||||
const hints = await collectHighEntropyHints();
|
||||
expect(hints.device_model).toHaveLength(64);
|
||||
expect(hints.platform_version).toHaveLength(32);
|
||||
expect(hints.architecture).toHaveLength(16);
|
||||
expect(hints.bitness).toHaveLength(8);
|
||||
});
|
||||
|
||||
it("空值键不挂(model 空串等)", async () => {
|
||||
stubUAD(async () => ({ model: "", platformVersion: undefined, architecture: null }));
|
||||
expect(await collectHighEntropyHints()).toEqual({});
|
||||
});
|
||||
|
||||
it("无 userAgentData / getHighEntropyValues 抛异常 → 空对象不崩", async () => {
|
||||
expect(await collectHighEntropyHints()).toEqual({}); // happy-dom 默认无 userAgentData
|
||||
stubUAD(() => Promise.reject(new Error("denied")));
|
||||
expect(await collectHighEntropyHints()).toEqual({});
|
||||
});
|
||||
});
|
||||
|
||||
describe("pageContext", () => {
|
||||
it("path 清洗 + utm 提取 + title 读取", async () => {
|
||||
await (window as any).happyDOM.setURL("http://localhost/p?token=1&utm_source=g");
|
||||
|
||||
Reference in New Issue
Block a user