import { createRequire } from "node:module"; import { randomUUID } from "node:crypto"; import { execFileSync } from "node:child_process"; import { mkdir } from "node:fs/promises"; import { fileURLToPath } from "node:url"; const require = createRequire(process.env.PLAYWRIGHT_REQUIRE_FROM ?? import.meta.url); const { chromium } = require("playwright"); const outputDirectory = new URL("./screenshots/", import.meta.url); const webURL = process.env.VISUAL_WEB_URL ?? "http://localhost:3100"; const webHostPattern = new RegExp(new URL(webURL).host.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")); const oidcURLPattern = new RegExp(process.env.VISUAL_OIDC_URL_PATTERN ?? "localhost:58180"); const username = process.env.VISUAL_USERNAME ?? "owner"; const password = process.env.VISUAL_PASSWORD ?? "LocalOnly123!"; const priceBookID = randomUUID(); const priceBookPublicID = randomUUID(); const rateID = randomUUID(); const provider = `visual-${priceBookPublicID}`.slice(0, 38); await mkdir(outputDirectory, { recursive: true }); function psql(statement) { return execFileSync("docker", ["compose", "exec", "-T", "postgres", "psql", "-v", "ON_ERROR_STOP=1", "-U", "voice", "-d", "voice_platform", "-Atc", statement], { cwd: process.cwd(), encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }).trim(); } function cleanup() { psql(`DELETE FROM billing_price_book WHERE id='${priceBookID}'::uuid;`); } psql(`BEGIN; INSERT INTO billing_price_book(id,public_id,tenant_id,provider,currency,version_no,status,source_ref,change_summary,effective_from,created_by,row_version) VALUES('${priceBookID}'::uuid,'${priceBookPublicID}'::uuid,'10000000-0000-0000-0000-000000000001'::uuid,'${provider}','CNY',1,'DRAFT','contract://visual/no-submit-v1','页面价格版本审批无提交验证',now()+interval '1 day','10000000-0000-0000-0000-000000000102'::uuid,4); INSERT INTO billing_price_rate(id,price_book_id,usage_category,metric_code,unit,unit_price_micros) VALUES('${rateID}'::uuid,'${priceBookID}'::uuid,'END_TO_END_VOICE','agent_minute','minute',22000); COMMIT;`); const browser = await chromium.launch({ headless: true, executablePath: process.env.CHROME_PATH ?? "C:/Program Files/Google/Chrome/Application/chrome.exe" }); const page = await browser.newPage({ viewport: { width: 1440, height: 1100 }, deviceScaleFactor: 1 }); const consoleErrors = []; const failedRequests = []; const publishMutations = []; page.on("console", (message) => { if (message.type() === "error") consoleErrors.push(message.text()); }); page.on("request", (request) => { if (request.method() === "PATCH" && /\/api\/v1\/ops\/price-books\/[^/]+\/publish$/.test(request.url())) publishMutations.push(`${request.method()} ${request.url()}`); }); page.on("requestfailed", (request) => { const failure = request.failure()?.errorText ?? "failed"; const requestURL = new URL(request.url()); if (request.method() === "GET" && requestURL.pathname === "/api/v1/events" && failure.includes("ERR_ABORTED")) return; failedRequests.push(`${request.method()} ${request.url()} ${failure}`); }); try { await page.goto(`${webURL}/?workspace=billing`, { waitUntil: "networkidle" }); await page.getByRole("button", { name: "登录研发环境" }).click(); await page.waitForURL(oidcURLPattern); await page.locator("#username").fill(username); await page.locator("#password").fill(password); await page.locator("#kc-login").click(); await page.waitForURL(webHostPattern, { timeout: 15000 }); await page.getByRole("heading", { name: "用量、费用与额度" }).waitFor(); const row = page.locator(".billing-ledger li").filter({ hasText: provider }); await row.waitFor(); await row.getByRole("button", { name: "审批发布" }).click(); const form = page.getByRole("form", { name: "价格版本审批发布" }); await form.waitFor(); const formText = await form.textContent(); const targetBound = formText?.includes(priceBookPublicID) === true && formText?.includes(provider) === true && formText?.includes("行版本 v4") === true && formText?.includes("1项费率") === true; const submit = form.getByRole("button", { name: "确认审批并发布" }); const emptyReasonDisabled = await submit.isDisabled(); await form.getByLabel("发布依据").fill("报价合同、币种、费率和明日生效时间均已复核"); const acknowledgementRequired = await submit.isDisabled(); await form.getByLabel(/我确认本人不是该版本创建人/).check(); const enabledAfterAcknowledgement = await submit.isEnabled(); await page.screenshot({ path: fileURLToPath(new URL("ops-billing-price-publish.png", outputDirectory)), fullPage: true }); await form.getByRole("button", { name: "取消" }).click(); const cancelClosed = await form.count() === 0; const overflow = await page.evaluate(() => [...document.querySelectorAll("body *")] .filter((element) => element instanceof HTMLElement && element.scrollWidth > element.clientWidth + 2 && getComputedStyle(element).overflowX === "visible") .slice(0, 20) .map((element) => `${element.tagName}.${element.className}: ${element.clientWidth}/${element.scrollWidth}`)); const result = { targetBound, emptyReasonDisabled, acknowledgementRequired, enabledAfterAcknowledgement, cancelClosed, publishMutations, overflow, consoleErrors, failedRequests }; console.log(JSON.stringify(result, null, 2)); if (!targetBound || !emptyReasonDisabled || !acknowledgementRequired || !enabledAfterAcknowledgement || !cancelClosed || publishMutations.length || overflow.length || consoleErrors.length || failedRequests.length) { throw new Error("Ops billing price publish visual smoke failed"); } } finally { await browser.close(); cleanup(); }