Files
learningcircuit--local-deep…/tests/ui_tests/chat/test_chat_archived_session_rejects.js
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

223 lines
7.5 KiB
JavaScript

/**
* E2E Test for Chat Mode Archived-Session Send Rejection
*
* send_message has a `status='active'` guard that returns
* HTTP 409 with a friendly "This chat is archived. Reactivate it to
* continue." error when a user tries to send into an archived (or
* deleted) session. There is also an atomic enforcement at the
* UPDATE...RETURNING level in insert_message_in_db, which we exercise
* indirectly via the API.
*
* This test:
* 1. Creates a session via API.
* 2. PATCHes status to "archived".
* 3. POSTs a message — expects HTTP 409 + error mentioning "archived".
* 4. Reactivates the session (status="active").
* 5. POSTs again — expects HTTP 200 (the post-archive guard releases).
*
* No LLM required: each test asserts on the HTTP envelope only and uses
* trigger_research=false to avoid background research spawning.
*
* 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 });
}
const file = path.join(
SCREENSHOTS_DIR,
`chat_archived_${name}_${Date.now()}.png`
);
try {
await page.screenshot({ path: file, 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 archived-session-reject 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 the chat page so we can pull the CSRF token from the
// meta tag (same flow as the existing session-management test).
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);
if (!csrf) {
console.log('FAILED: could not obtain CSRF token from chat page');
process.exit(1);
}
// Create a session.
const created = await api(page, csrf, '/api/chat/sessions', 'POST', {
initial_query: 'archive-guard probe',
});
if (!created.ok || !created.data?.success) {
throw new Error(
`Session creation failed: status=${created.status} body=${JSON.stringify(created.data)}`
);
}
const sessionId = created.data.session_id;
// Test 1: Archive then send → expect 409 + "archived" in error.
console.log('Test 1: archived session rejects send with 409');
try {
const archiveResp = await api(
page,
csrf,
`/api/chat/sessions/${sessionId}`,
'PATCH',
{ status: 'archived' }
);
if (!archiveResp.ok) {
throw new Error(
`Archive PATCH failed: status=${archiveResp.status} body=${JSON.stringify(archiveResp.data)}`
);
}
const sendResp = await api(
page,
csrf,
`/api/chat/sessions/${sessionId}/messages`,
'POST',
{ content: 'should not land', trigger_research: false }
);
if (sendResp.status !== 409) {
throw new Error(
`Expected 409, got status=${sendResp.status} body=${JSON.stringify(sendResp.data)}`
);
}
const errMsg = (sendResp.data?.error || '').toLowerCase();
if (!errMsg.includes('archived')) {
throw new Error(
`Expected error mentioning "archived", got: ${JSON.stringify(sendResp.data)}`
);
}
console.log('PASSED');
passed++;
} catch (e) {
console.log(`FAILED: ${e.message}`);
await snap(page, 'reject');
failed++;
}
// Test 2: Reactivate then send → expect 200.
console.log('Test 2: reactivated session accepts send');
try {
const reactivateResp = await api(
page,
csrf,
`/api/chat/sessions/${sessionId}`,
'PATCH',
{ status: 'active' }
);
if (!reactivateResp.ok) {
throw new Error(
`Reactivate PATCH failed: status=${reactivateResp.status} body=${JSON.stringify(reactivateResp.data)}`
);
}
const sendResp = await api(
page,
csrf,
`/api/chat/sessions/${sessionId}/messages`,
'POST',
{ content: 'should land now', trigger_research: false }
);
if (!sendResp.ok || !sendResp.data?.success) {
throw new Error(
`Expected success, got status=${sendResp.status} body=${JSON.stringify(sendResp.data)}`
);
}
console.log('PASSED');
passed++;
} catch (e) {
console.log(`FAILED: ${e.message}`);
await snap(page, 'reactivate');
failed++;
}
} catch (e) {
console.log(`Test suite error: ${e.message}`);
failed++;
} finally {
await browser.close();
}
console.log('-'.repeat(50));
console.log(`Chat Archived-Session 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);
});