automationWorkspaceOptions.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { describe, expect, it } from "vitest";
  2. import type { WindowTabState } from "@/store/tabStore.js";
  3. import {
  4. buildAutomationWorkspaceOptions,
  5. findAutomationWorkspaceOptionByKey,
  6. reconcileAutomationWorkspaceSelectionKey,
  7. resolveAutomationWorkspaceSelectionKey,
  8. } from "@/settings/automationWorkspaceOptions.js";
  9. describe("automation workspace options", () => {
  10. it("只保留当前打开且可用的项目 tab", () => {
  11. const tabs: WindowTabState[] = [
  12. { id: "settings", kind: "settings", label: "settings" } as never,
  13. {
  14. id: "project-open",
  15. kind: "workspace",
  16. workspacePath: "/repo/open",
  17. label: "open",
  18. workspacePurpose: "project",
  19. },
  20. {
  21. id: "conversation-default",
  22. kind: "workspace",
  23. workspacePath: "/app/default",
  24. label: "default",
  25. workspacePurpose: "conversation",
  26. },
  27. {
  28. id: "project-deleted",
  29. kind: "workspace",
  30. workspacePath: "/repo/deleted",
  31. label: "deleted",
  32. availability: "unavailable-local-directory",
  33. workspacePurpose: "project",
  34. },
  35. ] as WindowTabState[];
  36. expect(buildAutomationWorkspaceOptions(tabs)).toEqual([
  37. {
  38. workspacePath: "/repo/open",
  39. label: "open",
  40. },
  41. ]);
  42. });
  43. it("远程同路径项目按 workspaceIdentity 隔离并保留会话信息", () => {
  44. const tabs = [
  45. {
  46. id: "remote-a",
  47. kind: "workspace",
  48. workspacePath: "/repo",
  49. label: "remote-a",
  50. workspaceIdentity: "remote:ssh:a:/repo",
  51. remoteSessionId: "session-a",
  52. },
  53. {
  54. id: "remote-b",
  55. kind: "workspace",
  56. workspacePath: "/repo",
  57. label: "remote-b",
  58. workspaceIdentity: "remote:ssh:b:/repo",
  59. remoteSessionId: "session-b",
  60. },
  61. ] as WindowTabState[];
  62. expect(buildAutomationWorkspaceOptions(tabs)).toMatchObject([
  63. {
  64. label: "remote-a",
  65. workspaceIdentity: "remote:ssh:a:/repo",
  66. remoteSessionId: "session-a",
  67. },
  68. {
  69. label: "remote-b",
  70. workspaceIdentity: "remote:ssh:b:/repo",
  71. remoteSessionId: "session-b",
  72. },
  73. ]);
  74. const options = buildAutomationWorkspaceOptions(tabs);
  75. const selectedKey = resolveAutomationWorkspaceSelectionKey(options[1]!);
  76. expect(selectedKey).toBe("remote:ssh:b:/repo");
  77. expect(findAutomationWorkspaceOptionByKey(options, selectedKey)).toMatchObject({
  78. workspacePath: "/repo",
  79. workspaceIdentity: "remote:ssh:b:/repo",
  80. remoteSessionId: "session-b",
  81. });
  82. });
  83. it("无有效项目时选择收敛为 null,不回退到默认或 conversation workspace", () => {
  84. expect(
  85. reconcileAutomationWorkspaceSelectionKey([], "/app/default", {
  86. workspacePath: "/app/default",
  87. }),
  88. ).toBeNull();
  89. });
  90. it("候选恢复后优先选择有效默认项目,并保留仍有效的当前项目", () => {
  91. const options = [
  92. { workspacePath: "/repo/first", label: "first" },
  93. {
  94. workspacePath: "/repo",
  95. workspaceIdentity: "remote:ssh:b:/repo",
  96. label: "remote-b",
  97. },
  98. ];
  99. expect(
  100. reconcileAutomationWorkspaceSelectionKey(options, null, {
  101. workspacePath: "/repo",
  102. workspaceIdentity: "remote:ssh:b:/repo",
  103. }),
  104. ).toBe("remote:ssh:b:/repo");
  105. expect(
  106. reconcileAutomationWorkspaceSelectionKey(
  107. options,
  108. "/repo/first",
  109. {
  110. workspacePath: "/repo",
  111. workspaceIdentity: "remote:ssh:b:/repo",
  112. },
  113. ),
  114. ).toBe("/repo/first");
  115. });
  116. });