using FluentAssertions;
using Workflow.Application.Form.Schema;
using Xunit;
namespace BuildFlow.Workflow.Tests.Form;
///
/// 钉钉式节点级字段权限的纯单元测试。
/// 验证 FieldPermissionEvaluator.GetHiddenFieldsByNodePermission 正确按节点 config 的
/// fieldPermissions(字段 path → 动作)计算出 hidden 字段集合。
///
public class NodeFieldPermissionTests
{
private static readonly IReadOnlyList SampleFields = new[]
{
new FieldSummary("amount", "number", "InputNumber", false, "金额", [], [], [], null),
new FieldSummary("reason", "string", "Input", true, "事由", [], [], [], null),
new FieldSummary("attachment", "array", "Upload", false, "附件", [], [], [], null),
};
[Fact]
public void NullPermissions_ReturnsEmpty()
{
var hidden = FieldPermissionEvaluator.GetHiddenFieldsByNodePermission(SampleFields, null);
hidden.Should().BeEmpty();
}
[Fact]
public void EmptyPermissions_ReturnsEmpty()
{
var hidden = FieldPermissionEvaluator.GetHiddenFieldsByNodePermission(SampleFields, new Dictionary());
hidden.Should().BeEmpty();
}
[Fact]
public void HiddenFields_AreCollected()
{
var perms = new Dictionary
{
["amount"] = "editable",
["reason"] = "hidden",
["attachment"] = "visible",
};
var hidden = FieldPermissionEvaluator.GetHiddenFieldsByNodePermission(SampleFields, perms);
hidden.Should().ContainSingle().Which.Should().Be("reason");
}
[Fact]
public void UnknownPath_IsIgnored()
{
// 权限配置里写了表单不存在的字段路径,应被忽略(不误伤、不报错)
var perms = new Dictionary
{
["nonExistent"] = "hidden",
["amount"] = "hidden",
};
var hidden = FieldPermissionEvaluator.GetHiddenFieldsByNodePermission(SampleFields, perms);
hidden.Should().ContainSingle().Which.Should().Be("amount");
}
[Fact]
public void AllHidden_AllFieldsCollected()
{
var perms = new Dictionary
{
["amount"] = "hidden",
["reason"] = "hidden",
["attachment"] = "hidden",
};
var hidden = FieldPermissionEvaluator.GetHiddenFieldsByNodePermission(SampleFields, perms);
hidden.Should().HaveCount(3);
}
}