remoteConnectionPresentation.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { describe, expect, it } from "vitest";
  2. import { resolveRemoteConnectionPresentation } from "@/lib/remoteConnectionPresentation.js";
  3. describe("远程连接展示合同", () => {
  4. it("主动断开覆盖保留的错误和升级门禁", () => {
  5. expect(
  6. resolveRemoteConnectionPresentation({
  7. state: "failed",
  8. disconnectOrigin: "user",
  9. errorCode: "source-server-upgrade-required",
  10. }),
  11. ).toMatchObject({
  12. status: "offline",
  13. shortMessageId: "remote.status.offline",
  14. tone: "neutral",
  15. icon: "cloud-off",
  16. primaryAction: "connect",
  17. });
  18. });
  19. });
  20. it.each([
  21. [{ state: "online", attached: true }, "online", "online", "success", "cloud", "disconnect"],
  22. [{ state: "online", attached: false }, "connecting", "connecting", "progress", "spinner", "none"],
  23. [{ state: "bootstrapping" }, "connecting", "connecting", "progress", "spinner", "none"],
  24. [
  25. { state: "retrying", recoveryAttempt: 2, recoveryLimit: 3 },
  26. "connecting",
  27. "connecting",
  28. "progress",
  29. "spinner",
  30. "none",
  31. ],
  32. [
  33. { state: "updating", errorCode: "source-server-upgrade-required" },
  34. "connecting",
  35. "updating",
  36. "progress",
  37. "spinner",
  38. "none",
  39. ],
  40. [{ state: "offline" }, "offline", "offline", "neutral", "cloud-off", "connect"],
  41. [
  42. { state: "absent", disconnectOrigin: "startup" },
  43. "offline",
  44. "offline",
  45. "neutral",
  46. "cloud-off",
  47. "connect",
  48. ],
  49. [{ state: "needs-install" }, "pending", "needsInstall", "warning", "cloud-alert", "install"],
  50. [
  51. { state: "needs-upgrade", upgradeTarget: "server" },
  52. "pending",
  53. "needsUpdate",
  54. "warning",
  55. "cloud-alert",
  56. "update-server",
  57. ],
  58. [
  59. { state: "needs-upgrade", upgradeTarget: "controller" },
  60. "pending",
  61. "needsUpdate",
  62. "warning",
  63. "cloud-alert",
  64. "update-desktop",
  65. ],
  66. [
  67. { state: "identity-change-pending" },
  68. "pending",
  69. "needsIdentity",
  70. "warning",
  71. "cloud-alert",
  72. "confirm-identity",
  73. ],
  74. [
  75. { state: "failed", credentialsMissing: true },
  76. "pending",
  77. "needsCredentials",
  78. "warning",
  79. "cloud-alert",
  80. "credentials",
  81. ],
  82. [
  83. { state: "failed", errorCode: "remote-connection-timeout" },
  84. "failed",
  85. "failed",
  86. "danger",
  87. "cloud-x",
  88. "retry",
  89. ],
  90. [
  91. { state: "offline", recoveryBudgetExhausted: true },
  92. "failed",
  93. "failed",
  94. "danger",
  95. "cloud-x",
  96. "retry",
  97. ],
  98. [
  99. { state: "online", updateAvailability: "available" },
  100. "online",
  101. "online",
  102. "success",
  103. "cloud",
  104. "disconnect",
  105. ],
  106. [
  107. { state: "failed", errorCode: "remote-connection-canceled" },
  108. "offline",
  109. "offline",
  110. "neutral",
  111. "cloud-off",
  112. "connect",
  113. ],
  114. ] as const)("投影 %j", (input, status, short, tone, icon, action) => {
  115. expect(resolveRemoteConnectionPresentation(input)).toMatchObject({
  116. status,
  117. shortMessageId: `remote.status.${short}`,
  118. tone,
  119. icon,
  120. primaryAction: action,
  121. });
  122. });
  123. import { classifyRemoteConnectionError } from "@/lib/remoteConnectionPresentation.js";
  124. import { PUBLIC_ZCODE_ERROR_CODES } from "@zcode/shared";
  125. it.each([
  126. ["remote-authentication-failed", "authentication", "edit-auth"],
  127. ["remote-host-unreachable", "hostUnreachable", "retry"],
  128. ["remote-connection-timeout", "connectionTimeout", "retry"],
  129. ["remote-host-not-found", "hostNotFound", "edit-host"],
  130. ["remote-host-key-changed", "hostKeyChanged", "security"],
  131. ["remote-download-failed", "downloadFailed", "retry"],
  132. ["remote-upload-failed", "uploadFailed", "retry"],
  133. ["remote-preflight-failed", "preflightFailed", "retry"],
  134. ["remote-install-failed", "installFailed", "retry"],
  135. ["remote-space-insufficient", "spaceInsufficient", "details"],
  136. ["remote-permission-denied", "permissionDenied", "details"],
  137. ["remote-connection-canceled", "connectionCanceled", "connect"],
  138. ["remote-connection-unknown", "unknown", "retry"],
  139. ] as const)("错误 %s 给出原因与操作", (errorCode, title, primaryAction) => {
  140. expect(classifyRemoteConnectionError({ errorCode })).toMatchObject({
  141. titleId: `remote.error.${title}`,
  142. primaryAction,
  143. });
  144. });
  145. it.each(PUBLIC_ZCODE_ERROR_CODES)("公开错误码 %s 有明确映射", (errorCode) => {
  146. expect(classifyRemoteConnectionError({ errorCode }).category).not.toBe("unrecognized");
  147. });
  148. it.each([undefined, "", "future-error"])("未知错误 %s 安全兜底", (errorCode) => {
  149. expect(classifyRemoteConnectionError({ errorCode })).toMatchObject({
  150. titleId: "remote.error.unknown",
  151. primaryAction: "retry",
  152. });
  153. });
  154. it.each(["local-download-upload", "remote-download"] as const)(
  155. "SSH 下载方式 %s 决定切换建议",
  156. (assetInstallMode) => {
  157. expect(
  158. classifyRemoteConnectionError({
  159. errorCode: "remote-download-failed",
  160. target: { kind: "ssh", host: "host", username: "dev", assetInstallMode },
  161. }).allowRemoteDownload,
  162. ).toBe(assetInstallMode === "local-download-upload");
  163. },
  164. );
  165. it.each([
  166. { kind: "wsl", distro: "Ubuntu" },
  167. { kind: "docker", container: "app" },
  168. ] as const)("%j 使用专属文案,不提示远端下载", (target) => {
  169. expect(
  170. classifyRemoteConnectionError({ errorCode: "remote-host-not-found", target }),
  171. ).toMatchObject({
  172. titleId: `remote.error.hostNotFound.${target.kind}`,
  173. allowRemoteDownload: false,
  174. });
  175. });
  176. it("项目级失败保留 owner 已分类的原因,不被设备在线或未知错误模板覆盖", () => {
  177. const presentation = resolveRemoteConnectionPresentation({
  178. state: "online",
  179. attached: false,
  180. projectError: "远程目录不可用,请检查目录是否存在。",
  181. });
  182. expect(presentation.status).toBe("failed");
  183. expect(presentation.reasonText).toBe("远程目录不可用,请检查目录是否存在。");
  184. });