package server import ( "context" "testing" "git.a-wan.net/alan/pipelines/internal/protocol" ) type automationChat struct { *fakeChat info protocol.ChatAutomationInfo readCalled int } func (c *automationChat) Automations(context.Context, string) (protocol.ChatAutomationInfo, error) { c.readCalled++ return c.info, nil } func automationReply(t *testing.T, request protocol.Message, broker chatBroker, registry operatorRegistry) protocol.Message { t.Helper() var reply protocol.Message handleChatAutomations(context.Background(), request, broker, registry, func(message protocol.Message) error { reply = message return nil }) return reply } func TestChatAutomationsRequiresOwnedSession(t *testing.T) { store := newOperatorStore(t) if err := store.RecordOperatorSession(context.Background(), "ses_owned", t.TempDir(), "owned", protocol.ChatSessionTagOperator); err != nil { t.Fatal(err) } broker := &automationChat{fakeChat: scriptedChat(), info: protocol.ChatAutomationInfo{Status: protocol.ChatAutomationSupported}} reply := automationReply(t, protocol.Message{Type: protocol.ChatAutomations, ID: "a1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_foreign"}}, broker, store) if reply.Type != protocol.Error || reply.Error != "operator session is not available" { t.Fatalf("foreign automation read = %+v", reply) } if broker.readCalled != 0 { t.Fatal("provider read happened before ownership check") } } func TestChatAutomationsDistinguishesUnavailableFromEmpty(t *testing.T) { store := newOperatorStore(t) if err := store.RecordOperatorSession(context.Background(), "ses_empty", t.TempDir(), "empty", protocol.ChatSessionTagOperator); err != nil { t.Fatal(err) } request := protocol.Message{Type: protocol.ChatAutomations, ID: "a2", ChatQuery: &protocol.ChatQuery{SessionID: "ses_empty"}} unsupported := automationReply(t, request, scriptedChat(), store) if unsupported.Type != protocol.ChatAutomations || unsupported.ChatAutomations == nil || unsupported.ChatAutomations.Status != protocol.ChatAutomationUnavailable || unsupported.ChatAutomations.ActiveCount != nil { t.Fatalf("unsupported automation read = %+v", unsupported) } empty := 0 broker := &automationChat{fakeChat: scriptedChat(), info: protocol.ChatAutomationInfo{SessionID: "ses_empty", Provider: "opencode", Status: protocol.ChatAutomationSupported, ActiveCount: &empty}} supported := automationReply(t, request, broker, store) if supported.Type != protocol.ChatAutomations || supported.ChatAutomations == nil || supported.ChatAutomations.Status != protocol.ChatAutomationSupported || supported.ChatAutomations.ActiveCount == nil || *supported.ChatAutomations.ActiveCount != 0 { t.Fatalf("empty automation read = %+v", supported) } } func TestChatAutomationsRejectsMalformedSessionBounds(t *testing.T) { broker := &automationChat{fakeChat: scriptedChat()} for _, query := range []*protocol.ChatQuery{ nil, {SessionID: ""}, {SessionID: "not-a-session"}, {SessionID: "ses_" + string(make([]byte, 201))}, } { reply := automationReply(t, protocol.Message{Type: protocol.ChatAutomations, ID: "a3", ChatQuery: query}, broker, nil) if reply.Type != protocol.Error { t.Fatalf("malformed query %+v returned %+v", query, reply) } } } func TestChatAutomationsRejectsMalformedProviderMetadata(t *testing.T) { store := newOperatorStore(t) if err := store.RecordOperatorSession(context.Background(), "ses_bad", t.TempDir(), "bad", protocol.ChatSessionTagOperator); err != nil { t.Fatal(err) } count := -1 broker := &automationChat{fakeChat: scriptedChat(), info: protocol.ChatAutomationInfo{SessionID: "ses_bad", Status: protocol.ChatAutomationSupported, ActiveCount: &count}} reply := automationReply(t, protocol.Message{Type: protocol.ChatAutomations, ID: "a4", ChatQuery: &protocol.ChatQuery{SessionID: "ses_bad"}}, broker, store) if reply.Type != protocol.Error { t.Fatalf("malformed provider metadata = %+v", reply) } }