resolveConversationWorkspaceTarget.test.ts 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { describe, expect, it, vi } from "vitest";
  2. import { resolveConversationWorkspaceTarget } from "@/conversation-workspace-target/index.js";
  3. import { createTabStore } from "@/store/tabStore.js";
  4. describe("RPN-11 conversation target", () => {
  5. it.each(["project", "conversation"] as const)(
  6. "rejects a remote %s even when a local conversation has the same path",
  7. async (workspacePurpose) => {
  8. const store = createTabStore();
  9. const path = "/home/dev/.zcode/workspace/default";
  10. const identity = `remote:server:source-a:${path}`;
  11. store.getState().addTab(path, { workspacePurpose: "conversation" });
  12. store.getState().addTab(path, { workspacePurpose, workspaceIdentity: identity });
  13. const resolveLocalConversationWorkspacePath = vi.fn(async () => path);
  14. await expect(
  15. resolveConversationWorkspaceTarget({
  16. getTabs: () => store.getState().tabs,
  17. activeWorkspacePath: path,
  18. activeWorkspaceIdentity: identity,
  19. resolveLocalConversationWorkspacePath,
  20. }),
  21. ).rejects.toThrow("Remote projects require a project workspace");
  22. expect(resolveLocalConversationWorkspacePath).not.toHaveBeenCalled();
  23. },
  24. );
  25. it("resolves local project detachment locally", async () => {
  26. const store = createTabStore();
  27. store.getState().addTab("/local/project");
  28. const resolveLocalConversationWorkspacePath = vi.fn(async () => "/local/conversation");
  29. await expect(
  30. resolveConversationWorkspaceTarget({
  31. getTabs: () => store.getState().tabs,
  32. activeWorkspacePath: "/local/project",
  33. activeWorkspaceIdentity: null,
  34. resolveLocalConversationWorkspacePath,
  35. }),
  36. ).resolves.toEqual({ workspacePath: "/local/conversation" });
  37. expect(resolveLocalConversationWorkspacePath).toHaveBeenCalledOnce();
  38. });
  39. it.each(["remote-first", "local-first"] as const)(
  40. "resolves the active local project when a remote tab shares its path (%s)",
  41. async (order) => {
  42. const store = createTabStore();
  43. const path = "/local/project";
  44. const identity = `remote:server:source-a:${path}`;
  45. const localId = store.getState().addTab(path);
  46. store.getState().addTab(path, { workspaceIdentity: identity });
  47. if (order === "local-first") store.getState().reorderWorkspaceTabs(1, 0);
  48. store.getState().activateTab(localId);
  49. const resolveLocalConversationWorkspacePath = vi.fn(async () => "/local/conversation");
  50. await expect(
  51. resolveConversationWorkspaceTarget({
  52. getTabs: () => store.getState().tabs,
  53. activeWorkspacePath: store.getState().activeWorkspacePath,
  54. activeWorkspaceIdentity: store.getState().activeWorkspaceIdentity,
  55. resolveLocalConversationWorkspacePath,
  56. }),
  57. ).resolves.toEqual({ workspacePath: "/local/conversation" });
  58. expect(resolveLocalConversationWorkspacePath).toHaveBeenCalledOnce();
  59. },
  60. );
  61. it("reuses the active local conversation when a remote tab with the same path is first", async () => {
  62. const store = createTabStore();
  63. const path = "/local/conversation";
  64. const localId = store.getState().addTab(path, { workspacePurpose: "conversation" });
  65. store.getState().addTab(path, { workspaceIdentity: `remote:server:source-a:${path}` });
  66. store.getState().activateTab(localId);
  67. const resolveLocalConversationWorkspacePath = vi.fn(async () => "/unused");
  68. await expect(
  69. resolveConversationWorkspaceTarget({
  70. getTabs: () => store.getState().tabs,
  71. activeWorkspacePath: store.getState().activeWorkspacePath,
  72. activeWorkspaceIdentity: store.getState().activeWorkspaceIdentity,
  73. resolveLocalConversationWorkspacePath,
  74. }),
  75. ).resolves.toEqual({ workspacePath: path });
  76. expect(resolveLocalConversationWorkspacePath).not.toHaveBeenCalled();
  77. });
  78. it("reuses the local conversation without creating another workspace", async () => {
  79. const store = createTabStore();
  80. store.getState().addTab("/local/conversation", { workspacePurpose: "conversation" });
  81. const resolveLocalConversationWorkspacePath = vi.fn(async () => "/unused");
  82. await expect(
  83. resolveConversationWorkspaceTarget({
  84. getTabs: () => store.getState().tabs,
  85. activeWorkspacePath: "/local/conversation",
  86. activeWorkspaceIdentity: null,
  87. resolveLocalConversationWorkspacePath,
  88. }),
  89. ).resolves.toEqual({ workspacePath: "/local/conversation" });
  90. expect(resolveLocalConversationWorkspacePath).not.toHaveBeenCalled();
  91. });
  92. });