Files

67 lines
4.7 KiB
JavaScript
Raw Permalink Normal View History

const fs = require('node:fs')
const path = require('node:path')
const assert = require('node:assert/strict')
const { chromium } = require('/Users/macclaw/.cache/codex-runtimes/codex-primary-runtime/dependencies/node/node_modules/playwright')
const directory = path.resolve(__dirname, '../../acceptance')
const origin = 'https://agent.pducv.cn'
const checks = [], failures = [], snapshots = []
let auth, browser
function check(name, value) { checks.push({name, passed: !!value}); assert.ok(value, name) }
async function api(route, method = 'GET', body) {
const response = await fetch(origin + '/api/v1' + route, {
method, headers: {'Content-Type': 'application/json', ...(auth ? {Authorization: `Bearer ${auth.token}`, 'X-Tenant-ID': '10059'} : {})},
body: body === undefined ? undefined : JSON.stringify(body), signal: AbortSignal.timeout(45000),
})
assert.equal(response.status, 200, route)
return response.json()
}
async function main() {
const credentials = JSON.parse(fs.readFileSync(path.join(directory, 'super-admin-login.json'), 'utf8'))
auth = await api('/auth/login', 'POST', {username: credentials.username, password: credentials.password})
check('admin_login_opens_business_workspace', Number(auth.active_tenant.id) === 10059)
const baseline = JSON.parse(fs.readFileSync(path.join(directory, 'business-wiki-backfill.json'), 'utf8'))
for (const kb of baseline.before) {
const current = (await api(`/knowledge-bases/${kb.id}`)).data
check(`${kb.name}:rag_and_wiki_enabled`, current.indexing_strategy.wiki_enabled && current.indexing_strategy.vector_enabled && current.indexing_strategy.keyword_enabled)
const stats = await api(`/knowledgebase/${kb.id}/wiki/stats`)
const pages = await api(`/knowledgebase/${kb.id}/wiki/pages?page=1&page_size=5`)
check(`${kb.name}:wiki_readable`, typeof stats.total_pages === 'number' && Array.isArray(pages.pages))
snapshots.push({id: kb.id, name: kb.name, pages: stats.total_pages, pending_tasks: stats.pending_tasks, active: stats.is_active})
}
check('wiki_pages_actually_generated', snapshots.some(s => s.pages > 0))
const product = baseline.before.find(k => k.name === '\u4ea7\u54c1\u4fe1\u606f')
const start = Date.now()
const hits = (await api(`/knowledge-bases/${product.id}/hybrid-search`, 'POST', {
query_text: '\u4ea7\u54c1\u4fe1\u606f', match_count: 5, vector_threshold: 0, keyword_threshold: 0,
})).data
check('existing_rag_still_returns_source_chunks', Array.isArray(hits) && hits.some(hit => hit.knowledge_id))
snapshots.push({rag_query_ms: Date.now() - start, rag_hits: hits.length})
browser = await chromium.launch({headless: true, channel: 'chrome'})
const page = await browser.newPage({viewport: {width: 1440, height: 1000}})
await page.addInitScript(a => {
const values = {weknora_user: JSON.stringify(a.user), weknora_tenant: JSON.stringify(a.active_tenant), weknora_token: a.token, weknora_refresh_token: a.refresh_token, weknora_memberships: JSON.stringify(a.memberships)}
Object.entries(values).forEach(([k,v]) => localStorage.setItem(k,v))
localStorage.setItem('weknora:new-user-guide-done:v1', '1')
}, auth)
const errors = []
page.on('pageerror', e => errors.push(e.message))
page.on('response', response => {if(response.status() >= 400 && /\/(api|assets)\//.test(new URL(response.url()).pathname)) errors.push(`${response.status()} ${new URL(response.url()).pathname}`)})
await page.goto(`${origin}/platform/knowledge-bases/${product.id}?tab=wiki`)
await page.locator('.wiki-browser').waitFor({timeout: 30000})
await page.locator('.wiki-tab').first().waitFor({timeout: 30000})
await page.locator('.wiki-directory-item, .wiki-page-item').first().waitFor({timeout: 30000})
check('generated_wiki_entries_visible', await page.locator('.wiki-tab-count').first().innerText() !== '0')
await page.screenshot({path: path.join(directory, 'business-wiki-desktop.png'), animations: 'disabled'})
check('public_wiki_browser_visible', await page.locator('.wiki-browser').isVisible())
await page.setViewportSize({width: 390, height: 844})
await page.screenshot({path: path.join(directory, 'business-wiki-mobile.png'), animations: 'disabled'})
check('wiki_no_horizontal_overflow', await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 2))
check('wiki_no_failed_requests_or_page_errors', errors.length === 0)
}
main().catch(e => {failures.push(e.message); process.exitCode = 1}).finally(async () => {
if (browser) await browser.close()
const report = {checked_at: new Date().toISOString(), origin, checks, snapshots, failures, passed: failures.length === 0}
fs.writeFileSync(path.join(directory, 'business-wiki-verification.json'), JSON.stringify(report, null, 2))
console.log(JSON.stringify(report))
})