| 1 | import { screen, waitFor } from "@testing-library/dom"; |
| 2 | import { act } from "@testing-library/react"; |
| 3 | import { Editor } from "@tiptap/core"; |
| 4 | import { ChatMessage } from "core"; |
| 5 | import { MockIdeMessenger } from "../../context/MockIdeMessenger"; |
| 6 | |
| 7 | function logDomText() { |
| 8 | const bodyText = document.body.innerHTML; |
| 9 | const parser = new DOMParser(); |
| 10 | const doc = parser.parseFromString(bodyText, "text/html"); |
| 11 | let cleaned = doc.body.textContent ?? ""; |
| 12 | cleaned = cleaned?.replace( |
| 13 | "No resultsAaModelsRulesDocsPromptsToolsMCPCreate your first agent", |
| 14 | "", |
| 15 | ); |
| 16 | cleaned = cleaned?.replace( |
| 17 | /[⌘Ctrl]+⏎ (?:No )?[Aa]ctive file⏎ Enter⏎Log in to access a free trial of theModels Add-OnLog in to Continue HubOr, configure your own models/, |
| 18 | "", |
| 19 | ); |
| 20 | cleaned = cleaned.replaceAll("Select model", ""); |
| 21 | cleaned = cleaned.replaceAll("Mock LLM", ""); |
| 22 | cleaned = cleaned.replaceAll(/^Chat|Chat$/g, ""); |
| 23 | console.log(cleaned || "No body content"); |
| 24 | } |
| 25 | |
| 26 | export function logAllTestIds() { |
| 27 | const elements = document.querySelectorAll("*"); |
| 28 | |
| 29 | const testIds = Array.from(elements) |
| 30 | .map((el) => Array.from(el.attributes)) |
| 31 | .flat() |
| 32 | .filter((a) => a.name === "data-testid") |
| 33 | .map((a) => a.value); |
| 34 | console.log("Available test ids", testIds); |
| 35 | } |
| 36 | export async function getElementByTestId(testId: string): Promise<HTMLElement> { |
| 37 | try { |
| 38 | const testIdElement = await waitFor(() => screen.findByTestId(testId)); |
| 39 | expect(testIdElement).toBeInTheDocument(); |
| 40 | return testIdElement; |
| 41 | } catch (e) { |
| 42 | logAllTestIds(); |
| 43 | throw new Error(`Element with testId "${testId}" not found`); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | export async function verifyNotPresentByTestId(testId: string): Promise<void> { |
| 48 | const element = await waitFor(() => screen.queryByTestId(testId)); |
| 49 | expect(element).not.toBeInTheDocument(); |
| 50 | } |
| 51 | |
| 52 | export async function verifyNotPresentByText(text: string): Promise<void> { |
| 53 | const element = screen.queryByText(text); |
| 54 | expect(element).not.toBeInTheDocument(); |
| 55 | } |
| 56 | |
| 57 | export async function getElementByText(text: string): Promise<HTMLElement> { |
| 58 | try { |
| 59 | const textElement = await waitFor(() => screen.findByText(text)); |
| 60 | expect(textElement).toBeInTheDocument(); |
| 61 | return textElement; |
| 62 | } catch (e) { |
| 63 | logDomText(); |
| 64 | throw new Error(`Element with text "${text}" not found.`); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | export async function getMainEditor(): Promise<Editor> { |
| 69 | const editorElement = await getElementByTestId("editor-input-main"); |
| 70 | if ("editor" in editorElement) { |
| 71 | return editorElement.editor as Editor; |
| 72 | } else { |
| 73 | throw new Error("Main editor not found within editor container"); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | export async function sendInputWithMockedResponse( |
| 78 | ideMessenger: MockIdeMessenger, |
| 79 | input: string, |
| 80 | response: ChatMessage[], |
| 81 | ): Promise<void> { |
| 82 | // Setup the mock response before any actions |
| 83 | ideMessenger.chatResponse = response; |
| 84 | |
| 85 | // Wait for the editor to be in the document |
| 86 | const editor = await getMainEditor(); |
| 87 | |
| 88 | // Find the submit button |
| 89 | const sendButton = await getElementByTestId("submit-input-button"); |
| 90 | |
| 91 | // Insert content into the editor |
| 92 | await act(async () => { |
| 93 | editor.commands.insertContent(input); |
| 94 | }); |
| 95 | |
| 96 | await getElementByText(input); |
| 97 | |
| 98 | // Click the send button |
| 99 | await act(async () => { |
| 100 | sendButton.click(); |
| 101 | }); |
| 102 | } |