recentRemoteProjectPreference.test.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { describe, expect, it } from "vitest";
  2. import { resolveRecentRemoteProjectPreference } from "@/project-sidebar/remote-project-connection.js";
  3. import type { RemoteProjectDirectory, RemoteProjectEntry } from "@/project-sidebar/contract.js";
  4. const hostId = "remote:server:source-1";
  5. const entry: RemoteProjectEntry = {
  6. id: "entry-1",
  7. hostId,
  8. projectId: "project-1",
  9. name: "project",
  10. roots: [{ rootPath: "/srv/project", position: 0, isPrimary: true }],
  11. legacyWorkspace: null,
  12. lastSyncedAt: null,
  13. };
  14. const directory: RemoteProjectDirectory = {
  15. hosts: [{ id: hostId, membershipId: "member-1", kind: "ssh", target: null, position: 0 }],
  16. entries: [entry],
  17. };
  18. const input = { sourceServerId: "source-1", path: "/srv/project", profileId: "profile-1" };
  19. describe("设置最近远程项目的打开意图", () => {
  20. it("不依赖 renderer tab,按 Source 和路径固定已有项目入口", () => {
  21. expect(resolveRecentRemoteProjectPreference(directory, input)).toEqual({
  22. preferredProfileId: "profile-1",
  23. preferredDirectoryPath: "/srv/project",
  24. preferredProjectEntryId: "entry-1",
  25. });
  26. });
  27. it("其他设备的同路径项目不能命中", () => {
  28. const other = { ...entry, id: "other", hostId: "remote:server:source-2" };
  29. expect(
  30. resolveRecentRemoteProjectPreference({ ...directory, entries: [other, entry] }, input),
  31. ).toHaveProperty("preferredProjectEntryId", "entry-1");
  32. expect(
  33. resolveRecentRemoteProjectPreference({ ...directory, entries: [other] }, input),
  34. ).toBeNull();
  35. });
  36. it("没有指定线路时保留 Source,仍固定项目入口", () => {
  37. expect(
  38. resolveRecentRemoteProjectPreference(directory, { ...input, profileId: undefined }),
  39. ).toEqual({
  40. preferredSourceServerId: "source-1",
  41. preferredDirectoryPath: "/srv/project",
  42. preferredProjectEntryId: "entry-1",
  43. });
  44. });
  45. it("入口移除、Host 移除或同目录多个项目时不生成创建意图或猜选第一项", () => {
  46. expect(resolveRecentRemoteProjectPreference({ ...directory, entries: [] }, input)).toBeNull();
  47. expect(resolveRecentRemoteProjectPreference({ ...directory, hosts: [] }, input)).toBeNull();
  48. expect(
  49. resolveRecentRemoteProjectPreference(
  50. { ...directory, entries: [entry, { ...entry, id: "entry-2", projectId: "project-2" }] },
  51. input,
  52. ),
  53. ).toBeNull();
  54. });
  55. it("旧项目入口仍按保留的远程目录解析", () => {
  56. const legacy: RemoteProjectEntry = {
  57. ...entry,
  58. projectId: null,
  59. roots: [],
  60. legacyWorkspace: {
  61. kind: "remote",
  62. workspacePath: input.path,
  63. lastOpenedAt: 1,
  64. lastConnectionStatus: "connected",
  65. target: { kind: "ssh", host: "dev.example.com", username: "dev" },
  66. },
  67. };
  68. expect(
  69. resolveRecentRemoteProjectPreference({ ...directory, entries: [legacy] }, input),
  70. ).toHaveProperty("preferredProjectEntryId", "entry-1");
  71. });
  72. });