-
Notifications
You must be signed in to change notification settings - Fork 2
/
AppTest.js
187 lines (170 loc) · 5.71 KB
/
AppTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { expect } from 'playwright/test';
import { chromium } from 'playwright';
import { promises as fs } from 'fs';
import { openScreenshot, url, model } from './helpers';
const DEBUG = process.env.DEBUG === 'true' || false;
const DARK_MODE = process.env.DARK_MODE === 'true' || false;
const VIDEO = process.env.VIDEO === 'true' || false;
const MOBILE = process.env.MOBILE === 'true' || false;
export const GITHUB_ACTIONS = process.env.GITHUB_ACTIONS === 'true' || false;
export class AppTest {
async start() {
const viewportSize = { width: 1280, height: 720 };
const contextOptions = {};
const launchOptions = {
slowMo: 500,
};
if (DARK_MODE) {
contextOptions.forcedColors = 'active';
contextOptions.colorScheme = 'dark';
}
if (VIDEO) {
contextOptions.recordVideo = {
dir: 'recordings',
size: viewportSize,
};
}
this.browser = await chromium.launch(launchOptions);
this.context = await this.browser.newContext(contextOptions);
this.page = await this.context.newPage();
this.page.on('console', (message) => {
if (DEBUG) {
console.debug(message);
}
if (message.type() === 'error') {
throw Error(`Console error: ${message.text()}`);
}
});
if (MOBILE) {
this.mobileDevice();
}
await this.page.goto('http://localhost:1234');
this.sidebar = this.page.locator('#sidebar');
}
async mobileDevice() {
const height = 375;
const width = 667;
await this.page.setViewportSize({ width, height });
}
async close(title) {
await this.browser.close();
const video = await this.page.video();
const path = await video?.path();
if (path) {
console.log(` ✔ Video ${path}: ${title}`);
if (title === 'Send message') {
console.log(` ✔ Updated demo`);
const destinationPath = 'videos/video.webm';
await fs.copyFile(path, destinationPath);
console.log(` ✔ ${title} video copied to ${destinationPath}`);
}
}
}
async showSettings() {
// await this.page.click('#chats-menu-button')
await this.screenshot();
await this.page.click('.settings-button');
await expect(this.page.locator('#settings-dialog')).toBeVisible();
}
async updateSettings(url, model) {
await this.showSettings();
// Fill in URL
const urlInput = this.page.locator('#settings-dialog #input-url');
await urlInput.fill(url);
if (model && model !== '') {
// Refresh model list
await this.page
.locator('#settings-dialog .refresh-models-button')
.click();
// Select model from list
// await this.screenshot()
// this.page.locator('#model-list').getByText('mistral:latest').click()
// Wait for the element to be visible
const listItem = this.page.locator(
'#settings-dialog #model-list .list-item',
{
hasText: model,
},
);
await listItem.waitFor({ state: 'visible' });
// Click the element
await listItem.click();
}
// Save settings
await this.page.locator('#settings-dialog .button-close').click();
}
async sendMessage(message) {
await this.page.fill('#message-input', message);
// await this.page.click('#send-button');
await this.page.keyboard.press('Enter');
await expect(this.page.locator('#abort-button')).toBeVisible();
// await this.screenshot('1.png');
// Wait for response
// await this.page.waitForTimeout(3000);
await this.page.waitForSelector('#message-input', { state: 'visible' });
//await this.page.waitForSelector('#send-button', { timeout: 180000 });
// await this.screenshot('2.png');
}
async newChat(title) {
await this.page.click('#new-chat-button');
const newTitle = /Untitled/;
await expect(
this.page.locator('.chat-list-item.selected .chat-title'),
).toHaveText(newTitle);
await expect(this.page.locator('#chat-title')).toHaveText(newTitle);
await this.editChatTitle(title);
}
async editChatTitle(title) {
await this.page.click('#chat-title');
await this.page.fill('#chat-title', title);
await this.pressEnter();
// await this.screenshot();
await expect(
this.page.locator('.chat-list-item.selected .chat-title'),
).toHaveText(title);
await expect(this.page.locator('#chat-title')).toHaveText(title);
}
async pressEnter() {
await this.page.keyboard.press('Enter');
}
async hover(selector) {
await this.page.hover(selector);
}
async screenshot(path) {
if (process.env.GITHUB_ACTIONS === 'true') {
// console.log('Running in GitHub Actions, skipping screenshot')
return false;
}
if (path === undefined) {
path = 'screenshot.png';
}
if (MOBILE) {
path = `mobile-${path}`;
}
if (DARK_MODE) {
path = `darkmode-${path}`;
}
path = `screenshots/${path}`;
await this.page.screenshot({ path });
console.log(` ✔ Screenshot ${path}`);
if (process.env.OPEN_SCREENSHOT === 'true') {
openScreenshot(path);
}
}
async selectChat(title) {
const selector = `.chat-list-item:has-text("${title}")`;
await this.page.hover(selector);
const listItem = this.page.locator(selector);
await listItem.click();
await expect(listItem).toHaveClass(/.*selected.*/);
await expect(this.page.locator('#chat-title')).toHaveText(title);
}
async deleteChat(title) {
const selector = `.chat-list-item:has-text("${title}")`;
await this.page.locator(selector).click();
await this.page.click('#chat-menu-button');
await this.page.click('#delete-chat-button');
await expect(this.page.locator(selector)).not.toBeVisible();
await expect(this.page.locator('#chat-title')).toHaveText(/Untitled/);
}
}