Files
pipelines/internal/claude/commands_test.go
T
alan d1390994cc Chat: offer the session's Claude Code skills and slash commands in the palette
The server reads the commands Claude Code's initialize response lists (bundled,
user, project, plugin and claude.ai-synced skills plus CLI commands, with
descriptions and argument hints), drops terminal-only ones, and serves them per
chat over a new chat_commands request without waiting for a CLI start. The
composer palette lists them after the recorded commands; choosing one inserts
"/<name> " and sending passes it to the harness as an ordinary prompt.
Codex, OpenCode and Pi chats reply with an empty list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 20:53:57 +00:00

111 lines
3.9 KiB
Go

package claude
import (
"context"
"encoding/json"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestInitializeCommandsKeepNamesDescriptionsAndHints(t *testing.T) {
commands := parseInitializeCommands(json.RawMessage(`{"commands":[
{"name":"code-review","description":"Review the current diff","argumentHint":"[low|high]"},
{"name":" /docs ","description":" Docs (claude.ai sync) ","argumentHint":""},
{"name":"","description":"nameless"},
{"name":"two words","description":"bad"},
{"name":"__remote-workflow","description":"internal"},
{"name":"code-review","description":"duplicate"}]}`))
want := []protocol.ChatCommand{
{Name: "code-review", Description: "Review the current diff", ArgumentHint: "[low|high]"},
{Name: "docs", Description: "Docs (claude.ai sync)"},
}
if len(commands) != len(want) {
t.Fatalf("commands = %+v", commands)
}
for i := range want {
if commands[i] != want[i] {
t.Fatalf("command %d = %+v, want %+v", i, commands[i], want[i])
}
}
if got := parseInitializeCommands(json.RawMessage(`{"models":[]}`)); len(got) != 0 {
t.Fatalf("no commands field = %+v", got)
}
if got := parseInitializeCommands(nil); got != nil {
t.Fatalf("empty init = %+v", got)
}
}
// The chat lists the CLI's initialize commands and, once the init line names
// terminal-only commands, stops offering them. A slash prompt reaches the CLI
// verbatim, so the CLI expands the skill.
func TestClaudeCommandsComeFromInitializeAndSkipTerminalOnly(t *testing.T) {
chat := testChat(t)
session := newCreatedChat(t, chat)
ctx := context.Background()
names := func(commands []protocol.ChatCommand) []string {
out := []string{}
for _, c := range commands {
out = append(out, c.Name)
}
return out
}
// No CLI runs yet: the list is empty at once and the CLI starts in the
// background, so a later ask has its commands.
commands, err := chat.Commands(ctx, session.ID)
if err != nil || len(commands) != 0 {
t.Fatalf("commands before any CLI = %v, %v", names(commands), err)
}
eventually(t, "commands after the background start", func() bool {
commands, err = chat.Commands(ctx, session.ID)
return err == nil && len(commands) > 0
})
if got := names(commands); len(got) != 3 || got[0] != "code-review" || got[1] != "probe-skill" || got[2] != "color" {
t.Fatalf("commands before init = %v", got)
}
if commands[0].ArgumentHint != "[low|high] [<pr#>]" || commands[0].Description != "Review the current diff" {
t.Fatalf("code-review = %+v", commands[0])
}
frames := subscribe(t, chat, session.ID)
if err := chat.Prompt(ctx, session.ID, "/probe-skill banana"); err != nil {
t.Fatal(err)
}
waitStepEnded(t, frames)
commands, err = chat.Commands(ctx, session.ID)
if err != nil {
t.Fatal(err)
}
if got := names(commands); len(got) != 2 || got[0] != "code-review" || got[1] != "probe-skill" {
t.Fatalf("commands after init = %v", got)
}
// Another chat in the same directory lists them before its CLI starts.
other, err := chat.Create(ctx, "other", session.Directory)
if err != nil {
t.Fatal(err)
}
if got, _ := chat.Commands(ctx, other.ID); len(got) != 2 {
t.Fatalf("same-directory cached commands = %v", names(got))
}
eventually(t, "other chat's CLI", func() bool {
s := chat.session(other.ID)
s.mu.Lock()
defer s.mu.Unlock()
return s.proc != nil
})
eventually(t, "transcript", func() bool { return sessionTranscriptPath(session.ID) != "" })
page, err := chat.Messages(ctx, session.ID, 50, "")
if err != nil {
t.Fatal(err)
}
if len(page.Messages) == 0 || len(page.Messages[0].Parts) != 1 || page.Messages[0].Parts[0].Text != "/probe-skill banana" {
t.Fatalf("slash prompt did not reach the CLI verbatim: %+v", page.Messages)
}
}
func TestClaudeCommandsRejectUnknownSession(t *testing.T) {
chat := testChat(t)
if _, err := chat.Commands(context.Background(), "missing"); err == nil {
t.Fatal("unknown session listed commands")
}
}