1package palette
2
3import (
4 "testing"
5 "time"
6
7 "github.com/bnema/vev/internal/usecase/command"
8)
9
10func TestParseActionExactEffectiveToken(t *testing.T) {
11 results := CommandResults([]command.Command{
12 {Code: "BSK", Arguments: command.ArgumentsNone},
13 {Code: "JRS", Arguments: command.ArgumentsRequired, ContextHint: command.ContextHintRecentSessions},
14 })
15 tests := []struct {
16 name string
17 input string
18 ok bool
19 code string
20 args []string
21 }{
22 {name: "static token ignores case and whitespace", input: " bSk ", ok: true, code: "BSK"},
23 {name: "required arguments accept repeated whitespace", input: " JRS 2 ", ok: true, code: "JRS", args: []string{"2"}},
24 {name: "partial token rejected", input: "JR", ok: false},
25 {name: "concatenated token rejected", input: "JRS2", ok: false},
26 {name: "static arguments rejected", input: "BSK 2", ok: false},
27 {name: "missing required argument rejected", input: "JRS", ok: false},
28 }
29 for _, tt := range tests {
30 t.Run(tt.name, func(t *testing.T) {
31 action, ok := ParseAction(results, tt.input)
32 if ok != tt.ok {
33 t.Fatalf("ParseAction(%q) ok = %v, want %v", tt.input, ok, tt.ok)
34 }
35 if !ok {
36 return
37 }
38 if action.Command.Code != tt.code || !sameStrings(action.Args, tt.args) {
39 t.Fatalf("ParseAction(%q) = %#v, want %s %#v", tt.input, action, tt.code, tt.args)
40 }
41 })
42 }
43}
44
45func TestArgumentCommandKeepsExactRowVisible(t *testing.T) {
46 cmd := command.Command{Code: "JRS", Arguments: command.ArgumentsRequired}
47 if got, ok := ArgumentCommand(CommandResults([]command.Command{cmd}), "JRS 1"); !ok || got.Code != "JRS" {
48 t.Fatalf("ArgumentCommand() = %#v, %v; want JRS, true", got, ok)
49 }
50}
51
52func TestArgumentResultReturnsTheMatchingRowAndCommand(t *testing.T) {
53 jrs := command.Command{Code: "JRS", Arguments: command.ArgumentsRequired}
54 results := []Result{NewStoppedSessionResult("JRS", time.Time{}), NewCommandResult(jrs)}
55
56 result, got, ok := argumentResult(results, "jrs 1")
57
58 if !ok || result.Kind() != ResultKindCommand || got.Code != jrs.Code {
59 t.Fatalf("argumentResult() = %#v, %#v, %v; want matching JRS row and command", result, got, ok)
60 }
61}
62
63func sameStrings(a, b []string) bool {
64 if len(a) != len(b) {
65 return false
66 }
67 for i := range a {
68 if a[i] != b[i] {
69 return false
70 }
71 }
72 return true
73}