droboticflow/tests/frontend/workflow-run.test.mjs

283 lines
8.1 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import {
artifactSelectionKey,
decorateWorkflowEdges,
decorateWorkflowNodes,
getRunDisplayStatus,
getCurrentNodeLabel,
getEdgeOutputArtifacts,
getNodeArtifacts,
getNodeExecutionState,
getTrackedRunId,
isRunInFlight,
} from "../../src/lib/workflow-run.js";
test("uses artifact paths as independent selection keys when legacy ids are missing", () => {
const first = {
id: "",
name: "inspect_error_log.log",
path: "/workspace/runs/run-1/nodes/check/outputs/inspect_error_log.log",
};
const second = {
id: "",
name: "inspect_full_log.log",
path: "/workspace/runs/run-1/nodes/check/outputs/inspect_full_log.log",
};
assert.equal(artifactSelectionKey(first), "path:/workspace/runs/run-1/nodes/check/outputs/inspect_error_log.log");
assert.notEqual(artifactSelectionKey(first), artifactSelectionKey(second));
assert.equal(artifactSelectionKey({ ...first, id: "artifact-1" }), "id:artifact-1");
});
const nodes = [
{ id: "source", data: { label: "Source" } },
{ id: "infer", data: { label: "Infer" } },
{ id: "upload", data: { label: "Upload" } },
];
const edges = [
{ id: "e1", source: "source", target: "infer" },
{ id: "e2", source: "infer", target: "upload" },
];
test("identifies active run statuses", () => {
assert.equal(isRunInFlight({ status: "queued" }), true);
assert.equal(isRunInFlight({ status: "running" }), true);
assert.equal(isRunInFlight({ status: "stopping" }), true);
assert.equal(isRunInFlight({ status: "waiting_for_audit" }), true);
assert.equal(isRunInFlight({ status: "completed" }), false);
});
test("treats stale completed runs with active node executions as in flight", () => {
const run = {
status: "completed",
currentNodeId: "infer",
nodeExecutions: [
{ nodeId: "source", nodeLabel: "Source", status: "completed" },
{ nodeId: "infer", nodeLabel: "Infer", status: "running" },
],
};
assert.equal(getRunDisplayStatus(run), "running");
assert.equal(isRunInFlight(run), true);
});
test("treats a completed run with all-terminal nodes as completed even when currentNodeId is set", () => {
// The backend does not clear currentNodeId when a run completes, so it still points at
// the last executed node. The display status must be "completed", not "running".
const run = {
status: "completed",
currentNodeId: "upload",
nodeExecutions: [
{ nodeId: "source", nodeLabel: "Source", status: "completed" },
{ nodeId: "infer", nodeLabel: "Infer", status: "completed" },
{ nodeId: "upload", nodeLabel: "Upload", status: "completed" },
],
};
assert.equal(getRunDisplayStatus(run), "completed");
assert.equal(isRunInFlight(run), false);
});
test("derives node execution states from current run payload", () => {
const run = {
status: "running",
currentNodeId: "infer",
nodeExecutions: [
{ nodeId: "source", nodeLabel: "Source", status: "completed" },
{ nodeId: "infer", nodeLabel: "Infer", status: "running" },
],
};
assert.equal(getNodeExecutionState(run, "source"), "completed");
assert.equal(getNodeExecutionState(run, "infer"), "running");
assert.equal(getNodeExecutionState(run, "upload"), "idle");
});
test("decorates nodes and edges for an in-progress run", () => {
const run = {
status: "running",
currentNodeId: "infer",
nodeExecutions: [
{ nodeId: "source", nodeLabel: "Source", status: "completed" },
{ nodeId: "infer", nodeLabel: "Infer", status: "running" },
],
};
const decoratedNodes = decorateWorkflowNodes(nodes, run);
const decoratedEdges = decorateWorkflowEdges(edges, nodes, run);
assert.equal(decoratedNodes[0].data.executionState, "completed");
assert.equal(decoratedNodes[1].data.executionState, "running");
assert.equal(decoratedNodes[1].data.running, true);
assert.equal(decoratedEdges[0].animated, true);
assert.equal(decoratedEdges[0].data.executionState, "running");
assert.equal(decoratedEdges[1].data.executionState, "idle");
});
test("finds the current node label from node executions or graph data", () => {
const run = {
currentNodeId: "upload",
nodeExecutions: [],
};
assert.equal(getCurrentNodeLabel(run, nodes), "Upload");
});
test("restores tracked run id from route or running workflow metadata", () => {
assert.equal(
getTrackedRunId("run-from-query", {
status: "running",
lastRunId: "run-from-workflow",
}),
"run-from-query",
);
assert.equal(
getTrackedRunId("", {
status: "running",
lastRunId: "run-from-workflow",
}),
"run-from-workflow",
);
assert.equal(
getTrackedRunId("", {
status: "completed",
lastRunId: "run-from-workflow",
}),
"run-from-workflow",
);
});
test("falls back to run-level artifacts when node execution artifacts are absent", () => {
const run = {
artifacts: [
{
id: "artifact-1",
nodeId: "source",
portId: "out-1",
portName: "manifest",
},
],
nodeExecutions: [{ nodeId: "source", nodeLabel: "Source", status: "completed" }],
};
assert.deepEqual(getNodeArtifacts(run, "source"), run.artifacts);
});
test("resolves edge output artifacts by source handle and source port definition", () => {
const run = {
artifacts: [],
nodeExecutions: [
{
nodeId: "source",
nodeLabel: "Source",
status: "completed",
artifacts: [
{
id: "artifact-1",
nodeId: "source",
portId: "out-1",
portName: "manifest",
name: "manifest.json",
},
{
id: "artifact-2",
nodeId: "source",
portId: "out-2",
portName: "debug",
name: "debug.json",
},
],
},
],
};
const graphNodes = [
{
id: "source",
data: {
outputs: [
{ id: "out-1", name: "manifest" },
{ id: "out-2", name: "debug" },
],
},
},
];
assert.deepEqual(getEdgeOutputArtifacts({ id: "edge-1", source: "source", sourceHandle: "out-1" }, run, graphNodes), [
run.nodeExecutions[0].artifacts[0],
]);
});
test("resolves edge output artifacts by port name when the run payload lacks port ids", () => {
const run = {
nodeExecutions: [
{
nodeId: "source",
nodeLabel: "Source",
status: "completed",
artifacts: [
{
id: "artifact-1",
nodeId: "source",
portName: "审核通过",
name: "approved.json",
},
],
},
],
};
const graphNodes = [
{
id: "source",
data: {
outputs: [{ id: "approved", name: "审核通过" }],
},
},
];
assert.deepEqual(
getEdgeOutputArtifacts({ id: "edge-1", source: "source", sourceHandle: "approved" }, run, graphNodes),
[run.nodeExecutions[0].artifacts[0]],
);
});
test("does not fall back to unrelated artifacts for an explicitly named edge", () => {
const run = {
nodeExecutions: [
{
nodeId: "human",
status: "completed",
artifacts: [
{ id: "annotation", portId: "annotations", name: "annotations-manifest.json", type: "json" },
{ id: "headpose", portId: "headpose", name: "headpose-manifest.json", type: "json" },
],
},
],
};
const nodes = [
{
id: "human",
data: {
outputs: [
{ id: "approvedVideos", name: "approvedVideos" },
{ id: "annotations", name: "annotations" },
],
},
},
];
assert.deepEqual(getEdgeOutputArtifacts({ source: "human", sourceHandle: "approvedVideos" }, run, nodes), []);
});
test("keeps the historical single-output fallback when artifacts have no port metadata", () => {
const artifact = { id: "legacy", name: "legacy.json", type: "json" };
const run = { nodeExecutions: [{ nodeId: "legacy-node", status: "completed", artifacts: [artifact] }] };
const nodes = [{ id: "legacy-node", data: { outputs: [{ id: "out-1", name: "output" }] } }];
assert.deepEqual(getEdgeOutputArtifacts({ source: "legacy-node", sourceHandle: "out-1" }, run, nodes), [artifact]);
});