dev1/tests/e2e/32-chat-support-thread-persist.spec.mjs

70 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// @ts-check
import { test, expect } from '@playwright/test';
import { loadTestUser } from '../utils/testUser.js';
test.describe('@p0 Chat drawer — existing thread preload', () => {
test.setTimeout(20000);
test('uses existing thread from /api/chat/threads and shows its messages (persists across reload)', async ({ page }) => {
const u = loadTestUser();
// ---- Stub: existing thread list + preload messages; block POST creation ----
const threadId = 'persist-thread-1';
let sawCreate = false;
await page.route('**/api/chat/threads', async (route) => {
if (route.request().method() === 'GET') {
return route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ threads: [{ id: threadId }] }),
});
}
if (route.request().method() === 'POST') {
sawCreate = true; // shouldnt happen when list is non-empty
return route.fulfill({ status: 409, contentType: 'application/json', body: '{}' });
}
return route.fallback();
});
await page.route(`**/api/chat/threads/${threadId}`, async (route) => {
if (route.request().method() === 'GET') {
return route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({
messages: [
{ role: 'assistant', content: 'Welcome back. How can I help?' },
{ role: 'user', content: 'Earlier question…' },
],
}),
});
}
return route.fallback();
});
// ---- Sign in ----
await page.context().clearCookies();
await page.goto('/signin', { waitUntil: 'networkidle' });
await page.getByPlaceholder('Username', { exact: true }).fill(u.username);
await page.getByPlaceholder('Password', { exact: true }).fill(u.password);
await page.getByRole('button', { name: /^Sign In$/ }).click();
await page.waitForURL('**/signin-landing**', { timeout: 15000 });
// Open chat via FAB → should show preloaded messages
const fab = page.getByRole('button', { name: /^Open chat$/i });
await fab.click();
await expect(page.getByText(/^Welcome back\. How can I help\?/)).toBeVisible({ timeout: 8000 });
await expect(page.getByText(/^Earlier question…$/)).toBeVisible();
// No creation attempted
expect(sawCreate).toBeFalsy();
// Reload the page and open again → same preload visible
await page.reload({ waitUntil: 'networkidle' });
await fab.click();
await expect(page.getByText(/^Welcome back\. How can I help\?/)).toBeVisible({ timeout: 8000 });
});
});