sub2api/backend/internal/pkg/windsurf/chat_media_wire_test.go

69 lines
2.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package windsurf
import (
"bytes"
"encoding/hex"
"testing"
)
// 对比官方规划里给出的 known-good hex vector
// base64Data="QQ==", mimeType="image/png", caption="a"
// 内部 body: 0a 04 51 51 3d 3d 12 09 69 6d 61 67 65 2f 70 6e 67 1a 01 61
func TestEncodeCodeiumImage_KnownVector(t *testing.T) {
img := CascadeImage{
Base64Data: "QQ==",
MimeType: "image/png",
Caption: "a",
}
got := encodeCodeiumImage(img)
want, _ := hex.DecodeString("0a045151" + "3d3d" + "120969" + "6d6167652f706e67" + "1a0161")
if !bytes.Equal(got, want) {
t.Errorf("encoded mismatch:\n got=%x\nwant=%x", got, want)
}
}
func TestEncodeCodeiumImage_OmitsCaptionWhenEmpty(t *testing.T) {
img := CascadeImage{
Base64Data: "QQ==",
MimeType: "image/png",
}
got := encodeCodeiumImage(img)
// 不含 field 3 tag (0x1a)
if bytes.Contains(got, []byte{0x1a}) {
t.Errorf("should not contain field 3 tag when caption empty: %x", got)
}
}
func TestAppendSendUserCascadeImages_WrapsField6(t *testing.T) {
imgs := []CascadeImage{
{Base64Data: "QQ==", MimeType: "image/png", Caption: "a"},
}
body := appendSendUserCascadeImages(nil, imgs)
// 外层field 6 tag (0x32) + varint 长度 + 内层 body
// 内层 body 长度 = 6 + 11 + 3 = 20 (0x14)
want, _ := hex.DecodeString("3214" + "0a045151" + "3d3d" + "120969" + "6d6167652f706e67" + "1a0161")
if !bytes.Equal(body, want) {
t.Errorf("wrapped field 6 segment mismatch:\n got=%x\nwant=%x", body, want)
}
}
func TestAppendSendUserCascadeImages_MultipleImagesIndependentSegments(t *testing.T) {
imgs := []CascadeImage{
{Base64Data: "QQ==", MimeType: "image/png"},
{Base64Data: "Qg==", MimeType: "image/jpeg"},
}
body := appendSendUserCascadeImages(nil, imgs)
// 应该有两次 field 6 tag (0x32)
count := bytes.Count(body, []byte{0x32})
if count != 2 {
t.Errorf("expected 2 field-6 segments, got %d in %x", count, body)
}
}
func TestAppendSendUserCascadeImages_NilInput(t *testing.T) {
body := appendSendUserCascadeImages(nil, nil)
if len(body) != 0 {
t.Errorf("nil input should produce empty bytes, got %x", body)
}
}