2026-11-16 09:09:09 +08:00
|
|
|
package appui
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"sync"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/egoist/asuka/ui"
|
|
|
|
|
"github.com/egoist/asuka/uitest"
|
|
|
|
|
"github.com/openchamber/openchamber-native/internal/storage"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// memStore is a storage.Store that keeps everything in memory.
|
|
|
|
|
type memStore struct {
|
|
|
|
|
mu sync.Mutex
|
2026-11-16 11:52:38 +08:00
|
|
|
conns []storage.Connection
|
2026-11-16 09:09:09 +08:00
|
|
|
tokens map[string]string
|
|
|
|
|
id string
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
func (m *memStore) Connections() []storage.Connection {
|
2026-11-16 09:09:09 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
|
defer m.mu.Unlock()
|
2026-11-16 11:52:38 +08:00
|
|
|
out := make([]storage.Connection, len(m.conns))
|
|
|
|
|
copy(out, m.conns)
|
|
|
|
|
return out
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
2026-11-16 11:52:38 +08:00
|
|
|
|
|
|
|
|
func (m *memStore) SaveConnections(list []storage.Connection) {
|
2026-11-16 09:09:09 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
|
defer m.mu.Unlock()
|
2026-11-16 11:52:38 +08:00
|
|
|
out := make([]storage.Connection, len(list))
|
|
|
|
|
copy(out, list)
|
|
|
|
|
m.conns = out
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
2026-11-16 11:52:38 +08:00
|
|
|
|
2026-11-16 09:09:09 +08:00
|
|
|
func (m *memStore) InstallID() string {
|
2026-11-16 11:52:38 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
|
defer m.mu.Unlock()
|
2026-11-16 09:09:09 +08:00
|
|
|
if m.id == "" {
|
|
|
|
|
m.id = "test-install"
|
|
|
|
|
}
|
|
|
|
|
return m.id
|
|
|
|
|
}
|
2026-11-16 11:52:38 +08:00
|
|
|
|
|
|
|
|
func (m *memStore) LoadToken(c storage.Connection) (string, bool, error) {
|
2026-11-16 09:09:09 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
|
defer m.mu.Unlock()
|
2026-11-16 11:52:38 +08:00
|
|
|
t, ok := m.tokens[c.ID]
|
2026-11-16 09:09:09 +08:00
|
|
|
return t, ok, nil
|
|
|
|
|
}
|
2026-11-16 11:52:38 +08:00
|
|
|
|
|
|
|
|
func (m *memStore) SaveToken(id, token string) error {
|
2026-11-16 09:09:09 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
if m.tokens == nil {
|
|
|
|
|
m.tokens = map[string]string{}
|
|
|
|
|
}
|
2026-11-16 11:52:38 +08:00
|
|
|
m.tokens[id] = token
|
2026-11-16 09:09:09 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
func (m *memStore) ClearToken(c storage.Connection) error {
|
2026-11-16 09:09:09 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
|
defer m.mu.Unlock()
|
2026-11-16 11:52:38 +08:00
|
|
|
delete(m.tokens, c.ID)
|
|
|
|
|
return nil
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
func (m *memStore) token(id string) string {
|
2026-11-16 09:09:09 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
|
defer m.mu.Unlock()
|
2026-11-16 11:52:38 +08:00
|
|
|
return m.tokens[id]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *memStore) connections() []storage.Connection {
|
|
|
|
|
return m.Connections()
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fakeServer stands in for OpenChamber: /health, /auth/session and the
|
2026-11-16 11:52:38 +08:00
|
|
|
// session list. An empty password makes it passwordless.
|
|
|
|
|
func fakeServer(t *testing.T, password, validToken, sessionTitle string) *httptest.Server {
|
2026-11-16 09:09:09 +08:00
|
|
|
t.Helper()
|
2026-11-16 11:52:38 +08:00
|
|
|
if sessionTitle == "" {
|
|
|
|
|
sessionTitle = "First session"
|
|
|
|
|
}
|
2026-11-16 09:09:09 +08:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
authorized := func(r *http.Request) bool {
|
|
|
|
|
if validToken == "" {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return r.Header.Get("Authorization") == "Bearer "+validToken
|
|
|
|
|
}
|
|
|
|
|
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
fmt.Fprint(w, `{"status":"ok","openchamberVersion":"2.7.0","isOpenCodeReady":true,"serverId":"srv_1"}`)
|
|
|
|
|
})
|
|
|
|
|
mux.HandleFunc("/auth/session", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if password == "" {
|
|
|
|
|
fmt.Fprint(w, `{"authenticated":true,"disabled":true}`)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
switch r.Method {
|
|
|
|
|
case http.MethodGet:
|
|
|
|
|
if authorized(r) {
|
|
|
|
|
fmt.Fprint(w, `{"authenticated":true,"scope":"client"}`)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
|
fmt.Fprint(w, `{"authenticated":false,"locked":true}`)
|
|
|
|
|
case http.MethodPost:
|
|
|
|
|
var body struct {
|
|
|
|
|
Password string `json:"password"`
|
|
|
|
|
}
|
|
|
|
|
_ = json.NewDecoder(r.Body).Decode(&body)
|
|
|
|
|
if body.Password != password {
|
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
|
fmt.Fprint(w, `{"error":"Invalid credentials"}`)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fmt.Fprintf(w, `{"authenticated":true,"clientToken":%q}`, validToken)
|
|
|
|
|
}
|
|
|
|
|
})
|
2026-11-16 11:52:38 +08:00
|
|
|
mux.HandleFunc("/api/location", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
fmt.Fprint(w, `{"directory":"/home/me/project","project":{"id":"p1"}}`)
|
|
|
|
|
})
|
2026-11-16 09:09:09 +08:00
|
|
|
mux.HandleFunc("/api/session", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if !authorized(r) {
|
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-11-16 11:52:38 +08:00
|
|
|
fmt.Fprintf(w, `{"data":[{"id":"ses_1","title":%q,"time":{"created":1,"updated":2},
|
|
|
|
|
"location":{"directory":"/home/me/project"},"model":{"id":"m","providerID":"p"}}],"cursor":{}}`, sessionTitle)
|
2026-11-16 09:09:09 +08:00
|
|
|
})
|
|
|
|
|
mux.HandleFunc("/api/session/active", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
fmt.Fprint(w, `{"data":{"ses_1":{"type":"running"}}}`)
|
|
|
|
|
})
|
|
|
|
|
mux.HandleFunc("/api/permission/request", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
fmt.Fprint(w, `{"data":[]}`)
|
|
|
|
|
})
|
|
|
|
|
mux.HandleFunc("/api/event", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
|
fmt.Fprint(w, "data: {\"id\":\"evt_1\",\"type\":\"server.connected\",\"data\":{}}\n\n")
|
|
|
|
|
if f, ok := w.(http.Flusher); ok {
|
|
|
|
|
f.Flush()
|
|
|
|
|
}
|
|
|
|
|
<-r.Context().Done()
|
|
|
|
|
})
|
|
|
|
|
srv := httptest.NewServer(mux)
|
|
|
|
|
t.Cleanup(srv.Close)
|
|
|
|
|
return srv
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mount(t *testing.T, st *memStore) *uitest.App {
|
|
|
|
|
t.Helper()
|
|
|
|
|
return uitest.Mount(t, func() ui.Node { return rootWithStore(st) }, uitest.Platform(ui.PlatformAndroid))
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
// connectViaForm adds a server from the first screen (or the saved list)
|
|
|
|
|
// and taps Connect.
|
|
|
|
|
func connectViaForm(t *testing.T, a *uitest.App, url string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
if a.Has("手动输入地址") {
|
|
|
|
|
a.Tap("手动输入地址")
|
|
|
|
|
} else {
|
|
|
|
|
a.Tap("添加服务器")
|
|
|
|
|
}
|
|
|
|
|
a.Type("http://192.168.1.5:3000", url)
|
|
|
|
|
a.Tap("连接")
|
|
|
|
|
}
|
2026-11-16 09:09:09 +08:00
|
|
|
|
2026-11-16 13:21:20 +08:00
|
|
|
// disconnectViaInstances opens the instance list and drops the live
|
|
|
|
|
// connection from there; the sessions header has only the list button.
|
|
|
|
|
func disconnectViaInstances(t *testing.T, a *uitest.App) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
a.Tap("实例")
|
|
|
|
|
a.Tap("断开当前连接")
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
func unlockWith(t *testing.T, a *uitest.App, password string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
a.WaitFor("这台服务器需要密码。")
|
|
|
|
|
a.Type("密码", password)
|
|
|
|
|
a.Tap("解锁")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestConnectWithPassword(t *testing.T) {
|
|
|
|
|
srv := fakeServer(t, "secret", "oc_client_test", "")
|
2026-11-16 09:09:09 +08:00
|
|
|
st := &memStore{}
|
|
|
|
|
a := mount(t, st)
|
2026-11-16 11:52:38 +08:00
|
|
|
connectViaForm(t, a, srv.URL)
|
|
|
|
|
unlockWith(t, a, "secret")
|
2026-11-16 09:09:09 +08:00
|
|
|
a.WaitFor("First session")
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
conns := st.connections()
|
|
|
|
|
if len(conns) != 1 || conns[0].URL != srv.URL {
|
|
|
|
|
t.Fatalf("connections = %+v", conns)
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
2026-11-16 11:52:38 +08:00
|
|
|
if got := st.token(conns[0].ID); got != "oc_client_test" {
|
|
|
|
|
t.Errorf("token = %q", got)
|
|
|
|
|
}
|
|
|
|
|
if conns[0].Passwordless {
|
|
|
|
|
t.Error("password server marked passwordless")
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
func TestWrongPasswordThenRetry(t *testing.T) {
|
|
|
|
|
srv := fakeServer(t, "secret", "oc_client_test", "")
|
|
|
|
|
st := &memStore{}
|
|
|
|
|
a := mount(t, st)
|
|
|
|
|
connectViaForm(t, a, srv.URL)
|
|
|
|
|
unlockWith(t, a, "nope")
|
2026-11-16 09:09:09 +08:00
|
|
|
a.WaitFor("密码错误")
|
2026-11-16 11:52:38 +08:00
|
|
|
// The prompt stays up so the password can be retried.
|
|
|
|
|
a.Type("密码", "secret")
|
|
|
|
|
a.Tap("解锁")
|
|
|
|
|
a.WaitFor("First session")
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAutoReconnectWithStoredToken(t *testing.T) {
|
2026-11-16 11:52:38 +08:00
|
|
|
srv := fakeServer(t, "secret", "oc_client_test", "")
|
2026-11-16 09:09:09 +08:00
|
|
|
st := &memStore{
|
2026-11-16 11:52:38 +08:00
|
|
|
conns: []storage.Connection{{ID: "a", URL: srv.URL, Label: "Home", LastUsedAt: 1}},
|
|
|
|
|
tokens: map[string]string{"a": "oc_client_test"},
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
a := mount(t, st)
|
|
|
|
|
a.WaitFor("First session")
|
2026-11-16 11:52:38 +08:00
|
|
|
a.ExpectNo("手动输入地址")
|
|
|
|
|
a.ExpectNo("这台服务器需要密码。")
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestExpiredTokenAsksForPassword(t *testing.T) {
|
2026-11-16 11:52:38 +08:00
|
|
|
srv := fakeServer(t, "secret", "oc_client_test", "")
|
2026-11-16 09:09:09 +08:00
|
|
|
st := &memStore{
|
2026-11-16 11:52:38 +08:00
|
|
|
conns: []storage.Connection{{ID: "a", URL: srv.URL, Label: "Home", LastUsedAt: 1}},
|
|
|
|
|
tokens: map[string]string{"a": "revoked"},
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
a := mount(t, st)
|
2026-11-16 11:52:38 +08:00
|
|
|
a.WaitFor("登录已失效,请重新输入密码")
|
|
|
|
|
a.Until("the revoked token to be dropped", func() bool { return st.token("a") == "" })
|
|
|
|
|
unlockWith(t, a, "secret")
|
|
|
|
|
a.WaitFor("First session")
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPasswordlessServer(t *testing.T) {
|
2026-11-16 11:52:38 +08:00
|
|
|
srv := fakeServer(t, "", "", "")
|
2026-11-16 09:09:09 +08:00
|
|
|
st := &memStore{}
|
|
|
|
|
a := mount(t, st)
|
2026-11-16 11:52:38 +08:00
|
|
|
connectViaForm(t, a, srv.URL)
|
2026-11-16 09:09:09 +08:00
|
|
|
a.WaitFor("First session")
|
2026-11-16 11:52:38 +08:00
|
|
|
conns := st.connections()
|
|
|
|
|
if len(conns) != 1 || !conns[0].Passwordless {
|
|
|
|
|
t.Fatalf("connections = %+v", conns)
|
|
|
|
|
}
|
|
|
|
|
if got := st.token(conns[0].ID); got != "" {
|
|
|
|
|
t.Errorf("unexpected token %q", got)
|
2026-11-16 09:09:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-11-16 10:41:04 +08:00
|
|
|
|
2026-11-16 11:52:38 +08:00
|
|
|
func TestMultipleConnectionsAndSwitch(t *testing.T) {
|
|
|
|
|
srvA := fakeServer(t, "", "", "Session A")
|
|
|
|
|
srvB := fakeServer(t, "", "", "Session B")
|
|
|
|
|
st := &memStore{conns: []storage.Connection{
|
|
|
|
|
{ID: "a", URL: srvA.URL, Label: "A", Passwordless: true, LastUsedAt: 2},
|
|
|
|
|
{ID: "b", URL: srvB.URL, Label: "B", Passwordless: true, LastUsedAt: 1},
|
|
|
|
|
}}
|
2026-11-16 10:41:04 +08:00
|
|
|
a := mount(t, st)
|
2026-11-16 11:52:38 +08:00
|
|
|
a.WaitFor("Session A")
|
|
|
|
|
|
2026-11-16 13:21:20 +08:00
|
|
|
disconnectViaInstances(t, a)
|
2026-11-16 11:52:38 +08:00
|
|
|
a.Expect("A")
|
|
|
|
|
a.Expect("B")
|
|
|
|
|
|
|
|
|
|
a.Tap("B")
|
|
|
|
|
a.WaitFor("Session B")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAddAnotherConnection(t *testing.T) {
|
|
|
|
|
srvA := fakeServer(t, "", "", "Session A")
|
|
|
|
|
srvB := fakeServer(t, "", "", "Session B")
|
|
|
|
|
st := &memStore{conns: []storage.Connection{
|
|
|
|
|
{ID: "a", URL: srvA.URL, Label: "A", Passwordless: true, LastUsedAt: 1},
|
|
|
|
|
}}
|
|
|
|
|
a := mount(t, st)
|
|
|
|
|
a.WaitFor("Session A")
|
2026-11-16 13:21:20 +08:00
|
|
|
disconnectViaInstances(t, a)
|
2026-11-16 11:52:38 +08:00
|
|
|
a.Tap("添加服务器")
|
|
|
|
|
a.Type("http://192.168.1.5:3000", srvB.URL)
|
|
|
|
|
a.Type("名称(可选)", "B")
|
|
|
|
|
a.Tap("连接")
|
|
|
|
|
a.WaitFor("Session B")
|
|
|
|
|
|
|
|
|
|
if conns := st.connections(); len(conns) != 2 {
|
|
|
|
|
t.Fatalf("connections = %+v", conns)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDeleteConnection(t *testing.T) {
|
|
|
|
|
srv := fakeServer(t, "", "", "Session A")
|
|
|
|
|
st := &memStore{conns: []storage.Connection{
|
|
|
|
|
{ID: "a", URL: srv.URL, Label: "A", Passwordless: true, LastUsedAt: 1},
|
|
|
|
|
}}
|
|
|
|
|
a := mount(t, st)
|
|
|
|
|
a.WaitFor("Session A")
|
2026-11-16 13:21:20 +08:00
|
|
|
disconnectViaInstances(t, a)
|
2026-11-16 11:52:38 +08:00
|
|
|
|
|
|
|
|
a.Tap("操作")
|
|
|
|
|
a.Tap("删除")
|
|
|
|
|
a.Tap("删除")
|
|
|
|
|
a.Until("the connection list to empty", func() bool { return len(st.connections()) == 0 })
|
|
|
|
|
a.Expect("手动输入地址")
|
2026-11-16 10:41:04 +08:00
|
|
|
}
|
2026-11-16 12:05:50 +08:00
|
|
|
|
|
|
|
|
func TestLabelFallsBackToHost(t *testing.T) {
|
|
|
|
|
c := &controller{}
|
|
|
|
|
conn := storage.Connection{URL: "http://192.168.1.5:3000", Label: "http://192.168.1.5:3000"}
|
|
|
|
|
if got := c.labelOf(conn); got != "192.168.1.5:3000" {
|
|
|
|
|
t.Fatalf("label = %q", got)
|
|
|
|
|
}
|
|
|
|
|
conn.Label = "Home"
|
|
|
|
|
if got := c.labelOf(conn); got != "Home" {
|
|
|
|
|
t.Fatalf("label = %q", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-16 12:10:11 +08:00
|
|
|
|
|
|
|
|
func TestFailedAddReopensTheForm(t *testing.T) {
|
|
|
|
|
a := mount(t, &memStore{})
|
|
|
|
|
a.Tap("手动输入地址")
|
|
|
|
|
a.Type("http://192.168.1.5:3000", "http://127.0.0.1:1")
|
|
|
|
|
a.Tap("连接")
|
|
|
|
|
a.WaitFor("连接被拒绝,请确认服务器地址与端口")
|
|
|
|
|
a.Expect("添加服务器")
|
|
|
|
|
a.Expect("取消")
|
|
|
|
|
}
|