feat: advance local-first conflict and search indexing

- 补齐本地 markdown 冲突处理与合并写回路径\n- 增加本地搜索索引路由、刷新与 browser smoke\n- 同步更新 current-priority checklist 的阶段进度
This commit is contained in:
lix-2026
2026-05-19 09:38:57 +08:00
parent 8ed594f1c2
commit 1b5d6a2a2d
12 changed files with 1622 additions and 49 deletions
@@ -113,9 +113,13 @@ async function run() {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "mnote-local-conflict-ui-"));
const acceptFile = "accept-disk.md";
const keepFile = "keep-current.md";
const mergeFile = "merge-result.md";
const agentFile = "agent-conflict.md";
writeWorkspaceManifest(root, "user_real");
fs.writeFileSync(path.join(root, acceptFile), markdown("Accept Disk", ["initial accept"]), "utf8");
fs.writeFileSync(path.join(root, keepFile), markdown("Keep Current", ["initial keep"]), "utf8");
fs.writeFileSync(path.join(root, mergeFile), markdown("Merge Result", ["initial merge"]), "utf8");
fs.writeFileSync(path.join(root, agentFile), markdown("Agent Conflict", ["initial agent"]), "utf8");
const browser = await chromium.launch({
headless: true,
@@ -189,6 +193,57 @@ async function run() {
assert(!keepContent.includes(diskKeepToken), "保留当前版本后磁盘版本内容不应覆盖当前编辑器内容");
steps.push({ label: "keep-current", ok: true });
await openDocument(page, root, mergeFile);
await waitForEditorText(page, "initial merge");
const localMergeToken = `local-merge-${Date.now()}`;
const diskMergeToken = `disk-merge-${Date.now()}`;
const mergedToken = `merged-merge-${Date.now()}`;
await typeDirtyText(page, ` ${localMergeToken}`);
fs.writeFileSync(path.join(root, mergeFile), markdown("Merge Result", ["initial merge", diskMergeToken]), "utf8");
await waitForEditorStatus(page, "external-change-conflict");
await page.locator('[data-testid="mnote-editor-conflict-panel"]').waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
await page.locator('[data-testid="mnote-conflict-open-diff"]').click({ timeout: UI_TIMEOUT_MS });
await page.locator('[data-testid="mnote-conflict-diff-panel"]').waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
await page.locator('[data-testid="mnote-conflict-merge-text"]').fill(`initial merge\n${localMergeToken}\n${diskMergeToken}\n${mergedToken}`);
await page.locator('[data-testid="mnote-conflict-merge-save"]').click({ timeout: UI_TIMEOUT_MS });
await waitForFileText(path.join(root, mergeFile), mergedToken);
const mergeContent = fs.readFileSync(path.join(root, mergeFile), "utf8");
assert(mergeContent.includes(localMergeToken), "合并结果应保留当前编辑器内容");
assert(mergeContent.includes(diskMergeToken), "合并结果应保留磁盘内容");
assert(mergeContent.includes(mergedToken), "合并结果应写入合并后的新内容");
steps.push({ label: "merge-save", ok: true });
await openDocument(page, root, agentFile);
await waitForEditorText(page, "initial agent");
const localAgentToken = `local-agent-${Date.now()}`;
const diskAgentToken = `disk-agent-${Date.now()}`;
const agentRunId = `run-agent-conflict-${Date.now()}`;
await typeDirtyText(page, ` ${localAgentToken}`);
fs.writeFileSync(path.join(root, agentFile), markdown("Agent Conflict", ["initial agent", diskAgentToken]), "utf8");
await page.evaluate(({ documentId, runId }) => {
window.dispatchEvent(new CustomEvent("mnote:page-ai-tool-write-completed", {
detail: {
toolName: "agent.changed_files",
normalizedToolName: "agent.changed_files",
documentId,
runId,
traceId: `trace-${runId}`,
toolCallId: `${runId}:agent.changed_files`,
},
}));
}, { documentId: localMdDocumentId(agentFile), runId: agentRunId });
await waitForEditorStatus(page, "external-change-conflict");
await page.locator('[data-testid="mnote-editor-conflict-panel"]').waitFor({ state: "visible", timeout: UI_TIMEOUT_MS });
await page.waitForFunction(
(expected) => {
const panel = document.querySelector('[data-testid="mnote-editor-conflict-panel"]');
return (panel?.textContent || "").includes(expected);
},
`agent run ${agentRunId}`,
{ timeout: UI_TIMEOUT_MS },
);
steps.push({ label: "agent-conflict-source", ok: true });
const result = { ok: true, baseUrl: BASE_URL, root, steps };
fs.writeFileSync(RESULT_PATH, `${JSON.stringify(result, null, 2)}\n`, "utf8");
console.log(`task451 local markdown conflict resolution UI smoke passed: ${RESULT_PATH}`);
@@ -0,0 +1,158 @@
#!/usr/bin/env node
"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", "task452-local-search-index-browser-smoke");
const RESULT_PATH = path.join(OUTPUT_DIR, "result.json");
const CHROMIUM_EXECUTABLE_PATH = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH
|| ["/usr/bin/google-chrome-stable", "/usr/bin/google-chrome", "/snap/bin/chromium", "/usr/bin/chromium"]
.find((candidate) => fs.existsSync(candidate));
function fileUrl(localPath) {
return `file://${localPath}`;
}
function localMdDocumentId(relativePath) {
return `local-md:${relativePath.replaceAll("/", "~2F")}`;
}
function documentUrl(root, relativePath) {
const url = new URL(`${BASE_URL}/documents/${encodeURIComponent(localMdDocumentId(relativePath))}`);
url.searchParams.set("sourceKind", "local_folder");
url.searchParams.set("rootUri", fileUrl(root));
return url.toString();
}
function writeWorkspaceManifest(root, ownerId) {
const metadataDir = path.join(root, ".mnote");
fs.mkdirSync(metadataDir, { recursive: true });
fs.writeFileSync(
path.join(metadataDir, "workspace.json"),
`${JSON.stringify({
workspaceId: `local-ws:${ownerId}:task452`,
ownerId,
createdAt: new Date().toISOString(),
capabilities: ["local_files", "search"],
}, null, 2)}\n`,
"utf8",
);
}
async function browserSearch(page, root, query) {
return page.evaluate(async ({ rootUri, queryText }) => {
const response = await fetch("/api/search/documents", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
workspaceId: "local-ws:user_real:task452",
sourceKind: "local_folder",
rootUri,
query: queryText,
limit: 10,
}),
});
const payload = await response.json();
return {
status: response.status,
payload,
};
}, { rootUri: fileUrl(root), queryText: query });
}
async function run() {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
const root = fs.mkdtempSync(path.join(os.tmpdir(), "mnote-local-search-smoke-"));
const debug = { root, baseUrl: BASE_URL };
writeWorkspaceManifest(root, "user_real");
fs.mkdirSync(path.join(root, "docs"), { recursive: true });
fs.writeFileSync(path.join(root, "README.md"), "# Search Smoke\n打开搜索 smoke。\n", "utf8");
const browser = await chromium.launch({
headless: true,
...(CHROMIUM_EXECUTABLE_PATH ? { executablePath: CHROMIUM_EXECUTABLE_PATH } : {}),
});
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();
try {
await page.goto(documentUrl(root, "README.md"), {
waitUntil: "domcontentloaded",
timeout: UI_TIMEOUT_MS,
});
await page.locator('[data-testid="mnote-leptos-tiptap-island-editor-root"]').first().waitFor({
state: "visible",
timeout: UI_TIMEOUT_MS,
});
const token = `LOCAL-SEARCH-${Date.now()}`;
const firstRelativePath = "docs/search-target.md";
const renamedRelativePath = "docs/search-renamed.md";
fs.writeFileSync(
path.join(root, firstRelativePath),
`---\ntitle: Search Target\n---\n# Search Target\n${token}\n`,
"utf8",
);
const first = await browserSearch(page, root, token);
assert.equal(first.status, 200, `初次搜索应成功: ${JSON.stringify(first)}`);
debug.first = first.payload;
const firstResults = Array.isArray(first.payload.results) ? first.payload.results : [];
assert(
firstResults.some((item) => item.path === firstRelativePath && item.documentId === localMdDocumentId(firstRelativePath)),
`新建页面应立即可搜索: ${JSON.stringify(firstResults)}`,
);
fs.renameSync(path.join(root, firstRelativePath), path.join(root, renamedRelativePath));
const second = await browserSearch(page, root, token);
assert.equal(second.status, 200, `重命名后搜索应成功: ${JSON.stringify(second)}`);
debug.second = second.payload;
const secondResults = Array.isArray(second.payload.results) ? second.payload.results : [];
assert(
secondResults.some((item) => item.path === renamedRelativePath && item.documentId === localMdDocumentId(renamedRelativePath)),
`重命名后搜索结果路径应更新: ${JSON.stringify(secondResults)}`,
);
assert(
!secondResults.some((item) => item.path === firstRelativePath),
`重命名后搜索结果不应继续返回旧路径: ${JSON.stringify(secondResults)}`,
);
const result = {
ok: true,
root,
token,
firstPath: firstRelativePath,
renamedPath: renamedRelativePath,
indexExists: fs.existsSync(path.join(root, ".mnote", "index", "search-index.json")),
};
fs.writeFileSync(RESULT_PATH, `${JSON.stringify(result, null, 2)}\n`, "utf8");
console.log(`task452 local search index browser smoke passed: ${RESULT_PATH}`);
} catch (error) {
fs.writeFileSync(RESULT_PATH, `${JSON.stringify({
ok: false,
error: String(error && error.stack || error),
debug,
}, null, 2)}\n`, "utf8");
throw error;
} finally {
await browser.close().catch(() => {});
fs.rmSync(root, { recursive: true, force: true });
}
}
run().catch((error) => {
console.error(error);
process.exitCode = 1;
});