using System.Text.Json.Nodes;
using XFEExtension.NetCore.XUnit.Assertions;
using XFEExtension.NetCore.XUnit.Attributes;
using XFEWorkflow.Abstractions;
using XFEWorkflow.Core.Runtime;
namespace XFEWorkflow.Test;
[TestFixture]
public sealed class BasicDateEncodingTests
{
[Test]
public void DatesPreserveOffsetsAndCompareInstantsAndRoundTripTimestamps()
{
const string original = "2026-10-13T08:15:30.123+08:00";
var parsed = WorkflowBasicNodes.Execute("core.date.parse", new() { ["value"] = original });
var expected = DateTimeOffset.Parse(original, System.Globalization.CultureInfo.InvariantCulture);
Assert.Equal(expected.ToUnixTimeMilliseconds(), parsed["timestamp"]!.GetValue<long>());
var utc = WorkflowBasicNodes.Execute("core.date.timeZone", new() { ["value"] = original, ["zone"] = "UTC" }).String("value");
Assert.Contains("T00:15:30.123", utc);
Assert.Equal(0m, WorkflowBasicNodes.Execute("core.date.compare", new() { ["left"] = original, ["right"] = utc })["value"]!.GetValue<decimal>());
var parts = WorkflowBasicNodes.Execute("core.date.parts", new() { ["value"] = original });
Assert.Equal(2026, parts.Integer("year")); Assert.Equal(10, parts.Integer("month")); Assert.Equal(13, parts.Integer("day")); Assert.Equal(8, parts.Integer("hour")); Assert.Equal(15, parts.Integer("minute")); Assert.Equal(30, parts.Integer("second"));
var added = WorkflowBasicNodes.Execute("core.date.add", new() { ["value"] = "2024-01-31T12:00:00+08:00", ["amount"] = 1, ["unit"] = "months" }).String("value");
Assert.Contains("2024-02-29T12:00:00", added);
Assert.Equal(86400d, WorkflowBasicNodes.Execute("core.date.difference", new() { ["start"] = original, ["end"] = "2026-10-14T08:15:30.123+08:00" })["value"]!.GetValue<double>());
foreach (var unit in new[] { "seconds", "milliseconds" })
{
var timestamp = WorkflowBasicNodes.Execute("core.date.timestamp", new() { ["value"] = original, ["unit"] = unit })["value"]!.DeepClone();
var restored = WorkflowBasicNodes.Execute("core.date.fromTimestamp", new() { ["value"] = timestamp, ["unit"] = unit }).String("value");
var delta = Math.Abs((DateTimeOffset.Parse(restored) - expected).TotalMilliseconds);
Assert.True(delta < (unit == "milliseconds" ? 1 : 1000));
}
var exact = WorkflowBasicNodes.Execute("core.date.parse", new() { ["value"] = "20261013", ["format"] = "yyyyMMdd" }).String("value");
Assert.Contains("2026-10-13T00:00:00.0000000+00:00", exact);
var now = WorkflowBasicNodes.Execute("core.date.now", new() { ["utc"] = true });
Assert.True(Math.Abs(now["timestamp"]!.GetValue<long>() - DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()) < 3000);
Assert.Throws<FormatException>(() => WorkflowBasicNodes.Execute("core.date.parse", new() { ["value"] = "坏日期" }));
}
[Test]
public void EncodingsRoundTripFullUnicodeAndLiteralBackslashText()
{
var text = " 中文 🌚👨👩👧👦👍🏽🇨🇳\n<>&+% $var \\u4e2d ";
foreach (var method in new[] { "base64", "hex", "url", "html" })
{
var encoded = WorkflowBasicNodes.Execute("core.encoding." + method + "Encode", new() { ["value"] = text }).String("value");
var restored = WorkflowBasicNodes.Execute("core.encoding." + method + "Decode", new() { ["value"] = encoded }).String("value");
Assert.Equal(text, restored, method);
}
Assert.Equal("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", WorkflowBasicNodes.Execute("core.encoding.hash", new() { ["value"] = "abc" }).String("value"));
foreach (var format in new[] { "D", "N", "B", "P" }) Assert.True(Guid.TryParseExact(WorkflowBasicNodes.Execute("core.value.guid", new() { ["format"] = format }).String("value"), format, out _));
foreach (var pretty in new[] { true, false })
{
var obj = new JsonObject { ["名称"] = text }; var json = WorkflowBasicNodes.Execute("core.json.stringify", new() { ["value"] = obj.DeepClone(), ["indent"] = pretty }).String("value");
Assert.Contains("中文 🌚👨👩👧👦👍🏽🇨🇳", json); Assert.True(JsonNode.DeepEquals(obj, JsonNode.Parse(json)));
}
Assert.Throws<FormatException>(() => WorkflowBasicNodes.Execute("core.encoding.base64Decode", new() { ["value"] = "!!" }));
Assert.Throws<System.Text.DecoderFallbackException>(() => WorkflowBasicNodes.Execute("core.encoding.hexDecode", new() { ["value"] = "FF" }));
}
[Test]
public void PathOperationsHandleNamesWithoutAccessingTheFilesystem()
{
const string path = @"C:\工作区\报告🌚.json";
var parts = WorkflowBasicNodes.Execute("core.path.parts", new() { ["value"] = path });
Assert.Equal("报告🌚.json", parts.String("fileName")); Assert.Equal("报告🌚", parts.String("name")); Assert.Equal(".json", parts.String("extension")); Assert.Equal(@"C:\工作区", parts.String("directory"));
Assert.Equal(@"C:\工作区\报告🌚.txt", WorkflowBasicNodes.Execute("core.path.changeExtension", new() { ["value"] = path }).String("value"));
Assert.Equal(@"C:\工作区\报告🌚", WorkflowBasicNodes.Execute("core.path.changeExtension", new() { ["value"] = path, ["extension"] = "" }).String("value"));
Assert.Equal(@"C:\工作区\报告🌚.json", WorkflowBasicNodes.Execute("core.path.combine", new() { ["left"] = @"C:\工作区", ["right"] = "报告🌚.json" }).String("value"));
Assert.Equal(@"C:\工作区\b.txt", WorkflowBasicNodes.Execute("core.path.full", new() { ["value"] = @"a\..\b.txt", ["basePath"] = @"C:\工作区" }).String("value"));
}
}
using System.Text.Json.Nodes;
using XFEExtension.NetCore.XUnit.Assertions;
using XFEExtension.NetCore.XUnit.Attributes;
using XFEWorkflow.Abstractions;
using XFEWorkflow.Core.Runtime;
namespace XFEWorkflow.Test;
[TestFixture]
public sealed class BasicDateEncodingTests
{
[Test]
public void DatesPreserveOffsetsAndCompareInstantsAndRoundTripTimestamps()
{
const string original = "2026-10-13T08:15:30.123+08:00";
var parsed = WorkflowBasicNodes.Execute("core.date.parse", new() { ["value"] = original });
var expected = DateTimeOffset.Parse(original, System.Globalization.CultureInfo.InvariantCulture);
Assert.Equal(expected.ToUnixTimeMilliseconds(), parsed["timestamp"]!.GetValue<long>());
var utc = WorkflowBasicNodes.Execute("core.date.timeZone", new() { ["value"] = original, ["zone"] = "UTC" }).String("value");
Assert.Contains("T00:15:30.123", utc);
Assert.Equal(0m, WorkflowBasicNodes.Execute("core.date.compare", new() { ["left"] = original, ["right"] = utc })["value"]!.GetValue<decimal>());
var parts = WorkflowBasicNodes.Execute("core.date.parts", new() { ["value"] = original });
Assert.Equal(2026, parts.Integer("year")); Assert.Equal(10, parts.Integer("month")); Assert.Equal(13, parts.Integer("day")); Assert.Equal(8, parts.Integer("hour")); Assert.Equal(15, parts.Integer("minute")); Assert.Equal(30, parts.Integer("second"));
var added = WorkflowBasicNodes.Execute("core.date.add", new() { ["value"] = "2024-01-31T12:00:00+08:00", ["amount"] = 1, ["unit"] = "months" }).String("value");
Assert.Contains("2024-02-29T12:00:00", added);
Assert.Equal(86400d, WorkflowBasicNodes.Execute("core.date.difference", new() { ["start"] = original, ["end"] = "2026-10-14T08:15:30.123+08:00" })["value"]!.GetValue<double>());
foreach (var unit in new[] { "seconds", "milliseconds" })
{
var timestamp = WorkflowBasicNodes.Execute("core.date.timestamp", new() { ["value"] = original, ["unit"] = unit })["value"]!.DeepClone();
var restored = WorkflowBasicNodes.Execute("core.date.fromTimestamp", new() { ["value"] = timestamp, ["unit"] = unit }).String("value");
var delta = Math.Abs((DateTimeOffset.Parse(restored) - expected).TotalMilliseconds);
Assert.True(delta < (unit == "milliseconds" ? 1 : 1000));
}
var exact = WorkflowBasicNodes.Execute("core.date.parse", new() { ["value"] = "20261013", ["format"] = "yyyyMMdd" }).String("value");
Assert.Contains("2026-10-13T00:00:00.0000000+00:00", exact);
var now = WorkflowBasicNodes.Execute("core.date.now", new() { ["utc"] = true });
Assert.True(Math.Abs(now["timestamp"]!.GetValue<long>() - DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()) < 3000);
Assert.Throws<FormatException>(() => WorkflowBasicNodes.Execute("core.date.parse", new() { ["value"] = "坏日期" }));
}
[Test]
public void EncodingsRoundTripFullUnicodeAndLiteralBackslashText()
{
var text = " 中文 🌚👨👩👧👦👍🏽🇨🇳\n<>&+% $var \\u4e2d ";
foreach (var method in new[] { "base64", "hex", "url", "html" })
{
var encoded = WorkflowBasicNodes.Execute("core.encoding." + method + "Encode", new() { ["value"] = text }).String("value");
var restored = WorkflowBasicNodes.Execute("core.encoding." + method + "Decode", new() { ["value"] = encoded }).String("value");
Assert.Equal(text, restored, method);
}
Assert.Equal("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", WorkflowBasicNodes.Execute("core.encoding.hash", new() { ["value"] = "abc" }).String("value"));
foreach (var format in new[] { "D", "N", "B", "P" }) Assert.True(Guid.TryParseExact(WorkflowBasicNodes.Execute("core.value.guid", new() { ["format"] = format }).String("value"), format, out _));
foreach (var pretty in new[] { true, false })
{
var obj = new JsonObject { ["名称"] = text }; var json = WorkflowBasicNodes.Execute("core.json.stringify", new() { ["value"] = obj.DeepClone(), ["indent"] = pretty }).String("value");
Assert.Contains("中文 🌚👨👩👧👦👍🏽🇨🇳", json); Assert.True(JsonNode.DeepEquals(obj, JsonNode.Parse(json)));
}
Assert.Throws<FormatException>(() => WorkflowBasicNodes.Execute("core.encoding.base64Decode", new() { ["value"] = "!!" }));
Assert.Throws<System.Text.DecoderFallbackException>(() => WorkflowBasicNodes.Execute("core.encoding.hexDecode", new() { ["value"] = "FF" }));
}
[Test]
public void PathOperationsHandleNamesWithoutAccessingTheFilesystem()
{
const string path = @"C:\工作区\报告🌚.json";
var parts = WorkflowBasicNodes.Execute("core.path.parts", new() { ["value"] = path });
Assert.Equal("报告🌚.json", parts.String("fileName")); Assert.Equal("报告🌚", parts.String("name")); Assert.Equal(".json", parts.String("extension")); Assert.Equal(@"C:\工作区", parts.String("directory"));
Assert.Equal(@"C:\工作区\报告🌚.txt", WorkflowBasicNodes.Execute("core.path.changeExtension", new() { ["value"] = path }).String("value"));
Assert.Equal(@"C:\工作区\报告🌚", WorkflowBasicNodes.Execute("core.path.changeExtension", new() { ["value"] = path, ["extension"] = "" }).String("value"));
Assert.Equal(@"C:\工作区\报告🌚.json", WorkflowBasicNodes.Execute("core.path.combine", new() { ["left"] = @"C:\工作区", ["right"] = "报告🌚.json" }).String("value"));
Assert.Equal(@"C:\工作区\b.txt", WorkflowBasicNodes.Execute("core.path.full", new() { ["value"] = @"a\..\b.txt", ["basePath"] = @"C:\工作区" }).String("value"));
}
}