|
| 1 | +import React from "react"; |
| 2 | +import { screen, fireEvent, waitFor } from "@testing-library/react"; |
| 3 | +import { describe, it, expect } from "vitest"; |
| 4 | +import { type BaseEvent, type RunAgentInput } from "@ag-ui/client"; |
| 5 | +import { Observable } from "rxjs"; |
| 6 | +import { |
| 7 | + MockStepwiseAgent, |
| 8 | + renderWithCopilotKit, |
| 9 | + runStartedEvent, |
| 10 | + runFinishedEvent, |
| 11 | + textChunkEvent, |
| 12 | + testId, |
| 13 | +} from "@/__tests__/utils/test-helpers"; |
| 14 | +import { useAgent } from "../use-agent"; |
| 15 | +import { useCopilotKit } from "@/providers/CopilotKitProvider"; |
| 16 | +import { CopilotChat } from "@/components/chat/CopilotChat"; |
| 17 | + |
| 18 | +/** |
| 19 | + * Mock agent that captures RunAgentInput to verify state is passed correctly |
| 20 | + */ |
| 21 | +class StateCapturingMockAgent extends MockStepwiseAgent { |
| 22 | + public lastRunInput?: RunAgentInput; |
| 23 | + |
| 24 | + run(input: RunAgentInput): Observable<BaseEvent> { |
| 25 | + this.lastRunInput = input; |
| 26 | + return super.run(input); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +describe("useAgent e2e", () => { |
| 31 | + describe("setState passes state to agent run", () => { |
| 32 | + it("agent receives state set via setState when runAgent is called", async () => { |
| 33 | + const agent = new StateCapturingMockAgent(); |
| 34 | + |
| 35 | + /** |
| 36 | + * Test component that: |
| 37 | + * 1. Gets agent via useAgent() |
| 38 | + * 2. Gets copilotkit via useCopilotKit() |
| 39 | + * 3. Sets state on agent and calls runAgent |
| 40 | + */ |
| 41 | + function StateTestComponent() { |
| 42 | + const { agent: hookAgent } = useAgent(); |
| 43 | + const { copilotkit } = useCopilotKit(); |
| 44 | + |
| 45 | + const handleSetStateAndRun = async () => { |
| 46 | + hookAgent.setState({ testKey: "testValue", counter: 42 }); |
| 47 | + await copilotkit.runAgent({ agent: hookAgent }); |
| 48 | + }; |
| 49 | + |
| 50 | + return ( |
| 51 | + <button data-testid="trigger-btn" onClick={handleSetStateAndRun}> |
| 52 | + Set State and Run |
| 53 | + </button> |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + renderWithCopilotKit({ |
| 58 | + agent, |
| 59 | + children: <StateTestComponent />, |
| 60 | + }); |
| 61 | + |
| 62 | + // Click the button to set state and trigger runAgent |
| 63 | + const triggerBtn = await screen.findByTestId("trigger-btn"); |
| 64 | + fireEvent.click(triggerBtn); |
| 65 | + |
| 66 | + // Wait for the agent's run method to be called |
| 67 | + await waitFor(() => { |
| 68 | + expect(agent.lastRunInput).toBeDefined(); |
| 69 | + }); |
| 70 | + |
| 71 | + // Complete the agent run |
| 72 | + agent.emit(runStartedEvent()); |
| 73 | + agent.emit(runFinishedEvent()); |
| 74 | + agent.complete(); |
| 75 | + |
| 76 | + // Verify the state was passed to the agent |
| 77 | + expect(agent.lastRunInput?.state).toEqual({ |
| 78 | + testKey: "testValue", |
| 79 | + counter: 42, |
| 80 | + }); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe("addMessage + runAgent displays in CopilotChat", () => { |
| 85 | + it("messages added via useAgent show up in CopilotChat", async () => { |
| 86 | + const agent = new MockStepwiseAgent(); |
| 87 | + |
| 88 | + /** |
| 89 | + * Test component that: |
| 90 | + * 1. Gets agent via useAgent() |
| 91 | + * 2. Gets copilotkit via useCopilotKit() |
| 92 | + * 3. Adds a user message and calls runAgent |
| 93 | + */ |
| 94 | + function MessageTestComponent() { |
| 95 | + const { agent: hookAgent } = useAgent(); |
| 96 | + const { copilotkit } = useCopilotKit(); |
| 97 | + |
| 98 | + const handleAddMessageAndRun = async () => { |
| 99 | + hookAgent.addMessage({ |
| 100 | + id: testId("user-msg"), |
| 101 | + role: "user", |
| 102 | + content: "Hello from useAgent!", |
| 103 | + }); |
| 104 | + await copilotkit.runAgent({ agent: hookAgent }); |
| 105 | + }; |
| 106 | + |
| 107 | + return ( |
| 108 | + <div> |
| 109 | + <button data-testid="send-btn" onClick={handleAddMessageAndRun}> |
| 110 | + Send Message |
| 111 | + </button> |
| 112 | + <div style={{ height: 400 }}> |
| 113 | + <CopilotChat /> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + renderWithCopilotKit({ |
| 120 | + agent, |
| 121 | + children: <MessageTestComponent />, |
| 122 | + }); |
| 123 | + |
| 124 | + // Click the button to add message and trigger runAgent |
| 125 | + const sendBtn = await screen.findByTestId("send-btn"); |
| 126 | + fireEvent.click(sendBtn); |
| 127 | + |
| 128 | + // User message should appear in the chat |
| 129 | + await waitFor(() => { |
| 130 | + expect(screen.getByText("Hello from useAgent!")).toBeDefined(); |
| 131 | + }); |
| 132 | + |
| 133 | + // Simulate agent response |
| 134 | + const responseId = testId("assistant-msg"); |
| 135 | + agent.emit(runStartedEvent()); |
| 136 | + agent.emit(textChunkEvent(responseId, "Hello! I received your message.")); |
| 137 | + agent.emit(runFinishedEvent()); |
| 138 | + agent.complete(); |
| 139 | + |
| 140 | + // Assistant response should appear in the chat |
| 141 | + await waitFor(() => { |
| 142 | + expect(screen.getByText("Hello! I received your message.")).toBeDefined(); |
| 143 | + }); |
| 144 | + }); |
| 145 | + }); |
| 146 | +}); |
0 commit comments