|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import { renderHook, waitFor } from "@testing-library/react"; |
| 3 | +import { feedbackMessage, useChatbot } from "./useChatbot"; |
| 4 | +import type { MessageProps } from "@patternfly/chatbot/dist/dynamic/Message"; |
| 5 | +import { Sentiment } from "../Constants"; |
| 6 | +import type { ChatFeedback } from "../types/Message"; |
| 7 | +import * as fetchEventSourceModule from "@microsoft/fetch-event-source"; |
| 8 | +import axios from "axios"; |
| 9 | + |
| 10 | +const CONVERSATION_ID = "123e4567-e89b-12d3-a456-426614174000"; |
| 11 | + |
| 12 | +// Mock fetchEventSource |
| 13 | +vi.mock("@microsoft/fetch-event-source", () => ({ |
| 14 | + fetchEventSource: vi.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +// Mock axios |
| 18 | +vi.mock("axios"); |
| 19 | + |
| 20 | +describe("feedbackMessage", () => { |
| 21 | + it("should return a message with a thank you note for positive feedback", () => { |
| 22 | + const feedback: ChatFeedback = { |
| 23 | + sentiment: Sentiment.THUMBS_UP, |
| 24 | + query: "", |
| 25 | + response: { |
| 26 | + conversation_id: "", |
| 27 | + response: "", |
| 28 | + referenced_documents: [], |
| 29 | + truncated: false, |
| 30 | + }, |
| 31 | + message: { |
| 32 | + role: "user", |
| 33 | + content: "This is a test message", |
| 34 | + name: "User", |
| 35 | + avatar: "user_avatar", |
| 36 | + quickResponses: [], |
| 37 | + referenced_documents: [], |
| 38 | + }, |
| 39 | + }; |
| 40 | + const message: MessageProps = feedbackMessage(feedback, CONVERSATION_ID); |
| 41 | + |
| 42 | + expect(message.role).toBe("bot"); |
| 43 | + expect(message.content).toBe("Thank you for your feedback!"); |
| 44 | + expect(message.quickResponses).toEqual([]); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should return a message with a thank you note and a quick response for negative feedback", () => { |
| 48 | + const feedback: ChatFeedback = { |
| 49 | + sentiment: Sentiment.THUMBS_DOWN, |
| 50 | + query: "", |
| 51 | + response: { |
| 52 | + conversation_id: "", |
| 53 | + response: "", |
| 54 | + referenced_documents: [], |
| 55 | + truncated: false, |
| 56 | + }, |
| 57 | + message: { |
| 58 | + role: "user", |
| 59 | + content: "This is a test message", |
| 60 | + name: "User", |
| 61 | + avatar: "user_avatar", |
| 62 | + quickResponses: [], |
| 63 | + referenced_documents: [], |
| 64 | + }, |
| 65 | + }; |
| 66 | + const message: MessageProps = feedbackMessage(feedback, CONVERSATION_ID); |
| 67 | + |
| 68 | + expect(message.role).toBe("bot"); |
| 69 | + expect(message.content).toMatch( |
| 70 | + "Thank you for your feedback. If you have more to share, please click the button below (_requires GitHub login_). " + |
| 71 | + "\n\n Do not include any personal information or other sensitive information in your feedback. " + |
| 72 | + "Feedback may be used to improve Red Hat's products or services.", |
| 73 | + ); |
| 74 | + expect(message.quickResponses).toEqual([ |
| 75 | + { |
| 76 | + content: "Sure!", |
| 77 | + id: "response", |
| 78 | + onClick: expect.any(Function), |
| 79 | + }, |
| 80 | + ]); |
| 81 | + // Check for the two line breaks |
| 82 | + expect(message.content).toMatch(/\n\n/); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe("useChatbot - fetchEventSource openWhenHidden", () => { |
| 87 | + beforeEach(() => { |
| 88 | + vi.clearAllMocks(); |
| 89 | + // Mock axios.get for health check |
| 90 | + vi.mocked(axios.get).mockResolvedValue({ |
| 91 | + status: 200, |
| 92 | + data: { "streaming-chatbot-service": "ok" }, |
| 93 | + }); |
| 94 | + // Mock fetchEventSource to resolve immediately |
| 95 | + vi.mocked(fetchEventSourceModule.fetchEventSource).mockResolvedValue( |
| 96 | + undefined, |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should call fetchEventSource with openWhenHidden set to true", async () => { |
| 101 | + const { result } = renderHook(() => useChatbot()); |
| 102 | + |
| 103 | + // Wait for the health check to complete and streaming to be enabled |
| 104 | + await waitFor(() => { |
| 105 | + expect(result.current.isStreamingSupported()).toBe(true); |
| 106 | + }); |
| 107 | + |
| 108 | + // Send a message to trigger fetchEventSource |
| 109 | + await result.current.handleSend("test query"); |
| 110 | + |
| 111 | + // Wait for fetchEventSource to be called |
| 112 | + await waitFor(() => { |
| 113 | + expect(fetchEventSourceModule.fetchEventSource).toHaveBeenCalled(); |
| 114 | + }); |
| 115 | + |
| 116 | + // Verify that fetchEventSource was called with openWhenHidden: true |
| 117 | + expect(fetchEventSourceModule.fetchEventSource).toHaveBeenCalledWith( |
| 118 | + expect.any(String), |
| 119 | + expect.objectContaining({ |
| 120 | + openWhenHidden: true, |
| 121 | + }), |
| 122 | + ); |
| 123 | + }); |
| 124 | +}); |
0 commit comments