Files
cpdaily_xiaozhushou/backed/internal/handler/leave_apply_attach_test.go
T

98 lines
2.7 KiB
Go

package handler
import (
"testing"
cpd "cpdaily-new/internal/service/cpdaily"
)
// TestMergeApplyImageURLs 附件合并:多图字段 + 旧前端兼容单图字段,
// 保序去重、剔除空串与首尾空白(避免旧缓存包附件被静默丢弃或重复提交)。
func TestMergeApplyImageURLs(t *testing.T) {
cases := []struct {
name string
urls []string
legacy string
want []string
}{
{
name: "仅多图字段",
urls: []string{"a", "b"},
legacy: "",
want: []string{"a", "b"},
},
{
name: "仅旧单图字段",
urls: nil,
legacy: "a",
want: []string{"a"},
},
{
name: "旧字段与多图重复应去重",
urls: []string{"a", "b"},
legacy: "a",
want: []string{"a", "b"},
},
{
name: "旧字段不在多图中应追加",
urls: []string{"a"},
legacy: "c",
want: []string{"a", "c"},
},
{
name: "空串与空白剔除",
urls: []string{"", " ", "a"},
legacy: " ",
want: []string{"a"},
},
{
name: "首尾空白裁剪",
urls: []string{" a ", " b"},
legacy: "",
want: []string{"a", "b"},
},
{
name: "全部为空",
urls: []string{""},
legacy: "",
want: []string{},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := mergeApplyImageURLs(c.urls, c.legacy)
if len(got) != len(c.want) {
t.Fatalf("mergeApplyImageURLs(%v, %q) = %v, want %v", c.urls, c.legacy, got, c.want)
}
for i := range got {
if got[i] != c.want[i] {
t.Fatalf("mergeApplyImageURLs(%v, %q) = %v, want %v", c.urls, c.legacy, got, c.want)
}
}
})
}
}
// TestLeaveNewToLeaveTypesAttachGating 新套类型翻译:材料区不可见时不得带出数量上限,
// 否则前端按「上限>0」的条件会误显示上传入口(旧套学校 enableAttach 恒为 0 的同类问题)。
func TestLeaveNewToLeaveTypesAttachGating(t *testing.T) {
types := []cpd.LeaveNewType{
{TypeWid: 1, TypeName: "可见类型"},
{TypeWid: 2, TypeName: "不可见类型"},
}
details := map[int]*cpd.LeaveNewFormDetail{
1: {TypeName: "可见类型", IsVisibledEvidenceFile: true, EvidenceFileCountMax: 4},
2: {TypeName: "不可见类型", IsVisibledEvidenceFile: false, EvidenceFileCountMax: 10},
}
out := leaveNewToLeaveTypes(types, func(wid int) *cpd.LeaveNewFormDetail { return details[wid] })
if len(out) != 2 {
t.Fatalf("期望 2 个类型, got %d", len(out))
}
if out[0].EnableAttach != 1 || out[0].AttachmentNumberLimit != 4 {
t.Errorf("可见类型应带出开关与上限: %+v", out[0])
}
if out[1].EnableAttach != 0 || out[1].AttachmentNumberLimit != 0 {
t.Errorf("不可见类型不应带出上限(否则前端误显示): %+v", out[1])
}
}