| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631 |
- import { act, createElement, type ReactElement, type ReactNode } from "react";
- import { createRoot, type Root } from "react-dom/client";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import type { ZCodeTaskMeta } from "@zcode/shared";
- type TestEvent = {
- bubbles?: boolean;
- currentTarget?: TestElement;
- relatedTarget?: unknown;
- target?: TestElement;
- type: string;
- };
- type TestEventHandler = (event: TestEvent) => void;
- type TestElement = Element & {
- __attrs: Map<string, string>;
- __listeners: Map<string, TestEventHandler[]>;
- childNodes: TestElement[];
- dispatchEvent: (event: TestEvent) => boolean;
- parentNode: TestElement | null;
- };
- const {
- contextMenuMounts,
- contextMenuOpenChangeHandlers,
- contextActionsCalls,
- leadingIndicatorState,
- formatRelativeTimeCalls,
- tooltipCalls,
- triggerChildren,
- stableIntl,
- } = vi.hoisted(() => ({
- contextMenuMounts: [] as Array<Record<string, unknown>>,
- contextMenuOpenChangeHandlers: [] as Array<(open: boolean) => void>,
- contextActionsCalls: [] as Array<Record<string, unknown>>,
- leadingIndicatorState: { value: "none" },
- formatRelativeTimeCalls: [] as number[],
- tooltipCalls: [] as Array<Record<string, unknown>>,
- triggerChildren: [] as ReactElement[],
- stableIntl: {
- formatMessage: ({ id }: { id: string }) => id,
- },
- }));
- vi.mock("lucide-react", () => {
- const createIcon = (name: string) => (props: Record<string, unknown>) =>
- createElement("svg", { "data-icon": name, ...props });
- return {
- Archive: createIcon("archive"),
- CloudUpload: createIcon("cloud-upload"),
- Clock: createIcon("clock"),
- LoaderIcon: createIcon("loader"),
- Pin: createIcon("pin"),
- Smartphone: createIcon("smartphone"),
- };
- });
- vi.mock("@/components/ui/badge.js", () => ({
- Badge: ({ children }: { children?: ReactNode }) =>
- createElement("span", null, children),
- }));
- vi.mock("@/components/ui/button.js", () => ({
- Button: ({ children, ...props }: { children?: ReactNode }) =>
- createElement("button", props, children),
- }));
- vi.mock("@/components/ui/context-menu.js", () => ({
- ContextMenu: ({
- children,
- onOpenChange,
- }: {
- children?: ReactNode;
- onOpenChange?: (open: boolean) => void;
- }) => {
- if (onOpenChange) {
- contextMenuOpenChangeHandlers.push(onOpenChange);
- }
- return createElement("div", null, children);
- },
- ContextMenuTrigger: ({ children }: { children: ReactElement }) => {
- triggerChildren.push(children);
- return children;
- },
- }));
- vi.mock("@/lib/taskListItemPresentation.js", () => ({
- deriveTaskLeadingIndicator: () => leadingIndicatorState.value,
- formatTaskRelativeTime: (updatedAt: number) => {
- formatRelativeTimeCalls.push(updatedAt);
- return "now";
- },
- }));
- vi.mock("@/ControlHintTooltip.js", () => ({
- ControlHintTooltip: ({ children, ...props }: { children?: ReactNode }) => {
- tooltipCalls.push(props);
- return createElement("span", null, children);
- },
- }));
- vi.mock("@/TaskListItemContextMenu.js", () => ({
- TaskListItemContextMenu: (props: Record<string, unknown>) => {
- contextMenuMounts.push(props);
- return createElement("div", { "data-testid": "task-context-menu" });
- },
- }));
- vi.mock("@/useTaskListItemContextActions.js", () => ({
- useTaskListItemContextActions: (params: Record<string, unknown>) => {
- contextActionsCalls.push(params);
- return {
- fileManagerLabel: "Finder",
- handleCopyText: vi.fn(),
- handleOpenProviderConfig: vi.fn(),
- handleOpenTaskPathInFileManager: vi.fn(),
- providerConfigFile: { exists: false, loading: false, path: null },
- taskNativeSessionLogFile: { exists: false, loading: false, path: null },
- taskSessionFile: { exists: false, loading: false, path: null },
- };
- },
- }));
- vi.mock("@/hooks/usePlatform.js", () => ({
- useOptionalPlatform: () => null,
- }));
- vi.mock("@/feedback/feedbackStore.js", () => ({
- useFeedbackStore: (
- selector: (state: { openSubmit: () => void }) => unknown,
- ) => selector({ openSubmit: vi.fn() }),
- }));
- vi.mock("@/store/modelTrajectoryStore.js", () => ({
- useModelTrajectoryStore: {
- getState: () => ({
- requestOpen: vi.fn(),
- }),
- },
- }));
- vi.mock("@/components/ui/toast.js", () => ({
- toast: () => {},
- }));
- vi.mock("@/store/zcodeSessionStore.js", async () => {
- const actual = await vi.importActual<
- typeof import("@/store/zcodeSessionStore.js")
- >("@/store/zcodeSessionStore.js");
- return {
- ...actual,
- getTaskRuntimeState: () => "idle",
- getTaskUnreadIndicator: () => false,
- hasTaskPendingHumanInterventionRequest: () => false,
- hasTaskPendingPermissionRequest: () => false,
- selectWorkspaceZCodeState: () => ({}),
- useZCodeSessionStore: (
- selector: (state: Record<string, unknown>) => unknown,
- ) => selector({}),
- };
- });
- function createTask(): ZCodeTaskMeta {
- return {
- createdAt: 1,
- mode: "default",
- provider: "codex",
- taskId: "task-1",
- title: "Task 1",
- traceId: "trace-1",
- updatedAt: 2,
- workspacePath: "/workspace",
- };
- }
- function createMinimalElement(ownerDocument: Document, tagName = "div") {
- const element = {
- __attrs: new Map<string, string>(),
- __listeners: new Map<string, TestEventHandler[]>(),
- addEventListener: (type: string, handler: TestEventHandler) => {
- const handlers = element.__listeners.get(type) ?? [];
- handlers.push(handler);
- element.__listeners.set(type, handlers);
- },
- appendChild: (child: TestElement) => {
- child.parentNode = element as unknown as TestElement;
- element.childNodes.push(child);
- return child;
- },
- childNodes: [] as TestElement[],
- dispatchEvent: (event: TestEvent) => {
- event.target ??= element as unknown as TestElement;
- let current: TestElement | null = element as unknown as TestElement;
- while (current) {
- event.currentTarget = current;
- for (const handler of current.__listeners.get(event.type) ?? []) {
- handler(event);
- }
- if (!event.bubbles) {
- break;
- }
- current = current.parentNode;
- }
- return true;
- },
- getAttribute: (name: string) => element.__attrs.get(name) ?? null,
- insertBefore: (child: TestElement, beforeChild?: TestElement | null) => {
- child.parentNode = element as unknown as TestElement;
- const beforeIndex = beforeChild
- ? element.childNodes.indexOf(beforeChild)
- : -1;
- if (beforeIndex >= 0) {
- element.childNodes.splice(beforeIndex, 0, child);
- } else {
- element.childNodes.push(child);
- }
- return child;
- },
- nodeName: tagName.toUpperCase(),
- nodeType: 1,
- ownerDocument,
- parentNode: null as TestElement | null,
- querySelector: (selector: string) => {
- const attributeMatch = /^\[([^=\]]+)(?:=[^\]]+)?\]$/u.exec(selector);
- if (!attributeMatch) {
- return null;
- }
- return findByAttribute(
- element as unknown as TestElement,
- attributeMatch[1],
- );
- },
- removeAttribute: (name: string) => {
- element.__attrs.delete(name);
- },
- removeChild: (child: TestElement) => {
- element.childNodes = element.childNodes.filter((item) => item !== child);
- child.parentNode = null;
- return child;
- },
- removeEventListener: (type: string, handler: TestEventHandler) => {
- element.__listeners.set(
- type,
- (element.__listeners.get(type) ?? []).filter((item) => item !== handler),
- );
- },
- setAttribute: (name: string, value: string) => {
- element.__attrs.set(name, String(value));
- },
- style: {},
- tagName: tagName.toUpperCase(),
- };
- return element as unknown as TestElement;
- }
- function findByAttribute(node: TestElement, attributeName: string) {
- if (node.__attrs?.has(attributeName)) {
- return node;
- }
- for (const child of node.childNodes ?? []) {
- const match = findByAttribute(child, attributeName);
- if (match) {
- return match;
- }
- }
- return null;
- }
- function findByAttributeValue(
- node: TestElement,
- attributeName: string,
- attributeValue: string,
- ) {
- if (node.__attrs?.get(attributeName) === attributeValue) {
- return node;
- }
- for (const child of node.childNodes ?? []) {
- const match = findByAttributeValue(child, attributeName, attributeValue);
- if (match) {
- return match;
- }
- }
- return null;
- }
- function installMinimalDom(hoverNone = false) {
- const documentMock = {
- addEventListener: () => {},
- createElement: (tagName: string) =>
- createMinimalElement(documentMock as unknown as Document, tagName),
- createElementNS: (_namespace: string, tagName: string) =>
- createMinimalElement(documentMock as unknown as Document, tagName),
- createTextNode: (nodeValue: string) => ({
- nodeType: 3,
- nodeValue,
- ownerDocument: documentMock,
- parentNode: null,
- }),
- nodeType: 9,
- removeEventListener: () => {},
- } as unknown as Document;
- const windowMock = {
- addEventListener: () => {},
- document: documentMock,
- HTMLIFrameElement: function HTMLIFrameElement() {},
- HTMLElement: function HTMLElement() {},
- matchMedia: () => ({ matches: hoverNone }),
- Node: function Node() {},
- removeEventListener: () => {},
- };
- Object.defineProperty(globalThis, "document", {
- configurable: true,
- value: documentMock,
- });
- Object.defineProperty(globalThis, "window", {
- configurable: true,
- value: windowMock,
- });
- Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
- configurable: true,
- value: true,
- });
- return createMinimalElement(documentMock);
- }
- describe("TaskListItem hover action rendering", () => {
- afterEach(() => {
- contextMenuMounts.length = 0;
- contextMenuOpenChangeHandlers.length = 0;
- contextActionsCalls.length = 0;
- leadingIndicatorState.value = "none";
- formatRelativeTimeCalls.length = 0;
- tooltipCalls.length = 0;
- triggerChildren.length = 0;
- delete (globalThis as { document?: unknown }).document;
- delete (globalThis as { window?: unknown }).window;
- delete (globalThis as { IS_REACT_ACT_ENVIRONMENT?: unknown })
- .IS_REACT_ACT_ENVIRONMENT;
- });
- it("keeps idle rows free of per-row context menu and hover-only archive tooltip", async () => {
- const { MemoTaskItem } = await import("@/TaskListItem.js");
- const root: Root = createRoot(installMinimalDom());
- act(() => {
- root.render(
- createElement(MemoTaskItem, {
- intl: stableIntl,
- isActive: false,
- isArchiveConfirming: false,
- isPinned: false,
- onArchiveTask: vi.fn(),
- onArchiveTaskInline: vi.fn(),
- onCancelArchiveConfirm: vi.fn(),
- onMarkTaskAsUnread: vi.fn(),
- onSelectTask: vi.fn(),
- onStartRenameTask: vi.fn(),
- onTogglePinTask: vi.fn(),
- task: createTask(),
- workspacePath: "/workspace",
- }),
- );
- });
- expect(contextMenuOpenChangeHandlers).toHaveLength(0);
- expect(contextMenuMounts).toHaveLength(0);
- expect(contextActionsCalls).toHaveLength(0);
- expect(tooltipCalls).toHaveLength(0);
- act(() => {
- root.unmount();
- });
- });
- it("触屏常驻 actions 时仍保留 task 元信息", async () => {
- const { MemoTaskItem } = await import("@/TaskListItem.js");
- const container = installMinimalDom(true);
- const root: Root = createRoot(container);
- leadingIndicatorState.value = "unread";
- act(() => {
- root.render(
- createElement(MemoTaskItem, {
- intl: stableIntl,
- isActive: false,
- isArchiveConfirming: false,
- isPinned: false,
- onArchiveTask: vi.fn(),
- onArchiveTaskInline: vi.fn(),
- onCancelArchiveConfirm: vi.fn(),
- onMarkTaskAsUnread: vi.fn(),
- onSelectTask: vi.fn(),
- onStartRenameTask: vi.fn(),
- onTogglePinTask: vi.fn(),
- task: createTask(),
- workspacePath: "/workspace",
- }),
- );
- });
- expect(findByAttribute(container, "data-task-row-actions")).not.toBeNull();
- const metadata = findByAttribute(container, "data-task-row-metadata");
- expect(metadata).not.toBeNull();
- expect(metadata?.__attrs.get("class")).not.toContain("hidden");
- const unreadIndicator = findByAttribute(container, "data-unread-indicator");
- expect(unreadIndicator).not.toBeNull();
- expect(unreadIndicator?.parentNode?.__attrs.get("class")).not.toContain(
- "hidden",
- );
- act(() => {
- root.unmount();
- });
- });
- it("keeps default rows in the compact layout without idle dots", async () => {
- const { MemoTaskItem } = await import("@/TaskListItem.js");
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- act(() => {
- root.render(
- createElement(MemoTaskItem, {
- intl: stableIntl,
- isActive: false,
- isArchiveConfirming: false,
- isPinned: false,
- onArchiveTask: vi.fn(),
- onArchiveTaskInline: vi.fn(),
- onCancelArchiveConfirm: vi.fn(),
- onMarkTaskAsUnread: vi.fn(),
- onSelectTask: vi.fn(),
- onStartRenameTask: vi.fn(),
- onTogglePinTask: vi.fn(),
- task: {
- ...createTask(),
- workspacePath: "/Users/demo/.zcode/workspace/default",
- },
- variant: "default",
- workspacePath: "/Users/demo/.zcode/workspace/default",
- }),
- );
- });
- const row = container.querySelector("[data-task-item-key]");
- expect(row?.getAttribute("class")).toContain("items-center");
- expect(row?.getAttribute("class")).not.toContain("items-start");
- expect(container.querySelector("[data-idle-indicator]")).toBeNull();
- act(() => {
- root.unmount();
- });
- });
- it("does not re-render when task meta is replaced with equivalent display fields", async () => {
- const { MemoTaskItem } = await import("@/TaskListItem.js");
- const root: Root = createRoot(installMinimalDom());
- const task = createTask();
- const props = {
- intl: stableIntl,
- isActive: false,
- isArchiveConfirming: false,
- isPinned: false,
- onArchiveTask: vi.fn(),
- onArchiveTaskInline: vi.fn(),
- onCancelArchiveConfirm: vi.fn(),
- onMarkTaskAsUnread: vi.fn(),
- onSelectTask: vi.fn(),
- onStartRenameTask: vi.fn(),
- onTogglePinTask: vi.fn(),
- task,
- workspacePath: "/workspace",
- };
- act(() => {
- root.render(createElement(MemoTaskItem, props));
- });
- expect(formatRelativeTimeCalls).toHaveLength(1);
- act(() => {
- root.render(
- createElement(MemoTaskItem, {
- ...props,
- task: { ...task },
- }),
- );
- });
- expect(formatRelativeTimeCalls).toHaveLength(1);
- act(() => {
- root.unmount();
- });
- });
- it("re-renders when a task receives automation identity", async () => {
- const { MemoTaskItem } = await import("@/TaskListItem.js");
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- const task = createTask();
- const props = {
- intl: stableIntl,
- isActive: false,
- isArchiveConfirming: false,
- isPinned: false,
- onArchiveTask: vi.fn(),
- onArchiveTaskInline: vi.fn(),
- onCancelArchiveConfirm: vi.fn(),
- onMarkTaskAsUnread: vi.fn(),
- onSelectTask: vi.fn(),
- onStartRenameTask: vi.fn(),
- onTogglePinTask: vi.fn(),
- task,
- workspacePath: "/workspace",
- };
- act(() => {
- root.render(createElement(MemoTaskItem, props));
- });
- expect(container.querySelector("[data-cron-task-icon]")).toBeNull();
- act(() => {
- root.render(
- createElement(MemoTaskItem, {
- ...props,
- task: {
- ...task,
- automationId: "automation-1",
- } as ZCodeTaskMeta & { automationId: string },
- }),
- );
- });
- expect(container.querySelector("[data-cron-task-icon]")).not.toBeNull();
- act(() => {
- root.unmount();
- });
- });
- it("renders cron icon in the timeline time metadata", async () => {
- const { MemoTaskItem } = await import("@/TaskListItem.js");
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- act(() => {
- root.render(
- createElement(MemoTaskItem, {
- intl: stableIntl,
- isActive: false,
- isArchiveConfirming: false,
- isPinned: false,
- onArchiveTask: vi.fn(),
- onArchiveTaskInline: vi.fn(),
- onCancelArchiveConfirm: vi.fn(),
- onMarkTaskAsUnread: vi.fn(),
- onSelectTask: vi.fn(),
- onStartRenameTask: vi.fn(),
- onTogglePinTask: vi.fn(),
- task: {
- ...createTask(),
- cronAutomationId: "automation-1",
- },
- variant: "timeline",
- workspacePath: "/workspace",
- }),
- );
- });
- expect(container.querySelector("[data-cron-task-icon]")).not.toBeNull();
- act(() => {
- root.unmount();
- });
- });
- it("keeps archive confirmation active after the pointer leaves the row", async () => {
- const { MemoTaskItem } = await import("@/TaskListItem.js");
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- const onCancelArchiveConfirm = vi.fn();
- act(() => {
- root.render(
- createElement(MemoTaskItem, {
- intl: stableIntl,
- isActive: false,
- isArchiveConfirming: true,
- isPinned: false,
- onArchiveTask: vi.fn(),
- onArchiveTaskInline: vi.fn(),
- onCancelArchiveConfirm,
- onMarkTaskAsUnread: vi.fn(),
- onSelectTask: vi.fn(),
- onStartRenameTask: vi.fn(),
- onTogglePinTask: vi.fn(),
- task: createTask(),
- workspacePath: "/workspace",
- }),
- );
- });
- const row = container.querySelector("[data-task-item-key]") as
- | TestElement
- | null;
- expect(row).not.toBeNull();
- act(() => {
- row?.dispatchEvent({
- bubbles: true,
- relatedTarget: null,
- type: "mouseout",
- });
- });
- expect(onCancelArchiveConfirm).not.toHaveBeenCalled();
- const confirmButton = findByAttributeValue(
- container,
- "aria-label",
- "common.confirm",
- );
- expect(confirmButton?.__attrs.get("class")).toContain("flex");
- expect(confirmButton?.__attrs.get("class")).not.toContain("hidden");
- act(() => {
- root.unmount();
- });
- });
- });
|