|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + "github.com/docker/docker-agent/pkg/config/latest" |
| 10 | +) |
| 11 | + |
| 12 | +func TestHooksFromCLI_Empty(t *testing.T) { |
| 13 | + hooks := HooksFromCLI(nil, nil, nil, nil, nil) |
| 14 | + assert.Nil(t, hooks) |
| 15 | +} |
| 16 | + |
| 17 | +func TestHooksFromCLI_SkipsEmptyCommands(t *testing.T) { |
| 18 | + // All empty/whitespace-only commands should be filtered out |
| 19 | + hooks := HooksFromCLI([]string{""}, []string{" "}, []string{""}, []string{" \t"}, nil) |
| 20 | + assert.Nil(t, hooks) |
| 21 | +} |
| 22 | + |
| 23 | +func TestHooksFromCLI_MixedEmptyAndValid(t *testing.T) { |
| 24 | + hooks := HooksFromCLI([]string{"", "echo pre", " "}, nil, []string{"echo start", ""}, nil, nil) |
| 25 | + require.NotNil(t, hooks) |
| 26 | + |
| 27 | + require.Len(t, hooks.PreToolUse, 1) |
| 28 | + require.Len(t, hooks.PreToolUse[0].Hooks, 1) |
| 29 | + assert.Equal(t, "echo pre", hooks.PreToolUse[0].Hooks[0].Command) |
| 30 | + |
| 31 | + require.Len(t, hooks.SessionStart, 1) |
| 32 | + assert.Equal(t, "echo start", hooks.SessionStart[0].Command) |
| 33 | +} |
| 34 | + |
| 35 | +func TestHooksFromCLI_PreToolUse(t *testing.T) { |
| 36 | + hooks := HooksFromCLI([]string{"echo pre1", "echo pre2"}, nil, nil, nil, nil) |
| 37 | + require.NotNil(t, hooks) |
| 38 | + |
| 39 | + require.Len(t, hooks.PreToolUse, 1) |
| 40 | + require.Len(t, hooks.PreToolUse[0].Hooks, 2) |
| 41 | + assert.Equal(t, "command", hooks.PreToolUse[0].Hooks[0].Type) |
| 42 | + assert.Equal(t, "echo pre1", hooks.PreToolUse[0].Hooks[0].Command) |
| 43 | + assert.Equal(t, "echo pre2", hooks.PreToolUse[0].Hooks[1].Command) |
| 44 | + // Matcher is empty string, which matches all tools by default |
| 45 | + assert.Empty(t, hooks.PreToolUse[0].Matcher) |
| 46 | +} |
| 47 | + |
| 48 | +func TestHooksFromCLI_AllTypes(t *testing.T) { |
| 49 | + hooks := HooksFromCLI( |
| 50 | + []string{"pre-cmd"}, |
| 51 | + []string{"post-cmd"}, |
| 52 | + []string{"start-cmd"}, |
| 53 | + []string{"end-cmd"}, |
| 54 | + []string{"input-cmd"}, |
| 55 | + ) |
| 56 | + require.NotNil(t, hooks) |
| 57 | + |
| 58 | + assert.Len(t, hooks.PreToolUse, 1) |
| 59 | + assert.Len(t, hooks.PostToolUse, 1) |
| 60 | + assert.Len(t, hooks.SessionStart, 1) |
| 61 | + assert.Len(t, hooks.SessionEnd, 1) |
| 62 | + assert.Len(t, hooks.OnUserInput, 1) |
| 63 | + |
| 64 | + assert.Equal(t, "pre-cmd", hooks.PreToolUse[0].Hooks[0].Command) |
| 65 | + assert.Equal(t, "post-cmd", hooks.PostToolUse[0].Hooks[0].Command) |
| 66 | + assert.Equal(t, "start-cmd", hooks.SessionStart[0].Command) |
| 67 | + assert.Equal(t, "end-cmd", hooks.SessionEnd[0].Command) |
| 68 | + assert.Equal(t, "input-cmd", hooks.OnUserInput[0].Command) |
| 69 | +} |
| 70 | + |
| 71 | +func TestMergeHooks_BothNil(t *testing.T) { |
| 72 | + assert.Nil(t, MergeHooks(nil, nil)) |
| 73 | +} |
| 74 | + |
| 75 | +func TestMergeHooks_CLINil(t *testing.T) { |
| 76 | + base := &latest.HooksConfig{ |
| 77 | + SessionStart: []latest.HookDefinition{{Type: "command", Command: "echo base"}}, |
| 78 | + } |
| 79 | + result := MergeHooks(base, nil) |
| 80 | + assert.Equal(t, base, result) |
| 81 | +} |
| 82 | + |
| 83 | +func TestMergeHooks_BaseNil(t *testing.T) { |
| 84 | + cli := &latest.HooksConfig{ |
| 85 | + SessionStart: []latest.HookDefinition{{Type: "command", Command: "echo cli"}}, |
| 86 | + } |
| 87 | + result := MergeHooks(nil, cli) |
| 88 | + assert.Equal(t, cli, result) |
| 89 | +} |
| 90 | + |
| 91 | +func TestMergeHooks_BothNonNil(t *testing.T) { |
| 92 | + base := &latest.HooksConfig{ |
| 93 | + SessionStart: []latest.HookDefinition{{Type: "command", Command: "echo base"}}, |
| 94 | + PreToolUse: []latest.HookMatcherConfig{{ |
| 95 | + Matcher: "shell", |
| 96 | + Hooks: []latest.HookDefinition{{Type: "command", Command: "echo base-pre"}}, |
| 97 | + }}, |
| 98 | + } |
| 99 | + cli := &latest.HooksConfig{ |
| 100 | + SessionStart: []latest.HookDefinition{{Type: "command", Command: "echo cli"}}, |
| 101 | + PreToolUse: []latest.HookMatcherConfig{{ |
| 102 | + Hooks: []latest.HookDefinition{{Type: "command", Command: "echo cli-pre"}}, |
| 103 | + }}, |
| 104 | + } |
| 105 | + |
| 106 | + result := MergeHooks(base, cli) |
| 107 | + require.NotNil(t, result) |
| 108 | + |
| 109 | + // Session start hooks should be merged |
| 110 | + require.Len(t, result.SessionStart, 2) |
| 111 | + assert.Equal(t, "echo base", result.SessionStart[0].Command) |
| 112 | + assert.Equal(t, "echo cli", result.SessionStart[1].Command) |
| 113 | + |
| 114 | + // Pre tool use matchers should be merged |
| 115 | + require.Len(t, result.PreToolUse, 2) |
| 116 | + assert.Equal(t, "shell", result.PreToolUse[0].Matcher) |
| 117 | + assert.Equal(t, "echo base-pre", result.PreToolUse[0].Hooks[0].Command) |
| 118 | + assert.Empty(t, result.PreToolUse[1].Matcher) |
| 119 | + assert.Equal(t, "echo cli-pre", result.PreToolUse[1].Hooks[0].Command) |
| 120 | +} |
| 121 | + |
| 122 | +func TestMergeHooks_DoesNotMutateOriginals(t *testing.T) { |
| 123 | + base := &latest.HooksConfig{ |
| 124 | + SessionStart: []latest.HookDefinition{{Type: "command", Command: "echo base"}}, |
| 125 | + } |
| 126 | + cli := &latest.HooksConfig{ |
| 127 | + SessionStart: []latest.HookDefinition{{Type: "command", Command: "echo cli"}}, |
| 128 | + } |
| 129 | + |
| 130 | + result := MergeHooks(base, cli) |
| 131 | + |
| 132 | + // Originals should not be mutated |
| 133 | + assert.Len(t, base.SessionStart, 1) |
| 134 | + assert.Len(t, cli.SessionStart, 1) |
| 135 | + assert.Len(t, result.SessionStart, 2) |
| 136 | +} |
| 137 | + |
| 138 | +func TestRuntimeConfig_CLIHooks(t *testing.T) { |
| 139 | + rc := &RuntimeConfig{} |
| 140 | + assert.Nil(t, rc.CLIHooks()) |
| 141 | + |
| 142 | + rc.HookSessionStart = []string{"echo start"} |
| 143 | + hooks := rc.CLIHooks() |
| 144 | + require.NotNil(t, hooks) |
| 145 | + assert.Len(t, hooks.SessionStart, 1) |
| 146 | + assert.Equal(t, "echo start", hooks.SessionStart[0].Command) |
| 147 | +} |
| 148 | + |
| 149 | +func TestRuntimeConfig_Clone_CopiesHooks(t *testing.T) { |
| 150 | + rc := &RuntimeConfig{} |
| 151 | + rc.HookPreToolUse = []string{"pre"} |
| 152 | + rc.HookPostToolUse = []string{"post"} |
| 153 | + rc.HookSessionStart = []string{"start"} |
| 154 | + rc.HookSessionEnd = []string{"end"} |
| 155 | + rc.HookOnUserInput = []string{"input"} |
| 156 | + |
| 157 | + clone := rc.Clone() |
| 158 | + assert.Equal(t, rc.HookPreToolUse, clone.HookPreToolUse) |
| 159 | + assert.Equal(t, rc.HookPostToolUse, clone.HookPostToolUse) |
| 160 | + assert.Equal(t, rc.HookSessionStart, clone.HookSessionStart) |
| 161 | + assert.Equal(t, rc.HookSessionEnd, clone.HookSessionEnd) |
| 162 | + assert.Equal(t, rc.HookOnUserInput, clone.HookOnUserInput) |
| 163 | + |
| 164 | + // Mutating clone should not affect original |
| 165 | + clone.HookPreToolUse[0] = "changed" |
| 166 | + assert.Equal(t, "pre", rc.HookPreToolUse[0]) |
| 167 | +} |
0 commit comments