Files
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 13:08:55 +08:00

254 lines
8.6 KiB
JavaScript

/**
* E2E Test for Chat Mode Reload Persistence
*
* The chat schema guarantees that ChatMessage.content is stored
* inline (NOT NULL) and `get_session_messages` returns the persisted
* messages on session reload. The step-persistence dedup/emit symmetry
* ensures what users see live == what reload reconstructs.
*
* Verified here:
* - Create a session + insert a user message via API.
* - Navigate to /chat/<session_id> with a fresh page load.
* - The previously-sent message bubble appears in the rendered DOM.
* - Reloading again still renders the same bubble (idempotent).
*
* No LLM required: messages are seeded via the API with
* trigger_research=false.
*
* 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,
};
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_reload_${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 countUserBubbles(page, needle) {
return page.$$eval(
'.ldr-chat-message-user .ldr-chat-message-text',
(els, n) =>
els.filter((e) => (e.textContent || '').includes(n)).length,
needle
);
}
async function run() {
console.log(`Running chat reload-persistence 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();
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);
// Seed: create session + two distinct user messages so we can
// verify ordering survives a reload.
const created = await api(page, csrf, '/api/chat/sessions', 'POST', {
initial_query: 'reload-persistence-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;
const probeA = `reload probe A ${Date.now()}`;
const probeB = `reload probe B ${Date.now()}`;
await api(
page,
csrf,
`/api/chat/sessions/${sessionId}/messages`,
'POST',
{ content: probeA, trigger_research: false }
);
await api(
page,
csrf,
`/api/chat/sessions/${sessionId}/messages`,
'POST',
{ content: probeB, trigger_research: false }
);
// Test 1: navigating to /chat/<id> renders both seeded messages.
console.log('Test 1: fresh load of session renders seeded messages');
try {
await page.goto(`${BASE_URL}/chat/${sessionId}`, {
waitUntil: 'domcontentloaded',
timeout: TIMEOUTS.navigation,
});
await page.waitForSelector('.ldr-chat-container', {
timeout: TIMEOUTS.selector,
});
// Wait for both bubbles. loadSession is async.
await page.waitForFunction(
({ a, b }) => {
const nodes = document.querySelectorAll(
'.ldr-chat-message-user .ldr-chat-message-text'
);
const texts = Array.from(nodes).map((n) => n.textContent || '');
return (
texts.some((t) => t.includes(a)) &&
texts.some((t) => t.includes(b))
);
},
{ timeout: TIMEOUTS.selector },
{ a: probeA, b: probeB }
);
console.log('PASSED');
passed++;
} catch (e) {
console.log(`FAILED: ${e.message}`);
await snap(page, 'fresh_load');
failed++;
}
// Test 2: reload the page again — messages still there, exactly
// one bubble each (no double-rendering).
console.log('Test 2: reload renders messages exactly once');
try {
await page.reload({
waitUntil: 'domcontentloaded',
timeout: TIMEOUTS.navigation,
});
await page.waitForSelector('.ldr-chat-container', {
timeout: TIMEOUTS.selector,
});
await page.waitForFunction(
(needle) => {
const nodes = document.querySelectorAll(
'.ldr-chat-message-user .ldr-chat-message-text'
);
return Array.from(nodes).some((n) =>
(n.textContent || '').includes(needle)
);
},
{ timeout: TIMEOUTS.selector },
probeA
);
const aCount = await countUserBubbles(page, probeA);
const bCount = await countUserBubbles(page, probeB);
if (aCount !== 1 || bCount !== 1) {
throw new Error(
`Expected exactly one bubble each; got A=${aCount}, B=${bCount}`
);
}
console.log('PASSED');
passed++;
} catch (e) {
console.log(`FAILED: ${e.message}`);
await snap(page, 'reload');
failed++;
}
// Test 3: welcome screen is hidden on a session with messages
// (regression check for "empty session shows welcome").
console.log('Test 3: welcome hidden on session with messages');
try {
const hidden = await page.$eval(
'#chat-welcome',
(el) => !el || getComputedStyle(el).display === 'none'
);
if (!hidden) throw new Error('Welcome screen visible on session with messages');
console.log('PASSED');
passed++;
} catch (e) {
console.log(`FAILED: ${e.message}`);
await snap(page, 'welcome_hidden');
failed++;
}
} catch (e) {
console.log(`Test suite error: ${e.message}`);
failed++;
} finally {
await browser.close();
}
console.log('-'.repeat(50));
console.log(`Chat Reload Persistence 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);
});