| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- import { act, createElement, type ReactNode } from "react";
- import { createRoot, type Root } from "react-dom/client";
- import { renderToStaticMarkup } from "react-dom/server";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import type { ZCodeTaskMeta } from "@zcode/shared";
- const {
- memoTaskItemCalls,
- taskListItemContextMenuContentCalls,
- taskRenameDialogCalls,
- stableIntl,
- stableStartDraft,
- stableOpenSettingsTab,
- } = vi.hoisted(() => ({
- memoTaskItemCalls: [] as Array<Record<string, unknown>>,
- taskListItemContextMenuContentCalls: [] as Array<Record<string, unknown>>,
- taskRenameDialogCalls: [] as Array<Record<string, unknown>>,
- stableIntl: {
- formatMessage: ({ id }: { id: string }) => id,
- },
- stableOpenSettingsTab: vi.fn(),
- stableStartDraft: vi.fn(),
- }));
- vi.mock("lucide-react", () => ({
- Settings2: (props: Record<string, unknown>) =>
- createElement("svg", { "data-icon": "settings", ...props }),
- }));
- vi.mock("@/components/ui/button.js", () => ({
- Button: ({ children, ...props }: { children?: ReactNode }) =>
- createElement("button", props, children),
- }));
- vi.mock("@/components/ui/context-menu.js", () => ({
- ContextMenu: ({ children }: { children?: ReactNode }) => createElement("div", null, children),
- ContextMenuTrigger: ({ children }: { children?: ReactNode }) => children,
- }));
- vi.mock("@/components/ui/toast.js", () => ({
- toast: () => {},
- }));
- vi.mock("@/i18n/IntlProvider.js", () => ({
- useZCodeIntl: () => ({ intl: stableIntl }),
- }));
- vi.mock("@/store/zcodeSessionStore.js", () => ({
- useZCodeSessionStore: (selector: (state: { startDraft: () => void }) => unknown) =>
- selector({ startDraft: stableStartDraft }),
- }));
- vi.mock("@/store/TabStoreProvider.js", () => ({
- useTabStore: (selector: (state: { openSettingsTab: () => void }) => unknown) =>
- selector({ openSettingsTab: stableOpenSettingsTab }),
- }));
- vi.mock("@/NewTaskButtonGroup.js", () => ({
- NewTaskButtonGroup: () => null,
- }));
- vi.mock("@/TaskRenameDialog.js", () => ({
- TaskRenameDialog: (props: Record<string, unknown>) => {
- taskRenameDialogCalls.push(props);
- return null;
- },
- }));
- vi.mock("@/TaskListLoadingHint.js", () => ({
- TaskListLoadingHint: () => null,
- }));
- vi.mock("@/TaskListItem.js", () => ({
- MemoTaskItem: (props: Record<string, unknown>) => {
- memoTaskItemCalls.push(props);
- return createElement("li", null, (props.task as ZCodeTaskMeta).title);
- },
- TaskListItemContextMenuContent: (props: Record<string, unknown>) => {
- taskListItemContextMenuContentCalls.push(props);
- return null;
- },
- }));
- function createTask(overrides: Partial<ZCodeTaskMeta> = {}): ZCodeTaskMeta {
- return {
- createdAt: 1,
- mode: "default",
- provider: "codex",
- taskId: "task-1",
- title: "Task 1",
- traceId: "trace-1",
- updatedAt: 2,
- workspacePath: "/workspace",
- ...overrides,
- };
- }
- function createMinimalElement(ownerDocument: Document, tagName = "div") {
- const element = {
- addEventListener: () => {},
- appendChild: (child: { parentNode?: unknown }) => {
- child.parentNode = element;
- return child;
- },
- childNodes: [] as unknown[],
- getAttribute: () => null,
- insertBefore: (child: { parentNode?: unknown }) => {
- child.parentNode = element;
- return child;
- },
- nodeName: tagName.toUpperCase(),
- nodeType: 1,
- ownerDocument,
- parentNode: null as unknown,
- removeAttribute: () => {},
- removeChild: (child: { parentNode?: unknown }) => {
- child.parentNode = null;
- return child;
- },
- removeEventListener: () => {},
- setAttribute: () => {},
- style: {},
- tagName: tagName.toUpperCase(),
- };
- return element as unknown as Element;
- }
- function installMinimalDom() {
- 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() {},
- 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("TaskList render stability", () => {
- afterEach(() => {
- memoTaskItemCalls.length = 0;
- taskListItemContextMenuContentCalls.length = 0;
- taskRenameDialogCalls.length = 0;
- stableOpenSettingsTab.mockReset();
- stableStartDraft.mockReset();
- 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("does not mount the rename dialog while no row is being renamed", async () => {
- const { TaskList } = await import("@/TaskList.js");
- const root: Root = createRoot(installMinimalDom());
- act(() => {
- root.render(
- createElement(TaskList, {
- activeTaskId: null,
- isWorkspaceActive: true,
- onArchiveTask: vi.fn(async () => null),
- onRenameTask: vi.fn(async () => null),
- onSelectTask: vi.fn(),
- onSetTaskPinned: vi.fn(async () => null),
- onSetTaskUnread: vi.fn(async () => null),
- showCreateButton: false,
- showFooter: false,
- tasks: [createTask()],
- workspacePath: "/workspace",
- }),
- );
- });
- expect(taskRenameDialogCalls).toHaveLength(0);
- expect(taskListItemContextMenuContentCalls).toHaveLength(0);
- act(() => {
- root.unmount();
- });
- });
- it("shows only the one-way show-more entry while another page exists", async () => {
- const { TaskList } = await import("@/TaskList.js");
- const html = renderToStaticMarkup(
- createElement(TaskList, {
- activeTaskId: null,
- hasMore: true,
- onArchiveTask: vi.fn(async () => null),
- onRenameTask: vi.fn(async () => null),
- onSelectTask: vi.fn(),
- onSetTaskPinned: vi.fn(async () => null),
- onSetTaskUnread: vi.fn(async () => null),
- onShowMore: vi.fn(),
- showCreateButton: false,
- showFooter: false,
- tasks: [createTask()],
- workspacePath: "/workspace",
- }),
- );
- expect(html).toContain("taskList.showMore");
- expect(html).not.toContain("taskList.showLess");
- });
- it("hides the show-more entry after the final page", async () => {
- const { TaskList } = await import("@/TaskList.js");
- const html = renderToStaticMarkup(
- createElement(TaskList, {
- activeTaskId: null,
- hasMore: false,
- onArchiveTask: vi.fn(async () => null),
- onRenameTask: vi.fn(async () => null),
- onSelectTask: vi.fn(),
- onSetTaskPinned: vi.fn(async () => null),
- onSetTaskUnread: vi.fn(async () => null),
- onShowMore: vi.fn(),
- showCreateButton: false,
- showFooter: false,
- tasks: [createTask()],
- workspacePath: "/workspace",
- }),
- );
- expect(html).not.toContain("taskList.showMore");
- });
- it("does not re-render unchanged memo task rows when parent rerenders with identical list props", async () => {
- const { TaskList } = await import("@/TaskList.js");
- const root: Root = createRoot(installMinimalDom());
- const tasks = [createTask()];
- const onSelectTask = vi.fn();
- const onRenameTask = vi.fn(async () => null);
- const onSetTaskPinned = vi.fn(async () => null);
- const onArchiveTask = vi.fn(async () => null);
- const onSetTaskUnread = vi.fn(async () => null);
- function Parent({ tick }: { tick: number }) {
- return createElement(
- "section",
- null,
- createElement("span", null, tick),
- createElement(TaskList, {
- activeTaskId: null,
- isWorkspaceActive: true,
- onArchiveTask,
- onRenameTask,
- onSelectTask,
- onSetTaskPinned,
- onSetTaskUnread,
- showCreateButton: false,
- showFooter: false,
- tasks,
- workspacePath: "/workspace",
- }),
- );
- }
- act(() => {
- root.render(createElement(Parent, { tick: 1 }));
- });
- act(() => {
- root.render(createElement(Parent, { tick: 2 }));
- });
- expect(memoTaskItemCalls).toHaveLength(1);
- act(() => {
- root.unmount();
- });
- });
- it("keeps unchanged row action props stable when archive confirmation state changes", async () => {
- const { TaskList } = await import("@/TaskList.js");
- const root: Root = createRoot(installMinimalDom());
- const tasks = [
- createTask(),
- createTask({
- taskId: "task-2",
- title: "Task 2",
- traceId: "trace-2",
- updatedAt: 3,
- }),
- ];
- const onSelectTask = vi.fn();
- const onRenameTask = vi.fn(async () => null);
- const onSetTaskPinned = vi.fn(async () => null);
- const onArchiveTask = vi.fn(async () => null);
- const onSetTaskUnread = vi.fn(async () => null);
- act(() => {
- root.render(
- createElement(TaskList, {
- activeTaskId: null,
- isWorkspaceActive: true,
- onArchiveTask,
- onRenameTask,
- onSelectTask,
- onSetTaskPinned,
- onSetTaskUnread,
- showCreateButton: false,
- showFooter: false,
- tasks,
- workspacePath: "/workspace",
- }),
- );
- });
- const untouchedRowInitialProps = memoTaskItemCalls.find(
- (props) => (props.task as ZCodeTaskMeta).taskId === "task-2",
- );
- expect(untouchedRowInitialProps).toBeDefined();
- const archiveInline = memoTaskItemCalls[0]?.onArchiveTaskInline;
- expect(archiveInline).toBeDefined();
- await act(async () => {
- await archiveInline?.({ stopPropagation: vi.fn() }, "task-1");
- });
- const untouchedRowUpdatedProps = memoTaskItemCalls
- .slice(2)
- .find((props) => (props.task as ZCodeTaskMeta).taskId === "task-2");
- expect(untouchedRowUpdatedProps).toBeDefined();
- expect(untouchedRowUpdatedProps?.onSelectTask).toBe(untouchedRowInitialProps?.onSelectTask);
- expect(untouchedRowUpdatedProps?.onArchiveTaskInline).toBe(
- untouchedRowInitialProps?.onArchiveTaskInline,
- );
- expect(untouchedRowUpdatedProps?.onTogglePinTask).toBe(
- untouchedRowInitialProps?.onTogglePinTask,
- );
- expect(untouchedRowUpdatedProps?.onStartRenameTask).toBe(
- untouchedRowInitialProps?.onStartRenameTask,
- );
- expect(untouchedRowUpdatedProps?.onArchiveTask).toBe(untouchedRowInitialProps?.onArchiveTask);
- expect(untouchedRowUpdatedProps?.onMarkTaskAsUnread).toBe(
- untouchedRowInitialProps?.onMarkTaskAsUnread,
- );
- act(() => {
- root.unmount();
- });
- });
- });
|