This commit is contained in:
lxowalle 2026-05-15 15:39:31 +08:00
parent 475719012d
commit 3052b540c0

View file

@ -344,7 +344,9 @@ func TestPicoChannel_HandleMessageSend_AllowsMediaOnly(t *testing.T) {
} }
} }
func TestPicoClientChannel_HandleServerMessage_ForwardsMedia(t *testing.T) { func newTestPicoClientChannel(t *testing.T) (*PicoClientChannel, *bus.MessageBus) {
t.Helper()
mb := bus.NewMessageBus() mb := bus.NewMessageBus()
bc := &config.Channel{Type: config.ChannelPicoClient, Enabled: true} bc := &config.Channel{Type: config.ChannelPicoClient, Enabled: true}
ch, err := NewPicoClientChannel(bc, &config.PicoClientSettings{ ch, err := NewPicoClientChannel(bc, &config.PicoClientSettings{
@ -353,8 +355,40 @@ func TestPicoClientChannel_HandleServerMessage_ForwardsMedia(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("NewPicoClientChannel() error = %v", err) t.Fatalf("NewPicoClientChannel() error = %v", err)
} }
ch.ctx = context.Background() ch.ctx = context.Background()
return ch, mb
}
func assertInboundMessage(
t *testing.T,
mb *bus.MessageBus,
wantContent string,
wantMedia []string,
timeoutMessage string,
) {
t.Helper()
select {
case msg := <-mb.InboundChan():
if msg.Content != wantContent {
t.Fatalf("msg.Content = %q, want %s", msg.Content, wantContent)
}
if len(msg.Media) != len(wantMedia) {
t.Fatalf("msg.Media = %#v, want %#v", msg.Media, wantMedia)
}
for i := range wantMedia {
if msg.Media[i] != wantMedia[i] {
t.Fatalf("msg.Media = %#v, want %#v", msg.Media, wantMedia)
}
}
case <-time.After(time.Second):
t.Fatal(timeoutMessage)
}
}
func TestPicoClientChannel_HandleServerMessage_ForwardsMedia(t *testing.T) {
ch, mb := newTestPicoClientChannel(t)
pc := &picoConn{sessionID: "sess-media"} pc := &picoConn{sessionID: "sess-media"}
imageURL := "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+X2ioAAAAASUVORK5CYII=" imageURL := "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+X2ioAAAAASUVORK5CYII="
@ -371,30 +405,17 @@ func TestPicoClientChannel_HandleServerMessage_ForwardsMedia(t *testing.T) {
}, },
}) })
select { assertInboundMessage(
case msg := <-mb.InboundChan(): t,
if msg.Content != "describe this" { mb,
t.Fatalf("msg.Content = %q, want describe this", msg.Content) "describe this",
} []string{imageURL},
if len(msg.Media) != 1 || msg.Media[0] != imageURL { "timed out waiting for forwarded media message",
t.Fatalf("msg.Media = %#v, want forwarded image payload", msg.Media) )
}
case <-time.After(time.Second):
t.Fatal("timed out waiting for forwarded media message")
}
} }
func TestPicoClientChannel_HandleInbound_ForwardsMediaCreate(t *testing.T) { func TestPicoClientChannel_HandleInbound_ForwardsMediaCreate(t *testing.T) {
mb := bus.NewMessageBus() ch, mb := newTestPicoClientChannel(t)
bc := &config.Channel{Type: config.ChannelPicoClient, Enabled: true}
ch, err := NewPicoClientChannel(bc, &config.PicoClientSettings{
URL: "ws://localhost:8080/ws",
}, mb)
if err != nil {
t.Fatalf("NewPicoClientChannel() error = %v", err)
}
ch.ctx = context.Background()
pc := &picoConn{sessionID: "sess-media-create"} pc := &picoConn{sessionID: "sess-media-create"}
imageURL := "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+X2ioAAAAASUVORK5CYII=" imageURL := "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+X2ioAAAAASUVORK5CYII="
@ -411,17 +432,13 @@ func TestPicoClientChannel_HandleInbound_ForwardsMediaCreate(t *testing.T) {
}, },
}) })
select { assertInboundMessage(
case msg := <-mb.InboundChan(): t,
if msg.Content != "describe media.create" { mb,
t.Fatalf("msg.Content = %q, want describe media.create", msg.Content) "describe media.create",
} []string{imageURL},
if len(msg.Media) != 1 || msg.Media[0] != imageURL { "timed out waiting for media.create message",
t.Fatalf("msg.Media = %#v, want forwarded media.create image payload", msg.Media) )
}
case <-time.After(time.Second):
t.Fatal("timed out waiting for media.create message")
}
} }
func TestPicoClientChannel_HandleServerMessage_ForwardsTextWithDownloadAttachment(t *testing.T) { func TestPicoClientChannel_HandleServerMessage_ForwardsTextWithDownloadAttachment(t *testing.T) {