434 lines
17 KiB
JavaScript
434 lines
17 KiB
JavaScript
#!/usr/bin/env node
|
|
"use strict";
|
|
|
|
const assert = require("node:assert");
|
|
const fs = require("node:fs");
|
|
const os = require("node:os");
|
|
const path = require("node:path");
|
|
const { chromium } = require("playwright");
|
|
const {
|
|
BASE_URL,
|
|
UI_TIMEOUT_MS,
|
|
ensureAuthenticated,
|
|
} = require("./tree-shell-smoke-helpers");
|
|
|
|
const TASK = "task762-page-ai-board-first-smoke";
|
|
const OUTPUT_DIR = path.join(process.cwd(), "tmp", TASK);
|
|
const RESULT_PATH = path.join(OUTPUT_DIR, "result.json");
|
|
const CHROMIUM_EXECUTABLE_PATH = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH
|
|
|| ["/usr/bin/google-chrome-stable", "/usr/bin/google-chrome", "/snap/bin/chromium", "/usr/bin/chromium"]
|
|
.find((candidate) => fs.existsSync(candidate));
|
|
|
|
function fileUrl(localPath) {
|
|
return `file://${localPath}`;
|
|
}
|
|
|
|
function localMdDocumentId(relativePath) {
|
|
return `local-md:${relativePath.replaceAll("/", "~2F")}`;
|
|
}
|
|
|
|
function writeWorkspaceManifest(root, ownerId, workspaceId) {
|
|
fs.mkdirSync(path.join(root, ".mnote"), { recursive: true });
|
|
fs.writeFileSync(
|
|
path.join(root, ".mnote", "workspace.json"),
|
|
`${JSON.stringify({
|
|
workspaceId,
|
|
ownerId,
|
|
createdAt: new Date().toISOString(),
|
|
capabilities: ["local_files", "ai_sessions", "markdown_edit"],
|
|
}, null, 2)}\n`,
|
|
"utf8",
|
|
);
|
|
}
|
|
|
|
function documentUrl(root, workspaceId, relativePath) {
|
|
const url = new URL(`${BASE_URL}/documents/${encodeURIComponent(localMdDocumentId(relativePath))}`);
|
|
url.searchParams.set("sourceKind", "local_folder");
|
|
url.searchParams.set("rootUri", fileUrl(root));
|
|
url.searchParams.set("workspaceId", workspaceId);
|
|
url.searchParams.set("treeView", "filetree");
|
|
return url.toString();
|
|
}
|
|
|
|
async function waitForEditorText(page, expected) {
|
|
await page.waitForFunction(
|
|
(text) => {
|
|
const editor = document.querySelector('[data-testid="mnote-leptos-tiptap-island-editor-root"] .ProseMirror');
|
|
return (editor?.textContent || "").includes(text);
|
|
},
|
|
expected,
|
|
{ timeout: UI_TIMEOUT_MS },
|
|
);
|
|
}
|
|
|
|
async function saveScreenshot(page, name) {
|
|
const target = path.join(OUTPUT_DIR, `${name}.png`);
|
|
await page.screenshot({ path: target, fullPage: true });
|
|
return target;
|
|
}
|
|
|
|
async function main() {
|
|
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
|
|
const suffix = Date.now().toString(36);
|
|
const actorId = "mnote-e2e";
|
|
const workspaceId = `local-ws:${actorId}:task762-${suffix}`;
|
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), "mnote-task762-board-ai-"));
|
|
const rootUri = fileUrl(root);
|
|
const relativePath = "BoardFirstPage.md";
|
|
const documentId = localMdDocumentId(relativePath);
|
|
const filePath = path.join(root, relativePath);
|
|
const beforeToken = `BOARD_UI_EDIT_BEFORE_${suffix}`;
|
|
const afterToken = `BOARD_UI_EDIT_AFTER_${suffix}`;
|
|
const capturedBoardRuns = [];
|
|
const capturedBoardRunResponses = [];
|
|
const consoleErrors = [];
|
|
let caughtError = null;
|
|
|
|
writeWorkspaceManifest(root, actorId, workspaceId);
|
|
fs.writeFileSync(
|
|
filePath,
|
|
["# Board First Page AI", "", `页面段落:${beforeToken}`, ""].join("\n"),
|
|
"utf8",
|
|
);
|
|
|
|
const browser = await chromium.launch({
|
|
headless: process.env.HEADFUL !== "1",
|
|
...(CHROMIUM_EXECUTABLE_PATH ? { executablePath: CHROMIUM_EXECUTABLE_PATH } : {}),
|
|
});
|
|
const context = await browser.newContext({
|
|
viewport: { width: 1440, height: 960 },
|
|
locale: "zh-CN",
|
|
extraHTTPHeaders: {
|
|
"x-mnote-actor-id": actorId,
|
|
"x-mnote-actor-type": "user",
|
|
},
|
|
});
|
|
const page = await context.newPage();
|
|
|
|
page.on("console", (message) => {
|
|
if (["error", "warning"].includes(message.type())) {
|
|
consoleErrors.push({ type: message.type(), text: message.text() });
|
|
}
|
|
});
|
|
page.on("request", (request) => {
|
|
if (!request.url().includes("/api/page-ai/board/runs") || request.method() !== "POST") return;
|
|
let body = null;
|
|
try {
|
|
body = JSON.parse(request.postData() || "{}");
|
|
} catch {
|
|
body = request.postData() || "";
|
|
}
|
|
capturedBoardRuns.push({ url: request.url(), method: request.method(), body });
|
|
});
|
|
|
|
await page.route("**/api/page-ai/board/workers", async (route) => {
|
|
await route.fulfill({
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({
|
|
ok: true,
|
|
schema: "agent_board.page_ai_route.v2",
|
|
surface: "mnote-page-ai",
|
|
workerPresetId: "mnote-page-ai-zcode",
|
|
workerName: "MNote 页面 AI · ZCode",
|
|
allowedWorkerPresetIds: ["mnote-page-ai-zcode"],
|
|
modelOverride: "zcode-default",
|
|
modelOptions: [
|
|
{ id: "zcode-default", label: "默认", default: true },
|
|
{ id: "zcode-fast", label: "快速" },
|
|
{ id: "zcode-strong", label: "强力" },
|
|
],
|
|
workers: [{
|
|
id: "mnote-page-ai-zcode",
|
|
name: "MNote 页面 AI · ZCode",
|
|
surface: "mnote-page-ai",
|
|
agentType: "zcode",
|
|
modelOptions: [
|
|
{ id: "zcode-default", label: "默认", default: true },
|
|
{ id: "zcode-fast", label: "快速" },
|
|
{ id: "zcode-strong", label: "强力" },
|
|
],
|
|
}, {
|
|
id: "qa-browser-worker",
|
|
name: "QA Browser Worker",
|
|
surface: "agent-board-console",
|
|
role: "qa",
|
|
}],
|
|
}),
|
|
});
|
|
});
|
|
|
|
await page.route("**/api/page-ai/board/workflows", async (route) => {
|
|
await route.fulfill({
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({
|
|
ok: true,
|
|
schema: "agent_board.page_ai_route.v2",
|
|
surface: "mnote-page-ai",
|
|
workflowId: "builtin-mnote-page-ai-chat",
|
|
workflowName: "MNote 页面 AI",
|
|
allowedWorkflowIds: ["builtin-mnote-page-ai-chat"],
|
|
workflows: [{
|
|
id: "builtin-mnote-page-ai-chat",
|
|
name: "MNote 页面 AI",
|
|
surface: "mnote-page-ai",
|
|
}, {
|
|
id: "general-hotfix-workflow",
|
|
name: "通用 hotfix workflow",
|
|
surface: "agent-board-console",
|
|
}],
|
|
}),
|
|
});
|
|
});
|
|
|
|
await page.route("**/api/page-ai/board/runs", async (route) => {
|
|
if (route.request().method() !== "POST") return route.continue();
|
|
const body = JSON.parse(route.request().postData() || "{}");
|
|
capturedBoardRunResponses.push({ kind: "create", body });
|
|
await route.fulfill({
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({
|
|
ok: true,
|
|
schema: "mnote.page_ai_board_run.v1",
|
|
surface: "mnote-page-ai",
|
|
runId: `board-run-${suffix}`,
|
|
workflowId: body.workflowId,
|
|
workerPresetId: body.workerPresetId,
|
|
modelOverride: body.modelOverride,
|
|
board: { run: { id: `board-run-${suffix}`, status: "running" } },
|
|
}),
|
|
});
|
|
});
|
|
|
|
await page.route(`**/api/page-ai/board/runs/board-run-${suffix}`, async (route) => {
|
|
capturedBoardRunResponses.push({ kind: "get" });
|
|
fs.writeFileSync(filePath, ["# Board First Page AI", "", `页面段落:${afterToken}`, ""].join("\n"), "utf8");
|
|
await route.fulfill({
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({
|
|
ok: true,
|
|
schema: "mnote.page_ai_board_run_status.v1",
|
|
runId: `board-run-${suffix}`,
|
|
board: {
|
|
run: { id: `board-run-${suffix}`, status: "complete" },
|
|
events: [
|
|
{ type: "workflow.started", message: "run started", createdAt: new Date().toISOString() },
|
|
{ type: "worker.completed", message: "file edited", createdAt: new Date().toISOString() },
|
|
],
|
|
},
|
|
receipt: {
|
|
schema: "agent_board.workflow_run_receipt.v2",
|
|
runId: `board-run-${suffix}`,
|
|
surface: "mnote-page-ai",
|
|
workflowId: "builtin-mnote-page-ai-chat",
|
|
workerPresetId: "mnote-page-ai-zcode",
|
|
modelOverride: "zcode-fast",
|
|
finalAnswer: `已把当前页面中的 ${beforeToken} 替换为 ${afterToken}。`,
|
|
changedFiles: [{ path: filePath, status: "modified" }],
|
|
verification: [{ command: "read file", status: "passed", output: afterToken }],
|
|
remaining: [],
|
|
},
|
|
}),
|
|
});
|
|
});
|
|
|
|
try {
|
|
await page.route("**/api/user/access-policy**", async (route) => {
|
|
await route.fulfill({
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({
|
|
ok: true,
|
|
controlPlane: "sqlite",
|
|
grants: [{
|
|
id: `grant_task762_${suffix}`,
|
|
userId: actorId,
|
|
workspaceId,
|
|
rootUri,
|
|
rootPath: root,
|
|
permission: "write",
|
|
recursive: true,
|
|
capabilities: ["ai", "markdown_edit"],
|
|
source: "smoke",
|
|
status: "active",
|
|
}],
|
|
}),
|
|
});
|
|
});
|
|
await page.route("**/api/ui/preferences**", async (route) => {
|
|
await route.fulfill({
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ ok: true, owner: "mnote-web", result: {} }),
|
|
});
|
|
});
|
|
|
|
await ensureAuthenticated(page, context.request);
|
|
const response = await page.goto(documentUrl(root, workspaceId, relativePath), {
|
|
waitUntil: "domcontentloaded",
|
|
timeout: UI_TIMEOUT_MS,
|
|
});
|
|
assert(response && response.status() === 200, `文档页状态码异常: ${response && response.status()}`);
|
|
await page.locator('[data-testid="mnote-leptos-tiptap-island-editor-root"] .ProseMirror').first().waitFor({
|
|
state: "visible",
|
|
timeout: UI_TIMEOUT_MS,
|
|
});
|
|
await waitForEditorText(page, beforeToken);
|
|
await page.evaluate(() => localStorage.removeItem("mnote.page_ai.legacy_provider_mode"));
|
|
|
|
await page.getByTestId("wolai-floating-ai").click({ timeout: UI_TIMEOUT_MS });
|
|
await page.locator('[data-testid="wolai-page-ai-drawer"]').waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
|
await page.locator("[data-page-ai-agent-button]").click({ timeout: UI_TIMEOUT_MS });
|
|
await page.waitForFunction(
|
|
() => {
|
|
const drawerText = document.querySelector('[data-testid="wolai-page-ai-drawer"]')?.textContent || "";
|
|
return drawerText.includes("MNote 页面 AI · ZCode") && !drawerText.includes("QA Browser Worker") && !drawerText.includes("通用 hotfix workflow");
|
|
},
|
|
null,
|
|
{ timeout: UI_TIMEOUT_MS },
|
|
);
|
|
const modelSelect = page.locator("[data-page-ai-board-model]");
|
|
await modelSelect.selectOption("zcode-fast", { timeout: UI_TIMEOUT_MS });
|
|
await page.keyboard.press("Escape").catch(() => undefined);
|
|
await page.locator("[data-page-ai-input]").fill(
|
|
`请编辑当前页面真实 Markdown 文件,把 ${beforeToken} 替换为 ${afterToken}。完成后说明 MNote capability 已加载。`,
|
|
{ timeout: UI_TIMEOUT_MS },
|
|
);
|
|
await page.locator('[data-page-ai-action="send"]').click({ timeout: UI_TIMEOUT_MS });
|
|
|
|
await page.waitForFunction(
|
|
(expected) => {
|
|
const text = document.querySelector('[data-testid="wolai-page-ai-drawer"]')?.textContent || "";
|
|
return text.includes("正在处理") || text.includes(expected);
|
|
},
|
|
afterToken,
|
|
{ timeout: UI_TIMEOUT_MS },
|
|
);
|
|
await page.waitForFunction(
|
|
() => {
|
|
const drawer = document.querySelector('[data-testid="wolai-page-ai-drawer"]');
|
|
const subtitle = drawer?.querySelector('.wolai-page-ai-subtitle')?.textContent || "";
|
|
const agentChip = drawer?.querySelector('[data-page-ai-agent-chip]')?.textContent || "";
|
|
const hiddenLegacyTabs = Array.from(drawer?.querySelectorAll('[data-page-ai-tab="agent"], [data-page-ai-tab="reasonix-settings"], [data-page-ai-tab="hermes-settings"]') || [])
|
|
.every((node) => node instanceof HTMLElement && node.hidden === true);
|
|
return subtitle.includes("Agent Board") && agentChip.includes("MNote 页面 AI") && hiddenLegacyTabs;
|
|
},
|
|
null,
|
|
{ timeout: UI_TIMEOUT_MS },
|
|
);
|
|
await page.waitForFunction(
|
|
() => document.documentElement.getAttribute("data-mnote-page-ai-run-status") === "completed",
|
|
null,
|
|
{ timeout: Math.max(UI_TIMEOUT_MS, 180_000) },
|
|
);
|
|
await page.waitForFunction(
|
|
(expected) => (document.querySelector('[data-testid="wolai-page-ai-drawer"]')?.textContent || "").includes(expected),
|
|
afterToken,
|
|
{ timeout: UI_TIMEOUT_MS },
|
|
);
|
|
|
|
const finalDiskContent = fs.readFileSync(filePath, "utf8");
|
|
assert(finalDiskContent.includes(afterToken), "磁盘文件应包含 Board worker 写入后的标记");
|
|
assert(!finalDiskContent.includes(beforeToken), "磁盘文件不应再包含旧标记");
|
|
await waitForEditorText(page, afterToken);
|
|
assert.strictEqual(capturedBoardRuns.length, 1, `应只创建一个 Board Page AI run,实际 ${capturedBoardRuns.length}`);
|
|
const runBody = capturedBoardRuns[0].body;
|
|
assert.strictEqual(runBody.workflowId, "builtin-mnote-page-ai-chat", "Page AI 应默认走 MNote 专用 Board workflow");
|
|
assert.strictEqual(runBody.workerPresetId, "mnote-page-ai-zcode", "Page AI 应默认走 MNote 专用 ZCode worker");
|
|
assert.strictEqual(runBody.modelOverride, "zcode-fast", "Page AI 应传递 worker 内模型档位");
|
|
assert.strictEqual(runBody.envelope?.schema, "mnote.page_ai.board_task.v1", "run payload 应携带 Board envelope");
|
|
assert.strictEqual(runBody.envelope?.modelOverride, "zcode-fast", "envelope 应记录模型档位");
|
|
assert.strictEqual(runBody.envelope?.primaryTarget?.absolutePath, filePath, "primaryTarget 应指向当前真实 Markdown 文件");
|
|
assert(Array.isArray(runBody.envelope?.capabilities), "envelope 应携带 MNote capabilities");
|
|
assert(runBody.envelope.capabilities.includes("mnote.current_page.read"), "capabilities 应包含当前页读取能力");
|
|
assert(runBody.envelope.capabilities.includes("mnote.local_file.receipt"), "capabilities 应包含本地文件收据能力");
|
|
|
|
const drawerText = await page.locator('[data-testid="wolai-page-ai-drawer"]').textContent({ timeout: UI_TIMEOUT_MS });
|
|
assert(!drawerText.includes("Agent Board run 已创建"), "默认聊天面不应把 Board run 创建日志作为 assistant 气泡显示");
|
|
assert(drawerText.includes(`已把当前页面中的 ${beforeToken} 替换为 ${afterToken}。`), "主聊天应展示 receipt.finalAnswer 自然回复");
|
|
assert(!drawerText.includes("## Completed"), "主聊天不应展示 Board Completed 报告标题");
|
|
await page.locator(`[data-page-ai-board-run-detail="board-run-${suffix}"]`).click({ timeout: UI_TIMEOUT_MS });
|
|
await page.waitForFunction(
|
|
(expectedPath) => {
|
|
const text = document.querySelector('[data-testid="wolai-page-ai-drawer"]')?.textContent || "";
|
|
return text.includes("Run detail") && text.includes("changed files") && text.includes(expectedPath) && text.includes("timeline");
|
|
},
|
|
filePath,
|
|
{ timeout: UI_TIMEOUT_MS },
|
|
);
|
|
await page.reload({ waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS });
|
|
await page.getByTestId("wolai-floating-ai").click({ timeout: UI_TIMEOUT_MS });
|
|
const restoredStorage = await page.evaluate(() => {
|
|
const keys = Object.keys(window.localStorage).filter((key) => key.startsWith("hermes_page_ai_session:"));
|
|
return JSON.stringify(keys.map((key) => ({ key, value: window.localStorage.getItem(key) || "" })));
|
|
});
|
|
if (!restoredStorage.includes(afterToken)) {
|
|
throw new assert.AssertionError({
|
|
message: `刷新后 localStorage 应保留 Board-first session/history: ${restoredStorage.slice(0, 1000)}`,
|
|
});
|
|
}
|
|
await page.waitForFunction(
|
|
(expected) => (document.querySelector('[data-testid="wolai-page-ai-drawer"]')?.textContent || "").includes(expected),
|
|
afterToken,
|
|
{ timeout: UI_TIMEOUT_MS },
|
|
);
|
|
const screenshot = await saveScreenshot(page, "01-board-first-edit");
|
|
const result = {
|
|
ok: true,
|
|
task: TASK,
|
|
baseUrl: BASE_URL,
|
|
root,
|
|
rootUri,
|
|
workspaceId,
|
|
documentId,
|
|
relativePath,
|
|
filePath,
|
|
beforeToken,
|
|
afterToken,
|
|
screenshot,
|
|
capturedBoardRuns,
|
|
capturedBoardRunResponses,
|
|
drawerText,
|
|
consoleErrors,
|
|
finalDiskContent,
|
|
};
|
|
fs.writeFileSync(RESULT_PATH, `${JSON.stringify(result, null, 2)}\n`, "utf8");
|
|
console.log(JSON.stringify(result, null, 2));
|
|
} catch (error) {
|
|
caughtError = error;
|
|
await saveScreenshot(page, "failure").catch(() => undefined);
|
|
fs.writeFileSync(
|
|
path.join(OUTPUT_DIR, "failure.json"),
|
|
`${JSON.stringify({
|
|
ok: false,
|
|
error: error instanceof Error ? error.stack || error.message : String(error),
|
|
capturedBoardRuns,
|
|
consoleErrors,
|
|
root,
|
|
rootUri,
|
|
filePath,
|
|
diskContent: fs.existsSync(filePath) ? fs.readFileSync(filePath, "utf8") : "",
|
|
}, null, 2)}\n`,
|
|
"utf8",
|
|
);
|
|
} finally {
|
|
await page.close().catch(() => undefined);
|
|
await context.close().catch(() => undefined);
|
|
await browser.close().catch(() => undefined);
|
|
}
|
|
|
|
if (caughtError) {
|
|
throw caughtError;
|
|
}
|
|
}
|
|
|
|
if (require.main === module) {
|
|
main().catch((error) => {
|
|
console.error(error instanceof Error ? error.stack || error.message : String(error));
|
|
process.exit(1);
|
|
});
|
|
}
|