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