7a0da7932b
OSV-Scanner (Scheduled) / scan-scheduled (push) Failing after 0s
Create Release / test-gate (push) Has been cancelled
Create Release / release-gate (push) Has been cancelled
Create Release / ci-gate (push) Has been cancelled
Create Release / version-check (push) Has been cancelled
Create Release / e2e-test-gate (push) Has been cancelled
Create Release / responsive-test-gate (push) Has been cancelled
Create Release / compat-test-gate (push) Has been cancelled
Create Release / compose-integration-gate (push) Has been cancelled
Create Release / vulture-gate (push) Has been cancelled
Create Release / build (push) Has been cancelled
Create Release / provenance (push) Has been cancelled
Create Release / prerelease-docker (push) Has been cancelled
Create Release / publish-docker (push) Has been cancelled
Create Release / create-release (push) Has been cancelled
Create Release / cleanup-changelog (push) Has been cancelled
Create Release / trigger-pypi (push) Has been cancelled
Create Release / monitor-pypi (push) Has been cancelled
Create Release / Clean up orphan prerelease tags and signatures (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-form] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-metrics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-workflow] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-core] (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [history-news] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [library] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [link-analytics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-core] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-lifecycle] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [error-benchmark] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) (push) Has been cancelled
Docker Tests (Consolidated) / Accessibility Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Unit Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Example Tests (push) Has been cancelled
Docker Tests (Consolidated) / Production Image Smoke Test (push) Has been cancelled
Docker Tests (Consolidated) / Infrastructure Tests (push) Has been cancelled
OSSF Scorecard / OSSF Security Scorecard Analysis (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [mobile] (push) Has been cancelled
Backwards Compatibility / Verify Encryption Constants (push) Has been cancelled
Backwards Compatibility / PyPI Version Compatibility (push) Has been cancelled
Backwards Compatibility / Database Migration Tests (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Docker Tests (Consolidated) / detect-changes (push) Has been cancelled
Docker Tests (Consolidated) / Build Test Image (push) Has been cancelled
Docker Tests (Consolidated) / All Pytest Tests + Coverage (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [accessibility] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [api-crud] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-login] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-register] (push) Has been cancelled
296 lines
10 KiB
JavaScript
296 lines
10 KiB
JavaScript
/**
|
|
* E2E Test for Chat Mode In-Place Title Edit
|
|
*
|
|
* The pencil button (#edit-title-btn) is only rendered once a session has
|
|
* messages (showSessionButtons in chat.js). Clicking it pops a
|
|
* `window.prompt()` for the new title; on confirm, chat.js PATCHes the
|
|
* session and calls updateTitle() on success.
|
|
*
|
|
* Verified here:
|
|
* - Pencil is hidden on a fresh welcome screen.
|
|
* - After a session exists, the pencil becomes visible.
|
|
* - Clicking it (with window.prompt stubbed) issues a PATCH and the
|
|
* <h1 id="chat-title"> text updates.
|
|
*
|
|
* No LLM required: we create the session + first message via API with
|
|
* trigger_research=false, then drive the pencil flow.
|
|
*
|
|
* Prerequisites: Web server on http://127.0.0.1:5000 (override BASE_URL).
|
|
*/
|
|
|
|
const puppeteer = require('puppeteer');
|
|
const AuthHelper = require('../auth_helper');
|
|
const { getPuppeteerLaunchOptions } = require('../puppeteer_config');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const BASE_URL = process.env.BASE_URL || 'http://127.0.0.1:5000';
|
|
const isCI = !!process.env.CI;
|
|
|
|
const TIMEOUTS = {
|
|
navigation: isCI ? 60000 : 30000,
|
|
selector: isCI ? 30000 : 10000,
|
|
element: isCI ? 10000 : 5000,
|
|
};
|
|
|
|
const SCREENSHOTS_DIR = path.join(__dirname, '..', 'screenshots');
|
|
|
|
async function snap(page, name) {
|
|
if (!fs.existsSync(SCREENSHOTS_DIR)) {
|
|
fs.mkdirSync(SCREENSHOTS_DIR, { recursive: true });
|
|
}
|
|
try {
|
|
await page.screenshot({
|
|
path: path.join(
|
|
SCREENSHOTS_DIR,
|
|
`chat_edit_title_${name}_${Date.now()}.png`
|
|
),
|
|
fullPage: true,
|
|
});
|
|
} catch (_) {}
|
|
}
|
|
|
|
async function getCsrf(page) {
|
|
return page.evaluate(() => {
|
|
const m = document.querySelector('meta[name="csrf-token"]');
|
|
return m ? m.content : '';
|
|
});
|
|
}
|
|
|
|
async function api(page, csrfOuter, urlOuter, methodOuter = 'GET', bodyOuter = null) {
|
|
return page.evaluate(
|
|
async ({ url, method, body, csrf }) => {
|
|
const opts = {
|
|
method,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-CSRFToken': csrf,
|
|
},
|
|
};
|
|
if (body !== null) opts.body = JSON.stringify(body);
|
|
const r = await fetch(url, opts);
|
|
let data = null;
|
|
try {
|
|
data = await r.json();
|
|
} catch (_) {}
|
|
return { ok: r.ok, status: r.status, data };
|
|
},
|
|
{ url: urlOuter, method: methodOuter, body: bodyOuter, csrf: csrfOuter }
|
|
);
|
|
}
|
|
|
|
async function run() {
|
|
console.log(`Running chat edit-title tests (CI mode: ${isCI})`);
|
|
|
|
const browser = await puppeteer.launch(getPuppeteerLaunchOptions());
|
|
const page = await browser.newPage();
|
|
await page.setViewport({ width: 1280, height: 800 });
|
|
if (isCI) {
|
|
page.setDefaultTimeout(60000);
|
|
page.setDefaultNavigationTimeout(60000);
|
|
}
|
|
|
|
page.on('pageerror', (e) => console.log('PAGE ERROR:', e.message));
|
|
page.on('console', (m) => {
|
|
if (m.type() === 'error') console.log('BROWSER ERROR:', m.text());
|
|
});
|
|
|
|
const auth = new AuthHelper(page, BASE_URL);
|
|
|
|
let passed = 0;
|
|
let failed = 0;
|
|
|
|
try {
|
|
await auth.ensureAuthenticated();
|
|
|
|
// Land on chat page to get CSRF.
|
|
await page.goto(`${BASE_URL}/chat/`, {
|
|
waitUntil: 'domcontentloaded',
|
|
timeout: TIMEOUTS.navigation,
|
|
});
|
|
await page.waitForSelector('.ldr-chat-container', { timeout: TIMEOUTS.selector });
|
|
const csrf = await getCsrf(page);
|
|
|
|
// A bare /chat/ visit auto-resumes the most-recent session, which
|
|
// shows the pencil (edit-title) button and would break the "hidden
|
|
// on a fresh welcome" assertion below. Wait for chat.js init to
|
|
// settle, then force a clean New Chat state so the welcome screen is
|
|
// reliably shown regardless of what earlier tests in the shard (or a
|
|
// CI retry) left behind.
|
|
await page.waitForSelector('#chat-input[data-init-complete="true"]', {
|
|
timeout: TIMEOUTS.selector,
|
|
});
|
|
await page.evaluate(() => window.chatComponent.startNewChat());
|
|
await page.waitForFunction(
|
|
() => {
|
|
const el = document.getElementById('chat-welcome');
|
|
return el && getComputedStyle(el).display !== 'none';
|
|
},
|
|
{ timeout: TIMEOUTS.selector }
|
|
);
|
|
|
|
// Test 1: pencil hidden on a fresh welcome (no session)
|
|
console.log('Test 1: edit-title button hidden on fresh chat');
|
|
try {
|
|
const hidden = await page.$eval(
|
|
'#edit-title-btn',
|
|
(el) => getComputedStyle(el).display === 'none'
|
|
);
|
|
if (!hidden) throw new Error('Pencil visible before any session');
|
|
console.log('PASSED');
|
|
passed++;
|
|
} catch (e) {
|
|
console.log(`FAILED: ${e.message}`);
|
|
await snap(page, 'pencil_initial');
|
|
failed++;
|
|
}
|
|
|
|
// Create a session via API; populate at least one message so the
|
|
// pencil becomes visible after we navigate into the session.
|
|
const created = await api(page, csrf, '/api/chat/sessions', 'POST', {
|
|
initial_query: 'edit-title-test seed',
|
|
});
|
|
if (!created.ok || !created.data?.success) {
|
|
throw new Error(
|
|
`Session creation failed: ${created.status} ${JSON.stringify(created.data)}`
|
|
);
|
|
}
|
|
const sessionId = created.data.session_id;
|
|
await api(
|
|
page,
|
|
csrf,
|
|
`/api/chat/sessions/${sessionId}/messages`,
|
|
'POST',
|
|
{ content: 'hello', trigger_research: false }
|
|
);
|
|
|
|
// Test 2: navigating to /chat/<id> shows the pencil
|
|
console.log('Test 2: edit-title button visible inside a session');
|
|
try {
|
|
await page.goto(`${BASE_URL}/chat/${sessionId}`, {
|
|
waitUntil: 'domcontentloaded',
|
|
timeout: TIMEOUTS.navigation,
|
|
});
|
|
await page.waitForSelector('.ldr-chat-container', {
|
|
timeout: TIMEOUTS.selector,
|
|
});
|
|
// The pencil flip happens asynchronously inside loadSession —
|
|
// wait for the inline-block style instead of querying once.
|
|
await page.waitForFunction(
|
|
() => {
|
|
const el = document.getElementById('edit-title-btn');
|
|
return el && getComputedStyle(el).display !== 'none';
|
|
},
|
|
{ timeout: TIMEOUTS.selector }
|
|
);
|
|
console.log('PASSED');
|
|
passed++;
|
|
} catch (e) {
|
|
console.log(`FAILED: ${e.message}`);
|
|
await snap(page, 'pencil_in_session');
|
|
failed++;
|
|
}
|
|
|
|
// Test 3: clicking the pencil with a stubbed prompt() updates the
|
|
// title both in the DOM and on the server.
|
|
console.log('Test 3: pencil click PATCHes title and updates DOM');
|
|
try {
|
|
const newTitle = `Edited title ${Date.now()}`;
|
|
|
|
// Stub window.prompt so the click can proceed unattended.
|
|
await page.evaluate((t) => {
|
|
window.prompt = () => t;
|
|
}, newTitle);
|
|
|
|
// Click via evaluate so headless interactability heuristics
|
|
// can't refuse a small button at the page's top-right.
|
|
await page.evaluate(() => {
|
|
document.getElementById('edit-title-btn').click();
|
|
});
|
|
|
|
// Wait for the H1 text to flip — chat.js calls updateTitle on
|
|
// a successful PATCH.
|
|
await page.waitForFunction(
|
|
(expected) =>
|
|
(document.getElementById('chat-title').textContent || '').trim() ===
|
|
expected,
|
|
{ timeout: TIMEOUTS.selector },
|
|
newTitle
|
|
);
|
|
|
|
// Also verify the server actually persisted it by GETting back.
|
|
const fetched = await api(
|
|
page,
|
|
csrf,
|
|
`/api/chat/sessions/${sessionId}`
|
|
);
|
|
const serverTitle = fetched.data?.session?.title;
|
|
if (serverTitle !== newTitle) {
|
|
throw new Error(
|
|
`Server title mismatch: expected ${JSON.stringify(newTitle)}, got ${JSON.stringify(serverTitle)}`
|
|
);
|
|
}
|
|
console.log('PASSED');
|
|
passed++;
|
|
} catch (e) {
|
|
console.log(`FAILED: ${e.message}`);
|
|
await snap(page, 'patch_title');
|
|
failed++;
|
|
}
|
|
|
|
// Test 4: cancelling the prompt (returning null) leaves the title
|
|
// alone — no PATCH issued.
|
|
console.log('Test 4: cancelling prompt leaves title unchanged');
|
|
try {
|
|
const titleBefore = await page.$eval(
|
|
'#chat-title',
|
|
(h) => (h.textContent || '').trim()
|
|
);
|
|
|
|
await page.evaluate(() => {
|
|
window.prompt = () => null;
|
|
});
|
|
await page.evaluate(() => {
|
|
document.getElementById('edit-title-btn').click();
|
|
});
|
|
// Wait for the network to be quiet — if the cancel was
|
|
// (incorrectly) treated as a rename, a PATCH would have
|
|
// fired and we'd see network activity. waitForNetworkIdle
|
|
// is deterministic vs. a wall-clock setTimeout.
|
|
await page
|
|
.waitForNetworkIdle({ idleTime: 500, timeout: TIMEOUTS.element })
|
|
.catch(() => {});
|
|
const titleAfter = await page.$eval(
|
|
'#chat-title',
|
|
(h) => (h.textContent || '').trim()
|
|
);
|
|
if (titleAfter !== titleBefore) {
|
|
throw new Error(
|
|
`Title changed despite cancel: ${JSON.stringify(titleBefore)} -> ${JSON.stringify(titleAfter)}`
|
|
);
|
|
}
|
|
console.log('PASSED');
|
|
passed++;
|
|
} catch (e) {
|
|
console.log(`FAILED: ${e.message}`);
|
|
await snap(page, 'cancel_prompt');
|
|
failed++;
|
|
}
|
|
} catch (e) {
|
|
console.log(`Test suite error: ${e.message}`);
|
|
failed++;
|
|
} finally {
|
|
await browser.close();
|
|
}
|
|
|
|
console.log('-'.repeat(50));
|
|
console.log(`Chat Edit-Title Tests — passed: ${passed}, failed: ${failed}`);
|
|
console.log('-'.repeat(50));
|
|
if (failed > 0) process.exit(1);
|
|
}
|
|
|
|
run().catch((e) => {
|
|
console.error('Test runner error:', e);
|
|
process.exit(1);
|
|
});
|