provider-endpoint-routing-source-headers.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
  2. import { arch as readOsArch, release as readOsRelease, tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import { createProviderEndpointRoutingSourceHeaders } from "../src/provider-endpoint-routing-source-headers.js";
  6. const tempDirs: string[] = [];
  7. afterEach(async () => {
  8. await Promise.all(
  9. tempDirs.splice(0).map((dir) => rm(dir, { force: true, recursive: true })),
  10. );
  11. });
  12. describe("createProviderEndpointRoutingSourceHeaders", () => {
  13. it("adds runtime attribution and reuses an existing deviceMid for the config GET", async () => {
  14. const dataBaseDir = await mkdtemp(
  15. join(tmpdir(), "zcode-routing-source-headers-"),
  16. );
  17. tempDirs.push(dataBaseDir);
  18. const stateDir = join(dataBaseDir, ".zcode", "v2");
  19. await mkdir(stateDir, { recursive: true });
  20. await writeFile(
  21. join(stateDir, "telemetry-state.json"),
  22. JSON.stringify({ deviceMid: "mid-routing-1" }),
  23. "utf8",
  24. );
  25. const headers = await createProviderEndpointRoutingSourceHeaders({
  26. appVersion: "1.2.3",
  27. env: {
  28. ZCODE_DATA_BASE_DIR: dataBaseDir,
  29. ZCODE_ENV: "test",
  30. ZCODE_TEST_BASE_URL: "https://routing-config.example.test",
  31. },
  32. sourceTitle: "electron",
  33. });
  34. expect(headers).toMatchObject({
  35. "HTTP-Referer": "https://routing-config.example.test",
  36. "User-Agent": "ZCode/1.2.3",
  37. "X-Client-Language": expect.any(String),
  38. "X-Client-Timezone": expect.any(String),
  39. "X-Device-Mid": "mid-routing-1",
  40. "X-Os-Category": osCategory(process.platform),
  41. "X-Os-Version": readOsRelease(),
  42. "X-Platform": `${process.platform}-${readOsArch()}`,
  43. "X-Release-Channel": "test",
  44. "X-Title": "Z Code@electron",
  45. "X-ZCode-App-Version": "1.2.3",
  46. });
  47. expect(headers).not.toHaveProperty("Authorization");
  48. });
  49. it("defaults to CLI attribution and does not create a missing device identity", async () => {
  50. const dataBaseDir = await mkdtemp(
  51. join(tmpdir(), "zcode-routing-no-device-mid-"),
  52. );
  53. tempDirs.push(dataBaseDir);
  54. const headers = await createProviderEndpointRoutingSourceHeaders({
  55. env: {
  56. ZCODE_DATA_BASE_DIR: dataBaseDir,
  57. ZCODE_ENV: "production",
  58. ZCODE_APP_VERSION: "2.0.0",
  59. },
  60. });
  61. expect(headers).toMatchObject({
  62. "HTTP-Referer": "https://zcode.z.ai",
  63. "User-Agent": "ZCode/2.0.0",
  64. "X-Release-Channel": "production",
  65. "X-Title": "Z Code@cli",
  66. });
  67. expect(headers).not.toHaveProperty("X-Device-Mid");
  68. });
  69. });
  70. function osCategory(platform: NodeJS.Platform): string {
  71. if (platform === "darwin") return "macos";
  72. if (platform === "win32") return "windows";
  73. return "linux";
  74. }