droboticflow/scripts/playwright-preview-operators-check.mjs

606 lines
21 KiB
JavaScript

import { chromium } from "playwright";
const baseURL = (process.env.BASE_URL || process.argv[2] || "http://114.111.24.223:30080").replace(/\/+$/, "");
const suffix = Date.now().toString(36);
const prefix = `PWX ${suffix}`;
const names = {
user: `PWX Admin ${suffix}`,
email: `pwx-admin-${suffix}@playwright.local`,
project: `${prefix} Project`,
previewWorkflow: `${prefix} Preview`,
dataOpsWorkflow: `${prefix} DataOps`,
csvWorkflow: `${prefix} CSV`,
scanWorkflow: `${prefix} Scan`,
rosbagWorkflow: `${prefix} ROSBag`,
lerobotWorkflow: `${prefix} LeRobot`,
};
const requiredOperators = [
"目录扫描",
"CSV 转 JSON",
"JSON 转 CSV",
"JSON 字段提取",
"JSON 过滤",
"JSON 映射重命名",
"文本模板渲染",
"条件分流",
"LeRobot 数据集概览",
"ROSBag 数据概览",
"Shell 命令执行",
];
function log(step) {
process.stdout.write(`[preview-check] ${step}\n`);
}
function assert(condition, message) {
if (!condition) {
throw new Error(message);
}
}
async function api(path, init = {}) {
let lastError;
for (let attempt = 1; attempt <= 6; attempt += 1) {
try {
const response = await fetch(`${baseURL}${path}`, {
...init,
headers: {
"Content-Type": "application/json",
...(init.headers || {}),
},
});
const text = await response.text();
if (!response.ok) {
throw new Error(`${init.method || "GET"} ${path} failed: ${response.status} ${text}`);
}
if (!text) {
return null;
}
return JSON.parse(text);
} catch (error) {
lastError = error;
if (attempt === 6) {
break;
}
await new Promise((resolve) => setTimeout(resolve, Math.min(attempt, 3) * 1000));
}
}
throw lastError;
}
async function poll(fn, predicate, label, timeoutMs = 180000, intervalMs = 2000) {
const deadline = Date.now() + timeoutMs;
let lastValue;
while (Date.now() < deadline) {
lastValue = await fn();
if (predicate(lastValue)) {
return lastValue;
}
await new Promise((resolve) => setTimeout(resolve, intervalMs));
}
throw new Error(`Timed out waiting for ${label}. Last value: ${JSON.stringify(lastValue)}`);
}
async function gotoWithRetry(page, url) {
let lastError;
for (let attempt = 1; attempt <= 6; attempt += 1) {
try {
await page.goto(url, { waitUntil: "networkidle" });
return;
} catch (error) {
lastError = error;
if (attempt === 6) {
break;
}
await page.waitForTimeout(Math.min(attempt, 3) * 1000);
}
}
throw lastError;
}
async function cleanupTestData() {
log("cleanup previous PWX test data");
const projects = (await api("/api/projects")).items;
for (const project of projects) {
if (String(project.name || "").startsWith("PWX ")) {
await api(`/api/projects/${encodeURIComponent(project.id)}`, { method: "DELETE" });
}
}
}
async function ensureUIReady(page) {
await gotoWithRetry(page, baseURL);
const bootstrapVisible = await page.getByText("新增真实平台用户").isVisible({ timeout: 5000 }).catch(() => false);
if (bootstrapVisible) {
await page.getByPlaceholder("输入真实姓名或账号名").fill(names.user);
await page.getByPlaceholder("name@example.com").fill(names.email);
await page.locator("select").first().selectOption("admin");
await page.getByRole("button", { name: "创建并进入系统" }).click();
}
await page.getByRole("heading", { name: "DroboticFlow 仪表盘" }).waitFor();
}
async function createProject() {
return api("/api/projects", {
method: "POST",
body: JSON.stringify({
name: names.project,
description: "Created by preview/operator Playwright check",
}),
});
}
async function createWorkflow(projectId, name, nodes, edges) {
return api(`/api/projects/${projectId}/workflows`, {
method: "POST",
body: JSON.stringify({
name,
description: `Created by ${name}`,
type: "batch",
nodes,
edges,
}),
});
}
async function runWorkflow(workflowId) {
const queued = await api(`/api/workflows/${workflowId}/run`, {
method: "POST",
body: JSON.stringify({ trigger: "manual" }),
});
const finished = await poll(
async () => api(`/api/runs/${queued.id}`),
(run) => ["completed", "failed", "cancelled", "waiting_for_audit"].includes(run.status),
`run ${queued.id} to finish`,
180000,
3000,
);
assert(finished.status === "completed", `Run ${queued.id} finished with ${finished.status}: ${finished.error || "unknown error"}`);
return finished;
}
async function createWorkflows(projectId) {
const previewWorkflow = await createWorkflow(
projectId,
names.previewWorkflow,
[
{
id: "generate",
type: "operator",
data: {
label: "Shell 命令执行",
category: "UTIL",
description: "Generate preview artifacts",
inputs: [],
outputs: [{ id: "out-1", name: "result", type: "file" }],
hyperparams: [
{
key: "COMMAND",
value: "printf 'name,score\\nalpha,1\\nbeta,2\\n' > report.csv && printf 'hello\\nworld\\n' > notes.txt",
},
],
},
position: { x: 0, y: 0 },
},
],
[],
);
const dataOpsWorkflow = await createWorkflow(
projectId,
names.dataOpsWorkflow,
[
{
id: "generate",
type: "operator",
data: {
label: "Shell 命令执行",
category: "UTIL",
description: "Generate json payload",
inputs: [],
outputs: [{ id: "out-1", name: "payload", type: "json" }],
hyperparams: [
{
key: "COMMAND",
value: "printf '{\"device\":{\"id\":\"robot-1\",\"status\":\"ok\"},\"items\":[{\"kind\":\"keep\",\"value\":1},{\"kind\":\"drop\",\"value\":2}]}' > payload.json",
},
],
},
position: { x: 0, y: 0 },
},
{
id: "extract",
type: "operator",
data: {
label: "JSON 字段提取",
category: "DATA",
description: "Extract device id",
inputs: [{ id: "in-1", name: "input", type: "json" }],
outputs: [{ id: "out-1", name: "extracted", type: "json" }],
hyperparams: [{ key: "FIELD_PATH", value: "device.id" }],
},
position: { x: 220, y: -160 },
},
{
id: "filter",
type: "operator",
data: {
label: "JSON 过滤",
category: "DATA",
description: "Filter keep rows",
inputs: [{ id: "in-1", name: "input", type: "json" }],
outputs: [{ id: "out-1", name: "filtered", type: "json" }],
hyperparams: [
{ key: "FIELD_PATH", value: "kind" },
{ key: "OPERATOR", value: "eq" },
{ key: "EXPECTED_JSON", value: "\"keep\"" },
],
},
position: { x: 220, y: -20 },
},
{
id: "map",
type: "operator",
data: {
label: "JSON 映射重命名",
category: "DATA",
description: "Rename fields",
inputs: [{ id: "in-1", name: "input", type: "json" }],
outputs: [{ id: "out-1", name: "mapped", type: "json" }],
hyperparams: [{ key: "FIELD_MAP_JSON", value: "{\"kind\":\"label\",\"value\":\"score\"}" }],
},
position: { x: 220, y: 120 },
},
{
id: "csv",
type: "operator",
data: {
label: "JSON 转 CSV",
category: "DATA",
description: "Export csv",
inputs: [{ id: "in-1", name: "input", type: "json" }],
outputs: [{ id: "out-1", name: "csv", type: "file" }],
},
position: { x: 460, y: 120 },
},
{
id: "route",
type: "operator",
data: {
label: "条件分流",
category: "LOGIC",
description: "Route rows",
inputs: [{ id: "in-1", name: "input", type: "json" }],
outputs: [
{ id: "matched", name: "matched", type: "json" },
{ id: "unmatched", name: "unmatched", type: "json" },
],
hyperparams: [
{ key: "FIELD_PATH", value: "kind" },
{ key: "OPERATOR", value: "eq" },
{ key: "EXPECTED_JSON", value: "\"keep\"" },
],
},
position: { x: 220, y: 260 },
},
{
id: "keep-upload",
type: "operator",
data: {
label: "结果上传",
category: "IO",
description: "matched sink",
inputs: [{ id: "in-1", name: "input", type: "json" }],
outputs: [],
},
position: { x: 500, y: 220 },
},
{
id: "drop-upload",
type: "operator",
data: {
label: "结果上传",
category: "IO",
description: "unmatched sink",
inputs: [{ id: "in-1", name: "input", type: "json" }],
outputs: [],
},
position: { x: 500, y: 320 },
},
],
[
{ id: "generate-extract", source: "generate", sourceHandle: "out-1", target: "extract", targetHandle: "in-1" },
{ id: "generate-filter", source: "generate", sourceHandle: "out-1", target: "filter", targetHandle: "in-1" },
{ id: "generate-map", source: "generate", sourceHandle: "out-1", target: "map", targetHandle: "in-1" },
{ id: "generate-route", source: "generate", sourceHandle: "out-1", target: "route", targetHandle: "in-1" },
{ id: "map-csv", source: "map", sourceHandle: "out-1", target: "csv", targetHandle: "in-1" },
{ id: "route-keep", source: "route", sourceHandle: "matched", target: "keep-upload", targetHandle: "in-1" },
{ id: "route-drop", source: "route", sourceHandle: "unmatched", target: "drop-upload", targetHandle: "in-1" },
],
);
const csvWorkflow = await createWorkflow(
projectId,
names.csvWorkflow,
[
{
id: "generate",
type: "operator",
data: {
label: "Shell 命令执行",
category: "UTIL",
description: "Generate csv",
inputs: [],
outputs: [{ id: "out-1", name: "table", type: "file" }],
hyperparams: [
{
key: "COMMAND",
value: "printf 'col_a,col_b\\n1,2\\n3,4\\n' > table.csv",
},
],
},
position: { x: 0, y: 0 },
},
{
id: "convert",
type: "operator",
data: {
label: "CSV 转 JSON",
category: "DATA",
description: "csv to json",
inputs: [{ id: "in-1", name: "input", type: "json" }],
outputs: [{ id: "out-1", name: "table", type: "json" }],
},
position: { x: 240, y: 0 },
},
],
[{ id: "generate-convert", source: "generate", sourceHandle: "out-1", target: "convert", targetHandle: "in-1" }],
);
const scanWorkflow = await createWorkflow(
projectId,
names.scanWorkflow,
[
{
id: "generate",
type: "operator",
data: {
label: "Shell 命令执行",
category: "UTIL",
description: "Generate directory tree",
inputs: [],
outputs: [{ id: "out-1", name: "dir", type: "json" }],
hyperparams: [
{
key: "COMMAND",
value: "mkdir -p scan-root && printf 'alpha' > scan-root/a.txt && printf '{\"beta\":1}' > scan-root/b.json && printf '{\"sourcePath\":\"'\"$OUTPUT_DIR\"'/scan-root\"}' > manifest.json",
},
],
},
position: { x: 0, y: 0 },
},
{
id: "scan",
type: "operator",
data: {
label: "目录扫描",
category: "IO",
description: "directory scan",
inputs: [{ id: "in-1", name: "input", type: "json" }],
outputs: [{ id: "out-1", name: "summary", type: "json" }],
},
position: { x: 240, y: 0 },
},
],
[{ id: "generate-scan", source: "generate", sourceHandle: "out-1", target: "scan", targetHandle: "in-1" }],
);
const rosbagWorkflow = await createWorkflow(
projectId,
names.rosbagWorkflow,
[
{
id: "generate",
type: "operator",
data: {
label: "Shell 命令执行",
category: "UTIL",
description: "Generate rosbag db3",
inputs: [],
outputs: [{ id: "out-1", name: "db3", type: "file" }],
hyperparams: [
{
key: "COMMAND",
value: "python3 - <<'PY'\nimport sqlite3\nconn = sqlite3.connect('bag_0.db3')\ncur = conn.cursor()\ncur.execute('create table topics (id integer primary key, name text, type text, serialization_format text)')\ncur.execute('create table messages (id integer primary key, topic_id integer, timestamp integer, data blob)')\ncur.execute(\"insert into topics (id, name, type, serialization_format) values (1, '/camera', 'sensor_msgs/msg/Image', 'cdr')\")\ncur.execute(\"insert into messages (topic_id, timestamp, data) values (1, 100, X'01')\")\ncur.execute(\"insert into messages (topic_id, timestamp, data) values (1, 200, X'02')\")\nconn.commit()\nconn.close()\nPY",
},
],
},
position: { x: 0, y: 0 },
},
{
id: "summary",
type: "operator",
data: {
label: "ROSBag 数据概览",
category: "ROBOTICS",
description: "rosbag summary",
inputs: [{ id: "in-1", name: "input", type: "json" }],
outputs: [{ id: "out-1", name: "summary", type: "json" }],
},
position: { x: 240, y: 0 },
},
],
[{ id: "generate-summary", source: "generate", sourceHandle: "out-1", target: "summary", targetHandle: "in-1" }],
);
const lerobotWorkflow = await createWorkflow(
projectId,
names.lerobotWorkflow,
[
{
id: "generate",
type: "operator",
data: {
label: "Shell 命令执行",
category: "UTIL",
description: "Generate lerobot dataset",
inputs: [],
outputs: [{ id: "out-1", name: "dataset", type: "file" }],
hyperparams: [
{
key: "COMMAND",
value: "mkdir -p dataset/meta dataset/data dataset/videos && printf '{\"robot_type\":\"aloha\",\"fps\":30}' > dataset/meta/info.json && printf '{\"episodes\":2}' > dataset/meta/stats.json && printf '{\"task\":\"pick\"}\\n{\"task\":\"place\"}\\n' > dataset/meta/tasks.jsonl && printf 'PAR1' > dataset/data/episode_000.parquet && printf 'video' > dataset/videos/episode_000.mp4",
},
],
},
position: { x: 0, y: 0 },
},
{
id: "summary",
type: "operator",
data: {
label: "LeRobot 数据集概览",
category: "ROBOTICS",
description: "lerobot summary",
inputs: [{ id: "in-1", name: "input", type: "json" }],
outputs: [{ id: "out-1", name: "summary", type: "json" }],
},
position: { x: 240, y: 0 },
},
],
[{ id: "generate-summary", source: "generate", sourceHandle: "out-1", target: "summary", targetHandle: "in-1" }],
);
return {
previewWorkflow,
dataOpsWorkflow,
csvWorkflow,
scanWorkflow,
rosbagWorkflow,
lerobotWorkflow,
};
}
function artifactByName(items, name) {
const item = items.find((artifact) => artifact.name === name);
assert(item, `Missing artifact ${name}`);
return item;
}
async function openArtifactModal(page, workflowName) {
await page.getByRole("link", { name: "执行监控" }).click();
await page.getByRole("heading", { name: "执行监控" }).waitFor();
const search = page.getByPlaceholder("搜索运行 ID、工作流或项目...");
await search.fill(workflowName);
const row = page.locator("tr").filter({ hasText: workflowName }).first();
await row.waitFor({ state: "visible" });
await row.locator('button[title="查看数据"]').click();
await page.getByText("产出数据").waitFor();
}
async function closeArtifactModal(page) {
await page.getByRole("button", { name: "关闭" }).click();
await page.getByRole("button", { name: "关闭" }).waitFor({ state: "hidden" });
}
async function main() {
await cleanupTestData();
log("verify operator catalog");
const operators = (await api("/api/operators")).items;
for (const name of requiredOperators) {
assert(operators.some((item) => item.name === name), `Missing built-in operator ${name}`);
}
log("create workflows");
const project = await createProject();
const workflows = await createWorkflows(project.id);
log("run preview workflow");
const previewRun = await runWorkflow(workflows.previewWorkflow.id);
const previewArtifacts = (await api(`/api/runs/${previewRun.id}/artifacts`)).items;
assert(artifactByName(previewArtifacts, "report.csv").data.preview.kind === "table", "report.csv preview should be table");
assert(artifactByName(previewArtifacts, "notes.txt").data.preview.kind === "text", "notes.txt preview should be text");
log("run data ops workflow");
const dataOpsRun = await runWorkflow(workflows.dataOpsWorkflow.id);
const dataOpsArtifacts = (await api(`/api/runs/${dataOpsRun.id}/artifacts`)).items;
assert(artifactByName(dataOpsArtifacts, "extracted.json").data.value === "robot-1", "JSON extract should return robot-1");
assert(artifactByName(dataOpsArtifacts, "filtered.json").data.count === 1, "JSON filter should keep one row");
const mapped = artifactByName(dataOpsArtifacts, "mapped.json").data.items;
assert(Array.isArray(mapped) && mapped[0].label === "keep" && mapped[0].score === 1, "JSON map should rename fields");
assert(artifactByName(dataOpsArtifacts, "rows.csv").data.preview.kind === "table", "JSON to CSV should produce previewable csv");
assert(artifactByName(dataOpsArtifacts, "matched.json").portId === "matched", "matched artifact should carry portId");
assert(artifactByName(dataOpsArtifacts, "unmatched.json").portId === "unmatched", "unmatched artifact should carry portId");
log("run csv conversion workflow");
const csvRun = await runWorkflow(workflows.csvWorkflow.id);
const csvArtifacts = (await api(`/api/runs/${csvRun.id}/artifacts`)).items;
assert(artifactByName(csvArtifacts, "table.json").data.rowCount === 2, "CSV to JSON should convert two rows");
log("run directory scan workflow");
const scanRun = await runWorkflow(workflows.scanWorkflow.id);
const scanArtifacts = (await api(`/api/runs/${scanRun.id}/artifacts`)).items;
assert(artifactByName(scanArtifacts, "directory-scan.json").data.summary.fileCount >= 2, "Directory scan should report files");
log("run rosbag workflow");
const rosbagRun = await runWorkflow(workflows.rosbagWorkflow.id);
const rosbagArtifacts = (await api(`/api/runs/${rosbagRun.id}/artifacts`)).items;
const rosbagPreview = artifactByName(rosbagArtifacts, "rosbag-summary.json").data.preview;
assert(rosbagPreview.format === "rosbag2", "ROSBag summary should detect rosbag2 sqlite");
assert(Array.isArray(rosbagPreview.topics) && rosbagPreview.topics[0].name === "/camera", "ROSBag summary should include topics");
log("run lerobot workflow");
const lerobotRun = await runWorkflow(workflows.lerobotWorkflow.id);
const lerobotArtifacts = (await api(`/api/runs/${lerobotRun.id}/artifacts`)).items;
const lerobotPreview = artifactByName(lerobotArtifacts, "lerobot-summary.json").data.preview;
assert(lerobotPreview.format === "lerobot", "LeRobot summary should detect dataset");
assert(lerobotPreview.info.robot_type === "aloha", "LeRobot summary should expose info.json");
log("open browser and verify UI previews");
const browser = await chromium.launch({ headless: true });
const context = await browser.newContext({ viewport: { width: 1440, height: 1100 } });
const page = await context.newPage();
try {
await ensureUIReady(page);
await page.getByRole("link", { name: "算子管理" }).click();
await page.getByRole("heading", { name: "算子管理" }).waitFor();
await page.getByPlaceholder("搜索算子...").fill("ROSBag 数据概览");
await page.getByText("ROSBag 数据概览").waitFor();
await page.getByPlaceholder("搜索算子...").fill("LeRobot 数据集概览");
await page.getByText("LeRobot 数据集概览").waitFor();
await openArtifactModal(page, names.previewWorkflow);
await page.getByText("report.csv").click();
await page.getByText("alpha").waitFor();
await page.getByText("score").waitFor();
await page.getByText("notes.txt").click();
await page.getByText("hello").waitFor();
await closeArtifactModal(page);
await openArtifactModal(page, names.rosbagWorkflow);
await page.getByText("rosbag-summary.json").click();
await page.locator("pre").filter({ hasText: '"/camera"' }).first().waitFor();
await page.locator("pre").filter({ hasText: '"datasetType": "rosbag2"' }).first().waitFor();
await closeArtifactModal(page);
await openArtifactModal(page, names.lerobotWorkflow);
await page.getByText("lerobot-summary.json").click();
await page.locator("pre").filter({ hasText: '"robot_type": "aloha"' }).first().waitFor();
await page.locator("pre").filter({ hasText: '"format": "lerobot"' }).first().waitFor();
await closeArtifactModal(page);
} finally {
await browser.close();
}
log("preview and operator check completed");
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});