Skip to content

Commit 3ad38fe

Browse files
committed
fix:goimports and golangci-lint and remove some test
1 parent f8fd4ad commit 3ad38fe

File tree

4 files changed

+140
-13
lines changed

4 files changed

+140
-13
lines changed

examples/email/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"strings"
1111
"time"
12+
1213
"trpc.group/trpc-go/trpc-agent-go/agent/llmagent"
1314
"trpc.group/trpc-go/trpc-agent-go/event"
1415
"trpc.group/trpc-go/trpc-agent-go/model"
@@ -41,7 +42,7 @@ func main() {
4142
}
4243

4344
if err := chat.run(); err != nil {
44-
log.Fatal("Chat failed: %v", err)
45+
log.Fatalf("Chat failed: %s", err.Error())
4546
}
4647
}
4748

@@ -67,7 +68,7 @@ func (c *emailChat) run() error {
6768
}
6869

6970
// setup creates the runner with LLM agent and send email tool.
70-
func (c *emailChat) setup(ctx context.Context) error {
71+
func (c *emailChat) setup(_ context.Context) error {
7172
// Create OpenAI model.
7273
modelInstance := openai.New(c.modelName)
7374

tool/email/email.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ package email
1313

1414
import (
1515
"context"
16+
1617
"trpc.group/trpc-go/trpc-agent-go/tool"
1718
)
1819

@@ -21,27 +22,33 @@ const (
2122
defaultName = "email"
2223
)
2324

25+
// MailboxType mailbox
2426
type MailboxType int32
2527

2628
const (
27-
// unknown mail
29+
// MAIL_UNKNOWN unknown mail
2830
MAIL_UNKNOWN MailboxType = 0
29-
// qq mail
31+
// MAIL_QQ qq mail
3032
MAIL_QQ MailboxType = 1
31-
// 163 mail
33+
// MAIL_163 163 mail
3234
MAIL_163 MailboxType = 2
33-
// google email
35+
// MAIL_GMAIL google mail
3436
MAIL_GMAIL MailboxType = 3
3537
)
3638

39+
// MailboxTypeToString convert mailbox type to string
3740
func MailboxTypeToString(mailboxType MailboxType) string {
3841
switch mailboxType {
42+
// qq mail
3943
case MAIL_QQ:
4044
return "qq"
45+
// 163 mail
4146
case MAIL_163:
4247
return "163"
48+
// google mail
4349
case MAIL_GMAIL:
4450
return "gmail"
51+
// unknown mail
4552
default:
4653
return "unknown"
4754
}
@@ -72,7 +79,7 @@ type emailToolSet struct {
7279
}
7380

7481
// Tools implements the ToolSet interface.
75-
func (e *emailToolSet) Tools(ctx context.Context) []tool.Tool {
82+
func (e *emailToolSet) Tools(_ context.Context) []tool.Tool {
7683
return e.tools
7784
}
7885

tool/email/sendmail.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package email
33
import (
44
"context"
55
"fmt"
6-
"gopkg.in/gomail.v2"
76
"strings"
7+
8+
"gopkg.in/gomail.v2"
89
"trpc.group/trpc-go/trpc-agent-go/tool"
910
"trpc.group/trpc-go/trpc-agent-go/tool/function"
1011
)
@@ -33,7 +34,8 @@ type sendMailResponse struct {
3334
}
3435

3536
// sendMail performs the send mail operation.
36-
func (e *emailToolSet) sendMail(ctx context.Context, req *sendMailRequest) (rsp *sendMailResponse, err error) {
37+
// go smtp not support context, one send one mail, can't stop
38+
func (e *emailToolSet) sendMail(_ context.Context, req *sendMailRequest) (rsp *sendMailResponse, err error) {
3739
rsp = &sendMailResponse{}
3840

3941
mailBoxType, err := checkMailBoxType(req.Auth.Name)
@@ -63,8 +65,11 @@ func (e *emailToolSet) sendMail(ctx context.Context, req *sendMailRequest) (rsp
6365
s, err := dialer.Dial()
6466
if err != nil {
6567
rsp.Message = fmt.Sprintf("the address or password is incorrect,please check: %v", err)
66-
return rsp, fmt.Errorf("the address or password is incorrect,please check: %w", err)
68+
return rsp, nil
6769
}
70+
defer func() {
71+
_ = s.Close()
72+
}()
6873

6974
message := gomail.NewMessage()
7075
for _, m := range req.MailList {

tool/email/sendmail_test.go

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package email
33
import (
44
"context"
55
"testing"
6+
7+
"github.com/stretchr/testify/assert"
68
)
79

810
func TestMailTool_sendMail(t *testing.T) {
@@ -21,15 +23,15 @@ func TestMailTool_sendMail(t *testing.T) {
2123
Subject string
2224
Content string
2325
}{
24-
// qq to gmail
26+
2527
{
2628
2729
Password: "",
2830
ToEmail: "[email protected]",
2931
Subject: "test",
3032
Content: "test",
3133
},
32-
// gmail to qq
34+
3335
{
3436
3537
Password: "",
@@ -40,6 +42,10 @@ func TestMailTool_sendMail(t *testing.T) {
4042
}
4143
for _, tt := range tests {
4244

45+
if tt.Password == "" {
46+
t.Skip("no passwd skip")
47+
}
48+
4349
rsp, err := toolSet.(*emailToolSet).sendMail(context.Background(), &sendMailRequest{
4450
Auth: Auth{
4551
Name: tt.Name,
@@ -54,8 +60,116 @@ func TestMailTool_sendMail(t *testing.T) {
5460
},
5561
})
5662
if err != nil {
57-
t.Errorf("send mail failed, err: %v", err)
63+
t.Errorf("send mail err: %v", err)
5864
}
5965
t.Logf("rsp: %+v", rsp)
6066
}
6167
}
68+
69+
func Test_checkMailBoxType(t *testing.T) {
70+
type args struct {
71+
email string
72+
}
73+
tests := []struct {
74+
name string
75+
args args
76+
want MailboxType
77+
wantErr bool
78+
}{
79+
{
80+
name: "QQ domain",
81+
args: args{email: "[email protected]"},
82+
want: MAIL_QQ,
83+
wantErr: false,
84+
},
85+
{
86+
name: "QQ vip domain",
87+
args: args{email: "[email protected]"},
88+
want: MAIL_QQ,
89+
wantErr: false,
90+
},
91+
{
92+
name: "Foxmail domain",
93+
args: args{email: "[email protected]"},
94+
want: MAIL_QQ,
95+
wantErr: false,
96+
},
97+
{
98+
name: "Gmail domain",
99+
args: args{email: "[email protected]"},
100+
want: MAIL_GMAIL,
101+
wantErr: false,
102+
},
103+
{
104+
name: "Googlemail domain",
105+
args: args{email: "[email protected]"},
106+
want: MAIL_GMAIL,
107+
wantErr: false,
108+
},
109+
{
110+
name: "163 domain",
111+
args: args{email: "[email protected]"},
112+
want: MAIL_163,
113+
wantErr: false,
114+
},
115+
{
116+
name: "Unknown domain",
117+
args: args{email: "[email protected]"},
118+
want: MAIL_UNKNOWN,
119+
wantErr: false,
120+
},
121+
{
122+
name: "Empty email",
123+
args: args{email: ""},
124+
want: MAIL_UNKNOWN,
125+
wantErr: true,
126+
},
127+
{
128+
name: "No @ symbol",
129+
args: args{email: "invalid-email"},
130+
want: MAIL_UNKNOWN,
131+
wantErr: true,
132+
},
133+
{
134+
name: "Multiple @ symbols",
135+
args: args{email: "user@@example.com"},
136+
want: MAIL_UNKNOWN,
137+
wantErr: true,
138+
},
139+
{
140+
name: "Uppercase QQ domain",
141+
args: args{email: "[email protected]"},
142+
want: MAIL_QQ,
143+
wantErr: false,
144+
},
145+
{
146+
name: "Mixed case Gmail",
147+
args: args{email: "[email protected]"},
148+
want: MAIL_GMAIL,
149+
wantErr: false,
150+
},
151+
}
152+
for _, tt := range tests {
153+
t.Run(tt.name, func(t *testing.T) {
154+
got, err := checkMailBoxType(tt.args.email)
155+
if (err != nil) != tt.wantErr {
156+
t.Errorf("checkMailBoxType() error = %v, wantErr %v", err, tt.wantErr)
157+
return
158+
}
159+
if got != tt.want {
160+
t.Errorf("checkMailBoxType() = %v, want %v", got, tt.want)
161+
}
162+
})
163+
}
164+
}
165+
166+
func Test_emailToolSet_sendMailTool(t *testing.T) {
167+
e := &emailToolSet{}
168+
169+
got := e.sendMailTool()
170+
assert.NotNil(t, got)
171+
172+
decl := got.Declaration()
173+
assert.Equal(t, "send_email", decl.Name)
174+
assert.Equal(t, "send mail to other", decl.Description)
175+
}

0 commit comments

Comments
 (0)