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

276 lines
8.0 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import {
buildSelectedNodeEnvRows,
getDualTrackActionStates,
getWorkflowDebugRunActionState,
getWorkflowEditorLockState,
getWorkflowProductionRunActionState,
getWorkflowRunActionState,
getWorkflowRunPreparation,
isWorkflowSettingsLocked,
} from "../../src/lib/workflow-editor.js";
test("completed workflows can be edited and run again when no run is active", () => {
const action = getWorkflowRunActionState({
isTemplate: false,
isReadOnly: false,
activeRunStatus: "",
isRunContextLoading: false,
isExecuting: false,
});
assert.equal(action.disabled, false);
assert.equal(action.reason, "ready");
assert.equal(action.label, "运行工作流");
});
test("a synchronized workflow runs without issuing another save", () => {
assert.deepEqual(
getWorkflowRunPreparation({
currentFingerprint: "same",
lastSavedFingerprint: "same",
hasDraftRecovery: false,
}),
{ blocked: false, requiresSave: false, message: "" },
);
});
test("a dirty workflow saves before running and unresolved drafts block explicitly", () => {
assert.equal(
getWorkflowRunPreparation({
currentFingerprint: "dirty",
lastSavedFingerprint: "saved",
hasDraftRecovery: false,
}).requiresSave,
true,
);
const unresolved = getWorkflowRunPreparation({
currentFingerprint: "same",
lastSavedFingerprint: "same",
hasDraftRecovery: true,
});
assert.equal(unresolved.blocked, true);
assert.match(unresolved.message, /本地草稿/);
});
test("active runs take precedence over readonly button labels", () => {
const action = getWorkflowRunActionState({
isTemplate: false,
isReadOnly: true,
activeRunStatus: "running",
isRunContextLoading: false,
isExecuting: false,
});
assert.equal(action.disabled, true);
assert.equal(action.reason, "running");
assert.equal(action.label, "工作流运行中");
});
test("stale running workflow metadata does not lock an editor with no active run", () => {
const action = getWorkflowRunActionState({
isTemplate: false,
isReadOnly: false,
workflowStatus: "running",
activeRunStatus: "",
isRunContextLoading: false,
isExecuting: false,
});
assert.equal(action.disabled, false);
assert.equal(action.reason, "ready");
});
test("active run status overrides stale completed workflow status", () => {
const action = getWorkflowRunActionState({
isTemplate: false,
isReadOnly: false,
activeRunStatus: "running",
isRunContextLoading: false,
isExecuting: false,
});
assert.equal(action.disabled, true);
assert.equal(action.reason, "running");
assert.equal(action.label, "工作流运行中");
});
test("workflow settings lock while an active run is still in flight", () => {
assert.equal(
isWorkflowSettingsLocked({
isReadOnly: false,
activeRunStatus: "running",
isRunContextLoading: false,
}),
true,
);
});
test("workflow settings lock only for readonly or active workflows", () => {
assert.equal(
isWorkflowSettingsLocked({
isReadOnly: false,
activeRunStatus: "completed",
isRunContextLoading: false,
}),
false,
);
assert.equal(
isWorkflowSettingsLocked({
isReadOnly: true,
activeRunStatus: "",
isRunContextLoading: false,
}),
true,
);
assert.equal(
isWorkflowSettingsLocked({
isReadOnly: false,
activeRunStatus: "",
isRunContextLoading: false,
}),
false,
);
});
test("editor stays locked while active run context is being resolved", () => {
const lock = getWorkflowEditorLockState({
isReadOnly: false,
activeRunStatus: "",
isRunContextLoading: true,
});
const action = getWorkflowRunActionState({
isTemplate: false,
isReadOnly: false,
activeRunStatus: "",
isRunContextLoading: true,
isExecuting: false,
});
assert.deepEqual(lock, {
locked: true,
reason: "checkingRun",
message: "正在确认工作流运行状态,确认完成后即可编辑。",
});
assert.equal(action.disabled, true);
assert.equal(action.reason, "checkingRun");
});
test("terminal runs release every editor mutation lock", () => {
for (const activeRunStatus of ["completed", "failed", "cancelled", ""]) {
assert.deepEqual(
getWorkflowEditorLockState({
isReadOnly: false,
activeRunStatus,
isRunContextLoading: false,
}),
{ locked: false, reason: "ready", message: "" },
);
}
});
test("editor mutations lock during the save-and-start window", () => {
const lock = getWorkflowEditorLockState({
isReadOnly: false,
activeRunStatus: "",
isRunContextLoading: false,
isExecuting: true,
});
const action = getWorkflowRunActionState({
isTemplate: false,
isReadOnly: false,
activeRunStatus: "",
isRunContextLoading: false,
isExecuting: true,
});
assert.equal(lock.locked, true);
assert.equal(lock.reason, "starting");
assert.equal(action.label, "正在创建 Run…");
assert.match(action.message, /代码清单/);
});
test("released workflow definitions stay runnable but editor mutations remain locked", () => {
const lock = getWorkflowEditorLockState({
isReadOnly: false,
activeRunStatus: "completed",
isRunContextLoading: false,
isExecuting: false,
definitionState: "released",
});
assert.equal(lock.locked, true);
assert.equal(lock.reason, "released");
const action = getWorkflowRunActionState({
isTemplate: false,
isReadOnly: false,
activeRunStatus: "completed",
isRunContextLoading: false,
isExecuting: false,
});
assert.equal(action.disabled, false);
});
test("node env rows include inherited workflow envs but keep node overrides first", () => {
const rows = buildSelectedNodeEnvRows({
nodeHyperparams: [
{ key: "GLOBAL_ENV", value: "node-level" },
{ key: "NODE_ONLY", value: "yes" },
],
workflowHyperparams: [
{ key: "GLOBAL_ENV", value: "workflow-level" },
{ key: "WORKFLOW_ONLY", value: "shared" },
],
systemRows: [{ key: "WORKFLOW_CURRENT_RUN_TIMESTAMP", value: "2026-04-22T10:00:00+08:00", source: "system" }],
});
assert.deepEqual(
rows.map((item) => ({ key: item.key, value: item.value, source: item.source })),
[
{ key: "GLOBAL_ENV", value: "node-level", source: "custom" },
{ key: "NODE_ONLY", value: "yes", source: "custom" },
{ key: "WORKFLOW_ONLY", value: "shared", source: "workflow" },
{
key: "WORKFLOW_CURRENT_RUN_TIMESTAMP",
value: "2026-04-22T10:00:00+08:00",
source: "system",
},
],
);
});
test("dual-track action state helpers derive distinct debug and production action states", () => {
// Draft workflow
const draftActions = getDualTrackActionStates({
definitionState: "draft",
isTemplate: false,
isReadOnly: false,
activeRunStatus: "",
isRunContextLoading: false,
isExecuting: false,
});
assert.equal(draftActions.debugAction.disabled, false);
assert.equal(draftActions.debugAction.label, "调试运行");
assert.equal(draftActions.debugAction.tone, "amber");
assert.equal(draftActions.productionAction.disabled, true);
assert.equal(draftActions.productionAction.reason, "unreleased");
assert.equal(draftActions.productionAction.label, "正式运行 (需发布)");
// Released workflow
const releasedDebug = getWorkflowDebugRunActionState({ definitionState: "released" });
const releasedProd = getWorkflowProductionRunActionState({ definitionState: "released" });
assert.equal(releasedDebug.disabled, false);
assert.equal(releasedProd.disabled, false);
assert.equal(releasedProd.label, "正式运行");
assert.equal(releasedProd.tone, "blue");
// Executing state
const executingActions = getDualTrackActionStates({ isExecuting: true });
assert.equal(executingActions.debugAction.disabled, true);
assert.equal(executingActions.debugAction.reason, "starting");
assert.equal(executingActions.productionAction.disabled, true);
assert.equal(executingActions.productionAction.reason, "starting");
});