Files
mnote/scripts/task435-local-folder-watch-no-reload-smoke.js
T
lix-2026 384da4e44c feat(tree): checkpoint resource lifecycle work
提交当前顶层 mnote Git 工作区,范围集中在 04-tree-domain 的 resource/trash/filetree 生命周期、mnote-web resource_trash 路由、Convex/Next 兼容接口、sidebar/file-tree 客户端适配、smoke 脚本与对应设计/bug 记录。

不包含被 ignore 的 design/05-editor-mainline/reference-code/leptos-tiptap 嵌套仓库改动。新增 smoke 的测试密码改为运行时读取 MNOTE_E2E_PASSWORD,避免提交明文 credential assignment。
2026-05-16 07:38:45 +08:00

247 lines
10 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use strict";
const fs = require("fs");
const os = require("os");
const path = require("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", "task435-local-folder-watch-no-reload-smoke");
const RESULT_PATH = path.join(OUTPUT_DIR, "result.json");
function assert(condition, message) {
if (!condition) {
throw new Error(message);
}
}
function fileUrl(localPath) {
return `file://${localPath}`;
}
function treeUrl(root, mode) {
const url = new URL(`${BASE_URL}/`);
url.searchParams.set("treeView", mode);
url.searchParams.set("sourceKind", "local_folder");
url.searchParams.set("rootUri", fileUrl(root));
return url.toString();
}
function localMdDocumentId(relativePath) {
return `local-md:${relativePath.replaceAll("/", "~2F")}`;
}
async function quickLogin(page) {
await page.goto(`${BASE_URL}/auth`, { waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS });
const quickLoginButton = page.getByRole("button", { name: "测试账号快速登录" });
if (await quickLoginButton.count()) {
await quickLoginButton.click({ timeout: UI_TIMEOUT_MS });
await page.waitForURL((url) => url.pathname === "/", { timeout: UI_TIMEOUT_MS });
}
}
async function waitForVisibleText(page, text) {
await page.waitForFunction(
(expectedText) => Array.from(document.querySelectorAll("body *")).some((element) => {
const textContent = element.textContent ? element.textContent.trim() : "";
if (textContent !== expectedText) return false;
const style = window.getComputedStyle(element);
const rect = element.getBoundingClientRect();
return style.display !== "none" && style.visibility !== "hidden" && rect.width > 0 && rect.height > 0;
}),
text,
{ timeout: UI_TIMEOUT_MS },
);
}
async function waitForGone(page, selector) {
await page.locator(selector).waitFor({ state: "detached", timeout: UI_TIMEOUT_MS });
}
async function waitForFileTreeRow(page, rowId) {
await page.locator(`.tree-row[data-row-id="${rowId}"]`).waitFor({
state: "visible",
timeout: UI_TIMEOUT_MS,
});
}
async function waitForFileTreeRowGone(page, rowId) {
await waitForGone(page, `.tree-row[data-row-id="${rowId}"]`);
}
async function waitForPageTreeNode(page, documentId) {
await page.waitForFunction(
(expectedDocumentId) => Array.from(document.querySelectorAll("#sidebar-tree-root .tree-row[data-node-id]"))
.some((row) => row.getAttribute("data-node-id") === expectedDocumentId),
documentId,
{ timeout: UI_TIMEOUT_MS },
);
}
async function waitForPageTreeNodeGone(page, documentId) {
await page.waitForFunction(
(expectedDocumentId) => !Array.from(document.querySelectorAll("#sidebar-tree-root .tree-row[data-node-id]"))
.some((row) => row.getAttribute("data-node-id") === expectedDocumentId),
documentId,
{ timeout: UI_TIMEOUT_MS },
);
}
async function runStep(label, navigationEvents, action) {
const before = navigationEvents.length;
await action();
const after = navigationEvents.length;
assert(after === before, `${label} 不应触发浏览器导航或 reloadbefore=${before} after=${after}`);
return { label, navigationEventsBefore: before, navigationEventsAfter: after };
}
async function run() {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
const root = fs.mkdtempSync(path.join(os.tmpdir(), "mnote-local-watch-no-reload-"));
fs.mkdirSync(path.join(root, "docs"), { recursive: true });
fs.mkdirSync(path.join(root, ".mnote"), { recursive: true });
fs.writeFileSync(path.join(root, "README.md"), "# Local Root\n", "utf8");
fs.writeFileSync(path.join(root, "docs", "stable.md"), "# Stable Page\n", "utf8");
fs.writeFileSync(path.join(root, "docs", "stable-asset.txt"), "stable asset", "utf8");
const browser = await chromium.launch({ headless: true });
const context = await browser.newContext({
viewport: { width: 1280, height: 860 },
extraHTTPHeaders: {
"x-mnote-actor-id": "user_real",
"x-mnote-actor-type": "user",
},
});
const page = await context.newPage();
const navigationEvents = [];
page.on("framenavigated", (frame) => {
if (frame === page.mainFrame()) {
navigationEvents.push({ url: frame.url(), timestamp: Date.now() });
}
});
const steps = [];
try {
await quickLogin(page);
await page.goto(treeUrl(root, "page"), { waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS });
await waitForVisibleText(page, "Local Root");
await waitForVisibleText(page, "Stable Page");
await page.waitForResponse((response) => response.url().includes("/api/tree/local-folder-watch") && response.ok(), {
timeout: UI_TIMEOUT_MS,
}).catch(() => {});
await page.waitForTimeout(100);
navigationEvents.length = 0;
steps.push(await runStep("Markdown 外部创建后 page tree 原地更新", navigationEvents, async () => {
fs.writeFileSync(path.join(root, "docs", "watcher-create.md"), "# Watcher Create\n", "utf8");
await waitForPageTreeNode(page, localMdDocumentId("docs/watcher-create.md"));
}));
steps.push(await runStep("Markdown 外部重命名后 page tree 原地更新", navigationEvents, async () => {
fs.renameSync(
path.join(root, "docs", "watcher-create.md"),
path.join(root, "docs", "watcher-renamed.md"),
);
await waitForPageTreeNode(page, localMdDocumentId("docs/watcher-renamed.md"));
await waitForPageTreeNodeGone(page, localMdDocumentId("docs/watcher-create.md"));
}));
steps.push(await runStep("Markdown 外部删除后 page tree 原地更新", navigationEvents, async () => {
fs.rmSync(path.join(root, "docs", "watcher-renamed.md"));
await waitForPageTreeNodeGone(page, localMdDocumentId("docs/watcher-renamed.md"));
}));
await page.goto(treeUrl(root, "filetree"), { waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS });
await waitForFileTreeRow(page, "local:folder:docs");
await waitForFileTreeRow(page, "local:asset:docs/stable-asset.txt");
await page.waitForResponse((response) => response.url().includes("/api/tree/local-folder-watch") && response.ok(), {
timeout: UI_TIMEOUT_MS,
}).catch(() => {});
await page.waitForTimeout(100);
navigationEvents.length = 0;
steps.push(await runStep("Markdown 外部创建后 filetree 原地更新", navigationEvents, async () => {
fs.writeFileSync(path.join(root, "docs", "watcher-filetree-md.md"), "# Watcher Filetree Markdown\n", "utf8");
await waitForFileTreeRow(page, "local:markdown:docs/watcher-filetree-md.md");
}));
steps.push(await runStep("Markdown 外部重命名后 filetree 原地更新", navigationEvents, async () => {
fs.renameSync(
path.join(root, "docs", "watcher-filetree-md.md"),
path.join(root, "docs", "watcher-filetree-md-renamed.md"),
);
await waitForFileTreeRow(page, "local:markdown:docs/watcher-filetree-md-renamed.md");
await waitForFileTreeRowGone(page, "local:markdown:docs/watcher-filetree-md.md");
}));
steps.push(await runStep("Markdown 外部删除后 filetree 原地更新", navigationEvents, async () => {
fs.rmSync(path.join(root, "docs", "watcher-filetree-md-renamed.md"));
await waitForFileTreeRowGone(page, "local:markdown:docs/watcher-filetree-md-renamed.md");
}));
steps.push(await runStep("非 md 资源外部创建后 filetree 原地更新", navigationEvents, async () => {
fs.writeFileSync(path.join(root, "docs", "watcher-asset.txt"), "watcher asset", "utf8");
await waitForFileTreeRow(page, "local:asset:docs/watcher-asset.txt");
}));
steps.push(await runStep("非 md 资源外部重命名后 filetree 原地更新", navigationEvents, async () => {
fs.renameSync(
path.join(root, "docs", "watcher-asset.txt"),
path.join(root, "docs", "watcher-asset-renamed.txt"),
);
await waitForFileTreeRow(page, "local:asset:docs/watcher-asset-renamed.txt");
await waitForFileTreeRowGone(page, "local:asset:docs/watcher-asset.txt");
}));
steps.push(await runStep("非 md 资源外部删除后 filetree 原地更新", navigationEvents, async () => {
fs.rmSync(path.join(root, "docs", "watcher-asset-renamed.txt"));
await waitForFileTreeRowGone(page, "local:asset:docs/watcher-asset-renamed.txt");
}));
steps.push(await runStep("第二类非 md 资源外部创建后 filetree 原地更新", navigationEvents, async () => {
fs.writeFileSync(path.join(root, "docs", "watcher-image.png"), "png", "utf8");
await waitForFileTreeRow(page, "local:asset:docs/watcher-image.png");
}));
steps.push(await runStep("第二类非 md 资源外部重命名后 filetree 原地更新", navigationEvents, async () => {
fs.renameSync(
path.join(root, "docs", "watcher-image.png"),
path.join(root, "docs", "watcher-image-renamed.png"),
);
await waitForFileTreeRow(page, "local:asset:docs/watcher-image-renamed.png");
await waitForFileTreeRowGone(page, "local:asset:docs/watcher-image.png");
}));
steps.push(await runStep("第二类非 md 资源外部删除后 filetree 原地更新", navigationEvents, async () => {
fs.rmSync(path.join(root, "docs", "watcher-image-renamed.png"));
await waitForFileTreeRowGone(page, "local:asset:docs/watcher-image-renamed.png");
}));
const result = {
ok: true,
baseUrl: BASE_URL,
root,
steps,
navigationEvents,
};
fs.writeFileSync(RESULT_PATH, `${JSON.stringify(result, null, 2)}\n`, "utf8");
console.log(`task435 local folder watcher no-reload smoke passed: ${RESULT_PATH}`);
} finally {
await browser.close().catch(() => {});
fs.rmSync(root, { recursive: true, force: true });
}
}
run().catch((error) => {
const result = {
ok: false,
baseUrl: BASE_URL,
error: error && error.stack ? error.stack : String(error),
};
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
fs.writeFileSync(RESULT_PATH, `${JSON.stringify(result, null, 2)}\n`, "utf8");
console.error(error);
process.exit(1);
});