providerTelemetryIdentity.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { describe, expect, it, vi } from "vitest";
  2. import { encodeCustomModelValue, type ZCodeAutomation } from "@zcode/shared";
  3. import {
  4. buildV4ConversationPromptTelemetryExtraDetail,
  5. resolveLegacyRuntimeModelValue,
  6. } from "@/v4/telemetry/conversationPromptTelemetry.js";
  7. import {
  8. resolveAutomationSelectionTelemetry,
  9. reportAutomationActionClick,
  10. reportAutomationCreateResult,
  11. } from "@/lib/automationTelemetry.js";
  12. import { freezeOffPeakCreateTelemetrySnapshot } from "@/lib/offPeakTelemetry.js";
  13. import { buildCompactionTelemetryExtraDetail } from "@/lib/messageTelemetry.js";
  14. // 790884b1ce:Team 使用 canonical Coding Plan 身份;两家闲时共用同一 ID。
  15. const identities = [
  16. ["zai-api", "builtin:zai"],
  17. ["bigmodel-api", "builtin:bigmodel"],
  18. ...["zai", "bigmodel"].flatMap((family) => [
  19. [`account:${family}-individual-coding-plan`, `builtin:${family}-coding-plan`],
  20. [`account:${family}-team-coding-plan`, `builtin:${family}-coding-plan`],
  21. [`account:${family}-start-plan`, `builtin:${family}-start-plan`],
  22. [`account:${family}-offpeak-idle-plan`, "offpeak-idle-plan"],
  23. ]),
  24. ["personal-uuid", "personal-uuid"],
  25. ["builtin:zai-coding-plan", "builtin:zai-coding-plan"],
  26. ["account:future-plan", "account:future-plan"],
  27. ];
  28. describe("Provider 埋点旧口径", () => {
  29. it.each(["zai", "bigmodel"])(
  30. "%s 自动化成功/失败/立即运行的实际报告同桶,模型保持纯 ID",
  31. async (family) => {
  32. const platform = { reportTelemetryEvent: vi.fn(async () => {}) };
  33. const modelSelection = {
  34. providerId: `account:${family}-team-coding-plan`,
  35. modelId: "vendor/model:1%",
  36. };
  37. const modelFields = resolveAutomationSelectionTelemetry(modelSelection, null);
  38. await reportAutomationCreateResult(platform, {
  39. automationId: "automation",
  40. cronExpr: "0 * * * *",
  41. modelFields,
  42. });
  43. await reportAutomationCreateResult(platform, {
  44. error: "failed",
  45. cronExpr: "0 * * * *",
  46. modelFields,
  47. });
  48. await reportAutomationActionClick(platform, {
  49. action: "run_now",
  50. source: "list",
  51. automation: { automationId: "automation", modelSelection } as ZCodeAutomation,
  52. providerSettingsView: null,
  53. });
  54. expect(platform.reportTelemetryEvent).toHaveBeenCalledTimes(3);
  55. for (const [payload] of platform.reportTelemetryEvent.mock.calls) {
  56. expect(payload.eventExtraDetail).toMatchObject({
  57. model_provider: `builtin:${family}-coding-plan`,
  58. model_name: modelSelection.modelId,
  59. provider_name: "",
  60. });
  61. }
  62. },
  63. );
  64. it.each(identities)("%s → %s,模型 ID 无损且选择不变", (providerId, legacy) => {
  65. const modelId = "vendor/a:b%2F模型";
  66. const selection = Object.freeze({ providerId, modelId });
  67. const send = buildV4ConversationPromptTelemetryExtraDetail({
  68. configProvider: providerId,
  69. modelName: modelId,
  70. });
  71. expect(send.model_provider).toBe(legacy);
  72. expect(send.model_name).toBe(encodeCustomModelValue(legacy, modelId));
  73. expect(resolveLegacyRuntimeModelValue({ configProvider: providerId, modelName: modelId })).toBe(
  74. `${legacy}/${modelId}`,
  75. );
  76. expect(resolveAutomationSelectionTelemetry(selection, null)).toEqual({
  77. model_provider: legacy,
  78. model_name: modelId,
  79. provider_name: "",
  80. });
  81. expect(selection).toEqual({ providerId, modelId });
  82. });
  83. it.each(["zai", "bigmodel"])("%s 闲时冻结旧桶,纯模型值不变", (family) => {
  84. expect(
  85. freezeOffPeakCreateTelemetrySnapshot({
  86. providerId: `account:${family}-offpeak-idle-plan`,
  87. model: "GLM-5.3-Flash",
  88. }),
  89. ).toMatchObject({ modelProvider: "offpeak-idle-plan", modelName: "GLM-5.3-Flash" });
  90. });
  91. it("压缩保留原模型形式,不增 hostname", () => {
  92. const detail = buildCompactionTelemetryExtraDetail({
  93. timeline: { status: "completed", trigger: "manual" },
  94. modelName: "account:bigmodel-team-coding-plan/GLM-5.3",
  95. modelProvider: "account:bigmodel-team-coding-plan",
  96. });
  97. expect(detail).toMatchObject({
  98. model_name: "builtin:bigmodel-coding-plan/GLM-5.3",
  99. model_provider: "builtin:bigmodel-coding-plan",
  100. });
  101. expect(detail).not.toHaveProperty("provider_name");
  102. });
  103. });