projectTaskNavigationStability.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // @vitest-environment jsdom
  2. import { act, renderHook } from "@testing-library/react";
  3. import { beforeEach, expect, it, vi } from "vitest";
  4. import type { ProjectSidebarProps } from "@/project-sidebar/contract.js";
  5. import { useProjectTaskNavigation } from "@/project-sidebar/useProjectTaskNavigation.js";
  6. const focus = vi.hoisted(() => ({ activeTaskId: "original", draftFocusVersion: 0 }));
  7. vi.mock("@/store/zcodeSessionStore.js", () => ({
  8. useZCodeSessionStore: { getState: () => ({ getWorkspaceState: () => ({ ...focus }) }) },
  9. }));
  10. beforeEach(() => {
  11. focus.activeTaskId = "original";
  12. focus.draftFocusVersion = 0;
  13. });
  14. vi.mock("@/store/TabStoreProvider.js", () => ({
  15. useTabStore: (select: (state: unknown) => unknown) => select({ activeWorkspaceIdentity: null }),
  16. }));
  17. it("keeps the row action stable across tree refreshes and uses the latest navigation callback", async () => {
  18. const first = vi.fn();
  19. const second = vi.fn();
  20. const { result, rerender } = renderHook(
  21. ({ select }) =>
  22. useProjectTaskNavigation({ onSelectTask: select } as unknown as ProjectSidebarProps, {
  23. projects: [],
  24. tasks: [],
  25. }),
  26. { initialProps: { select: first } },
  27. );
  28. const before = result.current;
  29. rerender({ select: second });
  30. expect(result.current).toBe(before);
  31. result.current("/same", "task");
  32. expect(first).not.toHaveBeenCalled();
  33. expect(second).toHaveBeenCalledWith("/same", "task", undefined, undefined, undefined);
  34. });
  35. it.each(["task", "draft"])(
  36. "ignores a late directory response after another %s selection in the same workspace",
  37. async (kind) => {
  38. const pending = Promise.withResolvers<{ hosts: never[] }>();
  39. const select = vi.fn();
  40. const task = {
  41. taskId: "remote-task",
  42. workspacePath: "/same",
  43. workspaceIdentity: "remote:server:s:/same",
  44. hostId: "remote:server:s",
  45. };
  46. const { result } = renderHook(() =>
  47. useProjectTaskNavigation(
  48. { workspacePath: "/current", onSelectTask: select } as unknown as ProjectSidebarProps,
  49. { projects: [], tasks: [task] } as never,
  50. { readDirectory: () => pending.promise } as never,
  51. ),
  52. );
  53. await act(async () => {
  54. result.current(task.workspacePath, task.taskId, task.workspaceIdentity);
  55. if (kind === "task") focus.activeTaskId = "other";
  56. else focus.draftFocusVersion++;
  57. pending.resolve({ hosts: [] });
  58. await pending.promise;
  59. });
  60. expect(select).not.toHaveBeenCalled();
  61. },
  62. );