projectUpgradeOrder.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // @vitest-environment jsdom
  2. import { afterEach, expect, it } from "vitest";
  3. import { act, cleanup, renderHook } from "@testing-library/react";
  4. import { mergeProjectTrees, projectKey, selectSortedProjects } from "@/project-sidebar/view.js";
  5. import { useProjectSidebarPreferences } from "@/project-sidebar/useProjectSidebarPreferences.js";
  6. import type { VirtualProject } from "@/project-sidebar/contract.js";
  7. const storageKey = "zcode-project-sidebar-preferences";
  8. const project = (id: string, position: number, hostId = "local"): VirtualProject => ({
  9. id,
  10. name: id,
  11. hostId,
  12. position,
  13. roots: [{ rootPath: "/same", position: 0, isPrimary: true }],
  14. nameOverridden: false,
  15. pinned: false,
  16. deleted: false,
  17. createdAt: 1,
  18. updatedAt: 1,
  19. });
  20. function snapshot(online: boolean, migrated = true) {
  21. const hostId = online ? "remote:server:source" : "remote:ssh:example.test:22:dev";
  22. const remote = project("remote", 0, hostId);
  23. return mergeProjectTrees(
  24. { projects: [project("a", 0), project("b", 2)], tasks: [] },
  25. {
  26. hosts: [{ id: hostId, membershipId: "m", kind: "ssh", target: null, position: 0 }],
  27. entries: [
  28. {
  29. id: "entry",
  30. hostId,
  31. projectId: online ? remote.id : null,
  32. name: "remote",
  33. roots: remote.roots,
  34. lastSyncedAt: online ? 1 : null,
  35. legacyWorkspace: {
  36. kind: "remote",
  37. workspacePath: "/same",
  38. target: { kind: "ssh", host: "example.test", username: "dev" },
  39. lastOpenedAt: 1,
  40. lastConnectionStatus: "failed",
  41. },
  42. ...(migrated ? { initialPosition: 1 } : {}),
  43. },
  44. ],
  45. },
  46. online ? new Map([[hostId, { projects: [remote], tasks: [] }]]) : new Map(),
  47. );
  48. }
  49. const props = (online: boolean, migrated = true) => {
  50. const result = snapshot(online, migrated);
  51. return { projects: selectSortedProjects(result.tree), availability: result.availability };
  52. };
  53. const names = (projects: VirtualProject[]) => projects.map((p) => p.name);
  54. afterEach(() => {
  55. cleanup();
  56. localStorage.clear();
  57. });
  58. it("首次升级 remote 第 2 位:离线、Source 身份绑定、项目解析及重载保持原位", () => {
  59. const offline = props(false);
  60. expect(names(offline.projects)).toEqual(["a", "remote", "b"]);
  61. const hook = renderHook(
  62. ({ projects, availability }) => useProjectSidebarPreferences(projects, false, availability),
  63. { initialProps: offline },
  64. );
  65. const online = props(true);
  66. hook.rerender(online);
  67. expect(names(hook.result.current.sort(online.projects))).toEqual(["a", "remote", "b"]);
  68. const expected = online.projects.map(projectKey);
  69. expect(hook.result.current.order).toEqual(expected);
  70. expect(JSON.parse(localStorage.getItem(storageKey)!).order).toEqual(expected);
  71. hook.unmount();
  72. const reopened = renderHook(() =>
  73. useProjectSidebarPreferences(online.projects, false, online.availability),
  74. );
  75. expect(reopened.result.current.order).toEqual(expected);
  76. });
  77. it("首次升级后用户离线拖动的位置优先于初始序号,连接及重复刷新不重排", () => {
  78. const offline = props(false);
  79. const hook = renderHook(
  80. ({ projects, availability }) => useProjectSidebarPreferences(projects, false, availability),
  81. { initialProps: offline },
  82. );
  83. act(() =>
  84. hook.result.current.reorder(
  85. offline.projects,
  86. projectKey(offline.projects[1]!),
  87. projectKey(offline.projects[0]!),
  88. ),
  89. );
  90. const online = props(true);
  91. hook.rerender(online);
  92. expect(names(hook.result.current.sort(online.projects))).toEqual(["remote", "a", "b"]);
  93. const order = hook.result.current.order;
  94. hook.rerender(props(true));
  95. expect(hook.result.current.order).toBe(order);
  96. });
  97. it("未带首次升级标记的既有入口不执行排序修复,同路径本地项目不受影响", () => {
  98. const offline = props(false, false);
  99. const hook = renderHook(
  100. ({ projects, availability }) => useProjectSidebarPreferences(projects, false, availability),
  101. { initialProps: offline },
  102. );
  103. const before = hook.result.current.order;
  104. const online = props(true, false);
  105. hook.rerender(online);
  106. expect(hook.result.current.order).toEqual([
  107. ...before,
  108. projectKey(online.projects.find((p) => p.name === "remote")!),
  109. ]);
  110. expect(names(hook.result.current.sort(online.projects))).toEqual(["a", "b", "remote"]);
  111. });