1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
# frozen_string_literal: true
# rubocop:disable Metrics/ClassLength, Metrics/MethodLength, Metrics/AbcSize
require 'minitest/autorun'
require 'fileutils'
require 'rbconfig'
require 'shellwords'
require 'tmpdir'
require_relative '../../../lib/dsl'
class RCMAgentTest < Minitest::Test
MOCK_AGENT = File.expand_path('../../support/mock_agent.rb', __dir__).freeze
def setup
@dir_path = Dir.mktmpdir('.agent_test.rcmtmp.')
@original_argv = ARGV.dup
end
def teardown
ARGV.replace(@original_argv) if @original_argv
FileUtils.rm_rf(@dir_path) if @dir_path
end
def path(name)
File.join(@dir_path, name)
end
def mock_agent_command(mode, *args)
parts = [RbConfig.ruby, MOCK_AGENT, mode.to_s]
args.each do |arg|
parts << if %w[INPUT PROMPT FILE_PATH].include?(arg)
arg
else
Shellwords.escape(arg.to_s)
end
end
[Shellwords.escape(parts.shift), Shellwords.escape(parts.shift), Shellwords.escape(parts.shift), *parts].join(' ')
end
def test_duplicate_agent_definition
assert_raises(RCM::DSL::DuplicateDefinition) do
configure_from_scratch do
agent mock do
'ruby -e "print STDIN.read"'
end
agent mock do
'ruby -e "print STDIN.read"'
end
end
end
end
def test_duplicate_prompt_definition
assert_raises(RCM::DSL::DuplicateDefinition) do
configure_from_scratch do
prompt 'fix english' do
'Fix grammar'
end
prompt 'fix english' do
'Fix spelling'
end
end
end
end
def test_agent_processes_file_using_stdin_and_names_with_spaces
file_path = path('process.txt')
command = mock_agent_command(:upcase_prompt, 'PROMPT')
File.write(file_path, 'hello world')
configure_from_scratch do
agent mock do
command
end
prompt 'fix english' do
'Fix grammar'
end
file file_path do
agent mock, 'fix english'
end
end
assert_equal 'HELLO WORLD|Fix grammar', File.read(file_path)
end
def test_agent_processes_file_using_prompt_name_with_spaces
file_path = path('process-spaced-prompt.txt')
command = mock_agent_command(:upcase_prompt, 'PROMPT')
File.write(file_path, 'hello world')
configure_from_scratch do
agent mock do
command
end
prompt fix english do
'Fix grammar'
end
file file_path do
agent mock fix english
end
end
assert_equal 'HELLO WORLD|Fix grammar', File.read(file_path)
end
def test_agent_can_use_input_placeholder
file_path = path('input.txt')
command = mock_agent_command(:reverse_input, 'INPUT')
File.write(file_path, 'abc123')
configure_from_scratch do
agent reverse via file do
command
end
prompt no op do
''
end
file file_path do
agent reverse via file no op
end
end
assert_equal '321cba', File.read(file_path)
end
def test_agent_spec_raises_when_multiword_split_is_ambiguous
file_path = path('ambiguous.txt')
command = mock_agent_command(:pass_through)
File.write(file_path, 'hello')
assert_raises(RCM::File::InvalidAgentSpec) do
configure_from_scratch do
agent alpha do
command
end
agent alpha beta do
command
end
prompt gamma do
''
end
prompt beta gamma do
''
end
file file_path do
agent alpha beta gamma
end
end
end
end
def test_agent_can_use_file_path_placeholder
file_path = path('placeholder.txt')
command = mock_agent_command(:basename, 'FILE_PATH')
File.write(file_path, 'ignored')
configure_from_scratch do
agent 'show file name' do
command
end
prompt 'no op' do
''
end
file file_path do
agent 'show file name', 'no op'
end
end
assert_equal 'placeholder.txt', File.read(file_path)
end
def test_agent_processing_skips_backup_when_output_is_unchanged
file_path = path('unchanged.txt')
command = mock_agent_command(:pass_through)
File.write(file_path, 'same content')
configure_from_scratch do
agent 'pass through' do
command
end
prompt 'no op' do
''
end
file file_path do
agent 'pass through', 'no op'
end
end
backup_dir = File.join(File.dirname(file_path), '.rcmbackup')
assert_empty Dir.glob(File.join(backup_dir, 'unchanged.txt.*'))
assert_equal 'same content', File.read(file_path)
end
def test_agent_processing_creates_backup_when_output_changes
file_path = path('backup.txt')
command = mock_agent_command(:upcase)
File.write(file_path, 'hello')
configure_from_scratch do
agent 'make loud' do
command
end
prompt 'no op' do
''
end
file file_path do
agent 'make loud', 'no op'
end
end
backup_dir = File.join(@dir_path, '.rcmbackup')
assert_equal 'HELLO', File.read(file_path)
assert_equal 1, Dir.glob(File.join(backup_dir, 'backup.txt.*')).count
end
def test_unknown_agent_raises
file_path = path('unknown-agent.txt')
File.write(file_path, 'hello')
assert_raises(RCM::DSL::NoSuchAgentDefinition) do
configure_from_scratch do
prompt 'no op' do
''
end
file file_path do
agent 'missing agent', 'no op'
end
end
end
end
def test_unknown_prompt_raises
file_path = path('unknown-prompt.txt')
command = mock_agent_command(:pass_through)
File.write(file_path, 'hello')
assert_raises(RCM::DSL::NoSuchPromptDefinition) do
configure_from_scratch do
agent mock do
command
end
file file_path do
agent mock, 'missing prompt'
end
end
end
end
def test_missing_agent_input_raises
file_path = path('missing.txt')
command = mock_agent_command(:pass_through)
refute File.exist?(file_path)
assert_raises(RCM::File::MissingAgentInput) do
configure_from_scratch do
agent mock do
command
end
prompt 'no op' do
''
end
file file_path do
agent mock, 'no op'
end
end
end
end
def test_dry_run_does_not_execute_agent
file_path = path('dry-run.txt')
command = mock_agent_command(:fail, 'boom', '7')
File.write(file_path, 'keep me')
ARGV.replace(['--dry'])
configure_from_scratch do
agent 'should not run' do
command
end
prompt 'no op' do
''
end
file file_path do
agent 'should not run', 'no op'
end
end
assert_equal 'keep me', File.read(file_path)
end
def test_dry_run_unknown_agent_raises
file_path = path('dry-run-unknown-agent.txt')
File.write(file_path, 'keep me')
ARGV.replace(['--dry'])
assert_raises(RCM::DSL::NoSuchAgentDefinition) do
configure_from_scratch do
prompt 'no op' do
''
end
file file_path do
agent 'missing agent', 'no op'
end
end
end
end
def test_dry_run_unknown_prompt_raises
file_path = path('dry-run-unknown-prompt.txt')
command = mock_agent_command(:pass_through)
File.write(file_path, 'keep me')
ARGV.replace(['--dry'])
assert_raises(RCM::DSL::NoSuchPromptDefinition) do
configure_from_scratch do
agent mock do
command
end
file file_path do
agent mock, 'missing prompt'
end
end
end
end
def test_non_zero_exit_raises
file_path = path('broken.txt')
command = mock_agent_command(:fail, 'boom', '7')
File.write(file_path, 'hello')
error = assert_raises(RCM::File::AgentCommandFailed) do
configure_from_scratch do
agent 'broken agent' do
command
end
prompt 'no op' do
''
end
file file_path do
agent 'broken agent', 'no op'
end
end
end
assert_match('exit 7', error.message)
assert_match('boom', error.message)
end
end
# rubocop:enable Metrics/ClassLength, Metrics/MethodLength, Metrics/AbcSize
|