remoteConversationDisconnected.test.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import zhCN from "@/i18n/locales/zh-CN.js";
  6. const ports = vi.hoisted(() => ({ reconnect: vi.fn(), details: vi.fn() }));
  7. vi.mock("@/i18n/IntlProvider.js", () => ({
  8. useZCodeIntl: () => ({
  9. intl: { formatMessage: ({ id }: { id: keyof typeof zhCN }) => zhCN[id] ?? id },
  10. }),
  11. }));
  12. vi.mock("@/project-sidebar/useRemoteProjectConnectionPresentation.js", () => ({
  13. useRemoteProjectConnectionPresentation: () => ({
  14. presentation: {
  15. status: "offline",
  16. icon: "cloud-off",
  17. tone: "neutral",
  18. messageId: "remote.status.offline",
  19. surface: "bg-surface",
  20. foreground: "text-foreground-subtle",
  21. primaryAction: "connect",
  22. actionMessageId: "remote.action.connect",
  23. },
  24. reconnect: ports.reconnect,
  25. tab: null,
  26. deviceName: "dev",
  27. error: undefined,
  28. }),
  29. }));
  30. import { RemoteConversationDisconnected } from "@/project-sidebar/RemoteConversationDisconnected.js";
  31. import { persistV4ComposerDraft, readV4ComposerDraft } from "@/v4/composer/composerDraftStore.js";
  32. afterEach(() => {
  33. cleanup();
  34. localStorage.clear();
  35. vi.clearAllMocks();
  36. });
  37. describe("断线会话占位", () => {
  38. it("显示原因、只读草稿和重连入口;回车不提交,重连不清草稿", () => {
  39. const identity = "remote:server:dev:/repo";
  40. persistV4ComposerDraft("/repo", identity, "task-a", { text: "保留这份草稿" });
  41. render(
  42. createElement(RemoteConversationDisconnected, {
  43. workspacePath: "/repo",
  44. workspaceIdentity: identity,
  45. sessionId: "task-a",
  46. }),
  47. );
  48. expect(screen.getByText("设备未连接,连接后继续此会话")).toBeTruthy();
  49. const input = screen.getByRole("textbox") as HTMLTextAreaElement;
  50. expect(input.value).toBe("保留这份草稿");
  51. expect(input.readOnly).toBe(true);
  52. expect((screen.getByRole("button", { name: "发送" }) as HTMLButtonElement).disabled).toBe(true);
  53. fireEvent.keyDown(input, { key: "Enter" });
  54. expect(ports.reconnect).not.toHaveBeenCalled();
  55. fireEvent.click(screen.getByRole("button", { name: "重新连接" }));
  56. expect(ports.reconnect).toHaveBeenCalledOnce();
  57. expect(readV4ComposerDraft("/repo", identity, "task-a")?.text).toBe("保留这份草稿");
  58. });
  59. });
  60. it("离线项目占位只有一行连接入口,不渲染过期任务", async () => {
  61. const { OfflineRemoteProjectRows } =
  62. await import("@/project-sidebar/OfflineRemoteProjectRows.js");
  63. const reconnect = vi.fn();
  64. render(
  65. createElement(OfflineRemoteProjectRows, {
  66. workspaceTabs: [
  67. {
  68. id: "remote",
  69. kind: "workspace",
  70. label: "项目",
  71. workspacePath: "/repo",
  72. workspaceIdentity: "remote:server:dev:/repo",
  73. },
  74. ],
  75. onReconnect: reconnect,
  76. }),
  77. );
  78. fireEvent.click(screen.getByRole("button"));
  79. expect(reconnect).toHaveBeenCalledWith("remote:server:dev:/repo");
  80. expect(screen.getAllByRole("button")).toHaveLength(1);
  81. });