agentTelemetryEnv.test.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { describe, expect, it } from "vitest";
  2. import { buildAgentTelemetrySpawnEnv } from "../src/zcode-agent/agentTelemetryEnv.js";
  3. describe("buildAgentTelemetrySpawnEnv", () => {
  4. it("未配置 OTLP 时不注入身份", () => {
  5. expect(
  6. buildAgentTelemetrySpawnEnv({
  7. deviceMid: "device",
  8. runtimeSurface: "desktop_local_host",
  9. telemetryEnv: {},
  10. userId: "user",
  11. }),
  12. ).toEqual({});
  13. });
  14. it("App 注入 OTLP、device mid 和不可读 user subject,不传原始 UID", () => {
  15. const result = buildAgentTelemetrySpawnEnv({
  16. deviceMid: "device-1",
  17. runtimeSurface: "desktop_local_host",
  18. telemetryEnv: {
  19. OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://arms.example.com/v1/traces",
  20. OTEL_EXPORTER_OTLP_TRACES_HEADERS: "Authorization=secret",
  21. },
  22. userId: "account-1",
  23. });
  24. expect(result).toMatchObject({
  25. OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://arms.example.com/v1/traces",
  26. ZCODE_TELEMETRY_DEVICE_MID: "device-1",
  27. ZCODE_TELEMETRY_IDENTITY_STATE: "authenticated",
  28. ZCODE_TELEMETRY_RUNTIME_SURFACE: "desktop_local_host",
  29. });
  30. expect(result.ZCODE_TELEMETRY_USER_SUBJECT_ID).toMatch(/^[0-9a-f]{64}$/u);
  31. expect(result.ZCODE_TELEMETRY_USER_ID).toBeUndefined();
  32. });
  33. });