- 为本地 agent 写入补充审计事件,区分原生修改与 mnote tool 写入 - 只读 grant 写入尝试会记录拒绝事件,便于会话面板追踪 changed files - 页面 AI smoke 脚本补充 changed files 展示链路验证 - 更新当前优先级 checklist 的完成状态与验证记录
259 lines
10 KiB
JavaScript
259 lines
10 KiB
JavaScript
#!/usr/bin/env node
|
|
"use strict";
|
|
|
|
const assert = require("node:assert");
|
|
const fs = require("node:fs");
|
|
const { chromium } = require("playwright");
|
|
const {
|
|
BASE_URL,
|
|
UI_TIMEOUT_MS,
|
|
cleanupDocuments,
|
|
createTempDocument,
|
|
ensureAuthenticated,
|
|
renameDocument,
|
|
} = require("./tree-shell-smoke-helpers");
|
|
|
|
function resolveChromiumExecutablePath() {
|
|
const explicit = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH || "";
|
|
if (explicit && fs.existsSync(explicit)) return explicit;
|
|
return [
|
|
"/usr/bin/google-chrome-stable",
|
|
"/usr/bin/google-chrome",
|
|
"/snap/bin/chromium",
|
|
"/usr/bin/chromium",
|
|
"/usr/bin/chromium-browser",
|
|
].find((candidate) => fs.existsSync(candidate)) || "";
|
|
}
|
|
|
|
async function main() {
|
|
const suffix = Date.now().toString(36);
|
|
const title = `TEST-HERMES-AI-smoke-${suffix}`;
|
|
const sessionId = `mnote_smoke_${suffix}`;
|
|
const runId = `run_smoke_${suffix}`;
|
|
let sessionDetailHits = 0;
|
|
const captured = [];
|
|
const createdIds = [];
|
|
const executablePath = resolveChromiumExecutablePath();
|
|
const browser = await chromium.launch({
|
|
headless: true,
|
|
...(executablePath ? { executablePath } : {}),
|
|
});
|
|
const context = await browser.newContext({ viewport: { width: 1440, height: 960 } });
|
|
const page = await context.newPage();
|
|
|
|
try {
|
|
await page.route("**/api/ai-agent/run", async (route) => {
|
|
throw new Error(`页面 AI 不应请求旧 /api/ai-agent/run: ${route.request().url()}`);
|
|
});
|
|
await page.route("**/api/hermes/client/gateway/health**", async (route) => {
|
|
await route.fulfill({
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({
|
|
ok: true,
|
|
gateway: { ok: true, status: "mocked", upstream: "http://127.0.0.1:8644" },
|
|
profile: { name: "default", modelConfigured: true, apiKeyConfigured: true },
|
|
suggestions: [],
|
|
}),
|
|
});
|
|
});
|
|
await page.route("**/api/hermes/client/tools**", async (route) => {
|
|
await route.fulfill({
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ ok: true, tools: [] }),
|
|
});
|
|
});
|
|
await page.route("**/api/hermes/client/profiles", async (route) => {
|
|
await route.fulfill({
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({
|
|
ok: true,
|
|
active: "default",
|
|
profiles: [{ name: "default", label: "Default", modelConfigured: true, apiKeyConfigured: true }],
|
|
}),
|
|
});
|
|
});
|
|
await page.route("**/api/hermes/client/sessions", async (route) => {
|
|
captured.push({ kind: "session", method: route.request().method(), body: route.request().postData() || "" });
|
|
await route.fulfill({
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({
|
|
ok: true,
|
|
sessionId,
|
|
title: "当前页问答",
|
|
traceId: "trace_smoke",
|
|
persistence: "hermes_on_first_run",
|
|
}),
|
|
});
|
|
});
|
|
await page.route(`**/api/hermes/client/sessions/${sessionId}/resume`, async (route) => {
|
|
sessionDetailHits += 1;
|
|
captured.push({ kind: "session-detail", method: route.request().method(), body: "" });
|
|
assert.equal(route.request().method(), "POST", "刷新恢复必须调用 runtime resume 接口");
|
|
await route.fulfill({
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({
|
|
ok: true,
|
|
sessionId,
|
|
traceId: "trace_restore",
|
|
session: {
|
|
sessionId,
|
|
messages: [],
|
|
},
|
|
runtime: {
|
|
sessionId,
|
|
runId,
|
|
status: "completed",
|
|
profile: "default",
|
|
documentId: "doc_smoke",
|
|
traceId: "trace_restore",
|
|
lastToolName: "mnote.page.get",
|
|
},
|
|
}),
|
|
});
|
|
});
|
|
await page.route("**/api/hermes/client/runs", async (route) => {
|
|
captured.push({ kind: "run", method: route.request().method(), body: route.request().postData() || "" });
|
|
await route.fulfill({
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({
|
|
ok: true,
|
|
sessionId,
|
|
runId,
|
|
events: [],
|
|
traceId: "trace_smoke",
|
|
}),
|
|
});
|
|
});
|
|
await page.route(`**/api/hermes/client/events/${runId}`, async (route) => {
|
|
captured.push({ kind: "events", method: route.request().method(), body: "" });
|
|
await route.fulfill({
|
|
status: 200,
|
|
headers: { "content-type": "text/event-stream; charset=utf-8" },
|
|
body:
|
|
`data: ${JSON.stringify({ event: "tool.started", run_id: runId, session_id: sessionId, toolCallId: "call_smoke_page_get", name: "mnote.page.get", args: { includeBody: false } })}\n\n` +
|
|
`data: ${JSON.stringify({ event: "tool.completed", run_id: runId, session_id: sessionId, toolCallId: "call_smoke_page_get", name: "mnote.page.get", summary: "读取当前页面", auditId: "audit_smoke_page_get" })}\n\n` +
|
|
`data: ${JSON.stringify({ event: "tool.started", run_id: runId, session_id: sessionId, toolCallId: "call_smoke_page_save", name: "mnote.page.save", args: { dryRun: true } })}\n\n` +
|
|
`data: ${JSON.stringify({ event: "tool.failed", run_id: runId, session_id: sessionId, toolCallId: "call_smoke_page_save", name: "mnote.page.save", code: "permission_denied", error: "写入被拒绝", auditId: "audit_smoke_page_save" })}\n\n` +
|
|
`data: ${JSON.stringify({ event: "message.delta", run_id: runId, session_id: sessionId, delta: "Smoke " })}\n\n` +
|
|
`data: ${JSON.stringify({ event: "message.delta", run_id: runId, session_id: sessionId, delta: "response" })}\n\n` +
|
|
`data: ${JSON.stringify({
|
|
event: "run.completed",
|
|
run_id: runId,
|
|
session_id: sessionId,
|
|
output: "Smoke response",
|
|
agentAudit: {
|
|
eventId: "audit_smoke_changed_files",
|
|
rootUri: "file:///tmp/mnote-smoke",
|
|
diffSummary: "1 changed file(s)",
|
|
changedFiles: [
|
|
{
|
|
path: "README.md",
|
|
changeType: "modified",
|
|
summary: "修改文件 size:10→20 lines:1→2",
|
|
},
|
|
],
|
|
},
|
|
})}\n\n`,
|
|
});
|
|
});
|
|
|
|
await ensureAuthenticated(page, context.request);
|
|
const target = await createTempDocument(context.request);
|
|
createdIds.push(target.documentId);
|
|
await renameDocument(context.request, target.workspaceId, target.documentId, title);
|
|
await page.goto(
|
|
`${BASE_URL}/documents/${encodeURIComponent(target.documentId)}?workspaceId=${encodeURIComponent(target.workspaceId)}`,
|
|
{ waitUntil: "domcontentloaded", timeout: UI_TIMEOUT_MS },
|
|
);
|
|
|
|
await page.getByTestId("wolai-floating-ai").click({ timeout: UI_TIMEOUT_MS });
|
|
await page.locator("[data-page-ai-input]").fill(`请总结 ${title}`, { timeout: UI_TIMEOUT_MS });
|
|
await page.locator('[data-page-ai-action="send"]').click({ timeout: UI_TIMEOUT_MS });
|
|
await page.waitForFunction(
|
|
() => (document.querySelector('[data-testid="wolai-page-ai-drawer"]')?.textContent || "").includes("Smoke response"),
|
|
null,
|
|
{ timeout: UI_TIMEOUT_MS },
|
|
);
|
|
await page.waitForFunction(
|
|
() => (document.querySelector('[data-testid="wolai-page-ai-drawer"]')?.textContent || "").includes("mnote.page.get"),
|
|
null,
|
|
{ timeout: UI_TIMEOUT_MS },
|
|
);
|
|
await page.waitForFunction(
|
|
() => (document.querySelector("[data-page-ai-last-tool]")?.textContent || "").includes("mnote.page.save"),
|
|
null,
|
|
{ timeout: UI_TIMEOUT_MS },
|
|
);
|
|
const toolCards = await page.$$eval("[data-page-ai-tool-card]", (cards) =>
|
|
cards.map((card) => ({
|
|
id: card.getAttribute("data-page-ai-tool-call-id"),
|
|
status: card.getAttribute("data-page-ai-tool-status"),
|
|
text: card.textContent || "",
|
|
})),
|
|
);
|
|
assert.equal(toolCards.length, 3, `应渲染 3 张工具卡:${JSON.stringify(toolCards)}`);
|
|
assert(
|
|
toolCards.some((card) => card.status === "completed" && card.text.includes("call_smoke_page_get")),
|
|
`缺少 completed 工具卡:${JSON.stringify(toolCards)}`,
|
|
);
|
|
assert(
|
|
toolCards.some((card) => card.status === "failed" && card.text.includes("permission_denied")),
|
|
`缺少 failed 工具卡:${JSON.stringify(toolCards)}`,
|
|
);
|
|
assert(
|
|
toolCards.some((card) => card.status === "completed" && card.text.includes("agent.changed_files") && card.text.includes("README.md")),
|
|
`缺少 changed files 工具卡:${JSON.stringify(toolCards)}`,
|
|
);
|
|
|
|
const sessionRequest = captured.find((entry) => entry.kind === "session");
|
|
const runRequest = captured.find((entry) => entry.kind === "run");
|
|
const eventRequest = captured.find((entry) => entry.kind === "events");
|
|
assert(sessionRequest, "未捕获 /api/hermes/client/sessions 请求");
|
|
assert(runRequest, "未捕获 /api/hermes/client/runs 请求");
|
|
assert(eventRequest, "未捕获 /api/hermes/client/events 请求");
|
|
const runBody = JSON.parse(runRequest.body);
|
|
assert.equal(runBody.sessionId, sessionId, "run 请求必须携带 Hermes sessionId");
|
|
assert.equal(runBody.documentId, target.documentId, "run 请求必须携带 documentId");
|
|
assert(runBody.pageContext, "run 请求必须携带最小 pageContext");
|
|
assert.equal(runBody.pageContext.documentBlocks, null, "run 请求不应直接携带页面正文 blocks");
|
|
assert.equal(runBody.pageContext.subtree, null, "run 请求不应直接携带页面 subtree");
|
|
assert.equal(runBody.pageContext.outline, null, "run 请求不应直接携带页面 outline");
|
|
assert(
|
|
runBody.pageContext.contentAccess,
|
|
`run 请求必须声明正文访问方式:${JSON.stringify(runBody.pageContext)}`,
|
|
);
|
|
|
|
console.log(
|
|
JSON.stringify(
|
|
{
|
|
ok: true,
|
|
baseUrl: BASE_URL,
|
|
documentId: target.documentId,
|
|
workspaceId: target.workspaceId,
|
|
sessionId,
|
|
runId,
|
|
capturedKinds: captured.map((entry) => entry.kind),
|
|
sessionDetailHits,
|
|
},
|
|
null,
|
|
2,
|
|
),
|
|
);
|
|
} finally {
|
|
await cleanupDocuments(context.request, createdIds).catch(() => undefined);
|
|
await context.close().catch(() => undefined);
|
|
await browser.close().catch(() => undefined);
|
|
}
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error(error instanceof Error ? error.stack || error.message : String(error));
|
|
process.exit(1);
|
|
});
|