Files
blumia 2fb4a8ea79 feat: context usage ring and tool image previews
- the composer shows a context ring and percentage from the last
  token-bearing assistant message, toned at 75% and 90%; tapping opens a
  sheet with the model, used tokens, limits, output reserve and turn cost
- images a tool call names (read attachments as data URLs, input paths,
  and image paths in output text) get a link under the tool row; tapping
  previews it from the data URL or /api/fs/raw, allowing paths outside
  the workspace
- directory-scoped requests now send x-opencode-directory-encoding: uri,
  which scoped routes such as /api/fs/raw need to decode the header
- ocprobe gains a raw file command for checking previews

Bump version to 0.2.2 (build 16).
2026-11-18 02:15:11 +08:00

103 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")
}
}