remoteConnectionPhases.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. resolveRemoteConnectionPhases,
  4. resolveRemoteConnectionPhaseSub,
  5. resolveRemoteOperationProgress,
  6. } from "@/remote-connection/RemoteConnectionConnectingStep.js";
  7. describe("remote connection phase rail", () => {
  8. it.each(["verify-archive", "extract", "verify"])(
  9. "keeps %s in deployment before handshake",
  10. (stage) => {
  11. const phases = resolveRemoteConnectionPhases([
  12. { message: `[source-server:${stage}] processing`, level: "info" },
  13. ]);
  14. expect(phases.find((phase) => phase.key === "deploy")?.status).toBe("active");
  15. expect(phases.find((phase) => phase.key === "handshake")?.status).toBe("pending");
  16. },
  17. );
  18. it("moves restart into handshake without reporting readiness", () => {
  19. const phases = resolveRemoteConnectionPhases([
  20. { message: "[source-server:verify] checking", level: "info" },
  21. { message: "[source-server:restart] restarting", level: "info" },
  22. ]);
  23. expect(phases.find((phase) => phase.key === "deploy")?.status).toBe("complete");
  24. expect(phases.find((phase) => phase.key === "handshake")?.status).toBe("active");
  25. expect(phases.some((phase) => phase.key === "ready")).toBe(false);
  26. });
  27. it.each(["verify", "restart"])("preserves a %s failure", (stage) => {
  28. const phases = resolveRemoteConnectionPhases(
  29. [{ message: `[source-server:${stage}] failed`, level: "error" }],
  30. { connectionReady: true },
  31. );
  32. expect(
  33. phases.find((phase) => phase.key === (stage === "restart" ? "handshake" : "deploy"))?.status,
  34. ).toBe("failed");
  35. expect(phases.some((phase) => phase.key === "ready")).toBe(false);
  36. });
  37. it("projects source-server diagnostic prefixes into ordered progress", () => {
  38. const phases = resolveRemoteConnectionPhases([
  39. { message: "[source-server:probe] SSH channel", level: "info" },
  40. { message: "[source-server:daemon] Starting daemon", level: "info" },
  41. ]);
  42. expect(phases.find((phase) => phase.key === "channel")?.status).toBe("complete");
  43. expect(phases.find((phase) => phase.key === "handshake")?.status).toBe("active");
  44. expect(phases.some((phase) => phase.key === "ready")).toBe(false);
  45. });
  46. it("completes the terminal phase from connection readiness when its log is absent", () => {
  47. const phases = resolveRemoteConnectionPhases(
  48. [
  49. { message: "[source-server:probe] SSH channel", level: "info" },
  50. { message: "[source-server:daemon] Starting daemon", level: "info" },
  51. ],
  52. { connectionReady: true },
  53. );
  54. expect(phases.find((phase) => phase.key === "handshake")?.status).toBe("complete");
  55. expect(phases.some((phase) => phase.key === "ready")).toBe(false);
  56. });
  57. it("does not mask a failed phase with a stale readiness flag", () => {
  58. const phases = resolveRemoteConnectionPhases(
  59. [
  60. {
  61. message: "[source-server:protocol-handshake] rejected",
  62. level: "error",
  63. },
  64. ],
  65. { connectionReady: true },
  66. );
  67. expect(phases.find((phase) => phase.key === "handshake")?.status).toBe("failed");
  68. expect(phases.some((phase) => phase.key === "ready")).toBe(false);
  69. });
  70. it("marks the active phase failed when the connection ends with an error", () => {
  71. const phases = resolveRemoteConnectionPhases([
  72. {
  73. message: "[source-server:protocol-handshake] rejected",
  74. level: "error",
  75. },
  76. ]);
  77. expect(phases.find((phase) => phase.key === "handshake")?.status).toBe(
  78. "failed",
  79. );
  80. });
  81. it("keeps deployment progress visible between probe and daemon", () => {
  82. const phases = resolveRemoteConnectionPhases([
  83. { message: "[source-server:probe] checked", level: "info" },
  84. { message: "[source-server:upload] 40%", level: "info" },
  85. ]);
  86. expect(phases.find((phase) => phase.key === "deploy")?.status).toBe(
  87. "active",
  88. );
  89. expect(phases.find((phase) => phase.key === "handshake")?.status).toBe(
  90. "pending",
  91. );
  92. });
  93. });
  94. describe("remote connection phase sub copy", () => {
  95. it("maps structured milestones to prototype sub descriptions", () => {
  96. expect(
  97. resolveRemoteConnectionPhaseSub(
  98. { key: "channel", status: "complete" },
  99. {},
  100. ),
  101. ).toEqual({ id: "remote.phaseSub.channelEstablished" });
  102. expect(
  103. resolveRemoteConnectionPhaseSub(
  104. { key: "identify", status: "complete" },
  105. { recognizedDeviceName: "dev" },
  106. ),
  107. ).toEqual({
  108. id: "remote.phaseSub.identifyExisting",
  109. values: { device: "dev" },
  110. });
  111. expect(
  112. resolveRemoteConnectionPhaseSub(
  113. { key: "identify", status: "complete" },
  114. { installConfirmationPending: true },
  115. ),
  116. ).toEqual({ id: "remote.phaseSub.identifyNew" });
  117. expect(
  118. resolveRemoteConnectionPhaseSub(
  119. { key: "deploy", status: "active" },
  120. { installConfirmationPending: true },
  121. ),
  122. ).toEqual({ id: "remote.phaseSub.deployWaitingConfirm" });
  123. expect(
  124. resolveRemoteConnectionPhaseSub(
  125. { key: "deploy", status: "failed" },
  126. { deployErrorReason: "SFTP channel closed" },
  127. ),
  128. ).toEqual({
  129. id: "remote.phaseSub.uploadFailed",
  130. values: { reason: "SFTP channel closed" },
  131. });
  132. expect(
  133. resolveRemoteConnectionPhaseSub({ key: "ready", status: "complete" }, {}),
  134. ).toEqual({ id: "remote.phaseSub.readyOnline" });
  135. });
  136. it("stays empty when no structured data source backs the sub description", () => {
  137. // 缺结构化数据源的阶段(握手端口、部署版本等)留空,不透传英文原始日志。
  138. expect(
  139. resolveRemoteConnectionPhaseSub(
  140. { key: "handshake", status: "complete" },
  141. {},
  142. ),
  143. ).toBeNull();
  144. expect(
  145. resolveRemoteConnectionPhaseSub({ key: "channel", status: "active" }, {}),
  146. ).toBeNull();
  147. expect(
  148. resolveRemoteConnectionPhaseSub({ key: "deploy", status: "active" }, {}),
  149. ).toBeNull();
  150. });
  151. });
  152. describe("sidebar operation progress", () => {
  153. const log = (
  154. phase: string,
  155. timestamp: number,
  156. progress?: number,
  157. level: "info" | "warn" | "error" = "info",
  158. ) => ({
  159. phase,
  160. timestamp,
  161. level,
  162. ...(progress === undefined ? {} : { progress }),
  163. });
  164. it("starts at the first phase without elapsed time before any log", () => {
  165. expect(resolveRemoteOperationProgress([])).toEqual({
  166. index: 0,
  167. total: 4,
  168. titleId: "remote.phase.channel",
  169. });
  170. });
  171. it("shares the wizard phase grouping and reports upload percent", () => {
  172. expect(
  173. resolveRemoteOperationProgress([
  174. log("probe", 1_000),
  175. log("preflight", 2_000),
  176. log("upload", 3_000, 0.432),
  177. ]),
  178. ).toEqual({
  179. index: 2,
  180. total: 4,
  181. titleId: "remote.phase.deploy",
  182. percent: 43,
  183. startedAt: 1_000,
  184. });
  185. });
  186. it("drops the percent once the latest log has no transfer progress", () => {
  187. expect(
  188. resolveRemoteOperationProgress([log("upload", 1_000, 0.9), log("extract", 2_000)]).percent,
  189. ).toBeUndefined();
  190. });
  191. it("skips install phases and ignores unknown trailing phases", () => {
  192. const progress = resolveRemoteOperationProgress([
  193. log("probe", 1_000),
  194. log("daemon", 2_000),
  195. log("cleanup", 3_000, 0.5),
  196. ]);
  197. expect(progress).toMatchObject({ index: 3, titleId: "remote.phase.handshake" });
  198. expect(progress.percent).toBeUndefined();
  199. });
  200. it.each([
  201. { name: "成功结束的上一次连接", tail: log("ready", 3_000, 1) },
  202. { name: "失败结束的上一次尝试", tail: log("daemon", 3_000, undefined, "error") },
  203. ])("$name 不再算作进行中:回到第 1 步且不计时", ({ tail }) => {
  204. // 修复原因:Host 保留的是最近一次操作,断线后首次重试前、两次重试之间读到的都是已结束操作;
  205. // 曾据此显示“第 4/4 步 · 已用 120 分”。
  206. expect(resolveRemoteOperationProgress([log("probe", 1_000), log("daemon", 2_000), tail])).toEqual(
  207. { index: 0, total: 4, titleId: "remote.phase.channel" },
  208. );
  209. });
  210. });