telemetry-report-fixture.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import type { IncomingMessage, ServerResponse } from "node:http";
  2. export function createTelemetryReportFixture() {
  3. const reports: Array<Record<string, unknown>> = [];
  4. return async (req: IncomingMessage, res: ServerResponse) => {
  5. const path = new URL(req.url ?? "/", "http://localhost").pathname;
  6. if (path === "/__e2e/marketing/telemetry") {
  7. res.writeHead(200, { "Content-Type": "application/json" });
  8. res.end(JSON.stringify(reports));
  9. return true;
  10. }
  11. if (path !== "/api/v1/event/report") return false;
  12. const chunks: Buffer[] = [];
  13. for await (const chunk of req) chunks.push(Buffer.from(chunk));
  14. const body = JSON.parse(Buffer.concat(chunks).toString() || "{}");
  15. // 只保存格式/一致性断言,不把 JWT 放入 fixture 响应或 WDIO 日志。
  16. reports.push({
  17. element: body.element_name,
  18. authenticated: Boolean(req.headers.authorization),
  19. bearer: /^Bearer \S+$/.test(req.headers.authorization ?? ""),
  20. languageMatches: req.headers["x-client-language"] === body.client_language,
  21. timezoneMatches: req.headers["x-client-timezone"] === body.client_timezone,
  22. deviceMatches: Boolean(body.device_mid) && req.headers["x-device-mid"] === body.device_mid,
  23. versionMatches: req.headers["x-zcode-app-version"] === body.app_version,
  24. osMatches:
  25. req.headers["x-os-category"] === body.device_os_category &&
  26. req.headers["x-os-version"] === body.device_os_version,
  27. userAgentMatches: req.headers["user-agent"] === `ZCode/${body.app_version}`,
  28. title: req.headers["x-title"],
  29. platform: Boolean(req.headers["x-platform"]),
  30. channel: Boolean(req.headers["x-release-channel"]),
  31. refererMatches: req.headers["http-referer"] === `http://${req.headers.host}`,
  32. });
  33. res.writeHead(204);
  34. res.end();
  35. return true;
  36. };
  37. }