offPeakTaskListMenuHint.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // @vitest-environment jsdom
  2. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  3. import { createElement, type ReactNode } from "react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import type { ZCodeOffPeakTask, ZCodeOffPeakTaskStatus } from "@zcode/shared";
  6. import { OffPeakTaskList } from "@/settings/OffPeakTaskList.js";
  7. const tooltipCalls = vi.hoisted(
  8. () =>
  9. [] as Array<{
  10. className?: string;
  11. side?: string;
  12. sideOffset?: number;
  13. title: string;
  14. }>,
  15. );
  16. vi.mock("@/ControlHintTooltip.js", () => ({
  17. ControlHintTooltip: ({
  18. children,
  19. ...props
  20. }: {
  21. children: ReactNode;
  22. className?: string;
  23. side?: string;
  24. sideOffset?: number;
  25. title: string;
  26. }) => {
  27. tooltipCalls.push(props);
  28. return createElement("span", { "data-testid": `hint-${props.title}` }, children);
  29. },
  30. }));
  31. vi.mock("@/components/ui/dropdown-menu.js", () => ({
  32. DropdownMenu: ({ children }: { children: ReactNode }) => createElement("div", null, children),
  33. DropdownMenuContent: ({ children }: { children: ReactNode }) =>
  34. createElement("div", { role: "menu" }, children),
  35. DropdownMenuItem: ({
  36. children,
  37. onSelect,
  38. ...props
  39. }: {
  40. children: ReactNode;
  41. onSelect?: () => void;
  42. [key: string]: unknown;
  43. }) => createElement("div", { ...props, onClick: onSelect }, children),
  44. DropdownMenuSeparator: () => createElement("hr"),
  45. DropdownMenuTrigger: ({ children }: { children: ReactNode }) => children,
  46. }));
  47. vi.mock("@/i18n/IntlProvider.js", () => ({
  48. useZCodeIntl: () => ({
  49. intl: {
  50. formatMessage: ({ id }: { id: string }) => id,
  51. },
  52. }),
  53. }));
  54. function createTask(status: ZCodeOffPeakTaskStatus): ZCodeOffPeakTask {
  55. return {
  56. offPeakTaskId: `task-${status}`,
  57. title: `Task ${status}`,
  58. prompt: "Test prompt",
  59. permissionMode: "default",
  60. workspaceKey: "/workspace",
  61. workspacePath: "/workspace",
  62. status,
  63. queuedAt: 100,
  64. createdAt: 100,
  65. updatedAt: 100,
  66. };
  67. }
  68. function renderTask(
  69. status: "queued" | "paused",
  70. overrides: {
  71. onContinue?: ReturnType<typeof vi.fn>;
  72. onPause?: ReturnType<typeof vi.fn>;
  73. } = {},
  74. ) {
  75. const onPause = overrides.onPause ?? vi.fn();
  76. const onContinue = overrides.onContinue ?? vi.fn();
  77. render(
  78. createElement(OffPeakTaskList, {
  79. tasks: [createTask(status)],
  80. busyOperationId: null,
  81. onOpen: vi.fn(),
  82. onPause,
  83. onContinue,
  84. onCancel: vi.fn(),
  85. onDelete: vi.fn(),
  86. onOpenSession: vi.fn(),
  87. }),
  88. );
  89. return { onContinue, onPause };
  90. }
  91. afterEach(() => {
  92. cleanup();
  93. tooltipCalls.length = 0;
  94. });
  95. describe("OffPeakTaskList menu hints", () => {
  96. it.each([
  97. {
  98. action: "pause",
  99. hintId: "offPeak.action.pauseHint",
  100. status: "queued",
  101. },
  102. {
  103. action: "continue",
  104. hintId: "offPeak.action.continueHint",
  105. status: "paused",
  106. },
  107. ] as const)(
  108. "keeps the $action hint local and does not trigger the menu action",
  109. ({ action: _action, hintId, status }) => {
  110. const onPause = vi.fn();
  111. const onContinue = vi.fn();
  112. renderTask(status, { onContinue, onPause });
  113. const hint = screen.getByTestId(`hint-${hintId}`);
  114. const trigger = hint.firstElementChild;
  115. expect(trigger).not.toBeNull();
  116. fireEvent.click(trigger as Element);
  117. expect(onPause).not.toHaveBeenCalled();
  118. expect(onContinue).not.toHaveBeenCalled();
  119. expect(tooltipCalls).toContainEqual(
  120. expect.objectContaining({
  121. className: "max-w-[min(20rem,calc(100vw-1rem))]",
  122. side: "right",
  123. sideOffset: 16,
  124. title: hintId,
  125. }),
  126. );
  127. },
  128. );
  129. });