@@ -19,21 +19,72 @@ package umeng
1919import (
2020 "testing"
2121
22+ "github.com/bytedance/mockey"
23+ "github.com/stretchr/testify/assert"
24+
2225 "github.com/west2-online/fzuhelper-server/config"
2326 "github.com/west2-online/fzuhelper-server/pkg/constants"
2427)
2528
29+ func TestPushByType (t * testing.T ) {
30+ tests := []struct {
31+ name string
32+ pushType string
33+ androidErr error
34+ iosErr error
35+ }{
36+ {
37+ name : "send to both android and ios" ,
38+ pushType : constants .UmengPushTypeScore ,
39+ },
40+ {
41+ name : "ignore android and ios failures" ,
42+ pushType : constants .UmengPushTypeExam ,
43+ androidErr : assert .AnError ,
44+ iosErr : assert .AnError ,
45+ },
46+ }
47+
48+ for _ , tt := range tests {
49+ t .Run (tt .name , func (t * testing.T ) {
50+ var androidPushType string
51+ androidPatch := mockey .Mock (SendAndroidGroupcastWithGoApp ).To (
52+ func (pushType , title , text , ticker , tag , description , deeplink string ) error {
53+ androidPushType = pushType
54+ return tt .androidErr
55+ },
56+ ).Build ()
57+ defer androidPatch .UnPatch ()
58+
59+ iosCalled := false
60+ iosPatch := mockey .Mock (SendIOSGroupcast ).To (
61+ func (title , subtitle , body , tag , description , deeplink string ) error {
62+ iosCalled = true
63+ return tt .iosErr
64+ },
65+ ).Build ()
66+ defer iosPatch .UnPatch ()
67+
68+ // PushByType 为尽力而为,两端失败仅记录日志,不应 panic
69+ PushByType (tt .pushType , "title" , "text" , "ticker" , "tag" , "description" , "deeplink" )
70+
71+ assert .Equal (t , tt .pushType , androidPushType )
72+ assert .True (t , iosCalled )
73+ })
74+ }
75+ }
76+
2677func TestGetXiaomiNoticeProperties (t * testing.T ) {
2778 notice := config.XiaomiNotice {
28- Score : config. XiaomiNoticeTemplate {
79+ constants . UmengPushTypeScore : {
2980 ChannelID : "score-channel" ,
3081 TemplateID : "P12395" ,
3182 },
32- Exam : config. XiaomiNoticeTemplate {
83+ constants . UmengPushTypeExam : {
3384 ChannelID : "exam-channel" ,
3485 TemplateID : "P12394" ,
3586 },
36- Teaching : config. XiaomiNoticeTemplate {
87+ constants . UmengPushTypeTeaching : {
3788 ChannelID : "teaching-channel" ,
3889 TemplateID : "P12325" ,
3990 },
@@ -42,44 +93,44 @@ func TestGetXiaomiNoticeProperties(t *testing.T) {
4293 tests := []struct {
4394 name string
4495 text string
45- deeplink string
96+ pushType string
4697 wantChannelID string
4798 wantTemplate string
4899 wantKeyword string
49100 }{
50101 {
51102 name : "score" ,
52103 text : "数据结构" + constants .UmengGradeNotificationBodySuffix ,
53- deeplink : constants .UmengGradeDeeplink ,
104+ pushType : constants .UmengPushTypeScore ,
54105 wantChannelID : "score-channel" ,
55106 wantTemplate : "P12395" ,
56107 wantKeyword : "数据结构" ,
57108 },
58109 {
59110 name : "exam" ,
60111 text : "数据结构" + constants .UmengExamNotificationBodySuffix ,
61- deeplink : constants .UmengExamRoomDeeplink ,
112+ pushType : constants .UmengPushTypeExam ,
62113 wantChannelID : "exam-channel" ,
63114 wantTemplate : "P12394" ,
64115 wantKeyword : "数据结构" ,
65116 },
66117 {
67118 name : "teaching" ,
68119 text : "关于补考安排的通知" ,
69- deeplink : constants .UmengJwchNoticeDeeplink + "?url=https%3A%2F%2Fexample.com%2Fnotice" ,
120+ pushType : constants .UmengPushTypeTeaching ,
70121 wantChannelID : "teaching-channel" ,
71122 wantTemplate : "P12325" ,
72123 wantKeyword : "关于补考安排的通知" ,
73124 },
74125 {
75- name : "unknown deeplink " ,
76- deeplink : "fzuhelper:// unknown" ,
126+ name : "unknown push type " ,
127+ pushType : "unknown" ,
77128 },
78129 }
79130
80131 for _ , tt := range tests {
81132 t .Run (tt .name , func (t * testing.T ) {
82- gotChannelID , gotProperties := getXiaomiNoticeProperties (tt .text , tt .deeplink , notice )
133+ gotChannelID , gotProperties := getXiaomiNoticeProperties (tt .text , tt .pushType , notice )
83134 if gotChannelID != tt .wantChannelID {
84135 t .Fatalf ("channel ID = %q, want %q" , gotChannelID , tt .wantChannelID )
85136 }
0 commit comments