feat(webui): add a host directory picker and refine composer controls

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-08 20:33:15 +08:00
1 parent 3865bde3fe
commit 66c8d41f66
32 files changed
+1974 -464

No files matched your search

+103 -53
View File
@@ -866,6 +866,34 @@ describe("ThreadComposer", () => {
expect(onPresetChange).toHaveBeenCalledWith("dflash");
});
it("switches the same primary button between voice and send as the draft changes", () => {
const onSend = vi.fn();
render(<ThreadComposer onSend={onSend} onTranscribeAudio={vi.fn(async () => "voice")} />);
const input = screen.getByRole("textbox");
const action = screen.getByRole("button", { name: "Voice input" });
expect(screen.queryByRole("button", { name: "Send message" })).not.toBeInTheDocument();
fireEvent.change(input, { target: { value: " " } });
expect(screen.getByRole("button", { name: "Voice input" })).toBe(action);
fireEvent.change(input, { target: { value: "send this" } });
expect(screen.getByRole("button", { name: "Send message" })).toBe(action);
expect(screen.queryByRole("button", { name: "Voice input" })).not.toBeInTheDocument();
fireEvent.click(action);
expect(onSend).toHaveBeenCalledWith("send this", undefined, undefined);
expect(screen.getByRole("button", { name: "Voice input" })).toBe(action);
});
it("uses send for an attachment-only draft and restores voice after removal", async () => {
mockBlobUrls();
const { container } = render(<ThreadComposer onSend={vi.fn()} onTranscribeAudio={vi.fn(async () => "voice")} />);
fireEvent.change(container.querySelector('input[type="file"]')!, {
target: { files: [new File(["image"], "photo.png", { type: "image/png" })] },
});
await waitFor(() => expect(screen.getByRole("button", { name: "Send message" })).toBeEnabled());
expect(screen.queryByRole("button", { name: "Voice input" })).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Remove attachment" }));
expect(screen.getByRole("button", { name: "Voice input" })).toBeInTheDocument();
});
it("transcribes voice input into the composer without sending", async () => {
mockVoiceRecorder();
const onSend = vi.fn();
@@ -1106,10 +1134,11 @@ describe("ThreadComposer", () => {
it("ignores the delayed click emitted after a long-press voice recording", async () => {
const { getUserMedia } = mockVoiceRecorder();
const onSend = vi.fn();
const onTranscribeAudio = vi.fn(async () => "held once");
render(
<ThreadComposer
onSend={vi.fn()}
onSend={onSend}
onTranscribeAudio={onTranscribeAudio}
placeholder="Type your message..."
/>,
@@ -1131,8 +1160,9 @@ describe("ThreadComposer", () => {
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 20));
});
fireEvent.click(screen.getByRole("button", { name: "Voice input" }));
fireEvent.click(screen.getByRole("button", { name: "Send message" }));
expect(onSend).not.toHaveBeenCalled();
expect(getUserMedia).toHaveBeenCalledTimes(1);
expect(onTranscribeAudio).toHaveBeenCalledTimes(1);
});
@@ -1153,7 +1183,8 @@ describe("ThreadComposer", () => {
const input = screen.getByLabelText("Message input");
fireEvent.change(input, { target: { value: "draft" } });
fireEvent.click(screen.getByRole("button", { name: "Voice input" }));
fireEvent.keyDown(window, { key: "D", code: "KeyD", ctrlKey: true, shiftKey: true });
expect(await screen.findByLabelText("Recording 0:00")).toBeInTheDocument();
await waitForVoiceCapture();
fireEvent.click(await screen.findByRole("button", { name: "Stop recording" }));
@@ -1342,6 +1373,19 @@ describe("ThreadComposer", () => {
expect(screen.getByRole("progressbar", { name: "Context 50%" })).toBeVisible();
});
it.each([
["restricted", "Default Permission", "File access and command execution are restricted to the current working directory."],
["full", "Full Access", "Allows file access and command execution outside the current working directory."],
] as const)("explains %s workspace access on hover", async (mode, label, explanation) => {
const user = userEvent.setup();
render(<ThreadComposer onSend={vi.fn()} workspaceScope={{ project_path: "/tmp/project", project_name: "project", access_mode: mode, restrict_to_workspace: mode === "restricted" }} onWorkspaceScopeChange={vi.fn()} />);
const access = screen.getByRole("button", { name: `Workspace access mode: ${label}` });
expect(access).not.toHaveAttribute("title");
await user.hover(access);
expect(await screen.findByRole("tooltip")).toHaveTextContent(explanation);
expect(access).toHaveAttribute("aria-describedby");
});
it("toggles workspace access directly by click and keyboard", async () => {
const user = userEvent.setup();
const onWorkspaceScopeChange = vi.fn();
@@ -1403,7 +1447,7 @@ describe("ThreadComposer", () => {
expect(shortLabel).toHaveClass("hidden");
});
it("keeps project selection as a compact composer dropdown", async () => {
it("opens project selection in a searchable dialog", async () => {
const user = userEvent.setup();
const onWorkspaceScopeChange = vi.fn();
const defaultScope = {
@@ -1428,22 +1472,18 @@ describe("ThreadComposer", () => {
/>,
);
await user.click(screen.getByRole("button", { name: "Choose project" }));
await user.click(screen.getByRole("button", { name: "Switch working directory" }));
expect(await screen.findByRole("button", { name: /Default workspace/ })).toBeInTheDocument();
expect(await screen.findByRole("button", { name: "/Users/test/.nanobot/workspace" })).toBeInTheDocument();
expect(screen.getByRole("dialog")).toBeInTheDocument();
const input = screen.getByLabelText("Paste path");
fireEvent.change(input, { target: { value: "relative/project" } });
fireEvent.click(screen.getByRole("button", { name: "Use Path" }));
expect(screen.getByRole("alert")).toHaveTextContent(
"Enter an absolute folder path on this machine.",
);
expect(screen.queryByRole("option", { name: "relative/project" })).not.toBeInTheDocument();
expect(onWorkspaceScopeChange).not.toHaveBeenCalled();
fireEvent.change(input, { target: { value: "/Users/test/project-alpha" } });
fireEvent.click(screen.getByRole("button", { name: "Use Path" }));
fireEvent.click(screen.getByRole("option", { name: "/Users/test/project-alpha" }));
expect(onWorkspaceScopeChange).toHaveBeenCalledWith(expect.objectContaining({
project_path: "/Users/test/project-alpha",
@@ -1452,10 +1492,10 @@ describe("ThreadComposer", () => {
restrict_to_workspace: false,
}));
await user.click(screen.getByRole("button", { name: "Choose project" }));
await user.click(screen.getByRole("button", { name: "Switch working directory" }));
const reopenedInput = await screen.findByLabelText("Paste path");
fireEvent.change(reopenedInput, { target: { value: "~/Pictures/Photos" } });
fireEvent.click(screen.getByRole("button", { name: "Use Path" }));
fireEvent.click(screen.getByRole("option", { name: "~/Pictures/Photos" }));
expect(onWorkspaceScopeChange).toHaveBeenLastCalledWith(expect.objectContaining({
project_path: "~/Pictures/Photos",
@@ -1467,10 +1507,12 @@ describe("ThreadComposer", () => {
it.each([
["Windows", "D:\\Users\\test\\.nanobot\\workspace", "D:\\path\\to\\project"],
["Windows UNC", "\\\\server\\share\\workspace", "\\\\server\\share\\project"],
["macOS", "/Users/test/.nanobot/workspace", "/Users/name/project"],
["Linux", "/home/test/.nanobot/workspace", "/home/name/project"],
])("uses a %s path example for the project picker", async (_, projectPath, placeholder) => {
])("accepts a %s project path", async (_, projectPath, selectedPath) => {
const user = userEvent.setup();
const onWorkspaceScopeChange = vi.fn();
const defaultScope = {
project_path: projectPath,
project_name: "workspace",
@@ -1486,16 +1528,18 @@ describe("ThreadComposer", () => {
workspaceScope={defaultScope}
workspaceDefaultScope={defaultScope}
workspaceControls={{ can_change_project: true, can_use_full_access: true }}
onWorkspaceScopeChange={vi.fn()}
onWorkspaceScopeChange={onWorkspaceScopeChange}
/>,
);
await user.click(screen.getByRole("button", { name: "Choose project" }));
await user.click(screen.getByRole("button", { name: "Switch working directory" }));
expect(await screen.findByLabelText("Paste path")).toHaveAttribute("placeholder", placeholder);
fireEvent.change(await screen.findByLabelText("Paste path"), { target: { value: selectedPath } });
fireEvent.keyDown(screen.getByLabelText("Paste path"), { key: "Enter" });
expect(onWorkspaceScopeChange).toHaveBeenCalledWith(expect.objectContaining({ project_path: selectedPath }));
});
it("slides project controls closed without offering a compact replacement", () => {
it("groups the directory and access beside attachments and hides them with workspace controls", () => {
const defaultScope = {
project_path: "/Users/test/.nanobot/workspace",
project_name: "workspace",
@@ -1515,36 +1559,38 @@ describe("ThreadComposer", () => {
/>
);
const { container, rerender } = render(composer(false));
const drawer = container.querySelector("[data-composer-workspace-drawer]");
expect(drawer).toHaveClass("inline-disclosure");
expect(drawer?.firstElementChild).toHaveClass("inline-disclosure-clip");
expect(drawer?.firstElementChild?.firstElementChild).toHaveClass("inline-disclosure-content");
expect(drawer).toHaveAttribute("data-state", "open");
expect(drawer).not.toHaveAttribute("aria-hidden");
expect(container.querySelector("[data-composer-workspace-compact]")).not.toBeInTheDocument();
const trigger = screen.getByRole("button", { name: "Switch working directory" });
expect(trigger).toHaveTextContent("workspace");
expect(trigger.closest(".thread-composer-footer-primary")).not.toBeNull();
expect(trigger.closest(".thread-composer-workspace-group")).toContainElement(screen.getByRole("button", { name: "Workspace access mode: Full Access" }));
expect(container.querySelector("[data-composer-workspace-drawer]")).not.toBeInTheDocument();
rerender(composer(true));
expect(container.querySelector("[data-composer-workspace-drawer]")).toBe(drawer);
expect(drawer).toHaveAttribute("data-state", "closed");
expect(drawer).toHaveAttribute("aria-hidden", "true");
expect(within(drawer as HTMLElement).getByRole("button", {
hidden: true,
name: "Choose project",
})).toBeDisabled();
expect(screen.queryByRole("button", { name: "Choose project" })).not.toBeInTheDocument();
expect(screen.queryByRole("button", {
name: "Workspace access mode: Full Access",
})).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Switch working directory" })).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Workspace access mode: Full Access" })).not.toBeInTheDocument();
rerender(composer(false));
expect(screen.getByRole("button", { name: "Switch working directory" })).toBeEnabled();
});
expect(container.querySelector("[data-composer-workspace-drawer]")).toBe(drawer);
expect(drawer).toHaveAttribute("data-state", "open");
expect(within(drawer as HTMLElement).getByRole("button", {
name: "Choose project",
})).toBeEnabled();
it("places the directory left and access right on narrow composers, moving them into the toolbar on desktop", () => {
let width = 390;
vi.spyOn(HTMLFormElement.prototype, "getBoundingClientRect").mockImplementation(() => rect({ width, height: 160 }));
const defaultScope = { project_path: "/srv/workspace", project_name: "workspace", access_mode: "full" as const };
const { container } = render(<ThreadComposer variant="hero" onSend={vi.fn()} workspaceScope={defaultScope}
workspaceDefaultScope={defaultScope} workspaceControls={{ can_change_project: true }} onWorkspaceScopeChange={vi.fn()} />);
const input = screen.getByRole("textbox");
const row = container.querySelector(".thread-composer-workspace-row")!;
const trigger = screen.getByRole("button", { name: "Switch working directory" });
expect(row).toContainElement(trigger);
expect(trigger.closest(".ml-auto")).toBeNull();
const access = screen.getByRole("button", { name: "Workspace access mode: Full Access" });
expect(row).toContainElement(access);
expect(access.closest(".ml-auto")).not.toBeNull();
width = 800;
fireEvent(window, new Event("resize"));
expect(screen.getByRole("button", { name: "Switch working directory" }).closest(".thread-composer-footer-primary")).not.toBeNull();
expect(container.querySelector("[data-composer-workspace-drawer]")).not.toBeInTheDocument();
expect(screen.getByRole("textbox")).toBe(input);
expect(screen.getAllByRole("button", { name: "Switch working directory" })).toHaveLength(1);
});
it("uses the native folder picker for project selection on native host", async () => {
@@ -1583,10 +1629,12 @@ describe("ThreadComposer", () => {
/>,
);
fireEvent.click(screen.getByRole("button", { name: "Choose project" }));
fireEvent.click(screen.getByRole("button", { name: "Switch working directory" }));
expect(screen.getByRole("dialog", { name: "Choose project" })).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Use system folder picker…" }));
await waitFor(() => expect(pickFolder).toHaveBeenCalled());
expect(screen.queryByRole("button", { name: /Default workspace/ })).not.toBeInTheDocument();
expect(screen.queryByRole("dialog", { name: "Choose project" })).not.toBeInTheDocument();
expect(onWorkspaceScopeChange).toHaveBeenCalledWith(expect.objectContaining({
project_path: "/Users/test/native-project",
project_name: "native-project",
@@ -1628,7 +1676,7 @@ describe("ThreadComposer", () => {
/>,
);
await user.click(screen.getByRole("button", { name: "Choose project" }));
await user.click(screen.getByRole("button", { name: "Switch working directory" }));
expect(await screen.findByLabelText("Paste path")).toBeInTheDocument();
expect(pickFolder).not.toHaveBeenCalled();
@@ -1637,7 +1685,7 @@ describe("ThreadComposer", () => {
fireEvent.change(screen.getByLabelText("Paste path"), {
target: { value: "/srv/nas-project" },
});
fireEvent.click(screen.getByRole("button", { name: "Use Path" }));
fireEvent.click(screen.getByRole("option", { name: "/srv/nas-project" }));
expect(onWorkspaceScopeChange).toHaveBeenCalledWith(expect.objectContaining({
project_path: "/srv/nas-project",
@@ -1673,8 +1721,10 @@ describe("ThreadComposer", () => {
/>,
);
fireEvent.click(screen.getByRole("button", { name: "Choose project" }));
fireEvent.click(screen.getByRole("button", { name: "Switch working directory" }));
expect(screen.getByRole("dialog", { name: "Choose project" })).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Use system folder picker…" }));
await waitFor(() => expect(pickFolder).toHaveBeenCalled());
expect(screen.queryByLabelText("Paste path")).not.toBeInTheDocument();
expect(onWorkspaceScopeChange).toHaveBeenCalledWith(expect.objectContaining({
@@ -1706,9 +1756,9 @@ describe("ThreadComposer", () => {
/>,
);
await user.click(screen.getByRole("button", { name: "Choose project" }));
await user.click(screen.getByRole("button", { name: "Switch working directory" }));
expect(await screen.findByRole("button", { name: /Default workspace/ })).toBeInTheDocument();
expect(await screen.findByRole("button", { name: "/Users/test/.nanobot/workspace" })).toBeInTheDocument();
expect(screen.getByLabelText("Paste path")).toBeInTheDocument();
});