zcodeSourceHeaders.test.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildZCodeSourceHeadersFromContext,
  4. ZCODE_SOURCE_HEADERS,
  5. } from "../src/zcode-source-headers.js";
  6. describe("buildZCodeSourceHeadersFromContext", () => {
  7. it("builds the shared ZCode endpoint attribution headers", () => {
  8. expect(
  9. buildZCodeSourceHeadersFromContext({
  10. appVersion: " 2.14.0 ",
  11. arch: "arm64",
  12. clientLanguage: "zh-CN",
  13. clientTimezone: "Asia/Shanghai",
  14. deviceMid: "mid-1",
  15. endpointOrigin: "https://zcode.z.ai",
  16. osVersion: "Darwin 25.0.0",
  17. platform: "darwin",
  18. releaseChannel: "test",
  19. sourceTitle: "cli",
  20. }),
  21. ).toEqual({
  22. ...ZCODE_SOURCE_HEADERS,
  23. "HTTP-Referer": "https://zcode.z.ai",
  24. "User-Agent": "ZCode/2.14.0",
  25. "X-ZCode-App-Version": "2.14.0",
  26. "X-Title": "Z Code@cli",
  27. "X-Platform": "darwin-arm64",
  28. "X-Release-Channel": "test",
  29. "X-Client-Language": "zh-CN",
  30. "X-Client-Timezone": "Asia/Shanghai",
  31. "X-Os-Category": "macos",
  32. "X-Os-Version": "Darwin 25.0.0",
  33. "X-Device-Mid": "mid-1",
  34. });
  35. });
  36. it("drops invalid optional values without emitting malformed headers", () => {
  37. const headers = buildZCodeSourceHeadersFromContext({
  38. appVersion: "bad\nversion",
  39. clientLanguage: "\n",
  40. clientTimezone: "",
  41. deviceMid: "bad\nmid",
  42. platform: "win32",
  43. sourceTitle: "electron",
  44. });
  45. expect(headers).toMatchObject({
  46. "User-Agent": "ZCode/unknown",
  47. "X-Client-Language": "unknown",
  48. "X-Client-Timezone": "unknown",
  49. "X-Os-Category": "windows",
  50. "X-Title": "Z Code@electron",
  51. });
  52. expect(headers).not.toHaveProperty("X-Device-Mid");
  53. expect(headers).not.toHaveProperty("X-ZCode-App-Version");
  54. });
  55. });