The session screen now has a single server icon that opens the instance list; picking an instance drops the live connection and connects to it, and disconnecting moved into that list. A custom SVG rack stands in for the missing server symbol on Android. The agent sheet lists primary agents only: a session cannot run a subagent (General, Explore) as its own agent. Bump version to 0.1.5 (build 6).
282 lines
8.4 KiB
Go
282 lines
8.4 KiB
Go
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
|
|
models chan map[string]any
|
|
created 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),
|
|
models: make(chan map[string]any, 1),
|
|
created: 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/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_new/message", func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprint(w, `{"data":[],"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/location", func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprint(w, `{"directory":"/home/me/project","project":{"id":"p1"}}`)
|
|
})
|
|
mux.HandleFunc("/api/model", func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprint(w, `{"data":[
|
|
{"id":"gpt-4","providerID":"openai","name":"GPT-4"},
|
|
{"id":"claude","providerID":"anthropic","name":"Claude"}]}`)
|
|
})
|
|
mux.HandleFunc("/api/agent", func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprint(w, `{"data":[
|
|
{"id":"build","name":"Build","mode":"primary"},
|
|
{"id":"plan","name":"Plan","mode":"primary"},
|
|
{"id":"general","name":"General","mode":"subagent","hidden":false},
|
|
{"id":"explore","name":"Explore","mode":"subagent"}]}`)
|
|
})
|
|
mux.HandleFunc("/api/session/ses_1/model", func(w http.ResponseWriter, r *http.Request) {
|
|
var body map[string]any
|
|
_ = json.NewDecoder(r.Body).Decode(&body)
|
|
select {
|
|
case cs.models <- body:
|
|
default:
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
})
|
|
mux.HandleFunc("/api/session", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
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":{}}`)
|
|
return
|
|
}
|
|
var body map[string]any
|
|
_ = json.NewDecoder(r.Body).Decode(&body)
|
|
select {
|
|
case cs.created <- body:
|
|
default:
|
|
}
|
|
fmt.Fprint(w, `{"data":{"id":"ses_new","title":"New chat","time":{"created":3,"updated":3},
|
|
"location":{"directory":"/home/me/project"}}}`)
|
|
})
|
|
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))
|
|
|
|
connectViaForm(t, a, srv.URL)
|
|
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))
|
|
connectViaForm(t, a, srv.URL)
|
|
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.Settle()
|
|
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")
|
|
}
|
|
}
|
|
|
|
func TestSwitchModel(t *testing.T) {
|
|
srv := newChatServer(t)
|
|
a := uitest.Mount(t, func() ui.Node { return rootWithStore(&memStore{}) }, uitest.Platform(ui.PlatformAndroid))
|
|
connectViaForm(t, a, srv.URL)
|
|
a.WaitFor("First session")
|
|
a.Tap("First session")
|
|
a.WaitFor("Hello from the agent")
|
|
|
|
a.Tap("模型")
|
|
a.WaitFor("GPT-4")
|
|
a.Tap("GPT-4")
|
|
|
|
select {
|
|
case body := <-srv.models:
|
|
model, _ := body["model"].(map[string]any)
|
|
if model["id"] != "gpt-4" || model["providerID"] != "openai" {
|
|
t.Errorf("model switch = %v", body)
|
|
}
|
|
case <-time.After(3 * time.Second):
|
|
t.Fatal("no model switch reached the server")
|
|
}
|
|
}
|
|
|
|
func TestCreateSession(t *testing.T) {
|
|
srv := newChatServer(t)
|
|
a := uitest.Mount(t, func() ui.Node { return rootWithStore(&memStore{}) }, uitest.Platform(ui.PlatformAndroid))
|
|
connectViaForm(t, a, srv.URL)
|
|
a.WaitFor("First session")
|
|
|
|
a.Tap("新建")
|
|
a.WaitFor("新建会话")
|
|
a.Type("标题(可选,留空由服务端生成)", "Test chat")
|
|
a.Tap("创建")
|
|
a.WaitFor("New chat")
|
|
|
|
select {
|
|
case body := <-srv.created:
|
|
if body["title"] != "Test chat" {
|
|
t.Errorf("created title = %v", body["title"])
|
|
}
|
|
loc, _ := body["location"].(map[string]any)
|
|
if loc["directory"] != "/home/me/project" {
|
|
t.Errorf("created location = %v", body["location"])
|
|
}
|
|
case <-time.After(3 * time.Second):
|
|
t.Fatal("no create request reached the server")
|
|
}
|
|
}
|
|
|
|
func TestAgentPickerSkipsSubagents(t *testing.T) {
|
|
srv := newChatServer(t)
|
|
a := uitest.Mount(t, func() ui.Node { return rootWithStore(&memStore{}) }, uitest.Platform(ui.PlatformAndroid))
|
|
connectViaForm(t, a, srv.URL)
|
|
a.WaitFor("First session")
|
|
a.Tap("First session")
|
|
a.WaitFor("Hello from the agent")
|
|
|
|
a.Tap("Agent")
|
|
a.WaitFor("Build")
|
|
a.Expect("Plan")
|
|
a.ExpectNo("General")
|
|
a.ExpectNo("Explore")
|
|
}
|