|
| 1 | +package tui |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + tea "charm.land/bubbletea/v2" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/docker/docker-agent/pkg/app" |
| 12 | + "github.com/docker/docker-agent/pkg/session" |
| 13 | + "github.com/docker/docker-agent/pkg/tui/commands" |
| 14 | + "github.com/docker/docker-agent/pkg/tui/components/editor/completions" |
| 15 | +) |
| 16 | + |
| 17 | +// leanModeTestCategories mirrors the real "Session"/"Settings" categories |
| 18 | +// closely enough to exercise lean-mode filtering: /settings is a no-op in |
| 19 | +// lean mode (OpenSettingsDialogMsg is dropped), /exit is not. |
| 20 | +func leanModeTestCategories(context.Context, tea.Model) []commands.Category { |
| 21 | + noop := func(string) tea.Cmd { return func() tea.Msg { return nil } } |
| 22 | + return []commands.Category{ |
| 23 | + { |
| 24 | + Name: "Session", |
| 25 | + Commands: []commands.Item{ |
| 26 | + {ID: "session.exit", Label: "Exit", SlashCommand: "/exit", Immediate: true, Execute: noop}, |
| 27 | + }, |
| 28 | + }, |
| 29 | + { |
| 30 | + Name: "Settings", |
| 31 | + Commands: []commands.Item{ |
| 32 | + {ID: "settings.open", Label: "Preferences", SlashCommand: "/settings", Immediate: true, Execute: noop}, |
| 33 | + }, |
| 34 | + }, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func TestCommandCategories_LeanModeExcludesSettings(t *testing.T) { |
| 39 | + t.Parallel() |
| 40 | + |
| 41 | + t.Run("lean mode drops /settings from categories, completion, and the parser", func(t *testing.T) { |
| 42 | + t.Parallel() |
| 43 | + |
| 44 | + m := &appModel{ctx: t.Context, buildCommandCategories: leanModeTestCategories} |
| 45 | + WithLeanMode()(m) |
| 46 | + |
| 47 | + categories := m.commandCategories() |
| 48 | + require.Len(t, categories, 1, "the now-empty Settings category should be dropped") |
| 49 | + assert.Equal(t, "Session", categories[0].Name) |
| 50 | + |
| 51 | + items := completions.NewCommandCompletion(categories).Items() |
| 52 | + for _, item := range items { |
| 53 | + assert.NotEqual(t, "/settings", item.Value, "inline completion should not offer /settings in lean mode") |
| 54 | + } |
| 55 | + |
| 56 | + parser := commands.NewParser(categories...) |
| 57 | + assert.Nil(t, parser.Parse("/settings"), "the palette parser should not run /settings in lean mode") |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("classic mode keeps /settings in categories, completion, and the parser", func(t *testing.T) { |
| 61 | + t.Parallel() |
| 62 | + |
| 63 | + m := &appModel{ctx: t.Context, buildCommandCategories: leanModeTestCategories} |
| 64 | + |
| 65 | + categories := m.commandCategories() |
| 66 | + require.Len(t, categories, 2) |
| 67 | + |
| 68 | + items := completions.NewCommandCompletion(categories).Items() |
| 69 | + var sawSettings bool |
| 70 | + for _, item := range items { |
| 71 | + if item.Value == "/settings" { |
| 72 | + sawSettings = true |
| 73 | + } |
| 74 | + } |
| 75 | + assert.True(t, sawSettings, "inline completion should offer /settings in classic mode") |
| 76 | + |
| 77 | + parser := commands.NewParser(categories...) |
| 78 | + cmd := parser.Parse("/settings") |
| 79 | + require.NotNil(t, cmd, "the palette parser should run /settings in classic mode") |
| 80 | + }) |
| 81 | +} |
| 82 | + |
| 83 | +// TestLeanModeDisabledSlashCommandsMatchDroppedMessages guards |
| 84 | +// leanModeDisabledSlashCommands against drifting out of sync with |
| 85 | +// leanModeDroppedMessageTypes (the lean-mode message-drop switch in |
| 86 | +// appModel.update): every built-in slash command whose Execute produces a |
| 87 | +// dropped message type must be listed, and every listed command must |
| 88 | +// actually produce one of those types. |
| 89 | +func TestLeanModeDisabledSlashCommandsMatchDroppedMessages(t *testing.T) { |
| 90 | + t.Parallel() |
| 91 | + |
| 92 | + application := app.New(t.Context(), stubRuntime{}, session.New()) |
| 93 | + categories := commands.BuildCommandCategories(t.Context(), application) |
| 94 | + |
| 95 | + disabled := make(map[string]bool, len(leanModeDisabledSlashCommands)) |
| 96 | + for _, c := range leanModeDisabledSlashCommands { |
| 97 | + disabled[c] = true |
| 98 | + } |
| 99 | + |
| 100 | + matched := make(map[string]bool, len(leanModeDisabledSlashCommands)) |
| 101 | + for _, category := range categories { |
| 102 | + for _, item := range category.Commands { |
| 103 | + if item.SlashCommand == "" || !item.Immediate || item.Execute == nil { |
| 104 | + continue |
| 105 | + } |
| 106 | + cmd := item.Execute("") |
| 107 | + if cmd == nil { |
| 108 | + continue |
| 109 | + } |
| 110 | + msg := cmd() |
| 111 | + |
| 112 | + if isLeanModeNoOp(msg) { |
| 113 | + matched[item.SlashCommand] = true |
| 114 | + assert.True(t, disabled[item.SlashCommand], |
| 115 | + "%s produces %T, which lean mode drops, but is missing from leanModeDisabledSlashCommands", item.SlashCommand, msg) |
| 116 | + } else { |
| 117 | + assert.False(t, disabled[item.SlashCommand], |
| 118 | + "%s is listed in leanModeDisabledSlashCommands but produces %T, which lean mode does not drop", item.SlashCommand, msg) |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + for _, c := range leanModeDisabledSlashCommands { |
| 124 | + assert.True(t, matched[c], "%s is listed in leanModeDisabledSlashCommands but no built-in command produces a dropped message type for it", c) |
| 125 | + } |
| 126 | +} |
0 commit comments