2026-11-16 09:09:09 +08:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-16 20:04:42 +08:00
|
|
|
|
|
|
|
|
func TestPartShellsHeaderAndGetMessage(t *testing.T) {
|
|
|
|
|
var gotHeader string
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
switch r.URL.Path {
|
|
|
|
|
case "/api/session/ses_1/message":
|
|
|
|
|
gotHeader = r.Header.Get("x-openchamber-part-shells")
|
|
|
|
|
fmt.Fprint(w, `{"data":[{"id":"msg_1","type":"assistant","content":[
|
|
|
|
|
{"type":"tool","id":"call_1","name":"shell","state":{"status":"completed","input":{},"content":[],"metadata":{"openchamberContentShell":{}}}}]}],"cursor":{}}`)
|
|
|
|
|
case "/api/session/ses_1/message/msg_1":
|
|
|
|
|
fmt.Fprint(w, `{"id":"msg_1","type":"assistant","content":[
|
|
|
|
|
{"type":"tool","id":"call_1","name":"shell","state":{"status":"completed","input":{},"content":[{"type":"text","text":"full output"}]}}]}`)
|
|
|
|
|
default:
|
|
|
|
|
t.Errorf("unexpected path %q", r.URL.Path)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
c, _ := New(srv.URL, Options{})
|
|
|
|
|
page, err := c.ListMessages(context.Background(), "ses_1", ListMessagesOptions{Limit: 10, PartShells: true})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if gotHeader != "v1" {
|
|
|
|
|
t.Errorf("header = %q", gotHeader)
|
|
|
|
|
}
|
|
|
|
|
if len(page.Data) != 1 || len(page.Data[0].Content[0].State.Content) != 0 {
|
|
|
|
|
t.Fatalf("page = %+v", page)
|
|
|
|
|
}
|
|
|
|
|
msg, err := c.GetMessage(context.Background(), "ses_1", "msg_1", "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if got := msg.Content[0].State.Content[0].Text; got != "full output" {
|
|
|
|
|
t.Fatalf("message = %+v", msg)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-17 23:46:06 +08:00
|
|
|
|
|
|
|
|
func TestRevertEndpoints(t *testing.T) {
|
|
|
|
|
var staged map[string]any
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
switch r.URL.Path {
|
|
|
|
|
case "/api/session/ses_1/revert/stage":
|
|
|
|
|
_ = json.NewDecoder(r.Body).Decode(&staged)
|
|
|
|
|
fmt.Fprint(w, `{"data":{"messageID":"msg_1"}}`)
|
|
|
|
|
case "/api/session/ses_1/revert/commit":
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
default:
|
|
|
|
|
t.Errorf("unexpected path %q", r.URL.Path)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
c, _ := New(srv.URL, Options{})
|
|
|
|
|
if err := c.StageRevert(context.Background(), "ses_1", "", "msg_1"); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if staged["messageID"] != "msg_1" {
|
|
|
|
|
t.Errorf("staged = %v", staged)
|
|
|
|
|
}
|
|
|
|
|
if err := c.CommitRevert(context.Background(), "ses_1", ""); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|