Show recorded OpenCode models in the chat list
This commit is contained in:
@@ -0,0 +1,172 @@
|
||||
package opencode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
func TestResolveSessionModel(t *testing.T) {
|
||||
for _, backend := range []string{"v1", "v2"} {
|
||||
t.Run(backend, func(t *testing.T) {
|
||||
var reads atomic.Int32
|
||||
prefix := "/api/session/ses_operator"
|
||||
if backend == "v1" {
|
||||
prefix = "/session/ses_operator"
|
||||
}
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
reads.Add(1)
|
||||
if r.URL.Query().Get("directory") != "/operator" {
|
||||
t.Errorf("wrong directory: %s", r.URL)
|
||||
}
|
||||
switch r.URL.Path {
|
||||
case prefix:
|
||||
fmt.Fprint(w, `{"data":{"id":"ses_operator"}}`)
|
||||
case prefix + "/message":
|
||||
if backend == "v1" {
|
||||
if r.URL.Query().Get("limit") != "8" {
|
||||
t.Errorf("unbounded request: %s", r.URL)
|
||||
}
|
||||
fmt.Fprint(w, `[{"info":{"role":"assistant","modelID":"old","time":{"created":1}}},{"info":{"role":"assistant","modelID":"deepseek-v4.1-flash","providerID":"opencode-go","time":{"created":2}}},{"info":{"role":"user","modelID":"ignored","time":{"created":3}}}]`)
|
||||
} else {
|
||||
if r.URL.Query().Get("limit") != "1" || r.URL.Query().Get("order") != "desc" || r.URL.Query().Get("type") != "assistant" {
|
||||
t.Errorf("unbounded request: %s", r.URL)
|
||||
}
|
||||
fmt.Fprint(w, `{"data":[{"type":"assistant","model":{"id":"deepseek-v4.1-flash","providerID":"opencode-go"},"time":{"created":2}}]}`)
|
||||
}
|
||||
default:
|
||||
t.Errorf("unexpected read: %s", r.URL)
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
chat := NewChat(&Connection{Backend: backend, URL: srv.URL, Directory: "/wrong"})
|
||||
row := protocol.ChatSession{ID: "ses_operator", Directory: "/operator", UpdatedAt: 10}
|
||||
for range 2 {
|
||||
got, err := chat.ResolveSessionModel(context.Background(), row)
|
||||
if err != nil || got.Model != "deepseek-v4.1-flash" || got.ModelProvider != "opencode-go" {
|
||||
t.Fatalf("resolved: %+v %v", got, err)
|
||||
}
|
||||
}
|
||||
if reads.Load() != 2 {
|
||||
t.Fatalf("cache missed: %d reads", reads.Load())
|
||||
}
|
||||
row.UpdatedAt++
|
||||
if _, err := chat.ResolveSessionModel(context.Background(), row); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if reads.Load() != 4 {
|
||||
t.Fatalf("changed session was cached: %d reads", reads.Load())
|
||||
}
|
||||
cached := chat.sessionModels[row.ID]
|
||||
cached.at = time.Now().Add(-time.Minute)
|
||||
chat.sessionModels[row.ID] = cached
|
||||
if _, err := chat.ResolveSessionModel(context.Background(), row); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if reads.Load() != 6 {
|
||||
t.Fatalf("expired model was cached: %d reads", reads.Load())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveSessionModelPrefersSelectedModel(t *testing.T) {
|
||||
var reads atomic.Int32
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
reads.Add(1)
|
||||
if r.URL.Path != "/api/session/ses_operator" {
|
||||
t.Errorf("unnecessary history read: %s", r.URL)
|
||||
}
|
||||
fmt.Fprint(w, `{"data":{"id":"ses_operator","model":{"id":"selected","providerID":"provider"}}}`)
|
||||
}))
|
||||
defer srv.Close()
|
||||
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
|
||||
row := protocol.ChatSession{ID: "ses_operator"}
|
||||
got, err := chat.ResolveSessionModel(context.Background(), row)
|
||||
if err != nil || got.Model != "selected" || got.ModelProvider != "provider" {
|
||||
t.Fatalf("selected: %+v %v", got, err)
|
||||
}
|
||||
row.Model = "already-listed"
|
||||
got, err = chat.ResolveSessionModel(context.Background(), row)
|
||||
if err != nil || got.Model != "already-listed" || reads.Load() != 1 {
|
||||
t.Fatalf("listed: %+v %v reads=%d", got, err, reads.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveSessionModelRejectsUnboundedOrInvalidMetadata(t *testing.T) {
|
||||
for _, body := range []string{`{"data":[{},{}]}`, strings.Repeat(" ", 1<<20) + `[]`, `garbage`} {
|
||||
t.Run(fmt.Sprint(len(body)), func(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if strings.HasSuffix(r.URL.Path, "/message") {
|
||||
fmt.Fprint(w, body)
|
||||
} else {
|
||||
fmt.Fprint(w, `{"data":{"id":"ses_operator"}}`)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
|
||||
got, err := chat.ResolveSessionModel(context.Background(), protocol.ChatSession{ID: "ses_operator"})
|
||||
if err == nil || got.Model != "" {
|
||||
t.Fatalf("unverified model: %+v %v", got, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveSessionModelDoesNotInventModelForEmptyChat(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if strings.HasSuffix(r.URL.Path, "/message") {
|
||||
fmt.Fprint(w, `{"data":[]}`)
|
||||
} else {
|
||||
fmt.Fprint(w, `{"data":{"id":"ses_empty"}}`)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
|
||||
got, err := chat.ResolveSessionModel(context.Background(), protocol.ChatSession{ID: "ses_empty"})
|
||||
if err != nil || got.Model != "" || got.ModelProvider != "" {
|
||||
t.Fatalf("empty session: %+v %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSwitchModelInvalidatesListModelCache(t *testing.T) {
|
||||
var switched atomic.Bool
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if r.Method == http.MethodPost && r.URL.Path == "/api/session/ses_operator/model" {
|
||||
switched.Store(true)
|
||||
fmt.Fprint(w, `{}`)
|
||||
return
|
||||
}
|
||||
model := "original"
|
||||
if switched.Load() {
|
||||
model = "selected"
|
||||
}
|
||||
fmt.Fprintf(w, `{"data":{"id":"ses_operator","model":{"id":%q,"providerID":"provider"}}}`, model)
|
||||
}))
|
||||
defer srv.Close()
|
||||
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
|
||||
row := protocol.ChatSession{ID: "ses_operator"}
|
||||
if _, err := chat.ResolveSessionModel(context.Background(), row); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := chat.SwitchModel(context.Background(), row.ID, "provider", "selected"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := chat.ResolveSessionModel(context.Background(), row)
|
||||
if err != nil || got.Model != "selected" {
|
||||
t.Fatalf("model switch kept stale row: %+v %v", got, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user