remoteConnectionDeviceStepPreferredProfile.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 type { SourceServerManagementSnapshot } from "@zcode/shared";
  6. vi.mock("@/i18n/IntlProvider.js", () => ({
  7. useZCodeIntl: () => ({
  8. intl: { formatMessage: ({ id }: { id: string }) => id },
  9. locale: "zh-CN",
  10. }),
  11. }));
  12. import { RemoteConnectionSelection } from "@/remote-connection/RemoteConnectionSelection.js";
  13. afterEach(() => {
  14. cleanup();
  15. vi.clearAllMocks();
  16. });
  17. const sourceServerId = "3cd4f9da-cd79-426c-9d70-75f36e9f6a87";
  18. function profileOf(profileId: string, overrides: Partial<Record<string, unknown>> = {}) {
  19. return {
  20. profileId,
  21. transport: "docker" as const,
  22. target: { kind: "docker" as const, container: profileId },
  23. sourceServerId,
  24. createdAt: 1,
  25. ...overrides,
  26. };
  27. }
  28. function snapshotOf(
  29. profiles: Array<ReturnType<typeof profileOf>>,
  30. overrides: Partial<SourceServerManagementSnapshot> = {},
  31. ): SourceServerManagementSnapshot {
  32. return {
  33. generation: 1,
  34. profiles,
  35. servers: [
  36. {
  37. sourceServerId,
  38. displayName: "devbox",
  39. installState: "installed",
  40. preferredProfileId: "p-preferred",
  41. },
  42. ],
  43. workspaceHistory: [],
  44. states: [],
  45. failedPathCount: 0,
  46. abandonedPathCount: 0,
  47. migrationCounts: [],
  48. ...overrides,
  49. } as SourceServerManagementSnapshot;
  50. }
  51. function renderDeviceStep(snapshot: SourceServerManagementSnapshot) {
  52. const onSelectProfile = vi.fn();
  53. render(
  54. createElement(RemoteConnectionSelection, {
  55. snapshot,
  56. onSelectProfile,
  57. onNewConnection: vi.fn(),
  58. aliases: [],
  59. distros: [],
  60. containers: [],
  61. availableKinds: ["ssh"],
  62. dockerAvailable: null,
  63. loading: false,
  64. discoveryError: "",
  65. onSelectAlias: vi.fn(),
  66. onSelectTarget: vi.fn(),
  67. remoteConversationWorkspaceBySourceServerId: {},
  68. }),
  69. );
  70. return onSelectProfile;
  71. }
  72. /** 合同 §8.1.1 线路选择规则:preferred 有效用之 → 已删/归档回退第一条 active → 无 active 禁用。 */
  73. describe("RemoteConnectionDeviceStep preferred profile fallback", () => {
  74. it("connects through the preferred profile when it is still active", () => {
  75. const onSelectProfile = renderDeviceStep(
  76. snapshotOf([profileOf("p-first"), profileOf("p-preferred")], {
  77. states: [
  78. {
  79. profileId: "p-preferred",
  80. sourceServerId,
  81. state: "online",
  82. updateAvailability: "none",
  83. updatedAt: 1,
  84. },
  85. ],
  86. } as never),
  87. );
  88. fireEvent.click(screen.getByText("devbox"));
  89. expect(onSelectProfile).toHaveBeenCalledWith(
  90. expect.objectContaining({ profileId: "p-preferred" }),
  91. );
  92. });
  93. it("falls back to the first active profile when the preferred one is retired", () => {
  94. const onSelectProfile = renderDeviceStep(
  95. snapshotOf([
  96. profileOf("p-preferred", { lifecycleState: "retired" }),
  97. profileOf("p-first"),
  98. profileOf("p-second"),
  99. ]),
  100. );
  101. fireEvent.click(screen.getByText("devbox"));
  102. expect(onSelectProfile).toHaveBeenCalledWith(expect.objectContaining({ profileId: "p-first" }));
  103. });
  104. it("disables the connect entry when the device has no active profile left", () => {
  105. const onSelectProfile = renderDeviceStep(
  106. snapshotOf([
  107. profileOf("p-preferred", { lifecycleState: "retired" }),
  108. profileOf("p-old", { lifecycleState: "retired" }),
  109. ]),
  110. );
  111. const button = screen.getByText("devbox").closest("button");
  112. expect(button?.disabled).toBe(true);
  113. fireEvent.click(button!);
  114. expect(onSelectProfile).not.toHaveBeenCalled();
  115. });
  116. });