81 lines
2.9 KiB
JavaScript
81 lines
2.9 KiB
JavaScript
// 说明:用于本地快速验证 Wolai ZIP 导入流程(Playwright)。
|
|
// 注意:仅用于开发/测试;请勿在生产环境使用。
|
|
|
|
const { chromium } = require("@playwright/test");
|
|
|
|
const BASE_URL = process.env.MNOTE_TEST_BASE_URL || "http://127.0.0.1:3000";
|
|
const ZIP_PATH = process.env.MNOTE_TEST_ZIP_PATH || "C:\\\\Users\\\\liaib\\\\Downloads\\\\软件开发.zip";
|
|
const ROOT_MD_PATH = process.env.MNOTE_TEST_ROOT_MD_PATH || "ChB6p4/软件开发.md";
|
|
|
|
async function main() {
|
|
const browser = await chromium.launch({ headless: true });
|
|
const context = await browser.newContext();
|
|
const page = await context.newPage();
|
|
|
|
page.setDefaultTimeout(60_000);
|
|
|
|
// 1) 打开 /auth(若已登录会跳转到 /)
|
|
await page.goto(`${BASE_URL}/auth`, { waitUntil: "networkidle" });
|
|
|
|
// 2) 如果还没登录,点“测试账号快速登录”
|
|
const url1 = page.url();
|
|
if (url1.includes("/auth")) {
|
|
const quickLogin = page.getByRole("button", { name: "测试账号快速登录" });
|
|
if (await quickLogin.isVisible().catch(() => false)) {
|
|
await quickLogin.click();
|
|
} else {
|
|
// 兜底:尝试手动点击“登录”按钮(如果用户已填写)
|
|
const submit = page.getByRole("button", { name: "登录" });
|
|
if (await submit.isVisible().catch(() => false)) await submit.click();
|
|
}
|
|
}
|
|
|
|
// 3) 等跳转到首页(或至少不在 /auth)
|
|
await page.waitForURL((u) => !u.toString().includes("/auth"), { timeout: 120_000 });
|
|
|
|
// 4) 打开导入页
|
|
await page.goto(`${BASE_URL}/wolai-import`, { waitUntil: "networkidle" });
|
|
|
|
// 5) 填本地路径(大文件不要上传)
|
|
const zipInput = page.locator('input[placeholder*="个人.zip"]').first();
|
|
await zipInput.fill(ZIP_PATH);
|
|
const rootInput = page.locator('input[placeholder*="dQeAax/个人.md"]').first();
|
|
await rootInput.fill(ROOT_MD_PATH);
|
|
|
|
// 6) 开始导入
|
|
const respPromise = page.waitForResponse(
|
|
(r) => r.url().includes("/api/wolai-import") && r.request().method() === "POST",
|
|
{ timeout: 20 * 60_000 },
|
|
);
|
|
await page.getByRole("button", { name: "开始导入" }).click();
|
|
|
|
const resp = await respPromise;
|
|
const status = resp.status();
|
|
const text = await resp.text().catch(() => "");
|
|
console.log("导入接口返回:", status);
|
|
if (text) {
|
|
console.log("响应体:", text.slice(0, 4000));
|
|
}
|
|
|
|
if (status >= 200 && status < 300) {
|
|
let json = null;
|
|
try {
|
|
json = JSON.parse(text);
|
|
} catch {
|
|
// ignore
|
|
}
|
|
const rootDocumentId = json && typeof json.rootDocumentId === "string" ? json.rootDocumentId : "";
|
|
if (rootDocumentId) {
|
|
await page.goto(`${BASE_URL}/documents/${encodeURIComponent(rootDocumentId)}`, { waitUntil: "networkidle" });
|
|
console.log("导入成功,已打开:", page.url());
|
|
}
|
|
}
|
|
|
|
await browser.close();
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error("导入测试失败:", err);
|
|
process.exit(1);
|
|
});
|