fix(chat): pin composer, set reply language, and track model loading

This commit is contained in:
Jeffrey Wu
2026-09-07 12:00:27 +08:00
parent 5c61371d7e
commit f4e3f41c2c
9 changed files with 299 additions and 36 deletions
+88 -5
View File
@@ -1,5 +1,5 @@
// @vitest-environment jsdom
import { render, screen, waitFor } from "@testing-library/react";
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { HarnessEvent } from "@agent-studio/shared";
@@ -11,12 +11,95 @@ import { AppErrorBoundary } from "../src/components/AppErrorBoundary";
afterEach(() => { localStorage.clear(); sessionStorage.clear(); vi.restoreAllMocks(); vi.unstubAllGlobals(); });
describe("web UI", () => {
it("renders the Skills, Chat, and Timeline operating areas", () => { const store = new HarnessClientStore(); render(<HarnessRuntime store={store}><App/></HarnessRuntime>); expect(screen.getByLabelText("Skills Catalog")).toBeInTheDocument(); expect(screen.getByLabelText("Streaming Chat")).toBeInTheDocument(); expect(screen.getByLabelText("Event Timeline")).toBeInTheDocument(); expect(screen.getByRole("button", { name: /尚未配置模型/ })).toBeInTheDocument(); });
it("renders the Skills, Chat, and Timeline operating areas", () => { const store = new HarnessClientStore(); render(<HarnessRuntime store={store}><App/></HarnessRuntime>); expect(screen.getByLabelText("Skills Catalog")).toBeInTheDocument(); expect(screen.getByLabelText("Streaming Chat")).toBeInTheDocument(); expect(screen.getByLabelText("Event Timeline")).toBeInTheDocument(); expect(screen.getByRole("status")).toHaveTextContent("正在读取服务器模型配置"); expect(screen.queryByText(/尚未配置模型|重新输入 API Key|Model missing/)).not.toBeInTheDocument(); expect(screen.getByRole("button", { name: "发送消息" })).toBeDisabled(); });
it("keeps Composer outside the scrolling message viewport and inside the Thread", async () => {
const store = new HarnessClientStore();
store.setMessages(Array.from({ length: 30 }, (_, index) => ({ id: `message-${index}`, role: "assistant", content: [{ type: "text", text: `回复 ${index}` }] })));
render(<HarnessRuntime store={store}><App/></HarnessRuntime>);
const input = screen.getByLabelText("消息输入");
expect(input.closest(".thread-viewport")).toBeNull();
const root = input.closest(".thread-root")!;
expect(root.querySelector(".thread-viewport")).toContainElement(screen.getByText("回复 29"));
expect(root.lastElementChild).toHaveClass("composer-footer");
expect(root.lastElementChild).toContainElement(input);
await userEvent.setup().click(screen.getByRole("button", { name: "chat" }));
expect(input.closest(".mobile-panel")).toHaveClass("shown");
});
it("enables sending after an environment model loads, and preserves send and stop controls", async () => {
const store = new HarnessClientStore();
Object.assign(store.getSnapshot(), { sessionId: "ses_1", connection: "online" });
let release!: () => void;
const gate = new Promise<void>((resolve) => { release = resolve; });
const fetcher = vi.fn(async (url: string) => {
if (url.endsWith("/model-config")) { await gate; return { ok: true, json: async () => ({ configured: true, source: "environment", keyConfigured: true }) }; }
return { ok: true, json: async () => ({}) };
});
vi.stubGlobal("fetch", fetcher);
render(<HarnessRuntime store={store}><App/></HarnessRuntime>);
const user = userEvent.setup();
await user.type(screen.getByLabelText("消息输入"), "请分析账户");
expect(screen.getByRole("button", { name: "发送消息" })).toBeDisabled();
let refresh!: Promise<void>;
act(() => { refresh = store.refreshModel(); });
expect(screen.queryByText(/尚未配置模型|重新输入 API Key/)).not.toBeInTheDocument();
await act(async () => { release(); await refresh; });
expect(screen.getByRole("button", { name: "发送消息" })).toBeEnabled();
await user.click(screen.getByRole("button", { name: "发送消息" }));
expect(fetcher).toHaveBeenCalledWith("/api/sessions/ses_1/messages", expect.objectContaining({ method: "POST", body: JSON.stringify({ text: "请分析账户", attachmentIds: [] }) }));
act(() => store.reduce(harnessEvent(1, "run.started", { modelId: "env-model" })));
await user.click(screen.getByRole("button", { name: "停止运行" }));
expect(fetcher).toHaveBeenCalledWith("/api/sessions/ses_1/cancel", expect.objectContaining({ method: "POST" }));
});
it("preserves attachment drag-and-drop upload and removal in the fixed Composer", async () => {
const store = new HarnessClientStore();
Object.assign(store.getSnapshot(), { sessionId: "ses_1", connection: "online", modelLoadState: "loaded", model: { configured: true, source: "environment", keyConfigured: true, supportsImages: true } });
const fetcher = vi.fn().mockResolvedValue({ ok: true, json: async () => ({ attachmentId: "att_1", name: "photo.png", mediaType: "image/png" }) });
vi.stubGlobal("fetch", fetcher);
Object.defineProperty(URL, "createObjectURL", { configurable: true, value: vi.fn(() => "blob:attachment") });
Object.defineProperty(URL, "revokeObjectURL", { configurable: true, value: vi.fn() });
const { container } = render(<HarnessRuntime store={store}><App/></HarnessRuntime>);
const user = userEvent.setup();
expect(screen.getByRole("button", { name: "添加图片" })).toBeEnabled();
const dropzone = container.querySelector(".dropzone")!;
const dataTransfer = { types: ["Files"], files: [new File(["png"], "photo.png", { type: "image/png" })] };
fireEvent.dragEnter(dropzone, { dataTransfer });
fireEvent.drop(dropzone, { dataTransfer });
await waitFor(() => expect(screen.getByText("photo.png")).toBeInTheDocument());
expect(screen.getByText("photo.png").closest(".composer-footer")).not.toBeNull();
expect(fetcher).toHaveBeenCalledWith("/api/sessions/ses_1/attachments", expect.objectContaining({ method: "POST", body: expect.any(FormData) }));
await user.click(screen.getByRole("button", { name: "移除图片" }));
await waitFor(() => expect(screen.queryByText("photo.png")).not.toBeInTheDocument());
expect(fetcher).toHaveBeenCalledWith("/api/sessions/ses_1/attachments/att_1", expect.objectContaining({ method: "DELETE" }));
expect(URL.revokeObjectURL).toHaveBeenCalledWith("blob:attachment");
});
it("shows a recoverable read error without claiming missing credentials", async () => {
const store = new HarnessClientStore();
Object.assign(store.getSnapshot(), { sessionId: "ses_1", connection: "online", model: { configured: true, source: "environment", keyConfigured: true } });
vi.stubGlobal("fetch", vi.fn().mockRejectedValueOnce(new Error("HTTP 503"))
.mockResolvedValueOnce({ ok: true, json: async () => ({ configured: true, source: "environment", keyConfigured: true }) }));
render(<HarnessRuntime store={store}><App/></HarnessRuntime>);
const user = userEvent.setup(); await user.type(screen.getByLabelText("消息输入"), "任务");
expect(screen.getByRole("button", { name: "发送消息" })).toBeDisabled();
await act(() => store.refreshModel());
expect(screen.getByRole("alert")).toHaveTextContent("服务器模型配置读取失败:HTTP 503");
expect(screen.queryByText(/尚未配置模型|重新输入 API Key/)).not.toBeInTheDocument();
expect(screen.getByRole("button", { name: "发送消息" })).toBeDisabled();
await user.click(screen.getByRole("button", { name: "重试读取模型配置" }));
await waitFor(() => expect(screen.getByRole("button", { name: "发送消息" })).toBeEnabled());
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
});
it("only offers missing-model Settings after the server confirms it is unconfigured", async () => {
const store = new HarnessClientStore(); store.getSnapshot().modelLoadState = "loaded";
render(<HarnessRuntime store={store}><App/></HarnessRuntime>);
expect(screen.getByRole("button", { name: "发送消息" })).toBeDisabled();
await userEvent.setup().click(screen.getByRole("button", { name: /尚未配置模型/ }));
expect(screen.getByLabelText("API Key")).toHaveAttribute("type", "password");
});
it("renders the mock metrics Tool UI", () => { render(<MockAdsMetricsUI {...toolProps("mock_ads_metrics", { accountId: "demo-account", days: 7 }, { details: { spend: 100, ctr: 2, cpa: 4, roas: 5 } })}/>); expect(screen.getByText("demo-account", { exact: false })).toBeInTheDocument(); expect(screen.getByText("ROAS")).toBeInTheDocument(); });
it("renders approval actions inside save_report_draft", () => { const store = new HarnessClientStore(); render(<HarnessRuntime store={store}><SaveReportDraftUI {...toolProps("save_report_draft", { content: "报告正文" }, undefined, { id: "apr_1" })}/></HarnessRuntime>); expect(screen.getByRole("button", { name: "批准一次" })).toBeInTheDocument(); expect(screen.getByRole("button", { name: "拒绝" })).toBeInTheDocument(); });
it("opens settings with password input and does not use Web Storage", async () => { const user = userEvent.setup(); const store = new HarnessClientStore(); render(<HarnessRuntime store={store}><App/></HarnessRuntime>); await user.click(screen.getByRole("button", { name: /尚未配置模型/ })); expect(screen.getByLabelText("API Key")).toHaveAttribute("type", "password"); expect(localStorage.length).toBe(0); expect(sessionStorage.length).toBe(0); });
it("prompts for a new key when local model metadata was restored", () => { const store = new HarnessClientStore(); store.getSnapshot().model = { configured: true, source: "session", providerId: "saved", api: "openai-completions", modelId: "saved-model", supportsImages: false, keyConfigured: false }; render(<HarnessRuntime store={store}><App/></HarnessRuntime>); expect(screen.getByRole("button", { name: /模型配置已恢复/ })).toBeInTheDocument(); expect(screen.getByLabelText("消息输入")).toHaveAttribute("placeholder", "请重新输入 API Key"); });
it("omits empty optional model fields when saving settings", async () => { const user = userEvent.setup(); const store = new HarnessClientStore(); const save = vi.spyOn(store, "saveModel").mockResolvedValue(); render(<HarnessRuntime store={store}><App/></HarnessRuntime>); await user.click(screen.getByRole("button", { name: /尚未配置模型/ })); await user.type(screen.getByLabelText("API Key"), "sk-fake"); await user.click(screen.getByRole("button", { name: "保存配置" })); expect(save).toHaveBeenCalledWith({ providerId: "openai", api: "openai-responses", baseUrl: "https://api.openai.com/v1", apiKey: "sk-fake", modelId: "gpt-5.4", supportsImages: true }); });
it("opens settings with password input and does not use Web Storage", async () => { const user = userEvent.setup(); const store = new HarnessClientStore(); render(<HarnessRuntime store={store}><App/></HarnessRuntime>); await user.click(screen.getByRole("button", { name: "Settings" })); expect(screen.getByLabelText("API Key")).toHaveAttribute("type", "password"); expect(localStorage.length).toBe(0); expect(sessionStorage.length).toBe(0); });
it("prompts for a new key when local model metadata was restored", () => { const store = new HarnessClientStore(); store.getSnapshot().modelLoadState = "loaded"; store.getSnapshot().model = { configured: true, source: "session", providerId: "saved", api: "openai-completions", modelId: "saved-model", supportsImages: false, keyConfigured: false }; render(<HarnessRuntime store={store}><App/></HarnessRuntime>); expect(screen.getByRole("button", { name: /模型配置,但缺少 API Key/ })).toBeInTheDocument(); expect(screen.getByLabelText("消息输入")).toHaveAttribute("placeholder", "请重新输入 API Key"); });
it("omits empty optional model fields when saving settings", async () => { const user = userEvent.setup(); const store = new HarnessClientStore(); const save = vi.spyOn(store, "saveModel").mockResolvedValue(); render(<HarnessRuntime store={store}><App/></HarnessRuntime>); await user.click(screen.getByRole("button", { name: "Settings" })); await user.type(screen.getByLabelText("API Key"), "sk-fake"); await user.click(screen.getByRole("button", { name: "保存配置" })); expect(save).toHaveBeenCalledWith({ providerId: "openai", api: "openai-responses", baseUrl: "https://api.openai.com/v1", apiKey: "sk-fake", modelId: "gpt-5.4", supportsImages: true }); });
it("fills the classroom demo prompt without sending it", async () => { const user = userEvent.setup(); const store = new HarnessClientStore(); render(<HarnessRuntime store={store}><App/></HarnessRuntime>); await user.click(screen.getByRole("button", { name: "填入 Demo Prompt" })); expect((screen.getByLabelText("消息输入") as HTMLTextAreaElement).value).toContain("ads-analysis"); expect(store.getSnapshot().events).toHaveLength(0); });
it("shows a recoverable fallback instead of a black screen for render failures", () => { vi.spyOn(console, "error").mockImplementation(() => undefined); const Broken = () => { throw new Error("tool renderer failed"); }; render(<AppErrorBoundary><Broken/></AppErrorBoundary>); expect(screen.getByRole("alert")).toHaveTextContent("界面渲染出现异常"); expect(screen.getByRole("button", { name: "重新加载页面" })).toBeInTheDocument(); });
it("renders a duplicated Pi Tool sequence and approval without crashing", () => {