droboticflow/tests/frontend/templates.test.mjs
2026-06-15 17:50:39 +08:00

107 lines
4.0 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { buildTemplatePayload, buildWorkflowCopyPayload, buildWorkflowFromTemplate } from "../../src/lib/templates.js";
test("buildTemplatePayload keeps scan config and execution settings", () => {
const workflow = {
name: "扫描工作流",
description: "desc",
type: "streaming",
kind: "scan",
scanConfig: {
dataSource: "BOS",
sourcePath: "bos://wjzh-data-migration/wjzh-0330-Data-migration",
bucket: "wjzh-data-migration",
prefix: "wjzh-0330-Data-migration",
endpoint: "https://bj.bcebos.com",
accessKeyId: "ak-real",
secretAccessKey: "sk-real",
scanMode: "INCREMENTAL",
enabled: true,
},
hyperparams: [{ key: "GLOBAL_ENV", value: "workflow-level" }],
maxConcurrentRuns: 3,
nodeTimeoutSeconds: 60,
runTimeoutSeconds: 3600,
maxRetryAttempts: 2,
retryBackoffSeconds: 15,
schedule: { interval: 300, sourcePath: "bos://wjzh-data-migration/wjzh-0330-Data-migration" },
nodes: [{ id: "source", data: { hyperparams: [{ key: "SOURCE_PATH", value: "bos://..." }] } }],
edges: [{ id: "e1", source: "source", target: "upload" }],
};
const payload = buildTemplatePayload(workflow, { name: "扫描模板" });
assert.equal(payload.name, "扫描模板");
assert.equal(payload.kind, "scan");
assert.equal(payload.scanConfig.accessKeyId, "ak-real");
assert.equal(payload.scanConfig.secretAccessKey, "sk-real");
assert.equal(payload.maxConcurrentRuns, 3);
assert.equal(payload.runTimeoutSeconds, 3600);
assert.equal(payload.hyperparams[0].value, "workflow-level");
assert.equal(payload.schedule.interval, 300);
assert.equal(payload.nodes[0].data.hyperparams[0].value, "bos://...");
});
test("buildWorkflowFromTemplate keeps scan config and deep clones payload", () => {
const template = {
name: "扫描模板",
description: "desc",
type: "streaming",
kind: "scan",
scanConfig: {
accessKeyId: "ak-template",
secretAccessKey: "sk-template",
bucket: "wjzh-data-migration",
},
hyperparams: [{ key: "GLOBAL_ENV", value: "template-level" }],
nodes: [{ id: "source", data: { hyperparams: [{ key: "SOURCE_PATH", value: "bos://src" }] } }],
edges: [{ id: "e1", source: "source", target: "upload" }],
};
const workflow = buildWorkflowFromTemplate(template);
workflow.scanConfig.accessKeyId = "mutated";
workflow.hyperparams[0].value = "changed-global";
workflow.nodes[0].data.hyperparams[0].value = "changed";
assert.equal(template.scanConfig.accessKeyId, "ak-template");
assert.equal(template.hyperparams[0].value, "template-level");
assert.equal(template.nodes[0].data.hyperparams[0].value, "bos://src");
});
test("buildWorkflowCopyPayload keeps all workflow-level fields", () => {
const workflow = {
name: "原工作流",
description: "desc",
type: "streaming",
kind: "scan",
scanConfig: {
accessKeyId: "ak-copy",
secretAccessKey: "sk-copy",
sourcePath: "bos://wjzh-data-migration/wjzh-0330-Data-migration",
},
hyperparams: [{ key: "GLOBAL_ENV", value: "copy-level" }],
maxConcurrentRuns: 5,
nodeTimeoutSeconds: 120,
runTimeoutSeconds: 7200,
maxRetryAttempts: 4,
retryBackoffSeconds: 30,
schedule: { interval: 600, sourcePath: "bos://wjzh-data-migration/wjzh-0330-Data-migration" },
nodes: [
{ id: "n1", data: { label: "BOS 增量扫描", hyperparams: [{ key: "BOS_SECRET_ACCESS_KEY", value: "secret" }] } },
],
edges: [{ id: "e1", source: "n1", target: "n2" }],
};
const copy = buildWorkflowCopyPayload(workflow, { name: "原工作流 (副本)" });
assert.equal(copy.name, "原工作流 (副本)");
assert.equal(copy.kind, "scan");
assert.equal(copy.scanConfig.secretAccessKey, "sk-copy");
assert.equal(copy.hyperparams[0].value, "copy-level");
assert.equal(copy.maxRetryAttempts, 4);
assert.equal(copy.retryBackoffSeconds, 30);
assert.equal(copy.nodes[0].data.hyperparams[0].value, "secret");
});