Fix tiptap selection sync and toolbar event loop

This commit is contained in:
lix-2026
2026-04-19 21:03:25 +08:00
parent 111a87d4fd
commit 394e2a155c
87 changed files with 17415 additions and 527 deletions
+90 -7
View File
@@ -5,6 +5,7 @@ const MNOTE_WEB_BASE_URL = (process.env.MNOTE_WEB_SMOKE_BASE_URL || "http://127.
const REQUEST_TIMEOUT_MS = Number(process.env.MNOTE_SMOKE_TIMEOUT_MS || 20_000);
const UI_TIMEOUT_MS = Number(process.env.MNOTE_SMOKE_UI_TIMEOUT_MS || 30_000);
const TEST_LOGIN_BUTTON_NAME = "测试账号快速登录";
const TEST_USERNAME_PREFIX = "测试用户";
function assert(condition, message) {
if (!condition) {
@@ -92,19 +93,101 @@ async function getViewerIdentity(requestContext) {
return payload;
}
async function waitForAuthenticatedRedirect(page, timeout = 8_000) {
await page.waitForURL((url) => !url.toString().includes("/auth"), {
timeout,
waitUntil: "commit",
});
}
async function isVisible(locator) {
try {
return await locator.isVisible();
} catch {
return false;
}
}
async function completeUsernameSetupIfNeeded(page) {
const saveButton = page.getByRole("button", { name: "保存并继续" });
if (!(await isVisible(saveButton))) {
return false;
}
const usernameInput = page.locator('input[name="username"]').last();
await usernameInput.waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
const currentValue = ((await usernameInput.inputValue().catch(() => "")) || "").trim();
const username = currentValue || `${TEST_USERNAME_PREFIX}${Date.now().toString().slice(-6)}`;
await usernameInput.fill(username, { timeout: UI_TIMEOUT_MS });
await saveButton.click({ timeout: UI_TIMEOUT_MS });
await waitForAuthenticatedRedirect(page, UI_TIMEOUT_MS);
return true;
}
async function registerTestAccountIfNeeded(page) {
const switchButton = page.getByRole("button", { name: "还没有账户?立即注册" });
if (!(await isVisible(switchButton))) {
return false;
}
await switchButton.click({ timeout: UI_TIMEOUT_MS });
await page.locator('input[name="email"]').fill("test@example.com", { timeout: UI_TIMEOUT_MS });
await page.locator('input[name="username"]').fill(`${TEST_USERNAME_PREFIX}${Date.now().toString().slice(-6)}`, {
timeout: UI_TIMEOUT_MS,
});
await page.locator('input[name="password"]').fill("Test123456", { timeout: UI_TIMEOUT_MS });
await page.getByRole("button", { name: "注册" }).click({ timeout: UI_TIMEOUT_MS });
try {
await waitForAuthenticatedRedirect(page, UI_TIMEOUT_MS);
return true;
} catch {
return completeUsernameSetupIfNeeded(page);
}
}
async function waitForViewerIdentity(requestContext, attempts = 6) {
let lastError = null;
for (let index = 0; index < attempts; index += 1) {
try {
return await getViewerIdentity(requestContext);
} catch (error) {
lastError = error;
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
throw lastError instanceof Error ? lastError : new Error("获取当前用户失败");
}
async function ensureAuthenticated(page, requestContext) {
await page.goto(`${BASE_URL}/auth`, { waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS });
await page.goto(`${BASE_URL}/auth`, { waitUntil: "commit", timeout: UI_TIMEOUT_MS });
if (page.url().includes("/auth")) {
const quickLoginButton = page.getByRole("button", { name: TEST_LOGIN_BUTTON_NAME });
await quickLoginButton.waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
await Promise.race([
page.waitForURL((url) => !url.toString().includes("/auth"), {
timeout: UI_TIMEOUT_MS,
waitUntil: "commit",
}),
quickLoginButton.waitFor({ state: "visible", timeout: UI_TIMEOUT_MS }),
]);
if (!page.url().includes("/auth")) {
return await waitForViewerIdentity(requestContext);
}
await quickLoginButton.click({ timeout: UI_TIMEOUT_MS });
await page.waitForURL((url) => !url.toString().includes("/auth"), {
timeout: UI_TIMEOUT_MS,
});
try {
await waitForAuthenticatedRedirect(page, UI_TIMEOUT_MS);
} catch {
const completedUsername = await completeUsernameSetupIfNeeded(page);
if (!completedUsername) {
const registered = await registerTestAccountIfNeeded(page);
if (!registered) {
throw new Error("测试账号登录后仍停留在 /auth,且未进入可恢复的用户名/注册流程");
}
}
}
}
return await getViewerIdentity(requestContext);
return await waitForViewerIdentity(requestContext);
}
async function prepareTempTreeFixture(requestContext) {
@@ -137,7 +220,7 @@ async function cleanupDocuments(requestContext, createdIds) {
async function openDocument(page, workspaceId, documentId) {
const url = `${BASE_URL}/documents/${documentId}?workspaceId=${encodeURIComponent(workspaceId)}`;
await page.goto(url, { waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS });
await page.goto(url, { waitUntil: "commit", timeout: UI_TIMEOUT_MS });
return url;
}