Refactored orchestrator for staged file handling, added structured prompt support, adjusted Feishu file handling
This commit is contained in:
138
internal/transport/feishu/bot_test.go
Normal file
138
internal/transport/feishu/bot_test.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package feishu
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
|
||||
)
|
||||
|
||||
func mustEventFromJSON(t *testing.T, raw string) *larkim.P2MessageReceiveV1 {
|
||||
t.Helper()
|
||||
var evt larkim.P2MessageReceiveV1
|
||||
if err := json.Unmarshal([]byte(raw), &evt); err != nil {
|
||||
t.Fatalf("unmarshal event json failed: %v", err)
|
||||
}
|
||||
return &evt
|
||||
}
|
||||
|
||||
func TestParseIncomingText(t *testing.T) {
|
||||
evt := mustEventFromJSON(t, `{
|
||||
"event": {
|
||||
"message": {
|
||||
"message_id": "msg_text_1",
|
||||
"chat_id": "chat_1",
|
||||
"message_type": "text",
|
||||
"content": "{\"text\":\"你好\"}"
|
||||
},
|
||||
"sender": {
|
||||
"sender_type": "user",
|
||||
"sender_id": {"open_id": "u_open_1"}
|
||||
}
|
||||
}
|
||||
}`)
|
||||
|
||||
in, ok := parseIncoming(evt)
|
||||
if !ok {
|
||||
t.Fatal("expected text message parse success")
|
||||
}
|
||||
if in.MsgType != "text" {
|
||||
t.Fatalf("expected msg type text, got %s", in.MsgType)
|
||||
}
|
||||
if in.Text != "你好" {
|
||||
t.Fatalf("unexpected text: %q", in.Text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseIncomingFile(t *testing.T) {
|
||||
evt := mustEventFromJSON(t, `{
|
||||
"event": {
|
||||
"message": {
|
||||
"message_id": "msg_file_1",
|
||||
"chat_id": "chat_1",
|
||||
"message_type": "file",
|
||||
"content": "{\"file_key\":\"file_key_123\",\"file_name\":\"report.pdf\"}"
|
||||
},
|
||||
"sender": {
|
||||
"sender_type": "user",
|
||||
"sender_id": {"user_id": "u_id_1"}
|
||||
}
|
||||
}
|
||||
}`)
|
||||
|
||||
in, ok := parseIncoming(evt)
|
||||
if !ok {
|
||||
t.Fatal("expected file message parse success")
|
||||
}
|
||||
if in.MsgType != "file" {
|
||||
t.Fatalf("expected msg type file, got %s", in.MsgType)
|
||||
}
|
||||
if in.FileName != "report.pdf" || in.FileKey != "file_key_123" {
|
||||
t.Fatalf("unexpected file meta: name=%q key=%q", in.FileName, in.FileKey)
|
||||
}
|
||||
if !strings.Contains(in.Text, "飞书文件消息") {
|
||||
t.Fatalf("expected synthesized text mentions file message, got: %q", in.Text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseIncomingUnsupportedType(t *testing.T) {
|
||||
evt := mustEventFromJSON(t, `{
|
||||
"event": {
|
||||
"message": {
|
||||
"message_id": "msg_image_1",
|
||||
"chat_id": "chat_1",
|
||||
"message_type": "image",
|
||||
"content": "{\"image_key\":\"img_1\"}"
|
||||
},
|
||||
"sender": {
|
||||
"sender_type": "user",
|
||||
"sender_id": {"open_id": "u_open_1"}
|
||||
}
|
||||
}
|
||||
}`)
|
||||
|
||||
_, ok := parseIncoming(evt)
|
||||
if ok {
|
||||
t.Fatal("expected unsupported message type rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectMimeByName(t *testing.T) {
|
||||
if got := detectMimeByName("report.pdf"); !strings.Contains(got, "pdf") {
|
||||
t.Fatalf("expected pdf mime, got: %s", got)
|
||||
}
|
||||
if got := detectMimeByName("unknown.custom"); got != "application/octet-stream" {
|
||||
t.Fatalf("expected octet-stream fallback, got: %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSaveIncomingFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path, err := saveIncomingFile(dir, "report.pdf", []byte("hello"))
|
||||
if err != nil {
|
||||
t.Fatalf("saveIncomingFile error: %v", err)
|
||||
}
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("read saved file failed: %v", err)
|
||||
}
|
||||
if string(b) != "hello" {
|
||||
t.Fatalf("unexpected saved content: %q", string(b))
|
||||
}
|
||||
if filepath.Ext(path) != ".pdf" {
|
||||
t.Fatalf("expected .pdf extension, got: %s", path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeFileName(t *testing.T) {
|
||||
got := sanitizeFileName("../bad path/测 试?.pdf")
|
||||
if strings.Contains(got, "/") || strings.Contains(got, "\\") {
|
||||
t.Fatalf("expected sanitized basename only, got: %q", got)
|
||||
}
|
||||
if !strings.HasSuffix(got, ".pdf") {
|
||||
t.Fatalf("expected .pdf suffix, got: %q", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user