Fix tiptap selection sync and toolbar event loop
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
"use strict";
|
||||
|
||||
const { chromium } = require("playwright");
|
||||
const {
|
||||
BASE_URL,
|
||||
MNOTE_WEB_BASE_URL,
|
||||
UI_TIMEOUT_MS,
|
||||
assert,
|
||||
cleanupDocuments,
|
||||
ensureAuthenticated,
|
||||
openDocument,
|
||||
prepareTempTreeFixture,
|
||||
} = require("./tree-shell-smoke-helpers");
|
||||
|
||||
async function runDefaultShellPath(page, fixture) {
|
||||
await openDocument(page, fixture.workspaceId, fixture.parentId);
|
||||
|
||||
await page.getByRole("button", { name: "进入编辑" }).waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
assert(
|
||||
(await page.locator(`iframe[title="mnote-web-document-shell-${fixture.parentId}"]`).count()) === 0,
|
||||
"默认文档页不应继续挂载 mnote-web 文档壳 iframe",
|
||||
);
|
||||
await page.getByRole("button", { name: "进入编辑" }).click({ timeout: UI_TIMEOUT_MS });
|
||||
await page.getByLabel("页面标题").waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
await page.locator(".wolai-editor").first().waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
|
||||
return {
|
||||
compatReady: true,
|
||||
};
|
||||
}
|
||||
|
||||
async function runRuntimeDebugPath(page, fixture) {
|
||||
await page.goto(`${BASE_URL}/api/auth/mnote-web-token`, { waitUntil: "commit", timeout: UI_TIMEOUT_MS });
|
||||
await page.goto(
|
||||
`${MNOTE_WEB_BASE_URL}/document-debug?documentId=${fixture.parentId}&workspaceId=${fixture.workspaceId}&host=task103-smoke`,
|
||||
{ waitUntil: "commit", timeout: UI_TIMEOUT_MS },
|
||||
);
|
||||
await page.getByText("Document Runtime Debug").waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
await page.locator("#block-editor-list").waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
await page.locator("#editor-command-slash").waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
|
||||
return {
|
||||
debugUrl: page.url(),
|
||||
};
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const browser = await chromium.launch({ headless: true });
|
||||
const context = await browser.newContext({
|
||||
viewport: { width: 1440, height: 960 },
|
||||
});
|
||||
const page = await context.newPage();
|
||||
|
||||
let caughtError = null;
|
||||
let fixture = null;
|
||||
|
||||
try {
|
||||
const viewer = await ensureAuthenticated(page, context.request);
|
||||
fixture = await prepareTempTreeFixture(context.request);
|
||||
|
||||
const primary = await runDefaultShellPath(page, fixture);
|
||||
const debug = await runRuntimeDebugPath(page, fixture);
|
||||
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
ok: true,
|
||||
baseUrl: BASE_URL,
|
||||
viewerUserId: viewer.userId,
|
||||
workspaceId: fixture.workspaceId,
|
||||
parentId: fixture.parentId,
|
||||
childId: fixture.childId,
|
||||
primary,
|
||||
debug,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
caughtError = error;
|
||||
} finally {
|
||||
if (fixture) {
|
||||
try {
|
||||
await cleanupDocuments(context.request, fixture.createdIds);
|
||||
} catch (cleanupError) {
|
||||
if (!caughtError) {
|
||||
caughtError = cleanupError;
|
||||
} else {
|
||||
console.error(
|
||||
`清理临时页面失败:${
|
||||
cleanupError instanceof Error
|
||||
? cleanupError.stack || cleanupError.message
|
||||
: String(cleanupError)
|
||||
}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
await page.close().catch(() => undefined);
|
||||
await context.close().catch(() => undefined);
|
||||
await browser.close().catch(() => undefined);
|
||||
}
|
||||
|
||||
if (caughtError) {
|
||||
throw caughtError;
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error(error instanceof Error ? error.stack || error.message : String(error));
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -0,0 +1,100 @@
|
||||
"use strict";
|
||||
|
||||
const { chromium } = require("playwright");
|
||||
const {
|
||||
BASE_URL,
|
||||
MNOTE_WEB_BASE_URL,
|
||||
UI_TIMEOUT_MS,
|
||||
assert,
|
||||
cleanupDocuments,
|
||||
ensureAuthenticated,
|
||||
prepareTempTreeFixture,
|
||||
openDocument,
|
||||
} = require("./tree-shell-smoke-helpers");
|
||||
|
||||
async function openRuntimeDebug(page, fixture) {
|
||||
await page.goto(`${BASE_URL}/api/auth/mnote-web-token`, { waitUntil: "commit", timeout: UI_TIMEOUT_MS });
|
||||
await page.goto(
|
||||
`${MNOTE_WEB_BASE_URL}/document-debug?documentId=${fixture.parentId}&workspaceId=${fixture.workspaceId}&host=task104-smoke`,
|
||||
{ waitUntil: "commit", timeout: UI_TIMEOUT_MS },
|
||||
);
|
||||
await page.getByText("Document Runtime Debug").waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const browser = await chromium.launch({ headless: true });
|
||||
const context = await browser.newContext({
|
||||
viewport: { width: 1440, height: 960 },
|
||||
});
|
||||
const page = await context.newPage();
|
||||
|
||||
let fixture = null;
|
||||
let caughtError = null;
|
||||
|
||||
try {
|
||||
await ensureAuthenticated(page, context.request);
|
||||
fixture = await prepareTempTreeFixture(context.request);
|
||||
await openDocument(page, fixture.workspaceId, fixture.parentId);
|
||||
await openRuntimeDebug(page, fixture);
|
||||
|
||||
const textarea = page.locator("textarea[data-block-input-id]").first();
|
||||
await textarea.waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
await textarea.click({ timeout: UI_TIMEOUT_MS });
|
||||
await textarea.fill("你好 runtime input", { timeout: UI_TIMEOUT_MS });
|
||||
|
||||
await page.locator("#editor-save-status").getByText("saved").waitFor({
|
||||
state: "visible",
|
||||
timeout: UI_TIMEOUT_MS,
|
||||
});
|
||||
|
||||
const value = await textarea.inputValue();
|
||||
assert(value === "你好 runtime input", `输入值不符合预期:${value}`);
|
||||
|
||||
const selection = await page.locator("#editor-selection-id").textContent();
|
||||
assert(selection && selection.includes("_block_1"), `selection 未更新到真实输入块:${selection}`);
|
||||
|
||||
const eventLog = (await page.locator("#editor-event-log").textContent()) || "";
|
||||
assert(
|
||||
eventLog.includes("human_editor_input.beforeinput"),
|
||||
`未记录 beforeinput runtime 标记:${eventLog}`,
|
||||
);
|
||||
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
ok: true,
|
||||
baseUrl: BASE_URL,
|
||||
workspaceId: fixture.workspaceId,
|
||||
documentId: fixture.parentId,
|
||||
selection,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
caughtError = error;
|
||||
} finally {
|
||||
if (fixture) {
|
||||
try {
|
||||
await cleanupDocuments(context.request, fixture.createdIds);
|
||||
} catch (cleanupError) {
|
||||
if (!caughtError) {
|
||||
caughtError = cleanupError;
|
||||
}
|
||||
}
|
||||
}
|
||||
await page.close().catch(() => undefined);
|
||||
await context.close().catch(() => undefined);
|
||||
await browser.close().catch(() => undefined);
|
||||
}
|
||||
|
||||
if (caughtError) {
|
||||
throw caughtError;
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error(error instanceof Error ? error.stack || error.message : String(error));
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -0,0 +1,121 @@
|
||||
"use strict";
|
||||
|
||||
const { chromium } = require("playwright");
|
||||
const {
|
||||
BASE_URL,
|
||||
MNOTE_WEB_BASE_URL,
|
||||
UI_TIMEOUT_MS,
|
||||
assert,
|
||||
cleanupDocuments,
|
||||
ensureAuthenticated,
|
||||
prepareTempTreeFixture,
|
||||
openDocument,
|
||||
} = require("./tree-shell-smoke-helpers");
|
||||
|
||||
async function openRuntimeDebug(page, fixture) {
|
||||
await page.goto(`${BASE_URL}/api/auth/mnote-web-token`, { waitUntil: "commit", timeout: UI_TIMEOUT_MS });
|
||||
await page.goto(
|
||||
`${MNOTE_WEB_BASE_URL}/document-debug?documentId=${fixture.parentId}&workspaceId=${fixture.workspaceId}&host=task105-smoke`,
|
||||
{ waitUntil: "commit", timeout: UI_TIMEOUT_MS },
|
||||
);
|
||||
await page.getByText("Document Runtime Debug").waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const browser = await chromium.launch({ headless: true });
|
||||
const context = await browser.newContext({
|
||||
viewport: { width: 1440, height: 960 },
|
||||
});
|
||||
const page = await context.newPage();
|
||||
|
||||
let fixture = null;
|
||||
let caughtError = null;
|
||||
|
||||
try {
|
||||
await ensureAuthenticated(page, context.request);
|
||||
fixture = await prepareTempTreeFixture(context.request);
|
||||
await openDocument(page, fixture.workspaceId, fixture.parentId);
|
||||
await openRuntimeDebug(page, fixture);
|
||||
|
||||
const firstTextarea = page.locator("textarea[data-block-input-id]").first();
|
||||
await firstTextarea.waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
await firstTextarea.fill("AlphaBeta", { timeout: UI_TIMEOUT_MS });
|
||||
|
||||
await firstTextarea.evaluate((node) => {
|
||||
node.setSelectionRange(5, 5);
|
||||
});
|
||||
await firstTextarea.press("Enter", { timeout: UI_TIMEOUT_MS });
|
||||
|
||||
const textareas = page.locator("textarea[data-block-input-id]");
|
||||
await textareas.nth(1).waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
assert((await textareas.count()) === 2, "Enter 拆块后未出现第二个输入块");
|
||||
assert((await textareas.nth(0).inputValue()) === "Alpha", "拆块后首块文本不正确");
|
||||
assert((await textareas.nth(1).inputValue()) === "Beta", "拆块后次块文本不正确");
|
||||
|
||||
await textareas.nth(1).press("Tab", { timeout: UI_TIMEOUT_MS });
|
||||
await page.locator(".editor-row-meta").nth(1).getByText("depth=1").waitFor({
|
||||
state: "visible",
|
||||
timeout: UI_TIMEOUT_MS,
|
||||
});
|
||||
|
||||
await textareas.nth(1).press("Shift+Tab", { timeout: UI_TIMEOUT_MS });
|
||||
await page.locator(".editor-row-meta").nth(1).getByText("depth=0").waitFor({
|
||||
state: "visible",
|
||||
timeout: UI_TIMEOUT_MS,
|
||||
});
|
||||
|
||||
await textareas.nth(1).evaluate((node) => {
|
||||
node.setSelectionRange(0, 0);
|
||||
});
|
||||
await textareas.nth(1).press("Backspace", { timeout: UI_TIMEOUT_MS });
|
||||
|
||||
assert((await page.locator("textarea[data-block-input-id]").count()) === 1, "合并后块数量未回到 1");
|
||||
assert(
|
||||
(await page.locator("textarea[data-block-input-id]").first().inputValue()) === "AlphaBeta",
|
||||
"Backspace 合并后文本不正确",
|
||||
);
|
||||
const saveStatus = ((await page.locator("#editor-save-status").textContent()) || "").trim().toLowerCase();
|
||||
assert(
|
||||
saveStatus === "saved" || saveStatus === "saving" || saveStatus === "idle",
|
||||
`结构事务后保存状态异常:${saveStatus}`,
|
||||
);
|
||||
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
ok: true,
|
||||
baseUrl: BASE_URL,
|
||||
workspaceId: fixture.workspaceId,
|
||||
documentId: fixture.parentId,
|
||||
saveStatus,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
caughtError = error;
|
||||
} finally {
|
||||
if (fixture) {
|
||||
try {
|
||||
await cleanupDocuments(context.request, fixture.createdIds);
|
||||
} catch (cleanupError) {
|
||||
if (!caughtError) {
|
||||
caughtError = cleanupError;
|
||||
}
|
||||
}
|
||||
}
|
||||
await page.close().catch(() => undefined);
|
||||
await context.close().catch(() => undefined);
|
||||
await browser.close().catch(() => undefined);
|
||||
}
|
||||
|
||||
if (caughtError) {
|
||||
throw caughtError;
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error(error instanceof Error ? error.stack || error.message : String(error));
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -0,0 +1,101 @@
|
||||
"use strict";
|
||||
|
||||
const { chromium } = require("playwright");
|
||||
const {
|
||||
BASE_URL,
|
||||
MNOTE_WEB_BASE_URL,
|
||||
UI_TIMEOUT_MS,
|
||||
assert,
|
||||
cleanupDocuments,
|
||||
ensureAuthenticated,
|
||||
prepareTempTreeFixture,
|
||||
openDocument,
|
||||
} = require("./tree-shell-smoke-helpers");
|
||||
|
||||
async function openRuntimeDebug(page, fixture) {
|
||||
await page.goto(`${BASE_URL}/api/auth/mnote-web-token`, { waitUntil: "commit", timeout: UI_TIMEOUT_MS });
|
||||
await page.goto(
|
||||
`${MNOTE_WEB_BASE_URL}/document-debug?documentId=${fixture.parentId}&workspaceId=${fixture.workspaceId}&host=task106-smoke`,
|
||||
{ waitUntil: "commit", timeout: UI_TIMEOUT_MS },
|
||||
);
|
||||
await page.getByText("Document Runtime Debug").waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const browser = await chromium.launch({ headless: true });
|
||||
const context = await browser.newContext({
|
||||
viewport: { width: 1440, height: 960 },
|
||||
});
|
||||
const page = await context.newPage();
|
||||
|
||||
let fixture = null;
|
||||
let caughtError = null;
|
||||
|
||||
try {
|
||||
await ensureAuthenticated(page, context.request);
|
||||
fixture = await prepareTempTreeFixture(context.request);
|
||||
await openDocument(page, fixture.workspaceId, fixture.parentId);
|
||||
await openRuntimeDebug(page, fixture);
|
||||
|
||||
const textarea = page.locator("textarea[data-block-input-id]").first();
|
||||
await textarea.waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
await textarea.fill("Command", { timeout: UI_TIMEOUT_MS });
|
||||
|
||||
await page.locator("#editor-command-slash").click({ timeout: UI_TIMEOUT_MS });
|
||||
await page.locator('[data-slash-action="heading"]').click({ timeout: UI_TIMEOUT_MS });
|
||||
await page.locator("#editor-command-page-ref").click({ timeout: UI_TIMEOUT_MS });
|
||||
await page.locator("#editor-command-block-ref").click({ timeout: UI_TIMEOUT_MS });
|
||||
|
||||
await page.locator("#editor-save-status").getByText("saved").waitFor({
|
||||
state: "visible",
|
||||
timeout: UI_TIMEOUT_MS,
|
||||
});
|
||||
|
||||
const value = await textarea.inputValue();
|
||||
assert(
|
||||
value.includes("[[page]]") && value.includes("((block))"),
|
||||
`引用 token 未写入真实输入器:${value}`,
|
||||
);
|
||||
|
||||
const title = (await page.locator(".editor-row-title").first().textContent()) || "";
|
||||
assert(title.includes("Heading"), `slash 切块后未变成 heading:${title}`);
|
||||
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
ok: true,
|
||||
baseUrl: BASE_URL,
|
||||
workspaceId: fixture.workspaceId,
|
||||
documentId: fixture.parentId,
|
||||
value,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
caughtError = error;
|
||||
} finally {
|
||||
if (fixture) {
|
||||
try {
|
||||
await cleanupDocuments(context.request, fixture.createdIds);
|
||||
} catch (cleanupError) {
|
||||
if (!caughtError) {
|
||||
caughtError = cleanupError;
|
||||
}
|
||||
}
|
||||
}
|
||||
await page.close().catch(() => undefined);
|
||||
await context.close().catch(() => undefined);
|
||||
await browser.close().catch(() => undefined);
|
||||
}
|
||||
|
||||
if (caughtError) {
|
||||
throw caughtError;
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error(error instanceof Error ? error.stack || error.message : String(error));
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -0,0 +1,107 @@
|
||||
"use strict";
|
||||
|
||||
const { chromium } = require("playwright");
|
||||
const {
|
||||
BASE_URL,
|
||||
MNOTE_WEB_BASE_URL,
|
||||
UI_TIMEOUT_MS,
|
||||
assert,
|
||||
cleanupDocuments,
|
||||
ensureAuthenticated,
|
||||
prepareTempTreeFixture,
|
||||
openDocument,
|
||||
} = require("./tree-shell-smoke-helpers");
|
||||
|
||||
async function openRuntimeDebug(page, fixture) {
|
||||
await page.goto(`${BASE_URL}/api/auth/mnote-web-token`, { waitUntil: "commit", timeout: UI_TIMEOUT_MS });
|
||||
await page.goto(
|
||||
`${MNOTE_WEB_BASE_URL}/document-debug?documentId=${fixture.parentId}&workspaceId=${fixture.workspaceId}&host=task107-smoke`,
|
||||
{ waitUntil: "commit", timeout: UI_TIMEOUT_MS },
|
||||
);
|
||||
await page.getByText("Document Runtime Debug").waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const browser = await chromium.launch({ headless: true });
|
||||
const context = await browser.newContext({
|
||||
viewport: { width: 1440, height: 960 },
|
||||
});
|
||||
const page = await context.newPage();
|
||||
|
||||
let fixture = null;
|
||||
let caughtError = null;
|
||||
|
||||
try {
|
||||
await ensureAuthenticated(page, context.request);
|
||||
fixture = await prepareTempTreeFixture(context.request);
|
||||
await openDocument(page, fixture.workspaceId, fixture.parentId);
|
||||
await openRuntimeDebug(page, fixture);
|
||||
|
||||
const textarea = page.locator("textarea[data-block-input-id]").first();
|
||||
await textarea.waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
await textarea.fill("persist runtime content", { timeout: UI_TIMEOUT_MS });
|
||||
|
||||
await page.locator("#editor-save-status").getByText("saved").waitFor({
|
||||
state: "visible",
|
||||
timeout: UI_TIMEOUT_MS,
|
||||
});
|
||||
|
||||
await page.reload({ waitUntil: "commit", timeout: UI_TIMEOUT_MS });
|
||||
const reloadedTextarea = page.locator("textarea[data-block-input-id]").first();
|
||||
await reloadedTextarea.waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
assert(
|
||||
(await reloadedTextarea.inputValue()) === "persist runtime content",
|
||||
"刷新后未回放最近一次保存内容",
|
||||
);
|
||||
|
||||
await page.goto(
|
||||
`${BASE_URL}/documents/${fixture.parentId}?workspaceId=${encodeURIComponent(fixture.workspaceId)}`,
|
||||
{ waitUntil: "commit", timeout: UI_TIMEOUT_MS },
|
||||
);
|
||||
await page.getByRole("button", { name: "进入编辑" }).waitFor({
|
||||
state: "visible",
|
||||
timeout: UI_TIMEOUT_MS,
|
||||
});
|
||||
assert(
|
||||
(await page.locator(`iframe[title="mnote-web-document-shell-${fixture.parentId}"]`).count()) === 0,
|
||||
"默认文档页不应继续挂载 runtime debug iframe",
|
||||
);
|
||||
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
ok: true,
|
||||
baseUrl: BASE_URL,
|
||||
workspaceId: fixture.workspaceId,
|
||||
documentId: fixture.parentId,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
caughtError = error;
|
||||
} finally {
|
||||
if (fixture) {
|
||||
try {
|
||||
await cleanupDocuments(context.request, fixture.createdIds);
|
||||
} catch (cleanupError) {
|
||||
if (!caughtError) {
|
||||
caughtError = cleanupError;
|
||||
}
|
||||
}
|
||||
}
|
||||
await page.close().catch(() => undefined);
|
||||
await context.close().catch(() => undefined);
|
||||
await browser.close().catch(() => undefined);
|
||||
}
|
||||
|
||||
if (caughtError) {
|
||||
throw caughtError;
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error(error instanceof Error ? error.stack || error.message : String(error));
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user