221 lines
8.9 KiB
JavaScript
221 lines
8.9 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 = (process.env.MNOTE_WEB_SMOKE_BASE_URL || "http://127.0.0.1:3000").replace(/\/+$/, "");
|
|
const UI_TIMEOUT_MS = Number(process.env.MNOTE_SMOKE_UI_TIMEOUT_MS || 30_000);
|
|
const OUTPUT_DIR = path.join(process.cwd(), "tmp", "7-68-runtime");
|
|
const RESULT_PATH = path.join(OUTPUT_DIR, "task779-openhub-file-edit-document-pane-refresh-result.json");
|
|
const SCREENSHOT_PATH = path.join(OUTPUT_DIR, "task779-openhub-file-edit-document-pane-refresh.png");
|
|
const CHROMIUM_EXECUTABLE_PATH = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH
|
|
|| ["/usr/bin/chromium-browser", "/usr/bin/chromium", "/usr/bin/google-chrome-stable", "/usr/bin/google-chrome", "/snap/bin/chromium"]
|
|
.find((candidate) => fs.existsSync(candidate));
|
|
|
|
function fileUrl(localPath) {
|
|
return `file://${localPath}`;
|
|
}
|
|
|
|
function localMdDocumentId(relativePath) {
|
|
return `local-md:${relativePath.replaceAll("/", "~2F")}`;
|
|
}
|
|
|
|
function documentUrl(root, relativePath, workspaceId) {
|
|
const url = new URL(`${BASE_URL}/documents/${encodeURIComponent(localMdDocumentId(relativePath))}`);
|
|
url.searchParams.set("sourceKind", "local_folder");
|
|
url.searchParams.set("rootUri", fileUrl(root));
|
|
if (workspaceId) url.searchParams.set("workspaceId", workspaceId);
|
|
return url.toString();
|
|
}
|
|
|
|
function markdown(title, lines) {
|
|
return ["---", `title: ${title}`, "---", "", ...lines, ""].join("\n");
|
|
}
|
|
|
|
function writeWorkspaceManifest(root, ownerId, workspaceId) {
|
|
const metadataDir = path.join(root, ".mnote");
|
|
fs.mkdirSync(metadataDir, { recursive: true });
|
|
fs.writeFileSync(
|
|
path.join(metadataDir, "workspace.json"),
|
|
`${JSON.stringify({
|
|
workspaceId,
|
|
ownerId,
|
|
createdAt: new Date().toISOString(),
|
|
capabilities: ["local_files", "markdown_edit"],
|
|
}, null, 2)}\n`,
|
|
"utf8",
|
|
);
|
|
}
|
|
|
|
async function waitForEditorText(page, text) {
|
|
await page.waitForFunction(
|
|
(expected) => {
|
|
const editor = document.querySelector('.document-pane[data-pane-role="primary"] .editor-surface .ProseMirror');
|
|
return (editor?.textContent || "").includes(expected);
|
|
},
|
|
text,
|
|
{ timeout: UI_TIMEOUT_MS },
|
|
);
|
|
}
|
|
|
|
async function readDocumentPaneState(page) {
|
|
return page.evaluate(() => {
|
|
const root = document.querySelector('[data-testid="mnote-leptos-tiptap-island-editor-root"][data-pane-role="primary"]');
|
|
const pane = document.querySelector('.document-pane[data-pane-role="primary"]');
|
|
const editor = document.querySelector('.document-pane[data-pane-role="primary"] .editor-surface .ProseMirror');
|
|
const aggregateNode = document.getElementById("__MNOTE_PAGE_AGGREGATE__");
|
|
let aggregate = null;
|
|
try {
|
|
aggregate = JSON.parse(aggregateNode?.textContent || "null");
|
|
} catch {}
|
|
return {
|
|
paneDocumentId: pane?.getAttribute("data-pane-document-id") || "",
|
|
runtimeStatus: root?.getAttribute("data-runtime-editor-status") || "",
|
|
runtimeError: root?.getAttribute("data-runtime-editor-error") || "",
|
|
editorText: editor?.textContent || "",
|
|
aggregateText: JSON.stringify(aggregate?.body || aggregate || {}),
|
|
syncedAt: aggregateNode?.getAttribute("data-mnote-page-aggregate-synced-at") || "",
|
|
openhubRefreshMarker: document.documentElement.getAttribute("data-mnote-page-ai-openhub-document-pane-refresh") || "",
|
|
eventBusSource: document.documentElement.getAttribute("data-mnote-local-folder-event-bus-last-source") || "",
|
|
eventBusReason: document.documentElement.getAttribute("data-mnote-local-folder-event-bus-last-reason") || "",
|
|
documentSessionDebug: window.__mnoteDebugDocumentSessions?.snapshot?.() || null,
|
|
};
|
|
});
|
|
}
|
|
|
|
async function openDocument(page, root, relativePath, workspaceId) {
|
|
await page.goto(documentUrl(root, relativePath, workspaceId), { waitUntil: "commit", timeout: UI_TIMEOUT_MS });
|
|
await page.locator('[data-testid="mnote-leptos-tiptap-island-editor-root"]').first().waitFor({
|
|
state: "visible",
|
|
timeout: UI_TIMEOUT_MS,
|
|
});
|
|
await page.locator('.document-pane[data-pane-role="primary"] .editor-surface .ProseMirror').first().waitFor({
|
|
state: "visible",
|
|
timeout: UI_TIMEOUT_MS,
|
|
});
|
|
}
|
|
|
|
async function waitForPageAiRuntime(page) {
|
|
await page.waitForFunction(
|
|
() => typeof window.__mnoteSidebarPageAiRuntime?.openPageAiDrawer === "function"
|
|
&& typeof window.__mnoteDocumentPaneRuntime?.refreshPrimaryDocument === "function"
|
|
&& typeof window.__mnoteDocumentPaneRuntime?.openPrimaryDocument === "function",
|
|
null,
|
|
{ timeout: UI_TIMEOUT_MS },
|
|
);
|
|
}
|
|
|
|
async function installOpenHubChangedFileBridge(page) {
|
|
await page.evaluate(() => {
|
|
try {
|
|
localStorage.setItem("mnote.page_ai.openhub_host", "1");
|
|
} catch {}
|
|
window.__mnoteSidebarPageAiRuntime.openPageAiDrawer();
|
|
});
|
|
await page.locator('[data-testid="wolai-page-ai-drawer"][data-page-ai-openhub-host="true"]').first().waitFor({
|
|
state: "visible",
|
|
timeout: UI_TIMEOUT_MS,
|
|
});
|
|
}
|
|
|
|
async function simulateOpenHubChangedFile(page, root, relativePath, workspaceId) {
|
|
return page.evaluate(({ rootUri, path, workspaceId }) => {
|
|
window.postMessage({
|
|
type: "mnote:open-file",
|
|
source: "openhub-changed-files",
|
|
path,
|
|
rootUri,
|
|
workspaceId,
|
|
documentId: `local-md:${path.replaceAll("/", "~2F")}`,
|
|
}, window.location.origin);
|
|
return true;
|
|
}, { rootUri: fileUrl(root), path: relativePath, workspaceId });
|
|
}
|
|
|
|
async function run() {
|
|
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
|
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), "mnote-task779-openhub-pane-refresh-"));
|
|
const workspaceId = `local-ws:user_real:task779:${Date.now()}`;
|
|
const relativePath = "task779-openhub-refresh.md";
|
|
const initialText = "task779 initial document pane text";
|
|
const changedText = `task779 openhub changed file bridge ${Date.now()}`;
|
|
writeWorkspaceManifest(root, "user_real", workspaceId);
|
|
fs.writeFileSync(path.join(root, relativePath), markdown("Task 779 OpenHub Refresh", [initialText]), "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: 1360, height: 900 },
|
|
locale: "zh-CN",
|
|
extraHTTPHeaders: {
|
|
"x-mnote-actor-id": "user_real",
|
|
"x-mnote-actor-type": "user",
|
|
},
|
|
});
|
|
const page = await context.newPage();
|
|
const debug = { root, workspaceId, relativePath, initialText, changedText };
|
|
|
|
try {
|
|
await openDocument(page, root, relativePath, workspaceId);
|
|
await waitForEditorText(page, initialText);
|
|
await waitForPageAiRuntime(page);
|
|
await installOpenHubChangedFileBridge(page);
|
|
debug.before = await readDocumentPaneState(page);
|
|
|
|
fs.writeFileSync(
|
|
path.join(root, relativePath),
|
|
markdown("Task 779 OpenHub Refresh", [changedText]),
|
|
"utf8",
|
|
);
|
|
|
|
const opened = await simulateOpenHubChangedFile(page, root, relativePath, workspaceId);
|
|
assert.equal(opened, true, "OpenHub changed-file bridge 应接受当前 Markdown path");
|
|
await waitForEditorText(page, changedText);
|
|
debug.after = await readDocumentPaneState(page);
|
|
assert.equal(debug.after.paneDocumentId, localMdDocumentId(relativePath), "primary document pane 应仍打开测试 Markdown");
|
|
assert(debug.after.editorText.includes(changedText), `document pane 应显示磁盘新内容: ${debug.after.editorText}`);
|
|
assert(!debug.after.editorText.includes(initialText), `document pane 不应保留旧正文: ${debug.after.editorText}`);
|
|
assert.equal(debug.after.openhubRefreshMarker, relativePath, "应记录 OpenHub document pane refresh marker");
|
|
assert.equal(debug.after.eventBusSource, "openhub_changed_file_bridge", "应复用 local-folder event bus synthetic watch batch");
|
|
|
|
await page.screenshot({ path: SCREENSHOT_PATH, fullPage: true });
|
|
const result = {
|
|
ok: true,
|
|
task: "task779-openhub-file-edit-document-pane-refresh-smoke",
|
|
root,
|
|
relativePath,
|
|
documentId: localMdDocumentId(relativePath),
|
|
changedText,
|
|
beforeText: debug.before.editorText,
|
|
afterText: debug.after.editorText,
|
|
eventBusSource: debug.after.eventBusSource,
|
|
eventBusReason: debug.after.eventBusReason,
|
|
resultPath: RESULT_PATH,
|
|
screenshotPath: SCREENSHOT_PATH,
|
|
};
|
|
fs.writeFileSync(RESULT_PATH, `${JSON.stringify(result, null, 2)}\n`, "utf8");
|
|
console.log(`ok task779-openhub-file-edit-document-pane-refresh-smoke ${RESULT_PATH}`);
|
|
} catch (error) {
|
|
fs.writeFileSync(RESULT_PATH, `${JSON.stringify({
|
|
ok: false,
|
|
error: String(error && error.stack || error),
|
|
debug,
|
|
}, null, 2)}\n`, "utf8");
|
|
throw error;
|
|
} finally {
|
|
await browser.close().catch(() => {});
|
|
fs.rmSync(root, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
run().catch((error) => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|