1# frozen_string_literal: true
2
3require 'spec_helper'
4
5RSpec.describe Gitlab::QuickActions::Dsl do
6  before :all do
7    DummyClass = Struct.new(:project) do
8      include Gitlab::QuickActions::Dsl
9
10      desc 'A command with no args'
11      command :no_args, :none do
12        "Hello World!"
13      end
14
15      params 'The first argument'
16      explanation 'Static explanation'
17      warning 'Possible problem!'
18      command :explanation_with_aliases, :once, :first do |arg|
19        arg
20      end
21
22      desc do
23        "A dynamic description for #{noteable.upcase}"
24      end
25      execution_message do |arg|
26        "A dynamic execution message for #{noteable.upcase} passing #{arg}"
27      end
28      params 'The first argument', 'The second argument'
29      command :dynamic_description do |args|
30        args.split
31      end
32
33      command :cc
34
35      explanation do |arg|
36        "Action does something with #{arg}"
37      end
38      execution_message 'Command applied correctly'
39      condition do
40        project == 'foo'
41      end
42      command :cond_action do |arg|
43        arg
44      end
45
46      parse_params do |raw_arg|
47        raw_arg.strip
48      end
49      command :with_params_parsing do |parsed|
50        parsed
51      end
52
53      params '<Comment>'
54      substitution :something do |text|
55        "#{text} Some complicated thing you want in here"
56      end
57
58      desc 'A command with types'
59      types Issue, Commit
60      command :has_types do
61        "Has Issue and Commit types"
62      end
63    end
64  end
65
66  describe '.command_definitions' do
67    it 'returns an array with commands definitions' do
68      no_args_def, explanation_with_aliases_def, dynamic_description_def,
69      cc_def, cond_action_def, with_params_parsing_def, substitution_def, has_types =
70        DummyClass.command_definitions
71
72      expect(no_args_def.name).to eq(:no_args)
73      expect(no_args_def.aliases).to eq([:none])
74      expect(no_args_def.description).to eq('A command with no args')
75      expect(no_args_def.explanation).to eq('')
76      expect(no_args_def.execution_message).to eq('')
77      expect(no_args_def.params).to eq([])
78      expect(no_args_def.condition_block).to be_nil
79      expect(no_args_def.types).to eq([])
80      expect(no_args_def.action_block).to be_a_kind_of(Proc)
81      expect(no_args_def.parse_params_block).to be_nil
82      expect(no_args_def.warning).to eq('')
83
84      expect(explanation_with_aliases_def.name).to eq(:explanation_with_aliases)
85      expect(explanation_with_aliases_def.aliases).to eq([:once, :first])
86      expect(explanation_with_aliases_def.description).to eq('')
87      expect(explanation_with_aliases_def.explanation).to eq('Static explanation')
88      expect(explanation_with_aliases_def.execution_message).to eq('')
89      expect(no_args_def.params).to eq([])
90      expect(explanation_with_aliases_def.params).to eq(['The first argument'])
91      expect(explanation_with_aliases_def.condition_block).to be_nil
92      expect(explanation_with_aliases_def.types).to eq([])
93      expect(explanation_with_aliases_def.action_block).to be_a_kind_of(Proc)
94      expect(explanation_with_aliases_def.parse_params_block).to be_nil
95      expect(explanation_with_aliases_def.warning).to eq('Possible problem!')
96
97      expect(dynamic_description_def.name).to eq(:dynamic_description)
98      expect(dynamic_description_def.aliases).to eq([])
99      expect(dynamic_description_def.to_h(double('desc', noteable: 'issue'))[:description]).to eq('A dynamic description for ISSUE')
100      expect(dynamic_description_def.execute_message(double('desc', noteable: 'issue'), 'arg')).to eq('A dynamic execution message for ISSUE passing arg')
101      expect(dynamic_description_def.params).to eq(['The first argument', 'The second argument'])
102      expect(dynamic_description_def.condition_block).to be_nil
103      expect(dynamic_description_def.types).to eq([])
104      expect(dynamic_description_def.action_block).to be_a_kind_of(Proc)
105      expect(dynamic_description_def.parse_params_block).to be_nil
106      expect(dynamic_description_def.warning).to eq('')
107
108      expect(cc_def.name).to eq(:cc)
109      expect(cc_def.aliases).to eq([])
110      expect(cc_def.description).to eq('')
111      expect(cc_def.explanation).to eq('')
112      expect(cc_def.execution_message).to eq('')
113      expect(cc_def.params).to eq([])
114      expect(cc_def.condition_block).to be_nil
115      expect(cc_def.types).to eq([])
116      expect(cc_def.action_block).to be_nil
117      expect(cc_def.parse_params_block).to be_nil
118      expect(cc_def.warning).to eq('')
119
120      expect(cond_action_def.name).to eq(:cond_action)
121      expect(cond_action_def.aliases).to eq([])
122      expect(cond_action_def.description).to eq('')
123      expect(cond_action_def.explanation).to be_a_kind_of(Proc)
124      expect(cond_action_def.execution_message).to eq('Command applied correctly')
125      expect(cond_action_def.params).to eq([])
126      expect(cond_action_def.condition_block).to be_a_kind_of(Proc)
127      expect(cond_action_def.types).to eq([])
128      expect(cond_action_def.action_block).to be_a_kind_of(Proc)
129      expect(cond_action_def.parse_params_block).to be_nil
130      expect(cond_action_def.warning).to eq('')
131
132      expect(with_params_parsing_def.name).to eq(:with_params_parsing)
133      expect(with_params_parsing_def.aliases).to eq([])
134      expect(with_params_parsing_def.description).to eq('')
135      expect(with_params_parsing_def.explanation).to eq('')
136      expect(with_params_parsing_def.execution_message).to eq('')
137      expect(with_params_parsing_def.params).to eq([])
138      expect(with_params_parsing_def.condition_block).to be_nil
139      expect(with_params_parsing_def.types).to eq([])
140      expect(with_params_parsing_def.action_block).to be_a_kind_of(Proc)
141      expect(with_params_parsing_def.parse_params_block).to be_a_kind_of(Proc)
142      expect(with_params_parsing_def.warning).to eq('')
143
144      expect(substitution_def.name).to eq(:something)
145      expect(substitution_def.aliases).to eq([])
146      expect(substitution_def.description).to eq('')
147      expect(substitution_def.explanation).to eq('')
148      expect(substitution_def.execution_message).to eq('')
149      expect(substitution_def.params).to eq(['<Comment>'])
150      expect(substitution_def.condition_block).to be_nil
151      expect(substitution_def.types).to eq([])
152      expect(substitution_def.action_block.call('text')).to eq('text Some complicated thing you want in here')
153      expect(substitution_def.parse_params_block).to be_nil
154      expect(substitution_def.warning).to eq('')
155
156      expect(has_types.name).to eq(:has_types)
157      expect(has_types.aliases).to eq([])
158      expect(has_types.description).to eq('A command with types')
159      expect(has_types.explanation).to eq('')
160      expect(has_types.execution_message).to eq('')
161      expect(has_types.params).to eq([])
162      expect(has_types.condition_block).to be_nil
163      expect(has_types.types).to eq([Issue, Commit])
164      expect(has_types.action_block).to be_a_kind_of(Proc)
165      expect(has_types.parse_params_block).to be_nil
166      expect(has_types.warning).to eq('')
167    end
168  end
169end
170