Files
BBQ d3bf6bc90a fix(channel,attachment): channel quality refactor & attachment pipeline fixes (#349)
* feat(channel): add DingTalk channel adapter

- Add DingTalk channel adapter (`internal/channel/adapters/dingtalk/`) using dingtalk-stream-sdk-go, supporting inbound message receiving and outbound text/markdown reply
- Register DingTalk adapter in cmd/agent and cmd/memoh
- Add go.mod dependency: github.com/memohai/dingtalk-stream-sdk-go
- Add Dingtalk and Wecom SVG icons and Vue components to @memohai/icon
- Refactor existing icon components to remove redundant inline wrappers
- Add `channelTypeDisplayName` util for consistent channel label resolution
- Add DingTalk/WeCom i18n entries (en/zh) for types and typesShort
- Extend channel-icon, bot-channels, channel-settings-panel to support dingtalk/wecom
- Use channelTypeDisplayName in profile page to replace ad-hoc i18n lookup

* fix(channel,attachment): channel quality refactor & attachment pipeline fixes

Channel module:
- Fix RemoveAdapter not cleaning connectionMeta (stale status leak)
- Fix preparedAttachmentTypeFromMime misclassifying image/gif
- Fix sleepWithContext time.After goroutine/timer leak
- Export IsDataURL/IsHTTPURL/IsDataPath, dedup across packages
- Cache OutboundPolicy in managerOutboundStream to avoid repeated lookups
- Split OutboundAttachmentStore: extract ContainerAttachmentIngester interface
- Add ManagerOption funcs (WithInboundQueueSize, WithInboundWorkers, WithRefreshInterval)
- Add thread-safety docs on OutboundStream / managerOutboundStream
- Add debug logs on successful send/edit paths
- Expand outbound_prepare_test.go with 21 new cases
- Convert no-receiver adapter helpers to package-level funcs; drop unused params

DingTalk adapter:
- Implement AttachmentResolver: download inbound media via /v1.0/robot/messageFiles/download
- Fix pure-image inbound messages failing due to missing resolver

Attachment pipeline:
- Fix images invisible to LLM in pipeline (DCP) path: inject InlineImages into
  last user message when cfg.Query is empty
- Fix public_url fallback: skip direct URL-to-LLM when ContentHash is set,
  always prefer inlined persisted asset
- Inject path: carry ImageParts through agent.InjectMessage; inline persisted
  attachments in resolver inject goroutine so mid-stream images reach the model
- Fix ResolveMime for images: prefer content-sniffed MIME over platform-declared
  MIME (fixes Feishu sending image/png header for actual JPEG content → API 400)
2026-04-09 14:36:11 +08:00

182 lines
4.8 KiB
Go

package attachment
import (
"io"
"strings"
"testing"
"github.com/memohai/memoh/internal/media"
)
func TestMapMediaType(t *testing.T) {
cases := []struct {
name string
in string
want media.MediaType
}{
{name: "image", in: "image", want: media.MediaTypeImage},
{name: "gif", in: "gif", want: media.MediaTypeImage},
{name: "audio", in: "audio", want: media.MediaTypeAudio},
{name: "voice", in: "voice", want: media.MediaTypeAudio},
{name: "video", in: "video", want: media.MediaTypeVideo},
{name: "default", in: "file", want: media.MediaTypeFile},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := MapMediaType(tc.in)
if got != tc.want {
t.Fatalf("MapMediaType(%q) = %q, want %q", tc.in, got, tc.want)
}
})
}
}
func TestNormalizeBase64DataURL(t *testing.T) {
got := NormalizeBase64DataURL("AAAA", "image/png")
if got != "data:image/png;base64,AAAA" {
t.Fatalf("unexpected normalized value: %q", got)
}
already := "data:image/jpeg;base64,BBBB"
if NormalizeBase64DataURL(already, "image/png") != already {
t.Fatalf("expected data url to pass through")
}
}
func TestNormalizeMime(t *testing.T) {
got := NormalizeMime("IMAGE/JPEG; charset=utf-8")
if got != "image/jpeg" {
t.Fatalf("NormalizeMime unexpected result: %q", got)
}
if got := NormalizeMime("file"); got != "" {
t.Fatalf("NormalizeMime should drop invalid mime token, got %q", got)
}
}
func TestMimeFromDataURL(t *testing.T) {
got := MimeFromDataURL("data:image/png;base64,AAAA")
if got != "image/png" {
t.Fatalf("MimeFromDataURL unexpected result: %q", got)
}
if MimeFromDataURL("https://example.com/demo.png") != "" {
t.Fatalf("MimeFromDataURL should return empty for non-data-url")
}
}
func TestResolveMime(t *testing.T) {
cases := []struct {
name string
mediaType media.MediaType
source string
sniffed string
want string
}{
{
name: "image: sniffed preferred over generic source",
mediaType: media.MediaTypeImage,
source: "application/octet-stream",
sniffed: "image/jpeg",
want: "image/jpeg",
},
{
name: "image: sniffed preferred over wrong platform mime",
mediaType: media.MediaTypeImage,
source: "image/png",
sniffed: "image/jpeg",
want: "image/jpeg",
},
{
name: "image: fallback to source when sniff fails",
mediaType: media.MediaTypeImage,
source: "image/png",
sniffed: "",
want: "image/png",
},
{
name: "image: both empty returns octet-stream",
mediaType: media.MediaTypeImage,
source: "",
sniffed: "",
want: "application/octet-stream",
},
{
name: "file: source preferred over sniffed",
mediaType: media.MediaTypeFile,
source: "application/pdf",
sniffed: "application/octet-stream",
want: "application/pdf",
},
{
name: "file: sniffed used when source is generic",
mediaType: media.MediaTypeFile,
source: "application/octet-stream",
sniffed: "application/pdf",
want: "application/pdf",
},
{
name: "file: sniffed used when source token is invalid",
mediaType: media.MediaTypeFile,
source: "file",
sniffed: "text/plain",
want: "text/plain",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := ResolveMime(tc.mediaType, tc.source, tc.sniffed)
if got != tc.want {
t.Fatalf("ResolveMime(%v, %q, %q) = %q, want %q",
tc.mediaType, tc.source, tc.sniffed, got, tc.want)
}
})
}
}
func TestPrepareReaderAndMime(t *testing.T) {
reader, mime, err := PrepareReaderAndMime(strings.NewReader("\x89PNG\r\n\x1a\npayload"), media.MediaTypeImage, "")
if err != nil {
t.Fatalf("PrepareReaderAndMime returned error: %v", err)
}
if mime != "image/png" {
t.Fatalf("PrepareReaderAndMime mime = %q, want image/png", mime)
}
raw, err := io.ReadAll(reader)
if err != nil {
t.Fatalf("read prepared reader failed: %v", err)
}
if !strings.HasPrefix(string(raw), "\x89PNG\r\n\x1a\n") {
t.Fatalf("prepared reader lost prefix bytes")
}
}
func TestDecodeBase64(t *testing.T) {
reader, err := DecodeBase64("aGVsbG8=", 1024)
if err != nil {
t.Fatalf("DecodeBase64 returned error: %v", err)
}
raw, err := io.ReadAll(reader)
if err != nil {
t.Fatalf("read decoded bytes failed: %v", err)
}
if string(raw) != "hello" {
t.Fatalf("decoded content = %q, want hello", string(raw))
}
reader, err = DecodeBase64("data:text/plain;base64,aGVsbG8=", 1024)
if err != nil {
t.Fatalf("DecodeBase64 with data URL returned error: %v", err)
}
raw, err = io.ReadAll(reader)
if err != nil {
t.Fatalf("read decoded data URL bytes failed: %v", err)
}
if string(raw) != "hello" {
t.Fatalf("decoded data URL content = %q, want hello", string(raw))
}
_, err = DecodeBase64("", 1024)
if err == nil {
t.Fatalf("expected empty base64 to return error")
}
}