automationProjectOptions.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // @vitest-environment jsdom
  2. import { cleanup, renderHook, waitFor } from "@testing-library/react";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import type { VirtualProject } from "@zcode/services";
  5. import {
  6. resolveAutomationProjectOptions,
  7. useAutomationProjectOptions,
  8. } from "@/hooks/useAutomationProjectOptions.js";
  9. const service = vi.hoisted(() => ({
  10. readProjects: vi.fn(),
  11. readTree: vi.fn(async () => {
  12. throw new Error("task query forbidden");
  13. }),
  14. onMessage: () => ({ dispose() {} }),
  15. }));
  16. vi.mock("@/hooks/useWorkspaceServices.js", () => ({
  17. useBaseWorkspaceServices: () => services,
  18. }));
  19. const services = { projectService: service, broadcastService: { onMessage: service.onMessage } };
  20. afterEach(() => {
  21. cleanup();
  22. vi.clearAllMocks();
  23. });
  24. const project = (
  25. id: string,
  26. roots: string[] = [],
  27. extra: Partial<VirtualProject> = {},
  28. ): VirtualProject => ({
  29. id,
  30. hostId: "local",
  31. name: id,
  32. roots: roots.map((rootPath, position) => ({ rootPath, position, isPrimary: position === 0 })),
  33. deleted: false,
  34. pinned: false,
  35. nameOverridden: true,
  36. position: 0,
  37. createdAt: 1,
  38. updatedAt: 1,
  39. ...extra,
  40. });
  41. describe("automation virtual project options", () => {
  42. it("opening the project selector reads directories without querying tasks or auto-archiving", async () => {
  43. service.readProjects.mockResolvedValue([project("p", ["/unopened"])]);
  44. const { result } = renderHook(useAutomationProjectOptions);
  45. await waitFor(() => expect(result.current.map((option) => option.projectId)).toEqual(["p"]));
  46. expect(service.readProjects).toHaveBeenCalledOnce();
  47. expect(service.readTree).not.toHaveBeenCalled();
  48. });
  49. it("keeps empty projects and overlapping roots as distinct choices without open tabs", () => {
  50. expect(
  51. resolveAutomationProjectOptions([
  52. project("empty"),
  53. project("a", ["/same", "/extra"]),
  54. project("b", ["/same"]),
  55. ]),
  56. ).toEqual([
  57. { projectId: "empty", hostId: "local", workspacePath: "", label: "empty" },
  58. { projectId: "a", hostId: "local", workspacePath: "/same", label: "a" },
  59. { projectId: "b", hostId: "local", workspacePath: "/same", label: "b" },
  60. ]);
  61. });
  62. it("excludes deleted projects and remote entries from local scheduling", () => {
  63. expect(
  64. resolveAutomationProjectOptions([
  65. project("deleted", [], { deleted: true }),
  66. project("remote", [], { hostId: "remote:server:s" }),
  67. ]),
  68. ).toEqual([]);
  69. });
  70. });