Files
mnote/scripts/task437-local-folder-asset-trash-lifecycle-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

189 lines
7.1 KiB
JavaScript

"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", "task437-local-folder-asset-trash-lifecycle-smoke");
const RESULT_PATH = path.join(OUTPUT_DIR, "result.json");
function fileUrl(localPath) {
return `file://${localPath}`;
}
function treeUrl(root) {
const url = new URL(`${BASE_URL}/`);
url.searchParams.set("treeView", "filetree");
url.searchParams.set("sourceKind", "local_folder");
url.searchParams.set("rootUri", fileUrl(root));
return url.toString();
}
function commandUrl() {
return `${BASE_URL}/api/tree/commands`;
}
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 page.locator(`.tree-row[data-row-id="${rowId}"]`).waitFor({
state: "detached",
timeout: UI_TIMEOUT_MS,
});
}
async function postTreeCommand(page, payload) {
return await page.evaluate(async ({ url, body }) => {
const response = await fetch(url, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(body),
});
const json = await response.json().catch(() => null);
return { status: response.status, json };
}, { url: commandUrl(), body: payload });
}
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 runStep(label, navigationEvents, action) {
const before = navigationEvents.length;
const detail = await action();
const after = navigationEvents.length;
assert.equal(after, before, `${label} 不应触发浏览器导航或 reload`);
return {
label,
navigationEventsBefore: before,
navigationEventsAfter: after,
detail: detail || null,
};
}
async function run() {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
const root = fs.mkdtempSync(path.join(os.tmpdir(), "mnote-local-asset-trash-smoke-"));
fs.mkdirSync(path.join(root, "docs"), { recursive: true });
fs.writeFileSync(path.join(root, "README.md"), "# Local Root\n", "utf8");
fs.writeFileSync(path.join(root, "docs", "asset.txt"), "asset body", "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 rowId = "local:asset:docs/asset.txt";
const rootUri = fileUrl(root);
const steps = [];
try {
page.on("dialog", async (dialog) => {
if (dialog.type() === "confirm") await dialog.accept();
else await dialog.dismiss().catch(() => {});
});
await quickLogin(page);
await page.goto(treeUrl(root), { waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS });
await waitForFileTreeRow(page, rowId);
navigationEvents.length = 0;
steps.push(await runStep("local asset Delete 进入本地回收站", navigationEvents, async () => {
await page.locator(`.tree-row[data-row-id="${rowId}"]`).click({ timeout: UI_TIMEOUT_MS });
await page.keyboard.press("Delete");
await waitForFileTreeRowGone(page, rowId);
assert(!fs.existsSync(path.join(root, "docs", "asset.txt")), "Delete 后源文件应消失");
assert(fs.existsSync(path.join(root, ".mnote", "trash", "asset.txt")), "Delete 后文件应进入 .mnote/trash");
const index = fs.readFileSync(path.join(root, ".mnote", "trash-index.json"), "utf8");
assert(index.includes("local-file:docs/asset.txt"), "trash index 应记录 local_file entry");
return { trashIndexHasLocalFile: true };
}));
steps.push(await runStep("local asset restore 回原路径", navigationEvents, async () => {
const result = await postTreeCommand(page, {
action: "restore",
sourceKind: "local_folder",
rootUri,
documentId: rowId,
});
assert.equal(result.status, 200, `restore failed: ${JSON.stringify(result)}`);
assert.equal(result.json?.result?.execution?.canonicalCommand, "tree.resource.restore");
await waitForFileTreeRow(page, rowId);
assert(fs.existsSync(path.join(root, "docs", "asset.txt")), "restore 后源文件应恢复");
return result.json?.result?.execution || null;
}));
steps.push(await runStep("local asset purge 清理 trash 文件与索引", navigationEvents, async () => {
let result = await postTreeCommand(page, {
action: "delete",
sourceKind: "local_folder",
rootUri,
documentId: rowId,
});
assert.equal(result.status, 200, `delete before purge failed: ${JSON.stringify(result)}`);
await waitForFileTreeRowGone(page, rowId);
result = await postTreeCommand(page, {
action: "purge",
sourceKind: "local_folder",
rootUri,
documentId: rowId,
});
assert.equal(result.status, 200, `purge failed: ${JSON.stringify(result)}`);
assert.equal(result.json?.result?.execution?.canonicalCommand, "tree.resource.purge");
assert(!fs.existsSync(path.join(root, "docs", "asset.txt")), "purge 后源文件不应存在");
assert(!fs.existsSync(path.join(root, ".mnote", "trash", "asset.txt")), "purge 后 trash 文件不应存在");
const index = fs.readFileSync(path.join(root, ".mnote", "trash-index.json"), "utf8");
assert(!index.includes("local-file:docs/asset.txt"), "purge 后 trash index 应清理 entry");
return result.json?.result?.execution || null;
}));
const result = {
ok: true,
baseUrl: BASE_URL,
root,
steps,
navigationEvents,
};
fs.writeFileSync(RESULT_PATH, `${JSON.stringify(result, null, 2)}\n`, "utf8");
console.log(`task437 local folder asset trash lifecycle 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 && error.stack ? error.stack : error);
process.exit(1);
});