package server import ( "context" "encoding/json" "path/filepath" "strings" "testing" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" ) // commandChat is a harness that reports its session's slash commands. type commandChat struct { *fakeChat asked []string } func (c *commandChat) Commands(_ context.Context, id string) ([]protocol.ChatCommand, error) { c.asked = append(c.asked, id) return []protocol.ChatCommand{{Name: "code-review", Description: "Review the diff", ArgumentHint: "[low|high]"}, {Name: "probe-skill"}}, nil } func TestChatCommandsRouteToTheSessionHarness(t *testing.T) { ctx := context.Background() store, err := pipeline.Open(filepath.Join(t.TempDir(), "chat.db")) if err != nil { t.Fatal(err) } defer store.Close() claudeID := "01900000-0000-7000-8000-000000000011" codexID := "01900000-0000-7000-8000-000000000012" for id, backend := range map[string]string{claudeID: "claude", codexID: "codex"} { if err := store.RecordOperatorSession(ctx, id, "/fixture", "chat", protocol.ChatSessionTagOperator); err != nil { t.Fatal(err) } if err := store.SetOperatorSessionBackend(ctx, id, backend); err != nil { t.Fatal(err) } } claude := &commandChat{fakeChat: scriptedChat()} manager := &chatManager{claude: claude, codex: scriptedChat(), registry: store, cache: map[string]string{}} var reply protocol.Message capture := func(msg protocol.Message) error { reply = msg; return nil } handleChatCommands(ctx, protocol.Message{ID: "c1", ChatQuery: &protocol.ChatQuery{SessionID: claudeID}}, manager, capture) if reply.Type != protocol.ChatCommands || reply.ID != "c1" || len(reply.ChatCommands) != 2 || len(claude.asked) != 1 || claude.asked[0] != claudeID { t.Fatalf("claude reply = %+v, asked %v", reply, claude.asked) } wire, err := json.Marshal(reply) if err != nil { t.Fatal(err) } if !strings.Contains(string(wire), `"chat_commands":[{"name":"code-review","description":"Review the diff","argument_hint":"[low|high]"},{"name":"probe-skill"}]`) { t.Fatalf("wire = %s", wire) } // A harness without commands answers with an empty list, not an error. handleChatCommands(ctx, protocol.Message{ID: "c2", ChatQuery: &protocol.ChatQuery{SessionID: codexID}}, manager, capture) if reply.Type != protocol.ChatCommands || reply.ID != "c2" || len(reply.ChatCommands) != 0 { t.Fatalf("codex reply = %+v", reply) } handleChatCommands(ctx, protocol.Message{ID: "c3", ChatQuery: &protocol.ChatQuery{SessionID: "not a session"}}, manager, capture) if reply.Type != protocol.Error { t.Fatalf("invalid id reply = %+v", reply) } }