mobileProjectDraftBootstrap.test.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // @vitest-environment jsdom
  2. import { cleanup, renderHook } from "@testing-library/react";
  3. import { afterEach, beforeEach, expect, it, vi } from "vitest";
  4. import { useRootPlatformEffects } from "@/root/useRootPlatformEffects.js";
  5. import { persistV4ComposerDraft, V4_DRAFT_SCOPE_ROOT } from "@/v4/composer/composerDraftStore.js";
  6. import { useZCodeSessionStore } from "@/store/zcodeSessionStore.js";
  7. vi.mock("@/hooks/useWorkspaceServices.js", () => ({
  8. useOptionalBaseWorkspaceServices: () => null,
  9. }));
  10. vi.mock("@/store/TabStoreProvider.js", () => ({
  11. useTabStore: (selector: (value: unknown) => unknown) => selector({ tabs: [], activeTabId: null }),
  12. }));
  13. vi.mock("@/lib/rendererNavigation.js", () => ({ isRendererReloadNavigation: () => false }));
  14. beforeEach(() => {
  15. localStorage.clear();
  16. useZCodeSessionStore.setState({ workspaces: {} });
  17. });
  18. afterEach(cleanup);
  19. function mount(options: Partial<Parameters<typeof useRootPlatformEffects>[0]> = {}) {
  20. const startDraftInWorkspace = vi.fn();
  21. const subscribe = () => () => {};
  22. const platform = {
  23. onFocusTab: subscribe,
  24. onNewTab: subscribe,
  25. onNewTask: subscribe,
  26. onOpenWorkspace: subscribe,
  27. onTaskNotificationClick: subscribe,
  28. syncWindowTabs: vi.fn(),
  29. syncWindowUnreadCount: vi.fn(),
  30. syncActiveTaskSession: vi.fn(),
  31. setApplicationLocale: vi.fn(async () => {}),
  32. };
  33. renderHook(() =>
  34. useRootPlatformEffects({
  35. initialWorkspaceAbsPath: "/cwd",
  36. initialWorkspaceIdentity: "remote:server:s:/cwd",
  37. clientMode: "web-remote-replayable",
  38. addTab: vi.fn(),
  39. setIsBootstrappingInitialWorkspace: vi.fn(),
  40. platform: platform as never,
  41. activateTabByPath: vi.fn(() => true),
  42. startDraftInWorkspace,
  43. startNewTaskFromActiveWorkspace: vi.fn(),
  44. openWorkspace: vi.fn(),
  45. openWorkspacePath: vi.fn(),
  46. setWorkspaceActionError: vi.fn(),
  47. isDesktop: false,
  48. locale: "en-US",
  49. tabs: [],
  50. totalUnreadTaskCount: 0,
  51. intl: { formatMessage: ({ id }: { id: string }) => id } as never,
  52. isRestoringOAuthSession: false,
  53. ...options,
  54. }),
  55. );
  56. return startDraftInWorkspace;
  57. }
  58. it("mobile Root recreation restores project under the same identity before initializing the draft", () => {
  59. persistV4ComposerDraft("/cwd", "remote:server:s:/cwd", V4_DRAFT_SCOPE_ROOT, {
  60. text: "unsent",
  61. projectId: "p",
  62. taskGroupId: "g",
  63. });
  64. const start = mount();
  65. expect(start).toHaveBeenCalledWith("/cwd", "remote:server:s:/cwd", {
  66. projectId: "p",
  67. taskGroupId: "g",
  68. });
  69. });
  70. it("explicit new unclassified intent wins over a saved project draft", () => {
  71. persistV4ComposerDraft("/cwd", "remote:server:s:/cwd", V4_DRAFT_SCOPE_ROOT, {
  72. text: "unsent",
  73. projectId: "p",
  74. });
  75. const start = mount({ initialTaskDraft: { projectId: null } });
  76. expect(start).toHaveBeenCalledWith("/cwd", "remote:server:s:/cwd", { projectId: null });
  77. });
  78. it("desktop continuous and another remote identity do not restore this mobile project", () => {
  79. persistV4ComposerDraft("/cwd", "remote:server:s:/cwd", V4_DRAFT_SCOPE_ROOT, {
  80. text: "unsent",
  81. projectId: "p",
  82. });
  83. const desktop = mount({ clientMode: "desktop-continuous" });
  84. expect(desktop).toHaveBeenCalledWith("/cwd", "remote:server:s:/cwd", undefined);
  85. const other = mount({ initialWorkspaceIdentity: "remote:server:other:/cwd" });
  86. expect(other).toHaveBeenCalledWith("/cwd", "remote:server:other:/cwd", undefined);
  87. });
  88. it("an explicit task opens directly and never initializes a project draft", () => {
  89. const start = mount({ initialTaskId: "existing" });
  90. expect(start).not.toHaveBeenCalled();
  91. expect(
  92. useZCodeSessionStore.getState().getWorkspaceState("/cwd", "remote:server:s:/cwd").activeTaskId,
  93. ).toBe("existing");
  94. });