| 1 | package command |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func runSlash(t *testing.T, tl interface { |
| 11 | Execute(context.Context, json.RawMessage) (string, error) |
| 12 | }, args map[string]any) (string, error) { |
| 13 | t.Helper() |
| 14 | raw, _ := json.Marshal(args) |
| 15 | return tl.Execute(context.Background(), raw) |
| 16 | } |
| 17 | |
| 18 | func sampleTool() interface { |
| 19 | Execute(context.Context, json.RawMessage) (string, error) |
| 20 | Name() string |
| 21 | ReadOnly() bool |
| 22 | Description() string |
| 23 | } { |
| 24 | return NewSlashCommandTool([]SlashEntry{ |
| 25 | {Name: "review", Description: "review the diff", ArgHint: "[path]", |
| 26 | Render: func(a []string) string { return "REVIEW " + strings.Join(a, ",") }}, |
| 27 | // Leading slash on Name should be tolerated. |
| 28 | {Name: "/git:commit", Description: "commit", |
| 29 | Render: func(a []string) string { return "COMMIT" }}, |
| 30 | }).(interface { |
| 31 | Execute(context.Context, json.RawMessage) (string, error) |
| 32 | Name() string |
| 33 | ReadOnly() bool |
| 34 | Description() string |
| 35 | }) |
| 36 | } |
| 37 | |
| 38 | func TestSlashToolBasics(t *testing.T) { |
| 39 | tl := sampleTool() |
| 40 | if tl.Name() != "slash_command" { |
| 41 | t.Errorf("name = %q", tl.Name()) |
| 42 | } |
| 43 | if !tl.ReadOnly() { |
| 44 | t.Error("slash_command should be read-only") |
| 45 | } |
| 46 | if !strings.Contains(tl.Description(), "review") || !strings.Contains(tl.Description(), "git:commit") { |
| 47 | t.Errorf("description should list available commands: %q", tl.Description()) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestSlashToolExpandsWithArgs(t *testing.T) { |
| 52 | tl := sampleTool() |
| 53 | out, err := runSlash(t, tl, map[string]any{"command": "review", "arguments": "a b"}) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | if !strings.Contains(out, "REVIEW a,b") { |
| 58 | t.Errorf("args not passed to Render: %q", out) |
| 59 | } |
| 60 | if !strings.Contains(out, "follow these instructions now") { |
| 61 | t.Errorf("expansion should be framed as an instruction: %q", out) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestSlashToolLeadingSlashAndName(t *testing.T) { |
| 66 | tl := sampleTool() |
| 67 | // Caller passes a leading slash; entry was also registered with one. |
| 68 | out, err := runSlash(t, tl, map[string]any{"command": "/git:commit"}) |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | if !strings.Contains(out, "COMMIT") { |
| 73 | t.Errorf("leading-slash command not resolved: %q", out) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestSlashToolList(t *testing.T) { |
| 78 | tl := sampleTool() |
| 79 | for _, cmd := range []string{"", "list", "LIST"} { |
| 80 | out, err := runSlash(t, tl, map[string]any{"command": cmd}) |
| 81 | if err != nil { |
| 82 | t.Fatalf("list(%q): %v", cmd, err) |
| 83 | } |
| 84 | if !strings.Contains(out, "/review") || !strings.Contains(out, "[path]") || !strings.Contains(out, "/git:commit") { |
| 85 | t.Errorf("list(%q) missing entries: %q", cmd, out) |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func TestSlashToolUnknown(t *testing.T) { |
| 91 | tl := sampleTool() |
| 92 | _, err := runSlash(t, tl, map[string]any{"command": "nope"}) |
| 93 | if err == nil { |
| 94 | t.Fatal("unknown command should error") |
| 95 | } |
| 96 | if !strings.Contains(err.Error(), "review") { |
| 97 | t.Errorf("error should list available commands: %v", err) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func TestSlashToolEmptyRegistry(t *testing.T) { |
| 102 | tl := NewSlashCommandTool(nil) |
| 103 | out, err := tl.Execute(context.Background(), json.RawMessage(`{}`)) |
| 104 | if err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | if !strings.Contains(out, "No slash commands") { |
| 108 | t.Errorf("empty list = %q", out) |
| 109 | } |
| 110 | if !strings.Contains(tl.Description(), "No slash commands") { |
| 111 | t.Errorf("empty description = %q", tl.Description()) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestSlashToolNameClashCommandWins(t *testing.T) { |
| 116 | // Skills added first, command second — command should win the name. |
| 117 | tl := NewSlashCommandTool([]SlashEntry{ |
| 118 | {Name: "dup", Render: func([]string) string { return "FROM-SKILL" }}, |
| 119 | {Name: "dup", Render: func([]string) string { return "FROM-COMMAND" }}, |
| 120 | }) |
| 121 | out, err := tl.Execute(context.Background(), json.RawMessage(`{"command":"dup"}`)) |
| 122 | if err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
| 125 | if !strings.Contains(out, "FROM-COMMAND") { |
| 126 | t.Errorf("later entry should win the clash: %q", out) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestPluginSlashToolShowsOnlyCanonicalQualifiedName(t *testing.T) { |
| 131 | dir := t.TempDir() |
| 132 | write(t, dir, "plan.md", "---\ndescription: Plan work\n---\nPlan $ARGUMENTS") |
| 133 | plain, err := Load(dir) |
| 134 | if err != nil { |
| 135 | t.Fatal(err) |
| 136 | } |
| 137 | owned, err := LoadRoots(Root{Path: dir, Plugin: "pwf"}) |
| 138 | if err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | entries := func(cmds []Command) []SlashEntry { |
| 142 | out := make([]SlashEntry, 0, len(cmds)) |
| 143 | for _, cmd := range cmds { |
| 144 | if cmd.Hidden { |
| 145 | continue |
| 146 | } |
| 147 | cmd := cmd |
| 148 | out = append(out, SlashEntry{Name: cmd.Name, Description: cmd.Description, ArgHint: cmd.ArgHint, Render: func(args []string) string { return cmd.Render(args) }}) |
| 149 | } |
| 150 | return out |
| 151 | } |
| 152 | plainTool := NewSlashCommandTool(entries(plain)) |
| 153 | ownedTool := NewSlashCommandTool(entries(owned)) |
| 154 | if !strings.Contains(plainTool.Description(), "Available: plan.") { |
| 155 | t.Fatalf("plain tool description = %q", plainTool.Description()) |
| 156 | } |
| 157 | if !strings.Contains(ownedTool.Description(), "Available: pwf:plan.") || strings.Contains(ownedTool.Description(), "Available: plan,") { |
| 158 | t.Fatalf("plugin tool should list one canonical name, got %q", ownedTool.Description()) |
| 159 | } |
| 160 | if string(plainTool.Schema()) != string(ownedTool.Schema()) { |
| 161 | t.Fatal("plugin qualification must not change the slash_command schema") |
| 162 | } |
| 163 | } |
| 164 |