droboticflow/tests/frontend/templates.test.mjs
2026-06-18 13:30:56 +08:00

242 lines
7.3 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import {
buildTemplatePayload,
buildWorkflowCopyPayload,
buildWorkflowFromTemplate,
buildWorkflowPayload,
getTemplatePayloadIssue,
getWorkflowPayloadIssue,
getWorkflowFromTemplateIssue,
} 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://bos.example.test",
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("template payload validation allows incomplete scan drafts but rejects invalid endpoints", () => {
assert.equal(
getTemplatePayloadIssue({
name: "扫描草稿模板",
type: "batch",
kind: "scan",
scanConfig: {
dataSource: "BOS",
bucket: "",
prefix: "",
endpoint: "",
},
nodes: [],
edges: [],
}),
"",
);
assert.match(
getTemplatePayloadIssue({
name: "坏 endpoint 模板",
type: "batch",
kind: "scan",
scanConfig: {
dataSource: "BOS",
bucket: "",
prefix: "",
endpoint: "ftp://bos.example.test",
},
nodes: [],
edges: [],
}),
/absolute http\(s\) URL/,
);
assert.match(
getTemplatePayloadIssue({
name: "坏 scanMode 模板",
type: "batch",
kind: "scan",
scanConfig: {
dataSource: "BOS",
scanMode: "APPEND",
},
nodes: [],
edges: [],
}),
/scanMode must be FULL or INCREMENTAL/,
);
});
test("template instantiation validation rejects invalid scan endpoints", () => {
assert.match(
getWorkflowFromTemplateIssue({
name: "坏 endpoint 模板",
type: "batch",
kind: "scan",
scanConfig: {
dataSource: "BOS",
bucket: "datastorage",
prefix: "algo/X-platform-test",
endpoint: "https://bos.example.test/bad path",
accessKeyId: "ak",
secretAccessKey: "sk",
},
nodes: [],
edges: [],
}),
/without whitespace/,
);
});
test("buildWorkflowPayload preserves generated workflow-level scan settings", () => {
const generated = {
name: "AI 扫描",
description: "generated",
type: "streaming",
kind: "scan",
scanConfig: {
dataSource: "BOS",
bucket: "datastorage",
prefix: "algo/X-platform-test",
endpoint: "https://bos.example.test",
accessKeyId: "ak-generated",
secretAccessKey: "sk-generated",
scanMode: "INCREMENTAL",
enabled: true,
},
hyperparams: [{ key: "GLOBAL_ENV", value: "ai-generated" }],
maxConcurrentRuns: 2,
schedule: { interval: 900, sourcePath: "bos://datastorage/algo/X-platform-test" },
nodes: [{ id: "scan-source", data: { label: "BOS 增量扫描" } }],
edges: [],
};
const payload = buildWorkflowPayload(generated, {
name: "AI 扫描工作流",
description: "落库前覆盖描述",
});
assert.equal(payload.name, "AI 扫描工作流");
assert.equal(payload.description, "落库前覆盖描述");
assert.equal(payload.type, "streaming");
assert.equal(payload.kind, "scan");
assert.equal(payload.scanConfig.endpoint, "https://bos.example.test");
assert.equal(payload.maxConcurrentRuns, 2);
assert.equal(payload.schedule.interval, 900);
assert.equal(payload.hyperparams[0].value, "ai-generated");
});
test("workflow payload validation rejects invalid generated scan config", () => {
assert.match(
getWorkflowPayloadIssue({
name: "AI 坏扫描",
type: "streaming",
kind: "scan",
scanConfig: {
dataSource: "BOS",
endpoint: "ftp://bos.example.test",
},
nodes: [],
edges: [],
}),
/absolute http\(s\) URL/,
);
});
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");
});