Files
openchamber-android-native/third_party/mygo/plugins/terminal/images_test.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

92 lines
3.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 terminal
import (
"bytes"
"encoding/base64"
"fmt"
"image"
imagecolor "image/color"
"image/draw"
"image/png"
"testing"
"github.com/egoist/mygo/ui"
)
// kittyImage returns the Kitty graphics sequence that transmits and
// places a w×h image of one color, as RGBA, with more keys.
func kittyImage(id, w, h int, c ui.Color, keys string) string {
px := bytes.Repeat([]byte{c.R, c.G, c.B, 0xff}, w*h)
return fmt.Sprintf("\x1b_Ga=T,f=32,s=%d,v=%d,i=%d,q=2%s;%s\x1b\\", w, h, id, keys, base64.StdEncoding.EncodeToString(px))
}
// Images programs place (Kitty graphics) show where they placed them,
// over the text unless placed under it, and go when deleted: grok shows a
// pasted image's preview so.
func TestImages(t *testing.T) {
loadLib(t)
term, err := New(Options{Conn: newPipe(), Theme: LightTheme()})
if err != nil {
t.Fatal(err)
}
defer term.Close()
tt := ui.NewTester(func(c *ui.Context) { View(c, term).Fill() }, 400, 200)
tt.Frame()
at := func(col, row int) ui.Color {
x, y := cellCenter(term, col, row)
c := tt.Image().RGBAAt(int(x*term.v.scale), int(y*term.v.scale))
return ui.RGB(c.R, c.G, c.B)
}
red, blue := ui.RGB(255, 0, 0), ui.RGB(0, 0, 255)
bg := LightTheme().Background
// Two cells by two of red at row 1, column 2; the cursor stays.
term.Feed([]byte("\x1b[2;3H" + kittyImage(7, 2*term.v.cellW, 2*term.v.cellH, red, ",C=1")))
tt.Frame()
for _, c := range []struct {
col, row int
want ui.Color
}{{2, 1, red}, {3, 2, red}, {4, 1, bg}, {2, 3, bg}} {
if got := at(c.col, c.row); got != c.want {
t.Errorf("cell %d,%d is %v, want %v", c.col, c.row, got, c.want)
}
}
// Under the cells' backgrounds, a cell with its own hides it.
term.Feed([]byte("\x1b[5;1H" + kittyImage(8, term.v.cellW, term.v.cellH, blue, ",z=-1073741825,C=1") + "\x1b[42m \x1b[0m"))
tt.Frame()
if got := at(0, 4); got == blue {
t.Errorf("an image under the backgrounds shows over a cell's own")
}
// Deleted, it goes.
term.Feed([]byte("\x1b_Ga=d,d=I,i=7,q=2\x1b\\"))
tt.Frame()
if got := at(2, 1); got != bg {
t.Errorf("a deleted image's cell is %v, want %v", got, bg)
}
}
// Images sent as PNG (f=100), as grok sends a pasted image's preview,
// decode and show.
func TestImagePNG(t *testing.T) {
loadLib(t)
term, err := New(Options{Conn: newPipe(), Theme: LightTheme()})
if err != nil {
t.Fatal(err)
}
defer term.Close()
tt := ui.NewTester(func(c *ui.Context) { View(c, term).Fill() }, 400, 200)
tt.Frame()
src := image.NewNRGBA(image.Rect(0, 0, 3*term.v.cellW, 2*term.v.cellH))
draw.Draw(src, src.Bounds(), image.NewUniform(imagecolor.NRGBA{G: 255, A: 255}), image.Point{}, draw.Src)
var b bytes.Buffer
png.Encode(&b, src)
data := base64.StdEncoding.EncodeToString(b.Bytes())
// In chunks, as programs send big ones.
term.Feed([]byte("\x1b[2;2H\x1b_Ga=T,f=100,q=2,m=1;" + data[:40] + "\x1b\\\x1b_Gm=0;" + data[40:] + "\x1b\\"))
tt.Frame()
x, y := cellCenter(term, 2, 2)
c := tt.Image().RGBAAt(int(x*term.v.scale), int(y*term.v.scale))
if got := ui.RGB(c.R, c.G, c.B); got != ui.RGB(0, 255, 0) {
t.Errorf("the image's cell is %v, want green", got)
}
}