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

89 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import test from "node:test";
import assert from "node:assert/strict";
import { buildWorkflowSystemEnvVarsPreview, toWorkflowEnvDateTime } from "../../src/lib/workflow-system-env.js";
test("converts ISO timestamps to Asia/Shanghai datetime strings", () => {
assert.equal(toWorkflowEnvDateTime("2026-04-21T10:00:00Z"), "2026-04-21T18:00:00+08:00");
assert.equal(toWorkflowEnvDateTime(""), "");
assert.equal(toWorkflowEnvDateTime("not-a-date"), "");
});
test("builds next-run preview values from workflow state when no run is loaded", () => {
const preview = buildWorkflowSystemEnvVarsPreview({
workflowLastRunAt: "2026-04-21T10:00:00Z",
now: new Date("2026-04-21T11:30:45Z"),
});
assert.deepEqual(preview, [
{
key: "WORKFLOW_PREVIOUS_RUN_TIMESTAMP",
value: "2026-04-21T18:00:00+08:00",
description: "下一次运行将使用的上一轮触发时间(Asia/Shanghai)",
source: "system",
},
{
key: "WORKFLOW_CURRENT_RUN_TIMESTAMP",
value: "2026-04-21T19:30:45+08:00",
description: "如果现在运行,将注入这个本轮触发时间预览(Asia/Shanghai)",
source: "system",
},
]);
});
test("omits previous timestamp preview when scheduled previous env should not be injected", () => {
const preview = buildWorkflowSystemEnvVarsPreview({
workflowLastRunAt: "2026-04-21T10:00:00Z",
now: new Date("2026-04-21T11:30:45Z"),
includePrevious: false,
});
assert.deepEqual(preview, [
{
key: "WORKFLOW_CURRENT_RUN_TIMESTAMP",
value: "2026-04-21T19:30:45+08:00",
description: "如果现在运行,将注入这个本轮触发时间预览(Asia/Shanghai)",
source: "system",
},
]);
});
test("omits current timestamp preview when current env should not be injected", () => {
const preview = buildWorkflowSystemEnvVarsPreview({
workflowLastRunAt: "2026-04-21T10:00:00Z",
now: new Date("2026-04-21T11:30:45Z"),
includePrevious: false,
includeCurrent: false,
});
assert.deepEqual(preview, []);
});
test("prefers loaded run timestamps over workflow preview state", () => {
const preview = buildWorkflowSystemEnvVarsPreview({
workflowLastRunAt: "2026-04-21T10:00:00Z",
run: {
id: "run-1",
previousRunAt: "2026-04-21T09:00:00Z",
currentRunAt: "2026-04-21T11:30:00Z",
createdAt: "2026-04-21T11:30:05Z",
},
now: new Date("2026-04-21T11:30:45Z"),
});
assert.deepEqual(preview, [
{
key: "WORKFLOW_PREVIOUS_RUN_TIMESTAMP",
value: "2026-04-21T17:00:00+08:00",
description: "当前已加载 run 的上一轮触发时间(Asia/Shanghai)",
source: "system",
},
{
key: "WORKFLOW_CURRENT_RUN_TIMESTAMP",
value: "2026-04-21T19:30:00+08:00",
description: "当前已加载 run 的本轮触发时间(Asia/Shanghai)",
source: "system",
},
]);
});