import { render, screen } from "@testing-library/react"; import "@testing-library/jest-dom/vitest"; import userEvent from "@testing-library/user-event"; import { afterEach, describe, expect, it, vi } from "vitest"; import { App } from "../src/App.js"; import { createInitialHarnessState, type HarnessClientState } from "../src/store/reducer.js"; function makeStore() { const sendMessage = vi.fn().mockResolvedValue({ runId: "run_1" }); const state: HarnessClientState = { ...createInitialHarnessState(), sessionId: "ses_test", connection: "open", skills: [{ name: "ads-analysis", description: "分析广告投放指标", source: "project", filePath: ".agents/skills/ads-analysis/SKILL.md", diagnostics: [], }], }; return { getSnapshot: () => state, subscribe: () => () => undefined, sendMessage, cancel: vi.fn().mockResolvedValue({ cancelled: false }), dispose: vi.fn().mockResolvedValue(undefined), start: vi.fn().mockResolvedValue(undefined), getModelConfig: vi.fn().mockResolvedValue({ modelConfig: { configured: true, source: "environment", providerId: "openai", api: "openai-responses", modelId: "gpt-test", supportsImages: true }, }), }; } afterEach(() => vi.unstubAllGlobals()); describe("App", () => { it("renders the three primary work areas", () => { render(); expect(screen.getByRole("region", { name: "Skills Catalog" })).toBeVisible(); expect(screen.getByRole("main", { name: "Streaming Chat" })).toBeVisible(); expect(screen.getByRole("region", { name: "Event Timeline" })).toBeVisible(); }); it("fills but does not send the demo prompt", async () => { const user = userEvent.setup(); const store = makeStore(); render(); await user.click(screen.getByRole("button", { name: "填入演示任务" })); expect(screen.getByRole("textbox").value).toContain("demo-account"); expect(store.sendMessage).not.toHaveBeenCalled(); }); it("disables sending and points to settings when the model is missing", () => { render(); expect(screen.getByRole("button", { name: "发送消息" })).toBeDisabled(); expect(screen.getByText("请先配置模型连接")).toBeVisible(); }); it("switches to the event timeline in the mobile tab layout", async () => { vi.stubGlobal("matchMedia", vi.fn(() => ({ matches: true, media: "(max-width: 720px)", onchange: null, addEventListener: vi.fn(), removeEventListener: vi.fn(), addListener: vi.fn(), removeListener: vi.fn(), dispatchEvent: vi.fn(), }))); const user = userEvent.setup(); render(); await user.click(screen.getByRole("tab", { name: "Events" })); expect(screen.getByRole("tab", { name: "Events" })).toHaveAttribute("aria-selected", "true"); expect(screen.getByRole("region", { name: "Event Timeline" })).toBeVisible(); expect(screen.queryByRole("main", { name: "Streaming Chat" })).not.toBeInTheDocument(); }); it("deletes the old session before starting a reset session", async () => { const user = userEvent.setup(); const store = makeStore(); render(); await user.click(screen.getByRole("button", { name: "重置训练会话" })); expect(store.dispose).toHaveBeenCalledTimes(1); expect(store.start).toHaveBeenCalledTimes(1); expect(store.dispose.mock.invocationCallOrder[0]).toBeLessThan(store.start.mock.invocationCallOrder[0] ?? 0); }); });