102 lines
2.6 KiB
Go
102 lines
2.6 KiB
Go
package appui
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/openchamber/openchamber-native/internal/ocapi"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestToolImagesFromOutput(t *testing.T) {
|
||
|
|
ch := &chat{dir: "/work"}
|
||
|
|
part := ocapi.ContentPart{
|
||
|
|
Type: "tool",
|
||
|
|
State: &ocapi.ToolState{
|
||
|
|
Input: map[string]any{"path": "shots/a.png"},
|
||
|
|
Content: []ocapi.ContentPart{
|
||
|
|
{Type: "text", Text: "wrote /work/shots/a.png and /work/notes.txt"},
|
||
|
|
{Type: "file", MIME: "image/png", URL: "data:image/png;base64,AAAA", Filename: "b.png"},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
refs := ch.toolImages(part)
|
||
|
|
if len(refs) != 2 {
|
||
|
|
t.Fatalf("refs = %+v", refs)
|
||
|
|
}
|
||
|
|
if refs[0].Source != "/work/shots/a.png" || refs[0].Label != "a.png" {
|
||
|
|
t.Errorf("first = %+v", refs[0])
|
||
|
|
}
|
||
|
|
if refs[1].Source != "data:image/png;base64,AAAA" || refs[1].Label != "b.png" {
|
||
|
|
t.Errorf("second = %+v", refs[1])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestToolImagesPrefersReadAttachment(t *testing.T) {
|
||
|
|
ch := &chat{dir: "/work"}
|
||
|
|
part := ocapi.ContentPart{
|
||
|
|
Type: "tool",
|
||
|
|
State: &ocapi.ToolState{
|
||
|
|
Input: map[string]any{"filePath": "/tmp/upload.png"},
|
||
|
|
Attachments: []ocapi.ContentPart{{
|
||
|
|
Type: "file", MIME: "image/png", Filename: "upload.png",
|
||
|
|
URL: "data:image/png;base64,AAAA",
|
||
|
|
}},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
refs := ch.toolImages(part)
|
||
|
|
if len(refs) != 1 || refs[0].Source != "data:image/png;base64,AAAA" {
|
||
|
|
t.Errorf("refs = %+v", refs)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestToolImagesUsesURIFileContent(t *testing.T) {
|
||
|
|
ch := &chat{dir: "/work"}
|
||
|
|
part := ocapi.ContentPart{
|
||
|
|
Type: "tool",
|
||
|
|
State: &ocapi.ToolState{
|
||
|
|
Content: []ocapi.ContentPart{{
|
||
|
|
Type: "file", MIME: "image/jpeg", Name: "shot.jpg",
|
||
|
|
URI: "file:///work/shot.jpg",
|
||
|
|
}},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
refs := ch.toolImages(part)
|
||
|
|
if len(refs) != 1 || refs[0].Source != "/work/shot.jpg" || refs[0].Label != "shot.jpg" {
|
||
|
|
t.Errorf("refs = %+v", refs)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestToolImagesSkipsNonImages(t *testing.T) {
|
||
|
|
ch := &chat{dir: "/work"}
|
||
|
|
part := ocapi.ContentPart{
|
||
|
|
Type: "tool",
|
||
|
|
State: &ocapi.ToolState{
|
||
|
|
Input: map[string]any{"path": "/work/main.go"},
|
||
|
|
Content: []ocapi.ContentPart{
|
||
|
|
{Type: "text", Text: "read /work/main.go and /work/logo.webp"},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
refs := ch.toolImages(part)
|
||
|
|
if len(refs) != 1 || refs[0].Source != "/work/logo.webp" {
|
||
|
|
t.Errorf("refs = %+v", refs)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestLoadPreviewDataURL(t *testing.T) {
|
||
|
|
ch := &chat{}
|
||
|
|
data, err := ch.loadPreview("data:image/png;base64,iVBORw0KGgo=")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if !bytes.HasPrefix(data, []byte("\x89PNG\r\n\x1a\n")) {
|
||
|
|
t.Errorf("data = %x", data)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestLoadPreviewRejectsPlainDataURL(t *testing.T) {
|
||
|
|
ch := &chat{}
|
||
|
|
if _, err := ch.loadPreview("data:text/plain,hello"); err == nil {
|
||
|
|
t.Fatal("expected an error for a non-base64 data URL")
|
||
|
|
}
|
||
|
|
}
|