2025-09-20 12:47:10 -07:00
|
|
|
import fs from "node:fs";
|
|
|
|
|
import path from "node:path";
|
|
|
|
|
import { expect, type Page } from "@playwright/test";
|
|
|
|
|
import { chatModels } from "@/lib/ai/models";
|
|
|
|
|
|
|
|
|
|
const CHAT_ID_REGEX =
|
|
|
|
|
/^http:\/\/localhost:3000\/chat\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
2025-03-09 21:02:19 -07:00
|
|
|
|
|
|
|
|
export class ChatPage {
|
2025-09-20 12:47:10 -07:00
|
|
|
private readonly page: Page;
|
|
|
|
|
|
|
|
|
|
constructor(page: Page) {
|
|
|
|
|
this.page = page;
|
|
|
|
|
}
|
2025-03-09 21:02:19 -07:00
|
|
|
|
2025-09-20 12:47:10 -07:00
|
|
|
get sendButton() {
|
|
|
|
|
return this.page.getByTestId("send-button");
|
2025-03-09 21:02:19 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-20 12:47:10 -07:00
|
|
|
get stopButton() {
|
|
|
|
|
return this.page.getByTestId("stop-button");
|
2025-03-09 21:02:19 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-20 12:47:10 -07:00
|
|
|
get multimodalInput() {
|
|
|
|
|
return this.page.getByTestId("multimodal-input");
|
2025-03-09 21:02:19 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-20 12:47:10 -07:00
|
|
|
get scrollContainer() {
|
|
|
|
|
return this.page.locator(".overflow-y-scroll");
|
2025-05-01 02:28:24 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-20 12:47:10 -07:00
|
|
|
get scrollToBottomButton() {
|
|
|
|
|
return this.page.getByTestId("scroll-to-bottom-button");
|
2025-05-01 02:28:24 -07:00
|
|
|
}
|
|
|
|
|
|
2025-03-09 21:02:19 -07:00
|
|
|
async createNewChat() {
|
2025-09-20 12:47:10 -07:00
|
|
|
await this.page.goto("/");
|
2025-03-09 21:02:19 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-20 12:47:10 -07:00
|
|
|
getCurrentURL(): string {
|
2025-03-09 21:02:19 -07:00
|
|
|
return this.page.url();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async sendUserMessage(message: string) {
|
2025-03-11 14:39:36 -07:00
|
|
|
await this.multimodalInput.click();
|
|
|
|
|
await this.multimodalInput.fill(message);
|
|
|
|
|
await this.sendButton.click();
|
2025-03-09 21:02:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async isGenerationComplete() {
|
2025-09-20 12:47:10 -07:00
|
|
|
const response = await this.page.waitForResponse((currentResponse) =>
|
|
|
|
|
currentResponse.url().includes("/api/chat")
|
2025-03-11 14:39:36 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await response.finished();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async isVoteComplete() {
|
2025-09-20 12:47:10 -07:00
|
|
|
const response = await this.page.waitForResponse((currentResponse) =>
|
|
|
|
|
currentResponse.url().includes("/api/vote")
|
2025-03-11 14:39:36 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await response.finished();
|
2025-03-09 21:02:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async hasChatIdInUrl() {
|
2025-09-20 12:47:10 -07:00
|
|
|
await expect(this.page).toHaveURL(CHAT_ID_REGEX);
|
2025-03-09 21:02:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async sendUserMessageFromSuggestion() {
|
|
|
|
|
await this.page
|
2025-09-20 12:47:10 -07:00
|
|
|
.getByRole("button", { name: "What are the advantages of" })
|
2025-03-09 21:02:19 -07:00
|
|
|
.click();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async isElementVisible(elementId: string) {
|
|
|
|
|
await expect(this.page.getByTestId(elementId)).toBeVisible();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async isElementNotVisible(elementId: string) {
|
|
|
|
|
await expect(this.page.getByTestId(elementId)).not.toBeVisible();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async addImageAttachment() {
|
2025-09-20 12:47:10 -07:00
|
|
|
this.page.on("filechooser", async (fileChooser) => {
|
2025-03-09 21:02:19 -07:00
|
|
|
const filePath = path.join(
|
|
|
|
|
process.cwd(),
|
2025-09-20 12:47:10 -07:00
|
|
|
"public",
|
|
|
|
|
"images",
|
|
|
|
|
"mouth of the seine, monet.jpg"
|
2025-03-09 21:02:19 -07:00
|
|
|
);
|
|
|
|
|
const imageBuffer = fs.readFileSync(filePath);
|
|
|
|
|
|
|
|
|
|
await fileChooser.setFiles({
|
2025-09-20 12:47:10 -07:00
|
|
|
name: "mouth of the seine, monet.jpg",
|
|
|
|
|
mimeType: "image/jpeg",
|
2025-03-09 21:02:19 -07:00
|
|
|
buffer: imageBuffer,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-20 12:47:10 -07:00
|
|
|
await this.page.getByTestId("attachments-button").click();
|
2025-03-09 21:02:19 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-20 12:47:10 -07:00
|
|
|
async getSelectedModel() {
|
|
|
|
|
const modelId = await this.page.getByTestId("model-selector").innerText();
|
2025-03-09 21:02:19 -07:00
|
|
|
return modelId;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 12:47:10 -07:00
|
|
|
async chooseModelFromSelector(chatModelId: string) {
|
2025-03-09 21:02:19 -07:00
|
|
|
const chatModel = chatModels.find(
|
2025-09-20 12:47:10 -07:00
|
|
|
(currentChatModel) => currentChatModel.id === chatModelId
|
2025-03-09 21:02:19 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!chatModel) {
|
|
|
|
|
throw new Error(`Model with id ${chatModelId} not found`);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 12:47:10 -07:00
|
|
|
await this.page.getByTestId("model-selector").click();
|
2025-03-09 21:02:19 -07:00
|
|
|
await this.page.getByTestId(`model-selector-item-${chatModelId}`).click();
|
|
|
|
|
expect(await this.getSelectedModel()).toBe(chatModel.name);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 12:47:10 -07:00
|
|
|
async getSelectedVisibility() {
|
2025-05-01 17:47:48 -07:00
|
|
|
const visibilityId = await this.page
|
2025-09-20 12:47:10 -07:00
|
|
|
.getByTestId("visibility-selector")
|
2025-05-01 17:47:48 -07:00
|
|
|
.innerText();
|
|
|
|
|
return visibilityId;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 12:47:10 -07:00
|
|
|
async chooseVisibilityFromSelector(chatVisibility: "public" | "private") {
|
|
|
|
|
await this.page.getByTestId("visibility-selector").click();
|
2025-05-01 17:47:48 -07:00
|
|
|
await this.page
|
|
|
|
|
.getByTestId(`visibility-selector-item-${chatVisibility}`)
|
|
|
|
|
.click();
|
|
|
|
|
expect(await this.getSelectedVisibility()).toBe(chatVisibility);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-09 21:02:19 -07:00
|
|
|
async getRecentAssistantMessage() {
|
|
|
|
|
const messageElements = await this.page
|
2025-09-20 12:47:10 -07:00
|
|
|
.getByTestId("message-assistant")
|
2025-03-09 21:02:19 -07:00
|
|
|
.all();
|
2025-09-20 12:47:10 -07:00
|
|
|
const lastMessageElement = messageElements.at(-1);
|
|
|
|
|
|
|
|
|
|
if (!lastMessageElement) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2025-03-09 21:02:19 -07:00
|
|
|
|
|
|
|
|
const content = await lastMessageElement
|
2025-09-20 12:47:10 -07:00
|
|
|
.getByTestId("message-content")
|
2025-03-09 21:02:19 -07:00
|
|
|
.innerText()
|
|
|
|
|
.catch(() => null);
|
|
|
|
|
|
|
|
|
|
const reasoningElement = await lastMessageElement
|
2025-09-20 12:47:10 -07:00
|
|
|
.getByTestId("message-reasoning")
|
2025-03-09 21:02:19 -07:00
|
|
|
.isVisible()
|
|
|
|
|
.then(async (visible) =>
|
|
|
|
|
visible
|
|
|
|
|
? await lastMessageElement
|
2025-09-20 12:47:10 -07:00
|
|
|
.getByTestId("message-reasoning")
|
2025-03-09 21:02:19 -07:00
|
|
|
.innerText()
|
2025-09-20 12:47:10 -07:00
|
|
|
: null
|
2025-03-09 21:02:19 -07:00
|
|
|
)
|
|
|
|
|
.catch(() => null);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
element: lastMessageElement,
|
|
|
|
|
content,
|
|
|
|
|
reasoning: reasoningElement,
|
|
|
|
|
async toggleReasoningVisibility() {
|
|
|
|
|
await lastMessageElement
|
2025-09-20 12:47:10 -07:00
|
|
|
.getByTestId("message-reasoning-toggle")
|
2025-03-09 21:02:19 -07:00
|
|
|
.click();
|
|
|
|
|
},
|
2025-03-11 14:39:36 -07:00
|
|
|
async upvote() {
|
2025-09-20 12:47:10 -07:00
|
|
|
await lastMessageElement.getByTestId("message-upvote").click();
|
2025-03-11 14:39:36 -07:00
|
|
|
},
|
|
|
|
|
async downvote() {
|
2025-09-20 12:47:10 -07:00
|
|
|
await lastMessageElement.getByTestId("message-downvote").click();
|
2025-03-11 14:39:36 -07:00
|
|
|
},
|
2025-03-09 21:02:19 -07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getRecentUserMessage() {
|
2025-09-20 12:47:10 -07:00
|
|
|
const messageElements = await this.page.getByTestId("message-user").all();
|
2025-04-29 00:20:10 -07:00
|
|
|
const lastMessageElement = messageElements.at(-1);
|
|
|
|
|
|
|
|
|
|
if (!lastMessageElement) {
|
2025-09-20 12:47:10 -07:00
|
|
|
throw new Error("No user message found");
|
2025-04-29 00:20:10 -07:00
|
|
|
}
|
2025-03-09 21:02:19 -07:00
|
|
|
|
2025-04-28 23:18:02 -07:00
|
|
|
const content = await lastMessageElement
|
2025-09-20 12:47:10 -07:00
|
|
|
.getByTestId("message-content")
|
2025-04-28 23:18:02 -07:00
|
|
|
.innerText()
|
|
|
|
|
.catch(() => null);
|
2025-03-09 21:02:19 -07:00
|
|
|
|
|
|
|
|
const hasAttachments = await lastMessageElement
|
2025-09-20 12:47:10 -07:00
|
|
|
.getByTestId("message-attachments")
|
2025-03-09 21:02:19 -07:00
|
|
|
.isVisible()
|
|
|
|
|
.catch(() => false);
|
|
|
|
|
|
|
|
|
|
const attachments = hasAttachments
|
2025-09-20 12:47:10 -07:00
|
|
|
? await lastMessageElement.getByTestId("message-attachments").all()
|
2025-03-09 21:02:19 -07:00
|
|
|
: [];
|
|
|
|
|
|
|
|
|
|
const page = this.page;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
element: lastMessageElement,
|
|
|
|
|
content,
|
|
|
|
|
attachments,
|
|
|
|
|
async edit(newMessage: string) {
|
2025-09-20 12:47:10 -07:00
|
|
|
await page.getByTestId("message-edit-button").click();
|
|
|
|
|
await page.getByTestId("message-editor").fill(newMessage);
|
|
|
|
|
await page.getByTestId("message-editor-send-button").click();
|
2025-03-09 21:02:19 -07:00
|
|
|
await expect(
|
2025-09-20 12:47:10 -07:00
|
|
|
page.getByTestId("message-editor-send-button")
|
2025-03-09 21:02:19 -07:00
|
|
|
).not.toBeVisible();
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
2025-04-25 23:40:15 -07:00
|
|
|
|
|
|
|
|
async expectToastToContain(text: string) {
|
2025-09-20 12:47:10 -07:00
|
|
|
await expect(this.page.getByTestId("toast")).toContainText(text);
|
2025-04-25 23:40:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async openSideBar() {
|
2025-09-20 12:47:10 -07:00
|
|
|
const sidebarToggleButton = this.page.getByTestId("sidebar-toggle-button");
|
2025-04-25 23:40:15 -07:00
|
|
|
await sidebarToggleButton.click();
|
|
|
|
|
}
|
2025-05-01 02:28:24 -07:00
|
|
|
|
2025-09-20 12:47:10 -07:00
|
|
|
isScrolledToBottom(): Promise<boolean> {
|
2025-05-01 02:28:24 -07:00
|
|
|
return this.scrollContainer.evaluate(
|
2025-09-20 12:47:10 -07:00
|
|
|
(el) => Math.abs(el.scrollHeight - el.scrollTop - el.clientHeight) < 1
|
2025-05-01 02:28:24 -07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 12:47:10 -07:00
|
|
|
async waitForScrollToBottom(timeout = 5000): Promise<void> {
|
2025-05-01 02:28:24 -07:00
|
|
|
const start = Date.now();
|
|
|
|
|
|
|
|
|
|
while (Date.now() - start < timeout) {
|
2025-09-20 12:47:10 -07:00
|
|
|
if (await this.isScrolledToBottom()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-05-01 02:28:24 -07:00
|
|
|
await this.page.waitForTimeout(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Error(`Timed out waiting for scroll bottom after ${timeout}ms`);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 12:47:10 -07:00
|
|
|
async sendMultipleMessages(
|
2025-05-01 02:28:24 -07:00
|
|
|
count: number,
|
2025-09-20 12:47:10 -07:00
|
|
|
makeMessage: (i: number) => string
|
2025-05-01 02:28:24 -07:00
|
|
|
) {
|
|
|
|
|
for (let i = 0; i < count; i++) {
|
|
|
|
|
await this.sendUserMessage(makeMessage(i));
|
|
|
|
|
await this.isGenerationComplete();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 12:47:10 -07:00
|
|
|
async scrollToTop(): Promise<void> {
|
2025-05-01 02:28:24 -07:00
|
|
|
await this.scrollContainer.evaluate((element) => {
|
|
|
|
|
element.scrollTop = 0;
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-03-09 21:02:19 -07:00
|
|
|
}
|