fix local folder resource lifecycle and workspace switch
This commit is contained in:
@@ -109,6 +109,96 @@ async function waitForFileExists(filePath, timeoutMs) {
|
||||
return fs.existsSync(filePath);
|
||||
}
|
||||
|
||||
async function waitForFileContent(filePath, predicate, timeoutMs) {
|
||||
const startedAt = Date.now();
|
||||
let lastContent = "";
|
||||
while (Date.now() - startedAt < timeoutMs) {
|
||||
if (fs.existsSync(filePath)) {
|
||||
lastContent = fs.readFileSync(filePath, "utf8");
|
||||
if (predicate(lastContent)) return { ok: true, content: lastContent };
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 150));
|
||||
}
|
||||
return { ok: false, content: lastContent };
|
||||
}
|
||||
|
||||
async function waitForPrimaryAttachment(page, fileName) {
|
||||
const link = page
|
||||
.locator('.document-pane[data-pane-role="primary"] .editor-surface .ProseMirror a[href*="/api/local-folder/files/open"]', {
|
||||
hasText: fileName,
|
||||
})
|
||||
.first();
|
||||
await link.waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
await page.waitForFunction(
|
||||
(name) => {
|
||||
const editor = document.querySelector('.document-pane[data-pane-role="primary"] .editor-surface .ProseMirror');
|
||||
const link = Array.from(editor?.querySelectorAll('a[href*="/api/local-folder/files/open"]') || [])
|
||||
.find((node) => node instanceof HTMLAnchorElement && (node.textContent || "").includes(name));
|
||||
return link instanceof HTMLAnchorElement
|
||||
&& (link.classList.contains("mnote-uploaded-attachment-row") || getComputedStyle(link).display === "inline-flex");
|
||||
},
|
||||
fileName,
|
||||
{ timeout: UI_TIMEOUT_MS },
|
||||
);
|
||||
return link;
|
||||
}
|
||||
|
||||
async function selectPrimaryAttachmentLink(page, fileName) {
|
||||
await page.evaluate((name) => {
|
||||
const editor = document.querySelector('.document-pane[data-pane-role="primary"] .editor-surface .ProseMirror');
|
||||
if (!(editor instanceof HTMLElement)) throw new Error("editor_missing");
|
||||
const link = Array.from(editor.querySelectorAll("a")).find((node) => (
|
||||
node instanceof HTMLAnchorElement && (node.textContent || "").includes(name)
|
||||
));
|
||||
if (!(link instanceof HTMLAnchorElement)) throw new Error(`attachment_link_missing:${name}`);
|
||||
const range = document.createRange();
|
||||
range.selectNode(link);
|
||||
const selection = window.getSelection();
|
||||
if (!selection) throw new Error("selection_missing");
|
||||
selection.removeAllRanges();
|
||||
selection.addRange(range);
|
||||
editor.focus();
|
||||
}, fileName);
|
||||
}
|
||||
|
||||
async function deleteAttachmentByKeyboard(page, fileName) {
|
||||
await waitForPrimaryAttachment(page, fileName);
|
||||
await selectPrimaryAttachmentLink(page, fileName);
|
||||
await page.keyboard.press("Backspace");
|
||||
}
|
||||
|
||||
async function deleteAttachmentByBlockHandle(page, fileName) {
|
||||
const link = await waitForPrimaryAttachment(page, fileName);
|
||||
const box = await link.boundingBox();
|
||||
assert(box, `附件 ${fileName} 缺少可点击区域`);
|
||||
await page.mouse.move(box.x + Math.min(12, box.width / 2), box.y + box.height / 2, { steps: 8 });
|
||||
const trigger = page.locator('[data-testid="block-drag-handle-trigger"]').first();
|
||||
await trigger.waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
await trigger.click({ timeout: UI_TIMEOUT_MS });
|
||||
const menu = page.locator('[data-testid="block-drag-menu"]').first();
|
||||
await menu.waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
|
||||
await page.locator('[data-testid="block-drag-menu-item-delete"]').first().click({ timeout: UI_TIMEOUT_MS });
|
||||
}
|
||||
|
||||
function writeTwoAttachmentPage(root, relativePath, resourceDirName, heading) {
|
||||
const resourceDir = path.join(root, resourceDirName);
|
||||
fs.mkdirSync(resourceDir, { recursive: true });
|
||||
fs.writeFileSync(path.join(resourceDir, "first.md"), "# First\n\n第一个附件\n", "utf8");
|
||||
fs.writeFileSync(path.join(resourceDir, "second.md"), "# Second\n\n第二个附件\n", "utf8");
|
||||
fs.writeFileSync(
|
||||
path.join(root, relativePath),
|
||||
[
|
||||
`# ${heading}`,
|
||||
"",
|
||||
`[first.md](${resourceDirName}/first.md)`,
|
||||
"",
|
||||
`[second.md](${resourceDirName}/second.md)`,
|
||||
"",
|
||||
].join("\n"),
|
||||
"utf8",
|
||||
);
|
||||
}
|
||||
|
||||
// ─── main ────────────────────────────────────────────────────────────────────
|
||||
|
||||
async function main() {
|
||||
@@ -141,6 +231,10 @@ async function main() {
|
||||
// 为 Check 6 准备一个带 H1 的文件(文件名不含空格,避免空格编码干扰测试)
|
||||
fs.writeFileSync(path.join(root, "MyNotes.md"), "# Body Heading\n\n正文内容\n", "utf8");
|
||||
|
||||
// 为 Check 7 准备两个删除附件引用的独立页面,避免不同删除路径互相污染。
|
||||
writeTwoAttachmentPage(root, "DeleteKeyboard.md", "DeleteKeyboard", "Delete Keyboard");
|
||||
writeTwoAttachmentPage(root, "DeleteHandle.md", "DeleteHandle", "Delete Handle");
|
||||
|
||||
// 为 Check 5 准备足够的文件树条目来产生可滚动区域
|
||||
fs.mkdirSync(path.join(root, "docs"), { recursive: true });
|
||||
for (let i = 0; i < 30; i++) {
|
||||
@@ -586,6 +680,99 @@ async function main() {
|
||||
}
|
||||
}
|
||||
|
||||
// ──────────────────────────────────────────────────────────────────────────
|
||||
// Check 7: 主编辑区删除一个附件引用时,只删除正文链接,不级联删除真实文件;
|
||||
// 相邻附件仍保持可点击附件块,手柄删除刷新后不回来。
|
||||
// ──────────────────────────────────────────────────────────────────────────
|
||||
{
|
||||
const checkId = "editor-delete-one-attachment-preserves-adjacent";
|
||||
try {
|
||||
const keyboardPath = "DeleteKeyboard.md";
|
||||
const keyboardFile = path.join(root, keyboardPath);
|
||||
await page.goto(documentUrl(root, keyboardPath), {
|
||||
waitUntil: "domcontentloaded",
|
||||
timeout: UI_TIMEOUT_MS,
|
||||
});
|
||||
await page.locator('.document-pane[data-pane-role="primary"] .editor-surface .ProseMirror').first().waitFor({
|
||||
state: "visible",
|
||||
timeout: UI_TIMEOUT_MS,
|
||||
});
|
||||
await waitForPrimaryAttachment(page, "first.md");
|
||||
await waitForPrimaryAttachment(page, "second.md");
|
||||
await deleteAttachmentByKeyboard(page, "second.md");
|
||||
|
||||
const keyboardPersisted = await waitForFileContent(
|
||||
keyboardFile,
|
||||
(content) => content.includes("[first.md](DeleteKeyboard/first.md)") && !content.includes("second.md"),
|
||||
UI_TIMEOUT_MS,
|
||||
);
|
||||
assert(
|
||||
keyboardPersisted.ok,
|
||||
`Backspace 删除第二个附件后,Markdown 应保留 first 链接且移除 second。实际内容:\n${keyboardPersisted.content}`,
|
||||
);
|
||||
assert(fs.existsSync(path.join(root, "DeleteKeyboard", "first.md")), "Backspace 删除引用不应删除 first.md 真实文件");
|
||||
assert(fs.existsSync(path.join(root, "DeleteKeyboard", "second.md")), "Backspace 删除引用不应删除 second.md 真实文件");
|
||||
|
||||
await page.reload({ waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS });
|
||||
await waitForPrimaryAttachment(page, "first.md");
|
||||
const keyboardSecondVisible = await page
|
||||
.locator('.document-pane[data-pane-role="primary"] .editor-surface .ProseMirror a', { hasText: "second.md" })
|
||||
.first()
|
||||
.isVisible()
|
||||
.catch(() => false);
|
||||
assert(!keyboardSecondVisible, "Backspace 删除后刷新,second.md 引用不应回来");
|
||||
|
||||
const handlePath = "DeleteHandle.md";
|
||||
const handleFile = path.join(root, handlePath);
|
||||
await page.goto(documentUrl(root, handlePath), {
|
||||
waitUntil: "domcontentloaded",
|
||||
timeout: UI_TIMEOUT_MS,
|
||||
});
|
||||
await page.locator('.document-pane[data-pane-role="primary"] .editor-surface .ProseMirror').first().waitFor({
|
||||
state: "visible",
|
||||
timeout: UI_TIMEOUT_MS,
|
||||
});
|
||||
await waitForPrimaryAttachment(page, "first.md");
|
||||
await waitForPrimaryAttachment(page, "second.md");
|
||||
await deleteAttachmentByBlockHandle(page, "second.md");
|
||||
|
||||
const handlePersisted = await waitForFileContent(
|
||||
handleFile,
|
||||
(content) => content.includes("[first.md](DeleteHandle/first.md)") && !content.includes("second.md"),
|
||||
UI_TIMEOUT_MS,
|
||||
);
|
||||
assert(
|
||||
handlePersisted.ok,
|
||||
`手柄删除第二个附件后,Markdown 应保留 first 链接且移除 second。实际内容:\n${handlePersisted.content}`,
|
||||
);
|
||||
assert(fs.existsSync(path.join(root, "DeleteHandle", "first.md")), "手柄删除引用不应删除 first.md 真实文件");
|
||||
assert(fs.existsSync(path.join(root, "DeleteHandle", "second.md")), "手柄删除引用不应删除 second.md 真实文件");
|
||||
|
||||
await page.reload({ waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS });
|
||||
await waitForPrimaryAttachment(page, "first.md");
|
||||
const handleSecondVisible = await page
|
||||
.locator('.document-pane[data-pane-role="primary"] .editor-surface .ProseMirror a', { hasText: "second.md" })
|
||||
.first()
|
||||
.isVisible()
|
||||
.catch(() => false);
|
||||
assert(!handleSecondVisible, "手柄删除后刷新,second.md 引用不应回来");
|
||||
|
||||
checks[checkId] = {
|
||||
ok: true,
|
||||
message: "Backspace 与手柄删除单个附件引用后,相邻附件仍是可点击附件块,真实文件保留,刷新后被删引用不回来",
|
||||
keyboardContent: keyboardPersisted.content,
|
||||
handleContent: handlePersisted.content,
|
||||
};
|
||||
} catch (err) {
|
||||
overallOk = false;
|
||||
checks[checkId] = {
|
||||
ok: false,
|
||||
message: `删除单个附件引用检查失败: ${err.message}`,
|
||||
error: err.stack,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// ── 截图 ──
|
||||
{
|
||||
const shotTimestamp = Date.now();
|
||||
|
||||
Reference in New Issue
Block a user