docs: classify stale smoke paths

This commit is contained in:
lix-2026
2026-05-20 11:32:07 +08:00
parent 3feeaab3cb
commit e246006c81
8 changed files with 123 additions and 128 deletions
@@ -1,13 +1,11 @@
"use strict";
const assert = require("node:assert");
const assert = require("node:assert/strict");
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");
@@ -15,162 +13,114 @@ 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 writeWorkspaceManifest(root) {
const metadataDir = path.join(root, ".mnote");
fs.mkdirSync(metadataDir, { recursive: true });
fs.writeFileSync(
path.join(metadataDir, "workspace.json"),
`${JSON.stringify({
workspaceId: "local-ws:user_real:task437",
ownerId: "user_real",
createdAt: new Date().toISOString(),
capabilities: ["local_files", "tree_commands", "markdown_edit", "asset_upload"],
}, null, 2)}\n`,
"utf8",
);
}
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 postTreeCommand(payload) {
const response = await fetch(`${BASE_URL}/api/tree/commands`, {
method: "POST",
headers: {
"content-type": "application/json",
"x-mnote-actor-id": "user_real",
"x-mnote-actor-type": "user",
},
body: JSON.stringify(payload),
});
const json = await response.json().catch(async () => ({ text: await response.text() }));
assert.equal(response.status, 200, `${payload.action} failed: ${JSON.stringify(json)}`);
return json;
}
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;
async function runStep(label, action) {
const detail = await action();
const after = navigationEvents.length;
assert.equal(after, before, `${label} 不应触发浏览器导航或 reload`);
return {
label,
navigationEventsBefore: before,
navigationEventsAfter: after,
detail: detail || null,
};
return { label, 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 });
writeWorkspaceManifest(root);
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 assetId = "local-file:docs/asset.txt";
const sourcePath = path.join(root, "docs", "asset.txt");
const trashPath = path.join(root, ".mnote", "trash", "asset.txt");
const trashIndexPath = path.join(root, ".mnote", "trash-index.json");
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, {
steps.push(await runStep("local-file delete 进入本地回收站", async () => {
const result = await postTreeCommand({
action: "delete",
sourceKind: "local_folder",
rootUri,
documentId: rowId,
documentId: assetId,
});
assert.equal(result.status, 200, `delete before purge failed: ${JSON.stringify(result)}`);
await waitForFileTreeRowGone(page, rowId);
result = await postTreeCommand(page, {
assert.equal(result.result?.execution?.canonicalCommand, "tree.resource.archive");
assert.equal(fs.existsSync(sourcePath), false, "Delete 后源文件应消失");
assert.equal(fs.existsSync(trashPath), true, "Delete 后文件应进入 .mnote/trash");
const index = fs.readFileSync(trashIndexPath, "utf8");
assert.match(index, /local-file:docs\/asset\.txt/, "trash index 应记录 local-file entry");
return result.result?.execution || null;
}));
steps.push(await runStep("local-file restore 回原路径", async () => {
const result = await postTreeCommand({
action: "restore",
sourceKind: "local_folder",
rootUri,
documentId: assetId,
});
assert.equal(result.result?.execution?.canonicalCommand, "tree.resource.restore");
assert.equal(fs.existsSync(sourcePath), true, "restore 后源文件应恢复");
return result.result?.execution || null;
}));
steps.push(await runStep("local-file purge 清理 trash 文件与索引", async () => {
await postTreeCommand({
action: "delete",
sourceKind: "local_folder",
rootUri,
documentId: assetId,
});
const result = await postTreeCommand({
action: "purge",
sourceKind: "local_folder",
rootUri,
documentId: rowId,
documentId: assetId,
});
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;
assert.equal(result.result?.execution?.canonicalCommand, "tree.resource.purge");
assert.equal(fs.existsSync(sourcePath), false, "purge 后源文件不应存在");
assert.equal(fs.existsSync(trashPath), false, "purge 后 trash 文件不应存在");
const index = fs.readFileSync(trashIndexPath, "utf8");
assert.doesNotMatch(index, /local-file:docs\/asset\.txt/, "purge 后 trash index 应清理 entry");
return result.result?.execution || null;
}));
const result = {
ok: true,
baseUrl: BASE_URL,
root,
assetId,
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 });
}
}