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] []" || 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") } }