Files
ai-agent/internal/pkg/utils/content_chunk_test.go
T
mlogclub 5d7c10aeab refactor: rename agent widget references to AI agent for consistency
- Updated runtime configuration to use __CS_AI_AGENT_WIDGET_CONFIG__ instead of __CS_AGENT_WIDGET_CONFIG__.
- Changed message types in support host bridge from "cs-agent" to "cs-ai-agent".
- Minified SDK script updated to reflect new AI agent naming conventions.
- Adjusted scrollbar styles in main.scss to use .cs-ai-agent-scrollbar instead of .cs-agent-scrollbar.
2026-05-30 21:19:36 +08:00

86 lines
2.3 KiB
Go

package utils
import (
"cs-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: "<p>你好,微信用户</p>",
want: []ContentChunk{
{Type: ContentChunkTypeText, Content: "你好,微信用户"},
},
},
{
name: "single image",
content: `<p><img src="https://example.com/a.png" alt="a"></p>`,
want: []ContentChunk{
{Type: ContentChunkTypeImage, Content: "https://example.com/a.png"},
},
},
{
name: "mixed text and images keep order",
content: `<p>第一段</p><p><img src="https://example.com/1.png"></p><p>第二段<img src="https://example.com/2.png">第三段</p>`,
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: "<div> 第一行 <br><br> 第二行 </div>",
want: []ContentChunk{
{Type: ContentChunkTypeText, Content: "第一行\n\n第二行"},
},
},
{
name: "ignore image without src",
content: `<p>前文</p><img alt="missing-src"><p>后文</p>`,
want: []ContentChunk{
{Type: ContentChunkTypeText, Content: "前文"},
{Type: ContentChunkTypeText, Content: "后文"},
},
},
{
name: "image with asset metadata only",
content: `<p><img data-asset-id="asset_1" data-provider="local" data-storage-key="images/a.png" alt="a"></p>`,
want: []ContentChunk{
{
Type: ContentChunkTypeImage,
AssetID: "asset_1",
Provider: enums.AssetProviderLocal,
StorageKey: "images/a.png",
},
},
},
{
name: "empty html returns empty chunks",
content: "<p><br></p>",
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)
}
})
}
}