remoteUsageTelemetry.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildRemoteWorkspaceConnectResultTelemetry,
  4. buildWebRemoteControlBridgeResultTelemetry,
  5. buildWebRemoteControlEntryViewTelemetry,
  6. buildWebRemoteControlPairResultTelemetry,
  7. buildWebRemoteControlStartResultTelemetry,
  8. classifyRemoteUsageError,
  9. } from "../src/remoteUsageTelemetry.js";
  10. describe("remote usage telemetry", () => {
  11. it("构造远程工作区连接终态事件且不携带目标身份信息", () => {
  12. const event = buildRemoteWorkspaceConnectResultTelemetry({
  13. result: "success",
  14. remoteKind: "ssh",
  15. connectTrigger: "reconnect",
  16. });
  17. expect(event).toEqual({
  18. elementName: "remote_workspace_connect_result",
  19. eventRegion: "remote_workspace",
  20. eventType: "result",
  21. eventExtraDetail: {
  22. result: "success",
  23. remote_kind: "ssh",
  24. connect_trigger: "reconnect",
  25. error_category: "",
  26. },
  27. });
  28. expect(event.eventExtraDetail).not.toHaveProperty("workspacePath");
  29. expect(event.eventExtraDetail).not.toHaveProperty("workspaceIdentity");
  30. expect(event.eventExtraDetail).not.toHaveProperty("remoteSessionId");
  31. });
  32. it("构造 Web Remote Control 生命周期事件", () => {
  33. expect(
  34. buildWebRemoteControlEntryViewTelemetry({
  35. workspaceKind: "remote",
  36. remoteKind: "ssh",
  37. }),
  38. ).toEqual({
  39. elementName: "web_remote_control_entry_view",
  40. eventRegion: "web_remote_control",
  41. eventType: "view",
  42. eventExtraDetail: {
  43. workspace_kind: "remote",
  44. remote_kind: "ssh",
  45. },
  46. });
  47. expect(
  48. buildWebRemoteControlStartResultTelemetry({
  49. result: "failure",
  50. workspaceKind: "local",
  51. errorCategory: "relay",
  52. }),
  53. ).toMatchObject({
  54. elementName: "web_remote_control_start_result",
  55. eventExtraDetail: {
  56. result: "failure",
  57. workspace_kind: "local",
  58. remote_kind: "",
  59. error_category: "relay",
  60. },
  61. });
  62. expect(
  63. buildWebRemoteControlStartResultTelemetry({
  64. result: "cancelled",
  65. workspaceKind: "remote",
  66. remoteKind: "ssh",
  67. }),
  68. ).toMatchObject({
  69. elementName: "web_remote_control_start_result",
  70. eventExtraDetail: {
  71. result: "cancelled",
  72. workspace_kind: "remote",
  73. remote_kind: "ssh",
  74. error_category: "",
  75. },
  76. });
  77. expect(
  78. buildWebRemoteControlPairResultTelemetry({
  79. result: "success",
  80. pairKind: "reconnect",
  81. workspaceKind: "remote",
  82. remoteKind: "wsl",
  83. }),
  84. ).toMatchObject({
  85. elementName: "web_remote_control_pair_result",
  86. eventExtraDetail: {
  87. result: "success",
  88. pair_kind: "reconnect",
  89. workspace_kind: "remote",
  90. remote_kind: "wsl",
  91. error_category: "",
  92. },
  93. });
  94. expect(
  95. buildWebRemoteControlBridgeResultTelemetry({
  96. result: "success",
  97. workspaceKind: "remote",
  98. remoteKind: "docker",
  99. entryKind: "task",
  100. }),
  101. ).toMatchObject({
  102. elementName: "web_remote_control_bridge_result",
  103. eventExtraDetail: {
  104. result: "success",
  105. workspace_kind: "remote",
  106. remote_kind: "docker",
  107. entry_kind: "task",
  108. error_category: "",
  109. },
  110. });
  111. });
  112. it.each([
  113. [Object.assign(new Error("password rejected"), { code: "AUTH_FAILED" }), "auth"],
  114. [Object.assign(new Error("asset install failed"), { code: "DEPLOY_FAILED" }), "deploy"],
  115. [Object.assign(new Error("spawn host failed"), { code: "HOST_START_FAILED" }), "host_start"],
  116. [
  117. Object.assign(new Error("secret workspace missing"), { code: "REMOTE_SESSION_MISSING" }),
  118. "attach",
  119. ],
  120. [Object.assign(new Error("desktop unavailable"), { code: "DESKTOP_HOST_MISSING" }), "attach"],
  121. [Object.assign(new Error("relay websocket closed"), { code: "RELAY_CLOSED" }), "relay"],
  122. [Object.assign(new Error("ssh timeout"), { code: "ETIMEDOUT" }), "connect"],
  123. [new Error("包含私密目标但无法分类"), "unknown"],
  124. ] as const)("只把错误映射为低基数枚举 %#", (error, expected) => {
  125. expect(classifyRemoteUsageError(error)).toBe(expected);
  126. });
  127. });