remoteUsageTelemetry.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. });
  29. it("构造 Web Remote Control 生命周期事件", () => {
  30. expect(
  31. buildWebRemoteControlEntryViewTelemetry({
  32. workspaceKind: "remote",
  33. remoteKind: "ssh",
  34. }),
  35. ).toEqual({
  36. elementName: "web_remote_control_entry_view",
  37. eventRegion: "web_remote_control",
  38. eventType: "view",
  39. eventExtraDetail: {
  40. workspace_kind: "remote",
  41. remote_kind: "ssh",
  42. },
  43. });
  44. expect(
  45. buildWebRemoteControlStartResultTelemetry({
  46. result: "failure",
  47. workspaceKind: "local",
  48. errorCategory: "relay",
  49. }),
  50. ).toMatchObject({
  51. elementName: "web_remote_control_start_result",
  52. eventExtraDetail: {
  53. result: "failure",
  54. workspace_kind: "local",
  55. remote_kind: "",
  56. error_category: "relay",
  57. },
  58. });
  59. expect(
  60. buildWebRemoteControlStartResultTelemetry({
  61. result: "cancelled",
  62. workspaceKind: "remote",
  63. remoteKind: "ssh",
  64. }),
  65. ).toMatchObject({
  66. elementName: "web_remote_control_start_result",
  67. eventExtraDetail: {
  68. result: "cancelled",
  69. workspace_kind: "remote",
  70. remote_kind: "ssh",
  71. error_category: "",
  72. },
  73. });
  74. expect(
  75. buildWebRemoteControlPairResultTelemetry({
  76. result: "success",
  77. pairKind: "reconnect",
  78. workspaceKind: "remote",
  79. remoteKind: "wsl",
  80. }),
  81. ).toMatchObject({
  82. elementName: "web_remote_control_pair_result",
  83. eventExtraDetail: {
  84. result: "success",
  85. pair_kind: "reconnect",
  86. workspace_kind: "remote",
  87. remote_kind: "wsl",
  88. error_category: "",
  89. },
  90. });
  91. expect(
  92. buildWebRemoteControlBridgeResultTelemetry({
  93. result: "success",
  94. workspaceKind: "remote",
  95. remoteKind: "docker",
  96. entryKind: "task",
  97. }),
  98. ).toMatchObject({
  99. elementName: "web_remote_control_bridge_result",
  100. eventExtraDetail: {
  101. result: "success",
  102. workspace_kind: "remote",
  103. remote_kind: "docker",
  104. entry_kind: "task",
  105. error_category: "",
  106. },
  107. });
  108. });
  109. it.each([
  110. [Object.assign(new Error("password rejected"), { code: "AUTH_FAILED" }), "auth"],
  111. [Object.assign(new Error("asset install failed"), { code: "DEPLOY_FAILED" }), "deploy"],
  112. [Object.assign(new Error("spawn host failed"), { code: "HOST_START_FAILED" }), "host_start"],
  113. [
  114. Object.assign(new Error("secret workspace missing"), { code: "REMOTE_SESSION_MISSING" }),
  115. "attach",
  116. ],
  117. [Object.assign(new Error("desktop unavailable"), { code: "DESKTOP_HOST_MISSING" }), "attach"],
  118. [Object.assign(new Error("relay websocket closed"), { code: "RELAY_CLOSED" }), "relay"],
  119. [Object.assign(new Error("ssh timeout"), { code: "ETIMEDOUT" }), "connect"],
  120. [new Error("包含私密目标但无法分类"), "unknown"],
  121. ] as const)("只把错误映射为低基数枚举 %#", (error, expected) => {
  122. expect(classifyRemoteUsageError(error)).toBe(expected);
  123. });
  124. });