Initial commit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
谢宇宁
2026-11-05 20:30:26 +07:00
co-authored by Claude Opus 5.5
commit 6bd5cc665a
335 changed files with 44261 additions and 0 deletions
+99
View File
@@ -0,0 +1,99 @@
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import test from "node:test";
import ts from "typescript";
async function loadReportFields() {
const code = ts.transpileModule(
await readFile(new URL("../src/lib/analytics/report-fields.ts", import.meta.url), "utf8"),
{ compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2022 } },
).outputText;
return import(`data:text/javascript;base64,${Buffer.from(code).toString("base64")}`);
}
const { augmentReportBody, parseUAClientInfo } = await loadReportFields();
const iphone =
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Mobile/15E148 Safari/604.1";
const android =
"Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36";
const windows =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
const mac =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15";
test("上报公共字段能解析系统名称和系统版本", () => {
assert.deepEqual(parseUAClientInfo(iphone, {}), {
device_brand: "Apple",
device_model: "iPhone",
system_name: "iOS",
system_version: "17.2",
});
assert.deepEqual(parseUAClientInfo(android, {}), {
device_brand: "Google",
device_model: "Pixel 7",
system_name: "Android",
system_version: "13",
});
assert.deepEqual(parseUAClientInfo(windows, {}), {
device_brand: "",
device_model: "PC",
system_name: "Windows",
system_version: "10",
});
assert.deepEqual(parseUAClientInfo(mac, {}), {
device_brand: "Apple",
device_model: "Mac",
system_name: "macOS",
system_version: "10.15.7",
});
});
test("iPad 桌面模式按触控点数识别为 iOS", () => {
const ua =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Safari/605.1.15";
assert.deepEqual(parseUAClientInfo(ua, { touchPoints: 5 }), {
device_brand: "Apple",
device_model: "iPad",
system_name: "iOS",
system_version: "17.2",
});
});
test("广告批量上报会补上系统名称和系统版本", () => {
const body = JSON.stringify([
{
event: "advertising",
device_brand: "",
device_model: "",
user_agent: iphone,
payload: { event_type: "show", advertising_key: "dialog_ad", advertising_id: "9" },
},
]);
const next = augmentReportBody(body, { ua: iphone, env: {} });
const [record] = JSON.parse(next);
assert.equal(record.system_name, "iOS");
assert.equal(record.system_version, "17.2");
assert.equal(record.device_brand, "Apple");
assert.equal(record.device_model, "iPhone");
assert.equal(record.device, "iOS");
assert.equal(record.payload.advertising_key, "dialog_ad");
});
test("已有系统字段不会被覆盖", () => {
const body = JSON.stringify([
{
system_name: "Android",
system_version: "14",
device_brand: "Samsung",
device_model: "SM-S9110",
device: "Android",
device_fingerprint: "fp",
fp_version: "3.0.1",
sdk_version: "1.2.0",
app_version: "1.0.0",
user_agent: android,
},
]);
assert.equal(augmentReportBody(body, { ua: iphone, env: {} }), null);
});