remoteConnectionFailureContract.test.ts 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. hostRemoteWorkspaceConnectFailedResponseSchema,
  4. publicZCodeErrorCodeSchema,
  5. normalizeRemoteConnectionError,
  6. } from "@zcode/shared";
  7. describe("连接失败的 Host/App 契约", () => {
  8. it("携带未知 cause 的公开认证码不被预检阶段覆盖", () => {
  9. const error = Object.assign(new Error("SSH 认证失败", { cause: new Error("opaque cause") }), {
  10. code: "remote-authentication-failed",
  11. });
  12. const failure = normalizeRemoteConnectionError(error, "preflight");
  13. expect(failure.code).toBe("remote-authentication-failed");
  14. expect(
  15. hostRemoteWorkspaceConnectFailedResponseSchema.safeParse({
  16. type: "remote-workspace-connect-failed",
  17. requestId: "auth",
  18. error: failure.message,
  19. reasonCode: failure.code,
  20. }).success,
  21. ).toBe(true);
  22. });
  23. it("预检阶段不抢先认领未知子错误,仍能找到后面的系统错误", () => {
  24. const error = new AggregateError(
  25. [
  26. new Error("opaque cause"),
  27. Object.assign(new Error("connection refused"), { code: "ECONNREFUSED" }),
  28. ],
  29. "Failed /Users/private/project",
  30. );
  31. expect(normalizeRemoteConnectionError(error, "preflight")).toMatchObject({
  32. code: "remote-host-unreachable",
  33. message: "Failed [redacted-path]",
  34. });
  35. });
  36. it("阶段包装保留内层网络分类,不将阶段码视为已确认领域原因", () => {
  37. const error = Object.assign(
  38. new Error("probe failed", {
  39. cause: Object.assign(new Error("refused"), { code: "ECONNREFUSED" }),
  40. }),
  41. { code: "remote-preflight-failed" },
  42. );
  43. expect(normalizeRemoteConnectionError(error, "preflight").code).toBe("remote-host-unreachable");
  44. });
  45. it.each([
  46. [
  47. new AggregateError([Object.assign(new Error(""), { code: "ECONNREFUSED" })]),
  48. "remote-host-unreachable",
  49. ],
  50. [Object.assign(new Error(""), { code: "ETIMEDOUT" }), "remote-connection-timeout"],
  51. [Object.assign(new Error(""), { code: "ENOTFOUND" }), "remote-host-not-found"],
  52. [new Error(""), "remote-connection-unknown"],
  53. [new Error("\u001b[31m"), "remote-connection-unknown"],
  54. [{ code: "private-code", message: "" }, "remote-connection-unknown"],
  55. ])("空消息和嵌套错误均可经过 Host schema 返回", (error, code) => {
  56. const result = normalizeRemoteConnectionError(error);
  57. expect(result.code).toBe(code);
  58. expect(result.message.trim()).not.toBe("");
  59. expect(publicZCodeErrorCodeSchema.parse(result.code)).toBe(code);
  60. expect(
  61. hostRemoteWorkspaceConnectFailedResponseSchema.safeParse({
  62. type: "remote-workspace-connect-failed",
  63. requestId: "request",
  64. error: result.message,
  65. reasonCode: result.code,
  66. }).success,
  67. ).toBe(true);
  68. });
  69. it.each([
  70. ["ENOSPC", "remote-space-insufficient"],
  71. ["EACCES", "remote-permission-denied"],
  72. ["ABORT_ERR", "remote-connection-canceled"],
  73. ["ERR_SSH_HOST_KEY_MISMATCH", "remote-host-key-changed"],
  74. ])("识别系统错误 %s", (code, expected) => {
  75. expect(normalizeRemoteConnectionError({ code, message: "failure" }).code).toBe(expected);
  76. });
  77. it("探测阶段的 socket 中断按网络不可达分类,不冒充预检失败", () => {
  78. // 在线断连后的自动恢复经由懒连接 probe 抛出 ECONNRESET;它是传输失败,不是部署前置条件失败。
  79. const error = Object.assign(new Error("read ECONNRESET"), {
  80. code: "ECONNRESET",
  81. syscall: "read",
  82. });
  83. expect(normalizeRemoteConnectionError(error, "preflight").code).toBe("remote-host-unreachable");
  84. expect(normalizeRemoteConnectionError(new Error("read ECONNRESET"), "preflight").code).toBe(
  85. "remote-host-unreachable",
  86. );
  87. });
  88. it.each(["download", "upload", "preflight", "install"])("%s 失败保留阶段", (phase) => {
  89. expect(normalizeRemoteConnectionError(new Error("failed"), phase).code).toBe(
  90. `remote-${phase}-failed`,
  91. );
  92. });
  93. });