Files
openchamber-android-native/internal/ocapi/catalog_test.go
T
blumia 254d08b1bd feat: bootstrap OpenChamber native Android client
Add a Go client for OpenChamber built with Asuka's native widgets.

- internal/ocapi: health, password-to-device-token auth, session and
  message paging, prompt/interrupt/model/agent, permissions, SSE events,
  HTTP/SOCKS5 proxies and certificate pinning
- internal/storage: connection settings plus a device token in the
  platform keystore, with a per-install id for server-side dedupe
- internal/appui: connect, session list and chat screens with streaming
  Markdown, reasoning/tool folding, permission cards and session actions
- netcfg: native module that enables cleartext HTTP in release builds
- cmd/ocprobe: terminal probe for the API
- headless UI tests (uitest) and the research/plan document
2026-11-16 09:09:09 +08:00

158 lines
5.0 KiB
Go

package ocapi
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestCatalogAndPermissions(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/model":
if got := r.URL.Query().Get("location[directory]"); got != "/home/me/p" {
t.Errorf("model directory = %q", got)
}
fmt.Fprint(w, `{"data":[{"id":"gpt","providerID":"openai","name":"GPT"}]}`)
case "/api/agent":
fmt.Fprint(w, `{"data":[{"id":"build","name":"Build","mode":"primary"}]}`)
case "/api/permission/request":
fmt.Fprint(w, `{"data":[{"id":"per_1","sessionID":"ses_1","action":"bash","resources":["rm -rf x"]}]}`)
case "/api/session/ses_1/permission/per_1/reply":
var body map[string]any
_ = json.NewDecoder(r.Body).Decode(&body)
if body["decision"] != "always" || body["message"] != "ok" {
t.Errorf("reply body = %v", body)
}
w.WriteHeader(http.StatusNoContent)
case "/api/session/ses_1/model":
var body struct {
Model ModelRef `json:"model"`
}
_ = json.NewDecoder(r.Body).Decode(&body)
if body.Model.ID != "gpt" || body.Model.ProviderID != "openai" {
t.Errorf("model switch = %+v", body.Model)
}
w.WriteHeader(http.StatusNoContent)
case "/api/session/ses_1/agent":
var body map[string]any
_ = json.NewDecoder(r.Body).Decode(&body)
if body["agent"] != "plan" {
t.Errorf("agent switch = %v", body)
}
w.WriteHeader(http.StatusNoContent)
case "/api/session/ses_1":
if r.Method != http.MethodPatch {
t.Errorf("method = %s", r.Method)
}
var body map[string]any
_ = json.NewDecoder(r.Body).Decode(&body)
if body["title"] != "renamed" {
t.Errorf("patch = %v", body)
}
w.WriteHeader(http.StatusNoContent)
default:
t.Errorf("unexpected path %q", r.URL.Path)
w.WriteHeader(http.StatusNotFound)
}
}))
defer srv.Close()
ctx := context.Background()
c, _ := New(srv.URL, Options{})
models, err := c.Models(ctx, "/home/me/p")
if err != nil || len(models) != 1 || models[0].ID != "gpt" {
t.Fatalf("models: %+v %v", models, err)
}
agents, err := c.Agents(ctx, "")
if err != nil || len(agents) != 1 || agents[0].ID != "build" {
t.Fatalf("agents: %+v %v", agents, err)
}
perms, err := c.PendingPermissions(ctx, "")
if err != nil || len(perms) != 1 || perms[0].Action != "bash" {
t.Fatalf("permissions: %+v %v", perms, err)
}
if err := c.ReplyPermission(ctx, "ses_1", "per_1", PermissionAlways, "ok"); err != nil {
t.Fatal(err)
}
if err := c.SwitchModel(ctx, "ses_1", "", ModelRef{ID: "gpt", ProviderID: "openai"}); err != nil {
t.Fatal(err)
}
if err := c.SwitchAgent(ctx, "ses_1", "", "plan"); err != nil {
t.Fatal(err)
}
title := "renamed"
if err := c.UpdateSession(ctx, "ses_1", "", SessionPatch{Title: &title}); err != nil {
t.Fatal(err)
}
}
func TestCreateAndDeleteSession(t *testing.T) {
var createBody map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.URL.Path == "/api/session" && r.Method == http.MethodPost:
_ = json.NewDecoder(r.Body).Decode(&createBody)
fmt.Fprint(w, `{"data":{"id":"ses_new","title":"New","time":{"created":1,"updated":1}}}`)
case r.URL.Path == "/api/session/ses_new" && r.Method == http.MethodDelete:
w.WriteHeader(http.StatusNoContent)
default:
t.Errorf("unexpected %s %s", r.Method, r.URL.Path)
}
}))
defer srv.Close()
c, _ := New(srv.URL, Options{})
s, err := c.CreateSession(context.Background(), CreateSessionOptions{Directory: "/p", Title: "New"})
if err != nil || s.ID != "ses_new" {
t.Fatalf("create: %+v %v", s, err)
}
if loc, _ := createBody["location"].(map[string]any); loc["directory"] != "/p" {
t.Errorf("create location: %v", createBody)
}
if err := c.DeleteSession(context.Background(), "ses_new", ""); err != nil {
t.Fatal(err)
}
}
func TestWaitReady(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got := r.URL.Query().Get("wait"); got != "1500" {
t.Errorf("wait = %q", got)
}
fmt.Fprint(w, `{"healthy":true}`)
}))
defer srv.Close()
c, _ := New(srv.URL, Options{})
if err := c.WaitReady(context.Background(), 1500*time.Millisecond); err != nil {
t.Fatal(err)
}
}
func TestCertificateFingerprint(t *testing.T) {
// A TLS server with a self-signed certificate; pinning its fingerprint
// (or skipping verification) is the only way to reach it.
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"status":"ok"}`)
}))
defer srv.Close()
if _, err := New(srv.URL, Options{}); err != nil {
t.Fatal(err)
}
c, _ := New(srv.URL, Options{})
if _, err := c.Health(context.Background()); err == nil {
t.Fatal("self-signed certificate accepted without opting in")
}
c2, _ := New(srv.URL, Options{InsecureSkipVerify: true})
if _, err := c2.Health(context.Background()); err != nil {
t.Fatalf("insecure option did not work: %v", err)
}
}