package main import ( "bufio" "bytes" "encoding/json" "io" "net/http" "net/http/httptest" "strings" "testing" "github.com/egoist/mygo/ui" mujica "mujicaui" ) // mirrorRig serves a one-field view through the web mirror. type mirrorRig struct { t *testing.T m *mirror srv *httptest.Server text string } func newMirrorRig(t *testing.T) *mirrorRig { r := &mirrorRig{t: t} var h http.Handler r.m, h = newMirror(func(c *ui.Context) { mujica.Use(c, mujica.Settings{}) ui.Column(c).Padding(40).Children(func() { mujica.TextInput(c, &r.text, mujica.TextInputOptions{Label: "Ledger"}) }) }) r.srv = httptest.NewServer(h) t.Cleanup(r.srv.Close) return r } func (r *mirrorRig) post(in input) int { r.t.Helper() body, err := json.Marshal(in) if err != nil { r.t.Fatal(err) } res, err := http.Post(r.srv.URL+"/input", "application/json", bytes.NewReader(body)) if err != nil { r.t.Fatal(err) } res.Body.Close() return res.StatusCode } func (r *mirrorRig) get(path string) []byte { r.t.Helper() res, err := http.Get(r.srv.URL + path) if err != nil { r.t.Fatal(err) } defer res.Body.Close() if res.StatusCode != http.StatusOK { r.t.Fatalf("GET %s: %d", path, res.StatusCode) } b, err := io.ReadAll(res.Body) if err != nil { r.t.Fatal(err) } return b } func (r *mirrorRig) value() string { r.m.mu.Lock() defer r.m.mu.Unlock() return r.text } func TestMirrorServesPageAndFirstNotice(t *testing.T) { r := newMirrorRig(t) if page := r.get("/"); !bytes.Contains(page, []byte("