#!/usr/bin/env node const assert = require("assert"); 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"; const UI_TIMEOUT_MS = Number(process.env.UI_TIMEOUT_MS || 10_000); function fileUrl(filePath) { return `file://${filePath.split(path.sep).map((part, index) => ( index === 0 ? "" : encodeURIComponent(part) )).join("/")}`; } async function main() { const root = fs.mkdtempSync(path.join(os.tmpdir(), "mnote-main-local-folder-")); fs.mkdirSync(path.join(root, "docs"), { recursive: true }); fs.writeFileSync(path.join(root, "README.md"), "# Root Page\n", "utf8"); fs.writeFileSync(path.join(root, "docs", "child.md"), "# Child Page\n", "utf8"); fs.writeFileSync(path.join(root, "plain.txt"), "plain asset\n", "utf8"); const browser = await chromium.launch({ headless: true }); const context = await browser.newContext({ extraHTTPHeaders: { "x-mnote-actor-id": "user_real", "x-mnote-actor-type": "user", }, }); const page = await context.newPage(); const dialogs = []; page.on("dialog", async (dialog) => { dialogs.push(dialog.type()); await dialog.dismiss().catch(() => {}); }); try { await page.goto(BASE_URL, { waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS }); await page.locator('[data-mnote-action="open-local-folder"]').click({ timeout: UI_TIMEOUT_MS }); await page.locator('[data-testid="mnote-local-folder-dialog"]').waitFor({ state: "visible", timeout: UI_TIMEOUT_MS, }); assert.deepStrictEqual(dialogs, [], "打开本地文件夹不能再触发 prompt/alert/confirm 原生弹窗"); await page.locator('[data-testid="mnote-local-folder-path-input"]').fill(root); await page.locator('[data-testid="mnote-local-folder-open-confirm"]').click(); await page.waitForURL((url) => { return url.origin === new URL(BASE_URL).origin && url.pathname === "/" && url.searchParams.get("sourceKind") === "local_folder" && url.searchParams.get("rootUri") === fileUrl(root) && url.searchParams.get("treeView") === "filetree"; }, { timeout: UI_TIMEOUT_MS }); const bodyText = await page.locator("body").innerText({ timeout: UI_TIMEOUT_MS }); assert(!bodyText.includes("legacy_next_compat_disabled"), "本地文件夹主入口不能落入 legacy Next fallback"); const readmeRow = page.locator('.tree-row[data-row-id="local:markdown:README.md"]'); const docsRow = page.locator('.tree-row[data-row-id="local:folder:docs"]'); await readmeRow.waitFor({ state: "visible", timeout: UI_TIMEOUT_MS, }); await docsRow.waitFor({ state: "visible", timeout: UI_TIMEOUT_MS, }); await readmeRow.locator(":scope > .tree-link").click(); await readmeRow.waitFor({ state: "visible", timeout: UI_TIMEOUT_MS, }); await docsRow.locator(":scope > .tree-link").click({ modifiers: ["Shift"] }); const selectedRowIds = await page.locator('#sidebar-file-tree-root .tree-row[data-selected="true"]') .evaluateAll((rows) => rows.map((row) => row.getAttribute("data-row-id"))); assert( selectedRowIds.includes("local:markdown:README.md") && selectedRowIds.includes("local:folder:docs") && selectedRowIds.length >= 2, `Shift+Click 应形成文件树范围多选,实际选中: ${selectedRowIds.join(", ")}`, ); console.log("task164 desktop hot local folder main entry smoke passed"); } finally { await browser.close(); fs.rmSync(root, { recursive: true, force: true }); } } main().catch((error) => { console.error(error); process.exit(1); });