package lifecycle import ( "strings" "testing" "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model/entity" lifecycle "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/service/lifecycle" ) // TestBatchFailureSummaryUsesChineseAndCounts 固化用户要求的失败文案: // 必须是中文、带成功失败张数,并附带最主要的失败原因。 func TestBatchFailureSummaryUsesChineseAndCounts(t *testing.T) { got := batchFailureSummary(50, 28, 22, []string{ "当前卡状态不支持该操作", "当前卡状态不支持该操作", "请求上游超时", }) want := "共50张,成功28张,失败22张;主要失败原因:当前卡状态不支持该操作" if got != want { t.Fatalf("失败汇总=%q,期望%q", got, want) } if strings.Contains(got, "SIM cards failed") { t.Fatal("仍包含英文文案") } } // TestBatchFailureSummaryWithoutReason 覆盖没有具体原因的情况,不能拼出空的原因段。 func TestBatchFailureSummaryWithoutReason(t *testing.T) { if got := batchFailureSummary(3, 1, 2, nil); got != "共3张,成功1张,失败2张" { t.Fatalf("无原因时=%q", got) } if got := batchFailureSummary(3, 1, 2, []string{"", " "}); got != "共3张,成功1张,失败2张" { t.Fatalf("空白原因应被忽略,实际=%q", got) } } // TestDominantReasonStableOnTie 同频时按字节序取最小值,保证同一批次文案稳定。 // 注意这里是字节序而非拼音序:「乙」的 UTF-8 编码小于「甲」,所以同频时取「乙原因」。 func TestDominantReasonStableOnTie(t *testing.T) { if got := dominantReason([]string{"甲原因", "乙原因"}); got != "乙原因" { t.Fatalf("同频应按字节序取最小,实际=%q", got) } if got := dominantReason([]string{"少数", "多数", "多数"}); got != "多数" { t.Fatalf("应取出现次数最多,实际=%q", got) } } // TestStoredFailureSummarySkipsNilAndEmpty 覆盖重启复机阶段:落库明细里的空原因与空指针不能参与统计。 func TestStoredFailureSummarySkipsNilAndEmpty(t *testing.T) { details := []*entity.BatchDetail{ {DetailStatus: 2}, nil, {DetailStatus: 3, ErrorReason: "当前卡状态不支持该操作"}, {DetailStatus: 3, ErrorReason: ""}, } got := storedFailureSummary(details, 1, 2) if !strings.HasPrefix(got, "共4张,成功1张,失败2张") || !strings.Contains(got, "当前卡状态不支持该操作") { t.Fatalf("落库汇总=%q", got) } } // TestLifecycleFailureSummaryUsesDetailReason 覆盖停复机首阶段:内存明细的 Reason 作为主要失败原因。 func TestLifecycleFailureSummaryUsesDetailReason(t *testing.T) { details := []*lifecycle.Detail{ {Reason: ""}, {Reason: "停机或预销户卡,不能办理该业务"}, } got := lifecycleFailureSummary(details, 1, 1) if got != "共2张,成功1张,失败1张;主要失败原因:停机或预销户卡,不能办理该业务" { t.Fatalf("首阶段汇总=%q", got) } }