|
| 1 | +import { render, screen, fireEvent, waitFor } from "@testing-library/react"; |
| 2 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 3 | +import { TaosAssistantWindow } from "./TaosAssistantWindow"; |
| 4 | +import { useTaosAgentStore } from "@/stores/taos-agent-store"; |
| 5 | +import { useProcessStore } from "@/stores/process-store"; |
| 6 | + |
| 7 | +// Mock child that opens a modal — keeps the test surface narrow, mirrors |
| 8 | +// TaosAssistantPanel.test.tsx's approach. |
| 9 | +vi.mock("@/components/TaosAssistantSettings", () => ({ |
| 10 | + TaosAssistantSettings: ({ open }: { open: boolean }) => |
| 11 | + open ? <div data-testid="settings-modal" /> : null, |
| 12 | +})); |
| 13 | + |
| 14 | +function resetStores() { |
| 15 | + useTaosAgentStore.setState({ |
| 16 | + isOpen: false, |
| 17 | + messages: [], |
| 18 | + model: "qwen3", |
| 19 | + streaming: false, |
| 20 | + settingsOpen: false, |
| 21 | + }); |
| 22 | + useProcessStore.setState({ windows: [] }); |
| 23 | +} |
| 24 | + |
| 25 | +describe("TaosAssistantWindow", () => { |
| 26 | + beforeEach(() => { |
| 27 | + resetStores(); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + vi.unstubAllGlobals(); |
| 32 | + vi.clearAllMocks(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("renders the chat shell when launched directly (no popOut prop) — regression for #1615", () => { |
| 36 | + // Direct Launchpad/Dock launch calls openWindow("taos-agent", size) with |
| 37 | + // no props, so windows never carries a matching entry with |
| 38 | + // props.popOut. The window must still show its UI, not go blank. |
| 39 | + vi.stubGlobal( |
| 40 | + "fetch", |
| 41 | + vi.fn().mockResolvedValue({ ok: true, json: async () => ({ model: "qwen3" }) }), |
| 42 | + ); |
| 43 | + |
| 44 | + render(<TaosAssistantWindow windowId="win-1" />); |
| 45 | + |
| 46 | + expect(screen.getByText("taOS agent")).toBeInTheDocument(); |
| 47 | + expect(screen.getByRole("textbox", { name: /message taOS agent/i })).toBeInTheDocument(); |
| 48 | + expect(screen.getByRole("button", { name: /send message/i })).toBeInTheDocument(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("renders the chat shell when opened via the pop-out button (props.popOut)", () => { |
| 52 | + useProcessStore.setState({ |
| 53 | + windows: [ |
| 54 | + { |
| 55 | + id: "win-2", |
| 56 | + appId: "taos-agent", |
| 57 | + position: { x: 0, y: 0 }, |
| 58 | + size: { w: 420, h: 640 }, |
| 59 | + zIndex: 1, |
| 60 | + minimized: false, |
| 61 | + maximized: false, |
| 62 | + snapped: null, |
| 63 | + focused: true, |
| 64 | + props: { popOut: true }, |
| 65 | + launchNonce: 0, |
| 66 | + }, |
| 67 | + ], |
| 68 | + }); |
| 69 | + vi.stubGlobal( |
| 70 | + "fetch", |
| 71 | + vi.fn().mockResolvedValue({ ok: true, json: async () => ({ model: "qwen3" }) }), |
| 72 | + ); |
| 73 | + |
| 74 | + render(<TaosAssistantWindow windowId="win-2" />); |
| 75 | + |
| 76 | + expect(screen.getByText("taOS agent")).toBeInTheDocument(); |
| 77 | + expect(screen.getByRole("textbox", { name: /message taOS agent/i })).toBeInTheDocument(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("surfaces the runtime error inline instead of leaving the dialog blank", async () => { |
| 81 | + vi.stubGlobal( |
| 82 | + "fetch", |
| 83 | + vi.fn().mockImplementation((url: string) => { |
| 84 | + if (typeof url === "string" && url.includes("/api/taos-agent/settings")) { |
| 85 | + return Promise.resolve({ ok: true, json: async () => ({ model: "qwen3" }) }); |
| 86 | + } |
| 87 | + // /api/taos-agent/chat — simulate the runtime-unavailable 503. |
| 88 | + return Promise.resolve({ |
| 89 | + ok: false, |
| 90 | + body: null, |
| 91 | + text: async () => |
| 92 | + JSON.stringify({ error: "opencode is not installed. Install it with: curl -fsSL https://opencode.ai/install | bash" }), |
| 93 | + }); |
| 94 | + }), |
| 95 | + ); |
| 96 | + |
| 97 | + render(<TaosAssistantWindow windowId="win-3" />); |
| 98 | + |
| 99 | + const textarea = screen.getByRole("textbox", { name: /message taOS agent/i }); |
| 100 | + fireEvent.change(textarea, { target: { value: "hello" } }); |
| 101 | + fireEvent.click(screen.getByRole("button", { name: /send message/i })); |
| 102 | + |
| 103 | + // The shell stays up (input still present) and the error is shown inline. |
| 104 | + await waitFor(() => { |
| 105 | + expect(screen.getByText(/opencode is not installed/i)).toBeInTheDocument(); |
| 106 | + }); |
| 107 | + expect(screen.getByRole("textbox", { name: /message taOS agent/i })).toBeInTheDocument(); |
| 108 | + }); |
| 109 | +}); |
0 commit comments