sourceServerMigrationReportPanel.test.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  4. import type { SourceServerMigrationReport } from "@zcode/shared";
  5. import { afterEach, describe, expect, it, vi } from "vitest";
  6. import { SourceServerMigrationReportPanel } from "../src/settings/SourceServerMigrationReportPanel.js";
  7. vi.mock("@/i18n/IntlProvider.js", () => ({
  8. useZCodeIntl: () => ({
  9. locale: "zh-CN",
  10. intl: {
  11. formatMessage: ({ id }: { id: string }) => id,
  12. },
  13. }),
  14. }));
  15. afterEach(cleanup);
  16. describe("SourceServerMigrationReportPanel", () => {
  17. it("exposes whole-directory abandon and retry actions without a user-facing export", () => {
  18. const onAbandon = vi.fn();
  19. const onRetry = vi.fn();
  20. const report: SourceServerMigrationReport = {
  21. rows: [
  22. {
  23. migrationId: "migration-1",
  24. workspacePath: "/workspace/conflicted",
  25. canonicalKey: "remote:server:source-a:/workspace/conflicted",
  26. variantKeys: ["remote:ssh:host:22:user:/workspace/conflicted"],
  27. status: "failed",
  28. errorCode: "workspace-task-identity-conflict",
  29. updatedAt: 1,
  30. conflicts: [
  31. {
  32. kind: "task-identity",
  33. key: "task-1",
  34. taskId: "task-1",
  35. sourceKey: "remote:ssh:host:22:user:/workspace/conflicted",
  36. canonicalKey: "remote:server:source-a:/workspace/conflicted",
  37. reason: "same task id has divergent content",
  38. },
  39. ],
  40. },
  41. ],
  42. };
  43. render(
  44. createElement(SourceServerMigrationReportPanel, {
  45. report,
  46. onAbandon,
  47. onRetry,
  48. }),
  49. );
  50. expect(screen.getByText("/workspace/conflicted")).toBeTruthy();
  51. expect(screen.getByText(/remote\.error\.workspaceTaskIdentityConflict/u)).toBeTruthy();
  52. // 失败行说明带「N 处冲突」段(settings.source.migrationConflictCount)。
  53. expect(
  54. screen.getByText(/settings\.source\.migrationConflictCount/u),
  55. ).toBeTruthy();
  56. fireEvent.click(screen.getByRole("button", { name: /abandonMigration/u }));
  57. fireEvent.click(screen.getByRole("button", { name: /retryMigration/u }));
  58. expect(screen.queryByRole("button", { name: /exportMigrationReport/u })).toBeNull();
  59. expect(screen.getByText("/workspace/conflicted").getAttribute("title")).toBe("/workspace/conflicted");
  60. expect(onAbandon).toHaveBeenCalledWith("/workspace/conflicted", "migration-1");
  61. expect(onRetry).toHaveBeenCalledWith("/workspace/conflicted", "migration-1");
  62. });
  63. it("does not offer abandon for a resumable phase", () => {
  64. const report: SourceServerMigrationReport = {
  65. rows: [
  66. {
  67. migrationId: "resume",
  68. workspacePath: "/repo",
  69. canonicalKey: "remote:server:s:/repo",
  70. variantKeys: [],
  71. status: "failed",
  72. phase: "backups-created",
  73. updatedAt: 1,
  74. conflicts: [],
  75. },
  76. ],
  77. };
  78. render(
  79. createElement(SourceServerMigrationReportPanel, {
  80. report,
  81. onAbandon: vi.fn(),
  82. onRetry: vi.fn(),
  83. }),
  84. );
  85. expect(screen.queryByRole("button", { name: /abandonMigration/u })).toBeNull();
  86. });
  87. it("only exposes actions for the latest migration of a workspace path", () => {
  88. const onRetry = vi.fn();
  89. const report: SourceServerMigrationReport = {
  90. rows: [
  91. {
  92. migrationId: "newest",
  93. workspacePath: "/repo",
  94. canonicalKey: "remote:server:s:/repo",
  95. variantKeys: [],
  96. status: "failed",
  97. updatedAt: 2,
  98. conflicts: [],
  99. },
  100. {
  101. migrationId: "stale",
  102. workspacePath: "/repo",
  103. canonicalKey: "remote:server:s:/repo",
  104. variantKeys: [],
  105. status: "failed",
  106. updatedAt: 1,
  107. conflicts: [],
  108. },
  109. ],
  110. };
  111. render(
  112. createElement(SourceServerMigrationReportPanel, {
  113. report,
  114. onAbandon: vi.fn(),
  115. onRetry,
  116. }),
  117. );
  118. const retryButtons = screen.getAllByRole("button", { name: /retryMigration/u });
  119. expect(retryButtons).toHaveLength(1);
  120. fireEvent.click(retryButtons[0]!);
  121. expect(onRetry).toHaveBeenCalledWith("/repo", "newest");
  122. });
  123. it("allows an abandoned-diverged directory to re-enter forward-only merge", () => {
  124. const onRetry = vi.fn();
  125. const report: SourceServerMigrationReport = {
  126. rows: [
  127. {
  128. migrationId: "migration-2",
  129. workspacePath: "/workspace/diverged",
  130. canonicalKey: "remote:server:source-a:/workspace/diverged",
  131. variantKeys: ["remote:docker:container:/workspace/diverged"],
  132. status: "abandoned-diverged",
  133. updatedAt: 2,
  134. abandonedAt: 1,
  135. conflicts: [],
  136. },
  137. ],
  138. };
  139. render(
  140. createElement(SourceServerMigrationReportPanel, {
  141. report,
  142. onAbandon: vi.fn(),
  143. onRetry,
  144. }),
  145. );
  146. expect(screen.queryByRole("button", { name: /abandonMigration/u })).toBeNull();
  147. fireEvent.click(screen.getByRole("button", { name: /retryMigration/u }));
  148. expect(onRetry).toHaveBeenCalledWith("/workspace/diverged", "migration-2");
  149. });
  150. it("describes a completed row with the merged-history count instead of a bare number", () => {
  151. const report: SourceServerMigrationReport = {
  152. rows: [
  153. {
  154. migrationId: "done",
  155. workspacePath: "/home/dev",
  156. canonicalKey: "remote:server:s:/home/dev",
  157. variantKeys: [
  158. "remote:ssh:a:22:u:/home/dev",
  159. "remote:ssh:b:22:u:/home/dev",
  160. ],
  161. status: "completed",
  162. updatedAt: 1,
  163. conflicts: [],
  164. },
  165. ],
  166. };
  167. render(
  168. createElement(SourceServerMigrationReportPanel, {
  169. report,
  170. onAbandon: vi.fn(),
  171. onRetry: vi.fn(),
  172. }),
  173. );
  174. // 完成行说明使用「已合并 N 条线路的历史」(N=variantKeys 数),完成行不提供操作按钮。
  175. expect(screen.getByText(/settings\.source\.migrationMergedHistory/u)).toBeTruthy();
  176. expect(screen.queryByRole("button", { name: /retryMigration/u })).toBeNull();
  177. });
  178. });