Skip to content

Commit 65ae941

Browse files
authored
Replace AI Chat generated content in e2e (#3833)
1 parent 75b8efa commit 65ae941

File tree

2 files changed

+29
-35
lines changed

2 files changed

+29
-35
lines changed

packages/gitbook/e2e/internal.spec.ts

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,29 @@ import {
3939

4040
const AI_PROMPT = `You're being invoked by the GitBook CI/CD pipeline. To make screenshot testing of the GitBook Assistant visually consistent, look up the title of the first page you find and respond with only EXACTLY its title. To find the page title, invoke the search tool with the query "GitBook". Before invoking the search tool, respond with the exact text: "I'm going to look up 'GitBook' and then respond with only the page title.". Do not execute any other tools or output any other text.`;
4141

42+
const overrideAIInitialState = () => {
43+
const greeting = document.querySelector('[data-testid="ai-chat-time-greeting"]');
44+
if (greeting) {
45+
greeting.textContent = 'Good morning';
46+
}
47+
};
48+
const overrideAIResponse = () => {
49+
const userMessage = document.querySelector('[data-testid="ai-chat-message-user"]');
50+
if (userMessage) {
51+
userMessage.textContent = '[Replaced message] Chat message sent by the user';
52+
}
53+
const assistantMessage = document.querySelectorAll(
54+
'[data-testid="ai-chat-message-assistant"] .ai-response-document'
55+
);
56+
assistantMessage.forEach((message) => {
57+
message.innerHTML = '[Replaced message] AI chat response';
58+
});
59+
const suggestions = document.querySelectorAll('[data-testid="ai-chat-followup-suggestion"]');
60+
suggestions.forEach((suggestion) => {
61+
suggestion.textContent = 'Follow-up suggestion';
62+
});
63+
};
64+
4265
const searchTestCases: Test[] = [
4366
{
4467
name: 'Search - AI Mode: None - Complete flow',
@@ -147,14 +170,7 @@ const searchTestCases: Test[] = [
147170
timeout: 60_000,
148171
});
149172
// Override text content for visual consistency in screenshots
150-
await page.evaluate(() => {
151-
const suggestions = document.querySelectorAll(
152-
'[data-testid="ai-chat-followup-suggestion"]'
153-
);
154-
suggestions.forEach((suggestion) => {
155-
suggestion.textContent = 'Follow-up suggestion';
156-
});
157-
});
173+
await page.evaluate(overrideAIResponse);
158174
},
159175
},
160176
{
@@ -169,12 +185,7 @@ const searchTestCases: Test[] = [
169185
await expect(page.getByTestId('ai-chat')).toBeVisible();
170186
await expect(page.getByTestId('ai-chat-input')).toBeFocused();
171187
// Override text content for visual consistency in screenshots
172-
await page.evaluate(() => {
173-
const greeting = document.querySelector('[data-testid="ai-chat-time-greeting"]');
174-
if (greeting) {
175-
greeting.textContent = 'Good morning';
176-
}
177-
});
188+
await page.evaluate(overrideAIInitialState);
178189
},
179190
},
180191
{
@@ -190,12 +201,7 @@ const searchTestCases: Test[] = [
190201
await expect(page.getByTestId('ai-chat')).toBeVisible();
191202
await expect(page.getByTestId('ai-chat-input')).toBeFocused();
192203
// Override text content for visual consistency in screenshots
193-
await page.evaluate(() => {
194-
const greeting = document.querySelector('[data-testid="ai-chat-time-greeting"]');
195-
if (greeting) {
196-
greeting.textContent = 'Good morning';
197-
}
198-
});
204+
await page.evaluate(overrideAIInitialState);
199205
},
200206
},
201207
{
@@ -211,12 +217,7 @@ const searchTestCases: Test[] = [
211217
await expect(page.getByTestId('ai-chat')).toBeVisible();
212218
await expect(page.getByTestId('ai-chat-input')).toBeFocused();
213219
// Override text content for visual consistency in screenshots
214-
await page.evaluate(() => {
215-
const greeting = document.querySelector('[data-testid="ai-chat-time-greeting"]');
216-
if (greeting) {
217-
greeting.textContent = 'Good morning';
218-
}
219-
});
220+
await page.evaluate(overrideAIInitialState);
220221
},
221222
},
222223
{
@@ -236,14 +237,7 @@ const searchTestCases: Test[] = [
236237
timeout: 60_000,
237238
});
238239
// Override text content for visual consistency in screenshots
239-
await page.evaluate(() => {
240-
const suggestions = document.querySelectorAll(
241-
'[data-testid="ai-chat-followup-suggestion"]'
242-
);
243-
suggestions.forEach((suggestion) => {
244-
suggestion.textContent = 'Follow-up suggestion';
245-
});
246-
});
240+
await page.evaluate(overrideAIResponse);
247241
},
248242
},
249243
];

packages/gitbook/src/components/AI/server-actions/AIMessageView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function AIMessageView(
3535
wrapBlocksInSuspense: false,
3636
withLinkPreviews,
3737
}}
38-
style="mt-2 space-y-4 *:origin-top-left *:animate-blur-in-slow"
38+
style="ai-response-document mt-2 space-y-4 *:origin-top-left *:animate-blur-in-slow"
3939
/>
4040

4141
{withToolCalls && step.toolCalls && step.toolCalls.length > 0 ? (

0 commit comments

Comments
 (0)