Files
openchamber-android-native/third_party/mygo/plugins/terminal/internal/pty/pty_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

111 lines
2.6 KiB
Go

package pty
import (
"bytes"
"io"
"os"
"runtime"
"strings"
"testing"
"time"
"github.com/egoist/mygo/internal/wait"
)
// readAll reads the output of p until the process exits and the output
// ends.
func readAll(t *testing.T, p *PTY) string {
t.Helper()
var out bytes.Buffer
done := make(chan struct{})
go func() {
io.Copy(&out, p)
close(done)
}()
select {
case <-p.Exited():
case <-time.After(wait.Guard):
t.Fatal("the process did not exit")
}
// What the process wrote before it exited is still to read.
time.AfterFunc(200*time.Millisecond, func() { p.Close() })
<-done
return out.String()
}
func TestStart(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("uses sh")
}
p, err := Start(Options{Path: "/bin/sh", Args: []string{"sh", "-c", "echo hello; stty size; [ -t 0 ] && echo tty; exit 3"}, Cols: 100, Rows: 30, Env: []string{"TERM=dumb"}})
if err != nil {
t.Fatal(err)
}
out := readAll(t, p)
for _, want := range []string{"hello\r\n", "30 100\r\n", "tty\r\n"} {
if !strings.Contains(out, want) {
t.Errorf("output %q lacks %q", out, want)
}
}
if code, err := p.Wait(); code != 3 || err != nil {
t.Errorf("exit = %d, %v", code, err)
}
}
func TestResizeAndInput(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("uses sh")
}
p, err := Start(Options{Path: "/bin/sh", Args: []string{"sh", "-c", "read line; echo got $line; stty size"}, Env: []string{"TERM=dumb"}})
if err != nil {
t.Fatal(err)
}
if err := p.Resize(120, 40, 0, 0); err != nil {
t.Fatal(err)
}
if _, err := p.Write([]byte("typed\r")); err != nil {
t.Fatal(err)
}
out := readAll(t, p)
for _, want := range []string{"got typed\r\n", "40 120\r\n"} {
if !strings.Contains(out, want) {
t.Errorf("output %q lacks %q", out, want)
}
}
}
func TestClose(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("uses sh")
}
p, err := Start(Options{Path: "/bin/sh", Args: []string{"sh", "-c", "sleep 30"}})
if err != nil {
t.Fatal(err)
}
// Closing the terminal hangs up the process.
p.Close()
select {
case <-p.Exited():
case <-time.After(5 * time.Second):
p.Kill()
t.Fatal("the process outlived its terminal")
}
}
func TestConPTY(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("ConPTY")
}
cmd := os.Getenv("COMSPEC")
p, err := Start(Options{Path: cmd, Args: []string{"cmd", "/c", "echo hello& exit /b 3"}, Cols: 100, Rows: 30})
if err != nil {
t.Fatal(err)
}
if out := readAll(t, p); !strings.Contains(out, "hello") {
t.Errorf("output %q lacks hello", out)
}
if code, err := p.Wait(); code != 3 || err != nil {
t.Errorf("exit = %d, %v", code, err)
}
}