package appui import ( "encoding/json" "fmt" "net/http" "net/http/httptest" "sync" "testing" "time" "github.com/egoist/asuka/ui" "github.com/egoist/asuka/uitest" ) // chatServer serves one session with a short transcript, and records the // prompt a test sends. type chatServer struct { *httptest.Server prompts chan map[string]any replies chan map[string]any events chan string ready chan struct{} readyOne sync.Once } func (cs *chatServer) push(ev string) { select { case cs.events <- ev: case <-time.After(2 * time.Second): panic("event stream is not consuming") } } func newChatServer(t *testing.T) *chatServer { t.Helper() cs := &chatServer{ prompts: make(chan map[string]any, 1), replies: make(chan map[string]any, 1), events: make(chan string, 8), ready: make(chan struct{}), } mux := http.NewServeMux() mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, `{"status":"ok","openchamberVersion":"2.7.0","isOpenCodeReady":true}`) }) mux.HandleFunc("/auth/session", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, `{"authenticated":true,"disabled":true}`) }) mux.HandleFunc("/api/session", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, `{"data":[{"id":"ses_1","title":"First session","time":{"created":1,"updated":2}, "location":{"directory":"/home/me/project"},"model":{"id":"m","providerID":"p"}}],"cursor":{}}`) }) mux.HandleFunc("/api/session/active", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, `{"data":{}}`) }) mux.HandleFunc("/api/permission/request", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, `{"data":[]}`) }) mux.HandleFunc("/api/session/ses_1/message", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, `{"data":[ {"id":"msg_1","type":"user","text":"What is this?","time":{"created":10}}, {"id":"msg_2","type":"assistant","time":{"created":11,"completed":12}, "content":[{"type":"text","text":"**Hello** from the agent"}]} ],"cursor":{}}`) }) mux.HandleFunc("/api/session/ses_1/prompt", func(w http.ResponseWriter, r *http.Request) { var body map[string]any _ = json.NewDecoder(r.Body).Decode(&body) select { case cs.prompts <- body: default: } cs.events <- `{"id":"evt_2","created":100,"type":"session.step.started","data":{"sessionID":"ses_1","assistantMessageID":"msg_stream"}}` cs.events <- `{"id":"evt_3","created":101,"type":"session.text.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_stream","ordinal":0,"delta":"streamed"}}` fmt.Fprint(w, `{"data":{}}`) }) mux.HandleFunc("/api/session/ses_1/permission/per_1/reply", func(w http.ResponseWriter, r *http.Request) { var body map[string]any _ = json.NewDecoder(r.Body).Decode(&body) select { case cs.replies <- body: default: } w.WriteHeader(http.StatusNoContent) }) mux.HandleFunc("/api/event", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") cs.readyOne.Do(func() { close(cs.ready) }) fmt.Fprint(w, "data: {\"id\":\"evt_1\",\"type\":\"server.connected\",\"data\":{}}\n\n") if f, ok := w.(http.Flusher); ok { f.Flush() } for { select { case ev := <-cs.events: fmt.Fprintf(w, "data: %s\n\n", ev) if f, ok := w.(http.Flusher); ok { f.Flush() } case <-r.Context().Done(): return } } }) cs.Server = httptest.NewServer(mux) t.Cleanup(cs.Close) return cs } func TestOpenSessionAndSend(t *testing.T) { srv := newChatServer(t) st := &memStore{} a := uitest.Mount(t, func() ui.Node { return rootWithStore(st) }, uitest.Platform(ui.PlatformAndroid)) a.Type("http://192.168.1.5:3000", srv.URL) a.Tap("连接") a.WaitFor("First session") select { case <-srv.ready: case <-time.After(5 * time.Second): t.Fatal("the event stream was never opened") } a.Tap("First session") a.WaitFor("Hello from the agent") a.Expect("What is this?") a.Type("发消息…", "ping") a.Tap("发送") a.WaitFor("ping") a.WaitFor("streamed") select { case body := <-srv.prompts: if body["text"] != "ping" { t.Errorf("prompt text = %v", body["text"]) } if id, _ := body["id"].(string); len(id) < 5 || id[:4] != "msg_" { t.Errorf("prompt id = %v", body["id"]) } default: t.Fatal("no prompt reached the server") } } func TestPermissionCardAndReply(t *testing.T) { srv := newChatServer(t) // The session list's pending-permission read. a := uitest.Mount(t, func() ui.Node { return rootWithStore(&memStore{}) }, uitest.Platform(ui.PlatformAndroid)) a.Type("http://192.168.1.5:3000", srv.URL) a.Tap("连接") a.WaitFor("First session") select { case <-srv.ready: case <-time.After(5 * time.Second): t.Fatal("no event stream") } a.Tap("First session") a.WaitFor("Hello from the agent") srv.push(`{"id":"evt_10","created":200,"type":"permission.asked","data":{"sessionID":"ses_1","id":"per_1","action":"bash","resources":["npm test"]}}`) a.WaitFor("需要权限:bash") a.Expect("npm test") a.Tap("允许一次") select { case body := <-srv.replies: if body["decision"] != "once" { t.Errorf("decision = %v", body["decision"]) } case <-time.After(3 * time.Second): t.Fatal("no permission reply reached the server") } }