171 lines
6.1 KiB
JavaScript
171 lines
6.1 KiB
JavaScript
#!/usr/bin/env node
|
|
"use strict";
|
|
|
|
const { loginViaAuthForm } = require('./lib/browser-auth-login');
|
|
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 {
|
|
setupWorkspaceAccess,
|
|
seedAiPolicy,
|
|
} = require("./lib/control-plane-dev-seed");
|
|
|
|
const BASE = process.env.MNOTE_UI_BASE_URL || "http://127.0.0.1:3000";
|
|
const STAMP = Date.now();
|
|
const OUT = process.env.MNOTE_PI_FOLDER_TOOL_OUT || path.join(os.tmpdir(), `mnote-pi-folder-tool-${STAMP}`);
|
|
const TIMEOUT = Number.parseInt(process.env.UI_TIMEOUT_MS || "120000", 10);
|
|
const ACTOR_ID = process.env.MNOTE_E2E_ACTOR_ID || "mnote-e2e";
|
|
const WORKSPACE_ID = `local-ws:${ACTOR_ID}:pi-folder-tool`;
|
|
const ROOT_PATH = path.join(OUT, "workspace");
|
|
const ROOT_URI = `file://${ROOT_PATH}`;
|
|
const MODEL_PROVIDER = "omniroute";
|
|
const MODEL_ID = "gpt-5.4-mini";
|
|
const CHROMIUM_EXECUTABLE = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE
|
|
|| (fs.existsSync("/usr/bin/chromium-browser") ? "/usr/bin/chromium-browser" : "")
|
|
|| (fs.existsSync("/usr/bin/chromium") ? "/usr/bin/chromium" : "")
|
|
|| (fs.existsSync("/usr/bin/google-chrome") ? "/usr/bin/google-chrome" : "");
|
|
|
|
async function quickLogin(page) {
|
|
// 7-76 P0: 标准表单登录(无测试快速登录按钮)
|
|
const base =
|
|
(typeof BASE_URL !== "undefined" && BASE_URL) ||
|
|
(typeof baseUrl !== "undefined" && baseUrl) ||
|
|
process.env.MNOTE_UI_BASE_URL ||
|
|
"http://127.0.0.1:3000";
|
|
const timeout =
|
|
(typeof UI_TIMEOUT_MS !== "undefined" && UI_TIMEOUT_MS) ||
|
|
(typeof TIMEOUT !== "undefined" && TIMEOUT) ||
|
|
30_000;
|
|
if (!String(page.url() || "").includes("/auth")) {
|
|
await page.goto(String(base).replace(/\/+$/, "") + "/auth", {
|
|
waitUntil: "commit",
|
|
timeout,
|
|
});
|
|
}
|
|
await loginViaAuthForm(page, {
|
|
baseUrl: base,
|
|
timeoutMs: timeout,
|
|
gotoAuth: false,
|
|
});
|
|
await page
|
|
.waitForURL((url) => !String(url).includes("/auth"), { timeout })
|
|
.catch(() => {});
|
|
}
|
|
|
|
async function requestJson(page, url, options = {}) {
|
|
const response = await page.request.fetch(`${BASE}${url}`, {
|
|
...options,
|
|
headers: {
|
|
accept: "application/json",
|
|
"content-type": "application/json",
|
|
...(options.headers || {}),
|
|
},
|
|
timeout: options.timeout || TIMEOUT,
|
|
});
|
|
const text = await response.text();
|
|
let body = {};
|
|
try {
|
|
body = text ? JSON.parse(text) : {};
|
|
} catch {
|
|
body = { raw: text };
|
|
}
|
|
assert(response.ok(), `${options.method || "GET"} ${url} failed: ${response.status()} ${text.slice(0, 800)}`);
|
|
return body;
|
|
}
|
|
|
|
async function main() {
|
|
fs.mkdirSync(path.join(ROOT_PATH, "folder-a"), { recursive: true });
|
|
fs.writeFileSync(path.join(ROOT_PATH, "folder-a", "package.json"), JSON.stringify({ marker: `FOLDER_TOOL_${STAMP}` }), "utf8");
|
|
fs.writeFileSync(path.join(ROOT_PATH, "package.json"), JSON.stringify({ marker: "ROOT_SHOULD_NOT_BE_READ" }), "utf8");
|
|
fs.mkdirSync(OUT, { recursive: true });
|
|
const browser = await chromium.launch({
|
|
headless: process.env.MNOTE_PI_FOLDER_TOOL_HEADED === "1" ? false : true,
|
|
executablePath: CHROMIUM_EXECUTABLE || undefined,
|
|
});
|
|
const context = await browser.newContext({ viewport: { width: 1280, height: 900 } });
|
|
const page = await context.newPage();
|
|
const result = { ok: false, outputDir: OUT, checks: {} };
|
|
try {
|
|
await quickLogin(page);
|
|
await setupWorkspaceAccess(page.request, BASE, {
|
|
actorId: ACTOR_ID,
|
|
email: "mnote.e2e@example.com",
|
|
username: ACTOR_ID,
|
|
displayName: ACTOR_ID,
|
|
role: "admin",
|
|
workspaceId: WORKSPACE_ID,
|
|
workspaceName: "Pi folder tool smoke",
|
|
rootPath: ROOT_PATH,
|
|
rootUri: ROOT_URI,
|
|
permission: "write",
|
|
capabilities: ["ai", "read", "write"],
|
|
timeoutMs: TIMEOUT,
|
|
});
|
|
await seedAiPolicy(page.request, BASE, {
|
|
id: `pi-folder-tool-policy-${STAMP}`,
|
|
userId: ACTOR_ID,
|
|
workspaceId: WORKSPACE_ID,
|
|
allowedRootsJson: [{ rootUri: ROOT_URI, rootPath: ROOT_PATH, permission: "write" }],
|
|
modelPolicyJson: {
|
|
defaultModel: `${MODEL_PROVIDER}/${MODEL_ID}`,
|
|
allowedModels: [`${MODEL_PROVIDER}/${MODEL_ID}`],
|
|
tools: {
|
|
"mnote.local_file.read": "allow",
|
|
"mnote.local_file.patch": "allow",
|
|
},
|
|
skills: {},
|
|
mcpServers: {},
|
|
},
|
|
quotaJson: { daily: 20 },
|
|
timeoutMs: TIMEOUT,
|
|
});
|
|
const sessionId = `pi-folder-tool-${STAMP}`;
|
|
const start = await requestJson(page, "/api/page-ai/pi/start", {
|
|
method: "POST",
|
|
data: {
|
|
sessionId,
|
|
rootUri: ROOT_URI,
|
|
workspaceId: WORKSPACE_ID,
|
|
pagePath: "folder-a/readme.md",
|
|
pageTitle: "Pi folder tool smoke",
|
|
modelProvider: MODEL_PROVIDER,
|
|
modelId: MODEL_ID,
|
|
thinkingLevel: "off",
|
|
permissionMode: "full_access",
|
|
},
|
|
});
|
|
assert.equal(start.ok, true);
|
|
const read = await requestJson(page, "/api/page-ai/pi/tool-call", {
|
|
method: "POST",
|
|
data: {
|
|
sessionId,
|
|
toolName: "mnote.local_file.read",
|
|
params: {
|
|
path: "package.json",
|
|
folderPath: "folder-a",
|
|
},
|
|
},
|
|
});
|
|
result.checks.readOk = read.ok === true;
|
|
result.checks.relativePath = read.result && read.result.relativePath;
|
|
result.checks.content = read.result && read.result.content;
|
|
assert.equal(read.ok, true);
|
|
assert.equal(read.result.relativePath, "folder-a/package.json");
|
|
assert.match(read.result.content, new RegExp(`FOLDER_TOOL_${STAMP}`));
|
|
assert.doesNotMatch(read.result.content, /ROOT_SHOULD_NOT_BE_READ/);
|
|
await requestJson(page, "/api/page-ai/pi/abort", { method: "POST", data: { sessionId } }).catch(() => null);
|
|
result.ok = true;
|
|
} catch (error) {
|
|
result.error = error && error.stack ? error.stack : String(error);
|
|
process.exitCode = 1;
|
|
} finally {
|
|
fs.writeFileSync(path.join(OUT, "result.json"), JSON.stringify(result, null, 2), "utf8");
|
|
await browser.close();
|
|
console.log(JSON.stringify({ ok: result.ok, outputDir: OUT, result: path.join(OUT, "result.json") }, null, 2));
|
|
}
|
|
}
|
|
|
|
main();
|