remoteConnectionPlaceholderValues.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import zhCN from "@/i18n/locales/zh-CN.js";
  5. /**
  6. * §9 门禁 1(弹窗部分):connectedBanner 与识别卡的占位符必须渲染为实值。
  7. * mock 复刻 IntlProvider 真实语义(真实 zh-CN 消息 + replaceAll 插值)。
  8. */
  9. vi.mock("@/i18n/IntlProvider.js", () => ({
  10. useZCodeIntl: () => ({
  11. intl: {
  12. formatMessage: ({ id }: { id: string }, values?: Record<string, string | number>) => {
  13. let message = (zhCN as Record<string, string>)[id] ?? id;
  14. if (values) {
  15. for (const [key, value] of Object.entries(values)) {
  16. message = message.replaceAll(`{${key}}`, String(value));
  17. }
  18. }
  19. return message;
  20. },
  21. },
  22. locale: "zh-CN",
  23. }),
  24. }));
  25. vi.mock("@/DirectoryBrowser.js", () => ({
  26. DirectoryBrowser: () => createElement("div", { "data-testid": "directory-browser" }),
  27. }));
  28. vi.mock("@/RemoteConnectionFields.js", () => ({
  29. RemoteConnectionFields: () => createElement("div", { "data-testid": "remote-connection-fields" }),
  30. }));
  31. vi.mock("@/settings/RemoteSyncActions.js", () => ({
  32. RemoteSyncDialogs: () => createElement("div", { "data-testid": "remote-sync-dialogs" }),
  33. RemoteSyncDropdownButton: () => createElement("button", { type: "button" }, "sync"),
  34. shouldShowRemoteSyncActions: () => false,
  35. }));
  36. vi.mock("@/hooks/usePlatform.js", () => ({
  37. usePlatform: () => ({}),
  38. }));
  39. vi.mock("@/feedback/feedbackStore.js", () => ({
  40. useFeedbackStore: (selector: (state: unknown) => unknown) => selector({ openSubmit: vi.fn() }),
  41. }));
  42. import { RemoteConnectionDirectoryStep } from "@/RemoteConnectionDialogContent.js";
  43. import { RemoteConnectionConnectingStep } from "@/remote-connection/RemoteConnectionConnectingStep.js";
  44. describe("remote connection dialog placeholder values", () => {
  45. it("shows connection and version in the banner without repeating the device", () => {
  46. const html = renderToStaticMarkup(
  47. createElement(RemoteConnectionDirectoryStep, {
  48. services: {} as never,
  49. remoteTarget: { kind: "ssh", host: "example.com", username: "dev" },
  50. deviceName: "dev",
  51. profileName: "home-lab",
  52. deviceVersion: "3.8.1",
  53. onSelect: vi.fn(),
  54. onBack: vi.fn(),
  55. onCancel: vi.fn(),
  56. }),
  57. );
  58. expect(html).toContain("已连接");
  59. expect(html).toContain("home-lab");
  60. expect(html).toContain("v3.8.1");
  61. expect(html).not.toContain("已连接 dev");
  62. expect(html).not.toMatch(/\{(?:device|profile|version)\}/u);
  63. });
  64. it("interpolates the recognized device card title and profile body", () => {
  65. const html = renderToStaticMarkup(
  66. createElement(RemoteConnectionConnectingStep, {
  67. kind: "ssh",
  68. logs: [],
  69. errorMessage: "",
  70. loading: false,
  71. recognizedDeviceName: "dev",
  72. profileName: "home-lab",
  73. onBack: vi.fn(),
  74. onRetry: vi.fn(),
  75. }),
  76. );
  77. expect(html).toContain("已连接设备 dev");
  78. expect(html).not.toContain("remote.recognizedDeviceBody");
  79. expect(html).not.toMatch(/\{(?:device|profile)\}/u);
  80. });
  81. it("版本不兼容时将无效重试替换为就地更新", () => {
  82. const html = renderToStaticMarkup(
  83. createElement(RemoteConnectionConnectingStep, {
  84. kind: "ssh",
  85. logs: [],
  86. errorMessage: "远程设备版本不兼容",
  87. loading: false,
  88. upgradeRequired: true,
  89. onUpgrade: vi.fn(),
  90. onBack: vi.fn(),
  91. onRetry: vi.fn(),
  92. }),
  93. );
  94. expect(html).toContain("更新远端");
  95. expect(html).not.toMatch(/>重试<\/button>/u);
  96. });
  97. });