remoteConnectionInstallProgress.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { useFeedbackStore } from "@/feedback/feedbackStore.js";
  2. // @vitest-environment jsdom
  3. import { createElement } from "react";
  4. import { cleanup, render, screen, within, fireEvent, waitFor } from "@testing-library/react";
  5. import { afterEach, expect, it, vi } from "vitest";
  6. import type { RemoteConnectionLogEntry } from "@/hooks/useRemoteConnectionLogs.js";
  7. vi.mock("@/i18n/IntlProvider.js", () => ({
  8. useZCodeIntl: () => ({ intl: { formatMessage: ({ id }: { id: string }) => id } }),
  9. }));
  10. vi.mock("@/hooks/usePlatform.js", () => ({ usePlatform: () => ({}) }));
  11. import { RemoteConnectionConnectingStep } from "@/remote-connection/RemoteConnectionConnectingStep.js";
  12. afterEach(cleanup);
  13. it("replaces upload percentage with live install stages and preserves the failure log", () => {
  14. const logs: RemoteConnectionLogEntry[] = [
  15. {
  16. id: "upload",
  17. level: "info",
  18. message: "[source-server:upload] archive 100.0%",
  19. timestamp: "16:22:07",
  20. },
  21. ];
  22. const props = {
  23. kind: "ssh" as const,
  24. loading: true,
  25. errorMessage: "",
  26. onBack() {},
  27. onRetry() {},
  28. };
  29. const view = render(createElement(RemoteConnectionConnectingStep, { ...props, logs }));
  30. expect(screen.getByRole("progressbar").getAttribute("aria-valuenow")).toBe("100");
  31. for (const [phase, messageId] of [
  32. ["extract", "remote.phaseSub.extracting"],
  33. ["verify", "remote.phaseSub.verifyingFiles"],
  34. ["restart", "remote.phaseSub.restarting"],
  35. ]) {
  36. logs.push({
  37. id: phase!,
  38. level: "info",
  39. message: `[source-server:${phase}] working`,
  40. timestamp: "16:22:08",
  41. });
  42. view.rerender(createElement(RemoteConnectionConnectingStep, { ...props, logs: [...logs] }));
  43. expect(screen.queryByRole("progressbar")).toBeNull();
  44. expect(screen.getByText(messageId!)).toBeTruthy();
  45. }
  46. logs.push({
  47. id: "failed",
  48. level: "error",
  49. message: "[source-server:restart] restart failed",
  50. timestamp: "16:22:09",
  51. });
  52. view.rerender(
  53. createElement(RemoteConnectionConnectingStep, {
  54. ...props,
  55. loading: false,
  56. errorMessage: "restart failed",
  57. logs: [...logs],
  58. }),
  59. );
  60. expect(screen.getByText("restart failed")).toBeTruthy();
  61. expect(within(screen.getByRole("list")).queryByText("remote.phaseSub.readyOnline")).toBeNull();
  62. });
  63. it("连接反馈只附带当前可见日志,不自动上传全量诊断文件", async () => {
  64. render(
  65. createElement(RemoteConnectionConnectingStep, {
  66. kind: "ssh",
  67. loading: false,
  68. errorMessage: "连接失败",
  69. onBack() {},
  70. onRetry() {},
  71. logs: [{ id: "1", level: "error", message: "safe connection log", timestamp: "12:00:00" }],
  72. }),
  73. );
  74. fireEvent.click(screen.getByText("remoteConnection.feedback"));
  75. await waitFor(() =>
  76. expect(useFeedbackStore.getState().submitDraft?.description).toContain("safe connection log"),
  77. );
  78. expect(useFeedbackStore.getState().submitDraft?.includeLogs).toBe(false);
  79. });
  80. it("shows download progress and speed inside the installation stage with collapsed logs", () => {
  81. render(
  82. createElement(RemoteConnectionConnectingStep, {
  83. kind: "ssh",
  84. target: { kind: "ssh", host: "build", username: "dev" },
  85. recognizedDeviceName: "Build device",
  86. loading: true,
  87. errorMessage: "",
  88. onBack() {},
  89. onRetry() {},
  90. logs: [
  91. {
  92. id: "download",
  93. level: "info",
  94. message: "[source-server:download] download progress: 25.0% (20.0/80.0 MB, 4.00 MB/s)",
  95. timestamp: "12:00:01",
  96. },
  97. ],
  98. }),
  99. );
  100. expect(screen.getByText("Build device")).toBeTruthy();
  101. expect(screen.getByRole("progressbar").getAttribute("aria-valuenow")).toBe("25");
  102. expect(screen.getByText(/4.00 MB\/s/).closest("li")).not.toBeNull();
  103. expect(screen.getByText(/remote.transferTimeRemaining/)).toBeTruthy();
  104. expect(screen.queryByTestId("remote-connection-log")).toBeNull();
  105. expect(screen.queryByText("remote.phase.ready")).toBeNull();
  106. });
  107. it("keeps completed stage durations and advances the active stage elapsed time", () => {
  108. vi.useFakeTimers();
  109. vi.setSystemTime(new Date("2026-09-29T12:00:10"));
  110. try {
  111. render(
  112. createElement(RemoteConnectionConnectingStep, {
  113. kind: "wsl",
  114. loading: true,
  115. errorMessage: "",
  116. onBack() {},
  117. onRetry() {},
  118. logs: [
  119. {
  120. id: "probe",
  121. level: "info",
  122. message: "[source-server:probe] start",
  123. timestamp: "12:00:00",
  124. },
  125. {
  126. id: "deploy",
  127. level: "info",
  128. message: "[source-server:upload] 25%",
  129. timestamp: "12:00:03",
  130. },
  131. ],
  132. }),
  133. );
  134. expect(screen.getByText("3 s")).toBeTruthy();
  135. expect(screen.getByText("7 s")).toBeTruthy();
  136. } finally {
  137. vi.useRealTimers();
  138. }
  139. });
  140. it.each([
  141. ["2026-09-29T12:00:00.800", "2026-09-29T12:00:01.100", "12:00:01"],
  142. ["2026-09-29T23:59:59.800", "2026-09-30T00:00:00.100", "00:00:00"],
  143. ])(
  144. "does not turn a new cross-second log into a one-day duration: %s",
  145. (before, after, timestamp) => {
  146. vi.useFakeTimers();
  147. vi.setSystemTime(new Date(before));
  148. try {
  149. const props = {
  150. kind: "ssh" as const,
  151. loading: true,
  152. errorMessage: "",
  153. onBack() {},
  154. onRetry() {},
  155. };
  156. const view = render(createElement(RemoteConnectionConnectingStep, { ...props, logs: [] }));
  157. vi.setSystemTime(new Date(after));
  158. view.rerender(
  159. createElement(RemoteConnectionConnectingStep, {
  160. ...props,
  161. logs: [{ id: "1", level: "info", message: "[source-server:probe] start", timestamp }],
  162. }),
  163. );
  164. expect(screen.queryByText(/86399/)).toBeNull();
  165. } finally {
  166. vi.useRealTimers();
  167. }
  168. },
  169. );
  170. it("安装完成标记不再倒退成正在校验文件", () => {
  171. render(
  172. createElement(RemoteConnectionConnectingStep, {
  173. kind: "ssh",
  174. loading: true,
  175. errorMessage: "",
  176. onBack() {},
  177. onRetry() {},
  178. logs: [
  179. {
  180. id: "installed",
  181. level: "info",
  182. message: "[source-server:install] Source Server 3.8.0 installed",
  183. timestamp: "12:00:00",
  184. },
  185. ],
  186. }),
  187. );
  188. expect(screen.getByText("remote.phaseSub.installed")).toBeTruthy();
  189. expect(screen.queryByText("remote.phaseSub.verifyingFiles")).toBeNull();
  190. });