84 lines
2.8 KiB
Go
84 lines
2.8 KiB
Go
package warningnotify
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestSmallAndLargePlatformSummary(t *testing.T) {
|
|
b := Batch{Method: MethodPlatform, WarnStage: 2}
|
|
for i := 0; i < 2; i++ {
|
|
b.Items = append(b.Items, Event{ItemType: 1, ItemCode: fmt.Sprintf("898600000000000000%02d", i), WarnLevel: 3})
|
|
}
|
|
applyContent(&b)
|
|
for _, item := range b.Items {
|
|
if !strings.Contains(b.PlatformContent, item.ItemCode) {
|
|
t.Fatal("missing card")
|
|
}
|
|
}
|
|
if b.EmailBody != "" || b.SMSContent != "" {
|
|
t.Fatal("unrelated content generated")
|
|
}
|
|
for i := 2; i < 10000; i++ {
|
|
b.Items = append(b.Items, Event{ItemType: 1, ItemCode: "89860000000000000000", WarnLevel: 3})
|
|
}
|
|
applyContent(&b)
|
|
if !strings.Contains(b.PlatformContent, "10000") || len([]rune(b.PlatformContent)) > 150 {
|
|
t.Fatal("unbounded summary")
|
|
}
|
|
b.Method = MethodWechat
|
|
b.EmailBody = ""
|
|
applyContent(&b)
|
|
if b.EmailBody != "" {
|
|
t.Fatal("wechat generated email")
|
|
}
|
|
payload := buildWechatPayload(b, "openid", "template", "pages/message/index")
|
|
if payload.Data["character_string6"].Value != "TOTAL-10000" {
|
|
t.Fatal("missing total")
|
|
}
|
|
if !strings.Contains(payload.Data["thing9"].Value, "10000") || !strings.Contains(payload.Data["thing9"].Value, levelText(3)) {
|
|
t.Fatal("missing count or severity")
|
|
}
|
|
}
|
|
|
|
func TestCollectorKeepsFullCountWithBoundedRecipientPreview(t *testing.T) {
|
|
collector := NewCollector()
|
|
recipient := &platformRecipientGroup{SendUserType: 2, UserIDs: []int64{11}}
|
|
other := &platformRecipientGroup{SendUserType: 2, UserIDs: []int64{12}}
|
|
for page := 0; page < 50; page++ {
|
|
batch := Batch{Method: MethodPlatform, Business: BusinessSimWarning, TenantID: "tenant-a", Category: "traffic", WarnStage: 2}
|
|
for index := 0; index < 200; index++ {
|
|
batch.Items = append(batch.Items, Event{ItemCode: fmt.Sprintf("8986%016d", page*200+index), WarnLevel: 3, ItemType: 1})
|
|
}
|
|
collector.merge(batch, recipient)
|
|
batch.Method = MethodWechat
|
|
collector.merge(batch, recipient)
|
|
}
|
|
collector.merge(Batch{Method: MethodPlatform, TenantID: "tenant-a", Business: BusinessSimWarning, Category: "traffic", Items: []Event{{ItemCode: "private", ItemType: 1}}}, other)
|
|
if len(collector.batches) != 3 {
|
|
t.Fatalf("channels or recipients merged: %d", len(collector.batches))
|
|
}
|
|
for _, batch := range collector.batches {
|
|
if len(batch.Items) > 5 {
|
|
t.Fatal("preview grows with cards")
|
|
}
|
|
if batch.prepared.UserIDs[0] == 11 {
|
|
if batchItemCount(*batch) != 10000 {
|
|
t.Fatal("count lost across pages")
|
|
}
|
|
for _, item := range batch.Items {
|
|
if item.ItemCode == "private" {
|
|
t.Fatal("recipient data leak")
|
|
}
|
|
}
|
|
if batch.Method == MethodWechat {
|
|
payload := buildWechatPayload(*batch, "openid", "template", "pages/message/index")
|
|
if payload.Data["character_string6"].Value != "TOTAL-10000" {
|
|
t.Fatal("preview count used as total")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|