Files
openchamber-android-native/third_party/mygo/downloads_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

102 lines
3.1 KiB
Go

//go:build !js
package mygo
import (
"net/http"
"os"
"path/filepath"
"testing"
"time"
"github.com/egoist/mygo/internal/wait"
)
func TestDownloadEvents(t *testing.T) {
dir := t.TempDir()
App.SetPath(PathDownloads, dir)
defer App.SetPath(PathDownloads, "")
w, fw := testWindow(t, WindowOptions{})
// The default path is the suggested name in Downloads, numbered.
os.WriteFile(filepath.Join(dir, "report.csv"), nil, 0o644)
start := func(url, name string) string {
return onMainValue(func() string { return fw.H.DownloadStarted(url, name) })
}
if got := start("https://x/r", "report.csv"); got != filepath.Join(dir, "report (2).csv") {
t.Errorf("path = %q", got)
}
if got := start("https://x/r", "../../etc/passwd"); got != filepath.Join(dir, "passwd") {
t.Errorf("unsafe name: path = %q", got)
}
off := w.Page().OnWillDownload(func(e *DownloadEvent) { e.PreventDefault() })
if got := start("https://x/r", "a.txt"); got != "" {
t.Errorf("a prevented download got path %q", got)
}
off()
// Custom schemes are downloaded from their handler.
if err := Protocol.HandleFunc("dltest", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Disposition", `attachment; filename="data.json"`)
w.Write([]byte(`{"ok":true}`))
}); err != nil {
t.Fatal(err)
}
defer Protocol.Unhandle("dltest")
done := make(chan *Download, 1)
defer w.Page().OnDownloadDone(func(d *Download) { done <- d })()
onMain(func() { fw.H.SchemeDownload("dltest://localhost/export") })
select {
case d := <-done:
if d.Err != nil || filepath.Base(d.Path) != "data.json" {
t.Fatalf("download = %+v", d)
}
if b, _ := os.ReadFile(d.Path); string(b) != `{"ok":true}` {
t.Errorf("saved %q", b)
}
case <-time.After(wait.Guard):
t.Fatal("the scheme download did not finish")
}
}
// A handler that panics while a download is served from it fails the
// download, before it started or with part of the file written, which is
// removed. The app goes on.
func TestDownloadSchemePanic(t *testing.T) {
dir := t.TempDir()
App.SetPath(PathDownloads, dir)
defer App.SetPath(PathDownloads, "")
w, fw := testWindow(t, WindowOptions{})
if err := Protocol.HandleFunc("dlpanic", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/part" {
w.Header().Set("Content-Disposition", `attachment; filename="part.bin"`)
w.Write([]byte("half"))
}
panic("boom")
}); err != nil {
t.Fatal(err)
}
defer Protocol.Unhandle("dlpanic")
done := make(chan *Download, 1)
defer w.Page().OnDownloadDone(func(d *Download) { done <- d })()
for _, tt := range []struct{ url, path string }{
{"dlpanic://localhost/none", ""},
{"dlpanic://localhost/part", filepath.Join(dir, "part.bin")},
} {
onMain(func() { fw.H.SchemeDownload(tt.url) })
select {
case d := <-done:
if d.Err == nil || d.Path != tt.path {
t.Errorf("%s: download = %+v, want an error and the path %q", tt.url, d, tt.path)
}
case <-time.After(wait.Guard):
t.Fatalf("%s: the download did not finish", tt.url)
}
if tt.path != "" {
if _, err := os.Stat(tt.path); !os.IsNotExist(err) {
t.Errorf("%s: the partial file was left behind (%v)", tt.url, err)
}
}
}
}