| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- import { createElement, type ComponentType, type ReactNode } from "react";
- import { renderToStaticMarkup } from "react-dom/server";
- import { afterEach, describe, expect, it, vi } from "vitest";
- vi.mock("@/components/ui/button.js", () => ({
- Button: ({
- children,
- ...props
- }: {
- children: ReactNode;
- [key: string]: unknown;
- }) => createElement("button", props, children),
- }));
- vi.mock("@/components/ui/dropdown-menu.js", () => ({
- DropdownMenu: ({ children }: { children: ReactNode }) => createElement("div", null, children),
- DropdownMenuTrigger: ({ children }: { children: ReactNode }) =>
- createElement("div", null, children),
- DropdownMenuContent: ({ children }: { children: ReactNode }) =>
- createElement("div", null, children),
- DropdownMenuSub: ({ children }: { children: ReactNode }) => createElement("div", null, children),
- DropdownMenuSubTrigger: ({
- children,
- ...props
- }: {
- children: ReactNode;
- [key: string]: unknown;
- }) => createElement("div", props, children),
- DropdownMenuSubContent: ({ children }: { children: ReactNode }) =>
- createElement("div", null, children),
- DropdownMenuItem: ({
- children,
- ...props
- }: {
- children: ReactNode;
- [key: string]: unknown;
- }) => createElement("div", props, children),
- DropdownMenuCheckboxItem: ({
- children,
- ...props
- }: {
- children: ReactNode;
- [key: string]: unknown;
- }) => createElement("div", props, children),
- DropdownMenuSeparator: () => createElement("hr"),
- }));
- vi.mock("@/components/ui/dialog.js", () => ({
- Dialog: ({ children }: { children: ReactNode }) => createElement("div", null, children),
- DialogContent: ({ children }: { children: ReactNode }) => createElement("div", null, children),
- DialogFooter: ({ children }: { children: ReactNode }) => createElement("div", null, children),
- DialogHeader: ({ children }: { children: ReactNode }) => createElement("div", null, children),
- DialogTitle: ({ children }: { children: ReactNode }) => createElement("h2", null, children),
- }));
- vi.mock("@/components/ui/input.js", () => ({
- Input: (props: Record<string, unknown>) => createElement("input", props),
- }));
- vi.mock("@/i18n/IntlProvider.js", () => ({
- useZCodeIntl: () => ({
- intl: {
- formatMessage: ({ id }: { id: string }) => id,
- },
- }),
- }));
- vi.mock("@/logger.js", () => ({
- logger: {
- info: vi.fn(),
- warn: vi.fn(),
- error: vi.fn(),
- },
- }));
- vi.mock("@/SSHDialog.js", () => ({
- SSHDialog: () => createElement("div", { "data-testid": "ssh-dialog" }),
- }));
- async function renderMenu(
- showRemoteConnectionEntry: boolean,
- allowRemoteWorkspace = true,
- ) {
- vi.resetModules();
- vi.doMock("@/hooks/useRemoteConnectionEntryVisibility.js", () => ({
- useRemoteConnectionEntryVisibility: () => showRemoteConnectionEntry,
- }));
- vi.doMock("@zcode/shared", async () => {
- const actual = await vi.importActual<typeof import("@zcode/shared")>(
- "@zcode/shared"
- );
- return {
- ...actual,
- };
- });
- const { ChatEmptyWorkspacePreviewMenu } = await import("../src/ChatEmptyState.js");
- const Menu = ChatEmptyWorkspacePreviewMenu as unknown as ComponentType<
- Record<string, unknown>
- >;
- return renderToStaticMarkup(
- createElement(Menu, {
- workspacePath: "/Users/tester/project",
- workspaceTabs: [
- {
- workspacePath: "/Users/tester/project",
- label: "project",
- },
- ],
- onSelectWorkspace: vi.fn(),
- onSelectConversationWorkspace: vi.fn(),
- onOpenFolder: vi.fn(),
- allowRemoteWorkspace,
- onConnectRemote: async () => "session-id",
- onSelectRemoteProject: async () => {},
- onCancelRemoteProject: async () => {},
- }),
- );
- }
- afterEach(() => {
- vi.resetModules();
- });
- describe("ChatEmptyWorkspacePreviewMenu", () => {
- it("同路径远程项目按 workspaceIdentity 判定唯一选中项", async () => {
- const { isWorkspaceMenuTabSelected } = await import("../src/ChatEmptyState.js");
- const remoteA = {
- workspacePath: "/repo",
- workspaceIdentity: "remote:ssh:a:/repo",
- label: "remote-a",
- };
- const remoteB = {
- workspacePath: "/repo",
- workspaceIdentity: "remote:ssh:b:/repo",
- label: "remote-b",
- };
- expect(
- isWorkspaceMenuTabSelected(remoteA, {
- workspacePath: "/repo",
- workspaceIdentity: remoteB.workspaceIdentity,
- }),
- ).toBe(false);
- expect(
- isWorkspaceMenuTabSelected(remoteB, {
- workspacePath: "/repo",
- workspaceIdentity: remoteB.workspaceIdentity,
- }),
- ).toBe(true);
- });
- it("工作区搜索最多保留前 5 条且保持原顺序", async () => {
- const {
- filterVisibleWorkspaceMenuTabs,
- } = await import("../src/ChatEmptyState.js");
- const tabs = [
- "/workspace/alpha",
- "/workspace/beta",
- "/workspace/gamma",
- "/workspace/delta",
- "/workspace/epsilon",
- "/workspace/zeta",
- ].map((workspacePath) => ({
- workspacePath,
- label: workspacePath.split("/").pop()!,
- }));
- expect(
- filterVisibleWorkspaceMenuTabs({
- workspaceTabs: tabs,
- homeWorkspaceLabel: "Home",
- searchQuery: "",
- }).map((tab) => tab.label),
- ).toEqual(["alpha", "beta", "gamma", "delta", "epsilon"]);
- expect(
- filterVisibleWorkspaceMenuTabs({
- workspaceTabs: tabs,
- homeWorkspaceLabel: "Home",
- searchQuery: "workspace",
- }).map((tab) => tab.label),
- ).toEqual(["alpha", "beta", "gamma", "delta", "epsilon"]);
- });
- it("工作区搜索匹配名称、路径和远程目标信息", async () => {
- const {
- filterVisibleWorkspaceMenuTabs,
- } = await import("../src/ChatEmptyState.js");
- const tabs = [
- {
- workspacePath: "/repo/alpha",
- label: "Alpha",
- },
- {
- workspacePath: "/remote/project",
- label: "Remote Project",
- remoteSessionId: "remote-1",
- remoteTarget: {
- kind: "ssh",
- host: "dev.example.com",
- port: 2222,
- username: "alice",
- },
- },
- {
- workspacePath: "/repo/by-path",
- label: "Path Match",
- },
- ] as const;
- expect(
- filterVisibleWorkspaceMenuTabs({
- workspaceTabs: tabs,
- homeWorkspaceLabel: "Home",
- searchQuery: "alpha",
- }).map((tab) => tab.label),
- ).toEqual(["Alpha"]);
- expect(
- filterVisibleWorkspaceMenuTabs({
- workspaceTabs: tabs,
- homeWorkspaceLabel: "Home",
- searchQuery: "by-path",
- }).map((tab) => tab.label),
- ).toEqual(["Path Match"]);
- expect(
- filterVisibleWorkspaceMenuTabs({
- workspaceTabs: tabs,
- homeWorkspaceLabel: "Home",
- searchQuery: "dev.example.com",
- }).map((tab) => tab.label),
- ).toEqual(["Remote Project"]);
- });
- it("工作区搜索没有匹配项时保留新增工作区入口", async () => {
- const {
- filterVisibleWorkspaceMenuTabs,
- ChatEmptyWorkspacePreviewMenu,
- } = await import("../src/ChatEmptyState.js");
- const tabs = [
- {
- workspacePath: "/repo/alpha",
- label: "Alpha",
- },
- ];
- expect(
- filterVisibleWorkspaceMenuTabs({
- workspaceTabs: tabs,
- homeWorkspaceLabel: "Home",
- searchQuery: "missing",
- }),
- ).toEqual([]);
- const Menu = ChatEmptyWorkspacePreviewMenu as unknown as ComponentType<
- Record<string, unknown>
- >;
- const html = renderToStaticMarkup(
- createElement(Menu, {
- workspacePath: "/Users/tester/project",
- workspaceTabs: [],
- onSelectWorkspace: vi.fn(),
- onSelectConversationWorkspace: vi.fn(),
- onOpenFolder: vi.fn(),
- allowRemoteWorkspace: false,
- onConnectRemote: async () => "session-id",
- onSelectRemoteProject: async () => {},
- onCancelRemoteProject: async () => {},
- }),
- );
- expect(html).toContain("chat.empty.workspaceSearchEmpty");
- expect(html).toContain("workspace.openFolder");
- });
- it("隐藏 Home 快捷项并展示 Open folder 入口", async () => {
- const html = await renderMenu(false);
- expect(html).not.toContain("chat.empty.home");
- expect(html).not.toContain("workspace.openWorkspace");
- expect(html).toContain("workspace.openFolder");
- });
- it("Start from scratch 默认路径为空名称后缀,非法名称有校验", async () => {
- const {
- getScratchWorkspaceLocationHint,
- getScratchWorkspaceNameErrorKind,
- } = await import("../src/ChatEmptyState.js");
- expect(getScratchWorkspaceLocationHint("")).toBe("~/ZCodeProject/");
- expect(getScratchWorkspaceLocationHint("demo")).toBe("~/ZCodeProject/demo");
- expect(getScratchWorkspaceNameErrorKind("")).toBe("required");
- expect(getScratchWorkspaceNameErrorKind("bad/name")).toBe("separator");
- expect(getScratchWorkspaceNameErrorKind("bad\\name")).toBe("separator");
- expect(getScratchWorkspaceNameErrorKind("demo")).toBeNull();
- });
- it("在允许远程入口时展示远程连接项", async () => {
- const html = await renderMenu(true);
- expect(html).toContain("remote.trigger");
- });
- it("workspace 菜单提供不在项目中工作入口", async () => {
- const html = await renderMenu(false);
- expect(html).toContain("chat.empty.workOutsideProject");
- });
- it("对话工作区显示选择项目且不展示关闭按钮", async () => {
- const { ChatEmptyWorkspacePreviewMenu } =
- await import("../src/ChatEmptyState.js");
- const Menu = ChatEmptyWorkspacePreviewMenu as unknown as ComponentType<
- Record<string, unknown>
- >;
- const html = renderToStaticMarkup(
- createElement(Menu, {
- workspacePath: "/Users/tester/.zcode/workspace/default",
- workspaceTabs: [
- {
- workspacePath: "/Users/tester/.zcode/workspace/default",
- label: "default",
- workspacePurpose: "conversation",
- },
- ],
- onSelectWorkspace: vi.fn(),
- onSelectConversationWorkspace: vi.fn(),
- onOpenFolder: vi.fn(),
- allowRemoteWorkspace: false,
- onConnectRemote: async () => "session-id",
- onSelectRemoteProject: async () => {},
- onCancelRemoteProject: async () => {},
- }),
- );
- expect(html).toContain("chat.empty.selectProject");
- expect(html).not.toContain("chat.empty.detachProject");
- });
- it("可单独保留不在项目中工作菜单项并关闭项目 chip 的脱离按钮", async () => {
- const { ChatEmptyWorkspacePreviewMenu } =
- await import("../src/ChatEmptyState.js");
- const Menu = ChatEmptyWorkspacePreviewMenu as unknown as ComponentType<
- Record<string, unknown>
- >;
- const html = renderToStaticMarkup(
- createElement(Menu, {
- workspacePath: "/Users/tester/project",
- workspaceTabs: [
- {
- workspacePath: "/Users/tester/.zcode/workspace/default",
- label: "default",
- workspacePurpose: "conversation",
- },
- {
- workspacePath: "/Users/tester/project",
- label: "project",
- },
- ],
- allowConversationWorkspaceSelection: true,
- allowConversationWorkspaceDetach: false,
- onSelectWorkspace: vi.fn(),
- onSelectConversationWorkspace: vi.fn(),
- onOpenFolder: vi.fn(),
- allowOpenWorkspace: false,
- allowRemoteWorkspace: false,
- onConnectRemote: async () => "session-id",
- onSelectRemoteProject: async () => {},
- onCancelRemoteProject: async () => {},
- }),
- );
- expect(html).toContain("chat.empty.workOutsideProject");
- expect(html).toContain("lucide-message-circle");
- expect(html).not.toContain(">default<");
- expect(html).not.toContain("chat.empty.detachProject");
- expect(html).not.toContain("group-hover/workspace-chip:opacity-0");
- });
- it("禁用非项目工作区能力时同时隐藏脱离按钮和菜单项", async () => {
- const { ChatEmptyWorkspacePreviewMenu } =
- await import("../src/ChatEmptyState.js");
- const Menu = ChatEmptyWorkspacePreviewMenu as unknown as ComponentType<
- Record<string, unknown>
- >;
- const props = {
- workspacePath: "/Users/tester/project",
- workspaceTabs: [
- {
- workspacePath: "/Users/tester/project",
- label: "project",
- },
- ],
- onSelectWorkspace: vi.fn(),
- onSelectConversationWorkspace: vi.fn(),
- onOpenFolder: vi.fn(),
- allowRemoteWorkspace: false,
- onConnectRemote: async () => "session-id",
- onSelectRemoteProject: async () => {},
- onCancelRemoteProject: async () => {},
- };
- const defaultHtml = renderToStaticMarkup(createElement(Menu, props));
- const hiddenHtml = renderToStaticMarkup(
- createElement(Menu, {
- ...props,
- allowConversationWorkspaceSelection: false,
- }),
- );
- expect(defaultHtml).toContain("chat.empty.detachProject");
- expect(defaultHtml).toContain("chat.empty.workOutsideProject");
- expect(defaultHtml).toContain("group-hover/workspace-chip:opacity-0");
- expect(defaultHtml).toContain("group-focus-within/workspace-chip:opacity-0");
- expect(hiddenHtml).not.toContain("chat.empty.detachProject");
- expect(hiddenHtml).not.toContain("chat.empty.workOutsideProject");
- expect(hiddenHtml).not.toContain("group-hover/workspace-chip:opacity-0");
- expect(hiddenHtml).not.toContain("group-focus-within/workspace-chip:opacity-0");
- });
- it("普通会话 hover workspace trigger 时隐藏项目图标供关闭按钮替换", async () => {
- const html = await renderMenu(false);
- expect(html).toContain("group-hover/workspace-chip:opacity-0");
- expect(html).toContain("group-focus-within/workspace-chip:opacity-0");
- });
- it("在不允许远程入口时隐藏远程连接项", async () => {
- const html = await renderMenu(false);
- expect(html).not.toContain("remote.trigger");
- });
- it("在 Web 本地模式关闭远程能力时隐藏远程连接项", async () => {
- const html = await renderMenu(true, false);
- expect(html).not.toContain("remote.trigger");
- expect(html).not.toContain("ssh-dialog");
- });
- });
|