/** * 注册的验收:从登录页走到注册页,把账号建出来,再用它登回来。 * * 这里验的是"一个人真能自己进来",所以每一步都从界面走: * ① 登录页有「用邮箱注册」(服务端开着才会出现) * ② 注册页拿到验证码(本机 EMAIL_CODE_DEV_ECHO 会把码回给页面,不发真信) * ③ 提交后直接进首页,说明注册即登录 * ④ 退出、用「邮箱 + 密码」再登一次,证明邮箱真的是账号名 * ⑤ 同一个邮箱再发码,必须被挡(2019),而不是又建一个号 */ import { chromium } from "playwright-core"; const WEB = process.env.WEB ?? "http://127.0.0.1:5180"; const API = process.env.API ?? "http://127.0.0.1:8100/api/v1"; const CHROME = process.env.CHROME ?? `${process.env.LOCALAPPDATA}\\ms-playwright\\chromium-1223\\chrome-win64\\chrome.exe`; let pass = 0; let fail = 0; function check(name, ok, extra = "") { if (ok) { pass += 1; console.log(`PASS ${name}`); } else { fail += 1; console.log(`FAIL ${name}${extra ? ` — ${extra}` : ""}`); } } const email = `reg${Date.now()}@example.com`; const password = `dadao${Math.floor(Math.random() * 900000 + 1000)}`; const browser = await chromium.launch({ executablePath: CHROME }); const page = await browser.newPage({ viewport: { width: 1440, height: 960 } }); const errors = []; page.on("console", (m) => m.type() === "error" && errors.push(m.text())); page.on("pageerror", (e) => errors.push(String(e))); try { await page.goto(`${WEB}/login`); await page.waitForSelector("#account"); const registerLink = page.getByRole("link", { name: "用邮箱注册" }); check("登录页有「用邮箱注册」入口", (await registerLink.count()) > 0); await registerLink.click(); await page.waitForURL("**/register"); await page.waitForSelector("#email"); check("能进注册页", true); await page.locator("#email").fill(email); await page.getByRole("button", { name: "获取验证码" }).click(); await page.waitForSelector("text=开发环境验证码", { timeout: 15000 }); const notice = await page.locator("text=开发环境验证码").first().innerText(); const code = notice.match(/\d{6}/)?.[0] ?? ""; check("本机拿得到验证码(生产只发到邮箱)", code.length === 6, notice); await page.locator("#code").fill(code); await page.locator("#password").fill(password); await page.locator("#confirm").fill(password); await page.locator("#nickname").fill("注册验收"); await page.getByRole("button", { name: "注册并登录" }).click(); const landed = await page .waitForURL(`${WEB}/`, { timeout: 15000 }) .then(() => true) .catch(() => false); check("注册完直接进首页(注册即登录)", landed, page.url()); // 退出后用「邮箱 + 密码」登回来:邮箱真是账号名 await page.getByRole("button", { name: "退出登录" }).last().click(); await page.waitForURL("**/login"); await page.getByRole("tab", { name: "邮箱" }).click(); await page.locator("#account").fill(email); await page.locator("#password").fill(password); await page.getByRole("button", { name: "登录", exact: true }).click(); const back = await page .waitForURL(`${WEB}/`, { timeout: 15000 }) .then(() => true) .catch(() => false); check("邮箱 + 密码能登回来", back); // 同一个邮箱再发码:必须拦住,别再建一个号 const again = await page.request.post(`${API}/auth/email/code`, { data: { email } }); const body = await again.json(); check("已注册的邮箱再发码回 2019", body.code === 2019, JSON.stringify(body)); check("控制台无报错", errors.length === 0, errors.slice(0, 2).join(" | ")); } catch (err) { fail += 1; console.log(`FAIL 脚本异常 — ${err}`); } finally { await browser.close(); } console.log(`\n${fail === 0 ? "ALL PASS" : "FAILED"} — pass ${pass}, fail ${fail}`); process.exit(fail === 0 ? 0 : 1);