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

63 lines
1.5 KiB
Go

package ocapi
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestRawFile(t *testing.T) {
var gotPath, gotDir string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/fs/raw" {
t.Errorf("path = %q", r.URL.Path)
}
if got := r.Header.Get("x-opencode-directory-encoding"); got != "uri" {
t.Errorf("directory encoding = %q", got)
}
gotPath = r.URL.Query().Get("path")
if got := r.URL.Query().Get("allowOutsideWorkspace"); got != "true" {
t.Errorf("allowOutsideWorkspace = %q", got)
}
gotDir, _ = url.PathUnescape(r.Header.Get("x-opencode-directory"))
w.Header().Set("Content-Type", "image/png")
fmt.Fprint(w, "PNGDATA")
}))
defer srv.Close()
c, err := New(srv.URL, Options{})
if err != nil {
t.Fatal(err)
}
data, mime, err := c.RawFile(context.Background(), "/work/a.png", "/work")
if err != nil {
t.Fatal(err)
}
if string(data) != "PNGDATA" {
t.Errorf("data = %q", data)
}
if mime != "image/png" {
t.Errorf("mime = %q", mime)
}
if gotPath != "/work/a.png" {
t.Errorf("query path = %q", gotPath)
}
if gotDir != "/work" {
t.Errorf("directory = %q", gotDir)
}
}
func TestRawFileError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "nope", http.StatusNotFound)
}))
defer srv.Close()
c, _ := New(srv.URL, Options{})
if _, _, err := c.RawFile(context.Background(), "/work/missing.png", ""); err == nil {
t.Fatal("expected an error")
}
}