56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package fs
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func testCmds() []ctlCmd {
|
|
ok := func(_ []string) ([]byte, error) { return []byte("ok\n"), nil }
|
|
return []ctlCmd{
|
|
{"stop", "interrupt the current turn", ok},
|
|
{"compact", "compact context now", ok},
|
|
}
|
|
}
|
|
|
|
func TestDispatch_HelpListsCommands(t *testing.T) {
|
|
// Empty input and "help" both return the self-describing listing.
|
|
for _, in := range []string{"", "help", " "} {
|
|
out, err := dispatch(testCmds(), []byte(in))
|
|
if err != nil {
|
|
t.Fatalf("dispatch(%q) error: %v", in, err)
|
|
}
|
|
s := string(out)
|
|
if !strings.Contains(s, "stop") || !strings.Contains(s, "interrupt the current turn") {
|
|
t.Errorf("help listing missing stop/description: %q", s)
|
|
}
|
|
if !strings.Contains(s, "compact") {
|
|
t.Errorf("help listing missing compact: %q", s)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDispatch_UnknownVerbListsValid(t *testing.T) {
|
|
_, err := dispatch(testCmds(), []byte("stpo"))
|
|
if err == nil {
|
|
t.Fatal("expected error for unknown verb")
|
|
}
|
|
msg := err.Error()
|
|
if !strings.Contains(msg, "stpo") {
|
|
t.Errorf("error should name the bad verb: %q", msg)
|
|
}
|
|
if !strings.Contains(msg, "stop") || !strings.Contains(msg, "compact") {
|
|
t.Errorf("error should list valid verbs: %q", msg)
|
|
}
|
|
}
|
|
|
|
func TestDispatch_RoutesToHandler(t *testing.T) {
|
|
out, err := dispatch(testCmds(), []byte("stop"))
|
|
if err != nil {
|
|
t.Fatalf("dispatch error: %v", err)
|
|
}
|
|
if string(out) != "ok\n" {
|
|
t.Errorf("got %q, want ok", out)
|
|
}
|
|
}
|