import { chromium } from 'playwright'; import { readFile, writeFile, mkdir } from 'node:fs/promises'; import assert from 'node:assert/strict'; const credentials = JSON.parse(await readFile(new URL('../../.work/frontend-accounts.json',import.meta.url),'utf8')); const base = process.env.FRONTEND_URL || 'http://127.0.0.1:18762'; const browser = await chromium.launch({ executablePath:process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE || 'C:/Users/Administrator/AppData/Local/ms-playwright/chromium-1228/chrome-win64/chrome.exe',headless:true }); const context = await browser.newContext(); const page = await context.newPage(), errors = [], results = []; page.on('pageerror',error=>errors.push(error.message)); async function route(path) { await page.evaluate(value=>{location.hash=value;},`#/merchant/${path}`); await page.waitForTimeout(200); await page.getByText('正在读取数据…',{exact:true}).waitFor({state:'hidden'}); } async function confirm() { await page.getByRole('dialog').getByRole('button',{name:'确认操作',exact:true}).click(); await page.getByRole('dialog').waitFor({state:'hidden'}); } async function success(text) { await page.locator('.app-notice').filter({hasText:text}).or(page.getByRole('alert')).waitFor(); assert.equal(await page.getByRole('alert').count(),0,await page.locator('body').innerText()); } let ruleVersion; try { await page.goto(`${base}/#/merchant/login`); await page.getByLabel('商家标识').fill(credentials.tenant_id); await page.getByLabel('员工账户').fill('admin01'); await page.getByLabel('密码',{exact:true}).fill(credentials.password); await page.getByRole('button',{name:'登录',exact:true}).click(); await page.locator('.admin-shell').waitFor(); if (process.env.CONFIGURATION_RULES_ONLY !== 'true') { await route('knowledge'); await page.getByRole('button',{name:'新建资料',exact:true}).click(); await page.getByLabel('资料标题').fill(`发布撤回验收 ${Date.now()}`); await page.getByLabel('文本内容').fill('键盘售后服务需要客户确认方案,再由主管审批。知识发布功能浏览器验收。'); await page.getByRole('button',{name:'保存草稿',exact:true}).click(); await success('知识草稿已保存'); await page.getByRole('button',{name:'发布当前版本'}).click(); await confirm(); await success('知识资料已发布'); await page.getByRole('button',{name:'撤回当前版本'}).click(); await confirm(); await success('知识资料已撤回'); results.push('知识真实保存、向量发布、撤回通过'); } if (process.env.CONFIGURATION_KNOWLEDGE_ONLY !== 'true') { await route('rules'); await page.getByLabel('签收后申请天数').fill('8'); await page.getByLabel('规则依据与条款').fill('签收后八天内键盘故障可申请换货,需客户确认及主管审批。'); await page.getByRole('button',{name:'保存规则草稿'}).click(); await page.getByRole('button',{name:'发布新版本'}).click(); await confirm(); await success('规则已由服务端发布'); await page.getByLabel('撤销原因').fill('规则发布与撤销功能验收'); await page.getByRole('button',{name:'撤销当前规则'}).click(); await confirm(); await success('规则已撤销'); await page.getByLabel('签收后申请天数').fill('7'); await page.getByLabel('规则依据与条款').fill('签收后七天内键盘故障可申请换货,需客户确认及主管审批。'); await page.getByRole('button',{name:'保存规则草稿'}).click(); await page.getByRole('button',{name:'发布新版本'}).click(); const published = page.waitForResponse(response=>response.request().method()==='POST' && response.url().endsWith('/exchange-rules/draft/publish')); await confirm(); const result = await (await published).json(); ruleVersion = result.version; await success('规则已由服务端发布'); results.push(`规则真实发布、撤销与发布有效 V${ruleVersion} 通过`); } assert.deepEqual(errors,[]); await mkdir(new URL('../../.work/frontend-verification/',import.meta.url),{recursive:true}); const report = process.env.CONFIGURATION_KNOWLEDGE_ONLY === 'true' ? 'merchant-knowledge.json' : 'merchant-configuration.json'; await writeFile(new URL('../../.work/frontend-verification/'+report,import.meta.url),JSON.stringify({date:new Date().toISOString(),checks:results,ruleVersion,browserErrors:errors,generatedModelCalls:0},null,2)); console.log(results.join('\n')); } catch(error) { console.error(await page.locator('body').innerText()); throw error; } finally { await browser.close(); }