package utils
import (
"code.tczkiot.com/wlw/ai-agent/internal/pkg/enums"
"reflect"
"testing"
)
func TestSplitHTMLContentChunks(t *testing.T) {
tests := []struct {
name string
content string
want []ContentChunk
}{
{
name: "plain text paragraph",
content: "
你好,微信用户
",
want: []ContentChunk{
{Type: ContentChunkTypeText, Content: "你好,微信用户"},
},
},
{
name: "single image",
content: `
`,
want: []ContentChunk{
{Type: ContentChunkTypeImage, Content: "https://example.com/a.png"},
},
},
{
name: "mixed text and images keep order",
content: `第一段

第二段
第三段
`,
want: []ContentChunk{
{Type: ContentChunkTypeText, Content: "第一段"},
{Type: ContentChunkTypeImage, Content: "https://example.com/1.png"},
{Type: ContentChunkTypeText, Content: "第二段"},
{Type: ContentChunkTypeImage, Content: "https://example.com/2.png"},
{Type: ContentChunkTypeText, Content: "第三段"},
},
},
{
name: "normalize blank lines and spaces",
content: " 第一行
第二行
",
want: []ContentChunk{
{Type: ContentChunkTypeText, Content: "第一行\n\n第二行"},
},
},
{
name: "ignore image without src",
content: `前文
![missing-src]()
后文
`,
want: []ContentChunk{
{Type: ContentChunkTypeText, Content: "前文"},
{Type: ContentChunkTypeText, Content: "后文"},
},
},
{
name: "image with asset metadata only",
content: `![a]()
`,
want: []ContentChunk{
{
Type: ContentChunkTypeImage,
AssetID: "asset_1",
Provider: enums.AssetProviderLocal,
StorageKey: "images/a.png",
},
},
},
{
name: "empty html returns empty chunks",
content: "
",
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := SplitHTMLContentChunks(tt.content)
if err != nil {
t.Fatalf("SplitHTMLContentChunks() error = %v", err)
}
if !reflect.DeepEqual(got, tt.want) {
t.Fatalf("SplitHTMLContentChunks() = %#v, want %#v", got, tt.want)
}
})
}
}