Files
pipelines/internal/server/chat_session_model_test.go

76 lines
2.8 KiB
Go

package server
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestChatListResolvesOnlyListedOperatorModels(t *testing.T) {
ctx := context.Background()
store := newOperatorStore(t)
if err := store.RecordOperatorSession(ctx, "ses_operator", "/operator", "Operator", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
var detailReads, historyReads int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/api/session":
fmt.Fprint(w, `{"data":[{"id":"ses_operator","time":{"updated":12}},{"id":"ses_pipeline"},{"id":"ses_foreign"}]}`)
case "/api/session/ses_operator":
detailReads++
fmt.Fprint(w, `{"data":{"id":"ses_operator"}}`)
case "/api/session/ses_operator/message":
historyReads++
fmt.Fprint(w, `{"data":[{"type":"assistant","model":{"id":"deepseek-v4.1-flash","providerID":"opencode-go"}}]}`)
default:
t.Errorf("read unlisted history: %s", r.URL)
http.NotFound(w, r)
}
}))
defer srv.Close()
broker := NewChatManager(ctx, opencode.NewChat(&opencode.Connection{Backend: "v2", URL: srv.URL}), store)
for range 2 {
var reply protocol.Message
handleChatSessions(ctx, protocol.Message{ID: "list"}, broker, nil, store, func(msg protocol.Message) error { reply = msg; return nil })
if reply.Type != protocol.ChatSessions || len(reply.ChatSessions) != 1 {
t.Fatalf("list: %+v", reply)
}
row := reply.ChatSessions[0]
if row.ID != "ses_operator" || row.Model != "deepseek-v4.1-flash" || row.ModelProvider != "opencode-go" {
t.Fatalf("model: %+v", row)
}
}
if detailReads != 1 || historyReads != 1 {
t.Fatalf("metadata was not cached: detail=%d history=%d", detailReads, historyReads)
}
}
type unavailableSessionModel struct{ *fakeChat }
func (b unavailableSessionModel) ResolveSessionModel(_ context.Context, row protocol.ChatSession) (protocol.ChatSession, error) {
return row, errors.New("metadata unavailable")
}
func TestChatListSurvivesUnavailableModelMetadata(t *testing.T) {
ctx := context.Background()
store := newOperatorStore(t)
if err := store.RecordOperatorSession(ctx, "ses_operator", "/operator", "Operator", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
fake := scriptedChat()
fake.sessions = []protocol.ChatSession{{ID: "ses_operator"}}
var reply protocol.Message
handleChatSessions(ctx, protocol.Message{ID: "list"}, unavailableSessionModel{fake}, nil, store, func(msg protocol.Message) error { reply = msg; return nil })
if reply.Type != protocol.ChatSessions || len(reply.ChatSessions) != 1 || reply.ChatSessions[0].Model != "" {
t.Fatalf("unavailable metadata hid or invented a model: %+v", reply)
}
}